Federer wins thriller to reach showpiece