Austria’s Dominic Thiem eventually came through 3-6 7-6 (13-11) 6-4 to set up a meeting with world number one Novak Djokovic.
Fifth seed Thiem previously beat 20-time Grand Slam champion Federer in the Indian Wells final in March.

Fourth seed Federer, 37, was playing in his first clay-court tournament for three years.

Second seed Rafael Nadal cruised into the semi-finals with a 6-1 6-2 win over Switzerland’s Stan Wawrinka.

Spaniard Nadal, a five-time champion in Madrid, needed just 69 minutes to beat Wawrinka.

He will face ninth seed and rising star Stefanos Tsitsipas next after the Greek beat defending champion Alexander Zverev 7-5 3-6 6-2.