Federer held all 11 of his service games without facing a break point to prevail in one hour and 30 minutes.
The outgunned world number 134 resorted to trick shots late in the second set, briefly befuddling Federer with forehand slices, followed by a disguised cross-court forehand victor, which he swatted past the favourite to the delight of the crowd.
As for the defending champion, Mayer has not had the best of seasons so far. They are the sets you need to win to win tournaments. Federer, appropriately enough for a Swiss, perhaps, has mastered the art of placing his deliveries with stunning precision-using zip or angle or kick, but always with disguise.
Khachanov came out on top after a tough battle that belied the 68 places between the compatriots in the rankings, the world number 38 winning 7-6 (10-8) 4-6 6-3 after his inconsistent opponent gave up two crucial breaks of serve in the deciding set.
The world No 7 has yet to drop a set in three matches en route to his fourth semi-final appearance at the Wimbledon warm-up event in west London. Now that is some kind of incentive for a man who has been making strides with nearly every tournament he plays. He's big and strong and seems super excited to be on tour and he's working hard.
"After I got that in the bag I was really able to start to relax and really play and feel the way I want to feel out there". He's a legend. So, it would be nice to be on the court with him and to compete against him. "Roger has to beat him first and then hopefully I can play against him here tomorrow".
"Andy was exhausted after getting to No.1 at the end of last year and then has been struggling with injuries", the 35- year-old Swiss said in Halle before his fighting 7-6 (7-4), 6-4 win in an hour and a half over Mischa Zverev, the German left-hander who dumped Murray out of the Australian Open in the fourth round. The home crowd had their combined fingers and toes crossed.