Roger Federer’s race to his sixth U.S. Open title came to a stunning end Tuesday night.

Bulgarian upstart Grigor Dimitrov upset the 20-time Grand Slam titlist in five sets, winning 3-6, 6-4, 3-6, 6-4, 6-2 to advance to just the third Grand Slam semifinal of his career, where he will meet No. 5 Daniil Medvedev on Friday.

Despite Federer holding a convincing 7-0 record over Dimitrov heading into the match, Federer had to work for every set he won as the 28-year-old Dimitrov played some of the best tennis of his career and took advantage of Federer’s struggle to command his strokes and several misfires, especially near the end of the match.

With a 2-1 set advantage in the match, Federer fell behind early in the fourth set but fought through eight deuces and seven break points in an over 11-minute seventh game to stay alive and trail 4-3. But Dimitrov held on to force a fifth set, capitalizing on almost every long rally and managing to outwork Federer on the baseline.

Federer retreated to the locker room just before the fifth set to get looked at by trainers, but made it back to the court only to get smoked in the first four games, which served as too big of a hole for the Swiss star to crawl out of as he fell 6-2 in the last set.

“I was just trying to stay in my game and make him stay on the court as much as possible,” Dimitrov said in his on-court interview. “For sure in the end, he was not 100 percent of himself. It’s best-of-five, anything can happen.”

Federer had started strong, commanding a 3-0 lead early in the opening set. Dimitrov struggled with double faults early on, committing two in the second game alone and seven throughout the entire match compared to one for Federer.

Dimitrov took the fourth game before Federer won three of the next five to take the first set. He smashed three aces throughout the first, including one to go up 40-0 on the way to a triple-set point.

The two traded holds for the first four games of the second set with Federer rallying from behind and avoiding a break point in the second game. But Dimitrov found a rhythm and rode it to win three straight games to go up 5-2. Federer gave it his best shot with back-to-back wins to come within one game, but couldn’t pull through and dropped his third set to Dimitrov in his career.

“No, because [I’ve] played him many times before,” Federer said in response to what Dimitrov did differently this time around. “I mean, it’s the Grigor I expected. He has returned against me in the past also a little bit further back. He has been in, chipped, come over. He has the arsenal to do all sorts of things.

“He used it all tonight to great effect. He played well.”

Once Dimitrov went up 3-2 in the third set, Federer tightened his baseline coverage and stormed back with four unanswered wins, including a double-set point to secure the 2-1 set lead. But a 55-minute fourth set saw Federer’s serve lose its edge and Dimitrov own the baseline game to force the final set.

When world-No. 1 Novak Djokavic retired with a shoulder injury in the fourth round on Monday, it seemed as though Federer’s path to the final became a lot more straightforward. But Dimitrov had other plans, pulling off the biggest upset of the tournament thus far.

Dimitrov played through four sets against Andreas Seppi in the opening round before Borna Coric granted him a walkover in the second round. He then won six straight sets through the third round and round of 16 to get his eighth chance at defeating Federer.

Dimitrov suffered from an inflamed shoulder in the offseason that kept a racket out of his hands for four weeks and forced him to withdraw from four tournaments.

He was once referred to as “Baby Federer,” but hasn’t accumulated as big a résumé as the nickname would suggest. Now, it seems as though Dimitrov has come into his own.