Dominic Thiem saved two match points to end Roger Federer's clay-court return and reach the Madrid Open semi-finals.