[llvm] [InstCombine] Fold tan(x) * cos(x) => sin(x) (PR #136319)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 23 12:56:10 PDT 2025


================
@@ -947,6 +947,14 @@ Instruction *InstCombinerImpl::foldFMulReassoc(BinaryOperator &I) {
     return BinaryOperator::CreateFMulFMF(XX, Y, &I);
   }
 
+  // tan(X) * cos(X) -> sin(X)
+  if (match(&I,
----------------
arsenm wrote:

>  have an expectation that what they are doing is enabling the hardware-specific contraction operations.

This is not a semantic interpretation. This footnote is informative and I don't see how it makes sense to interpret it as a restriction. The IR is not a vehicle for directly mapping source constructs onto machine instructions and has to have a freestanding meaning with no concern for how the backend happens to emit it on a particular target

>  then this becomes very much like the other fast-math flags.

That's the point, contract should not be special. It's far too narrow if it were to only mean "do FMA but only if the hardware has it".

> can even decrease accuracy for containing expressions

I do not think we rely on contract alone in these cases, just contract should only permit improved accuracy. 

Plus I think we have here the usual confusion that there are actually 3 contraction modes. The contract flag only appears when using clang extensions. The C standard FP_CONTRACT ON pragmatic does *not* enable emission of the contract flag. You need to use the clang fp pragma https://godbolt.org/z/Po5shY5fs or -ffp-contract=fast (or more commonly one of the other combo -ffast-math flags)

https://github.com/llvm/llvm-project/pull/136319


More information about the llvm-commits mailing list