[llvm] [SPIR-V] Implement selection for llvm.canonicalize(x) as fmul(x, 1.0) (PR #178439)

Mészáros Gergely via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 29 03:01:16 PST 2026


================
@@ -3007,6 +3012,33 @@ SPIRVInstructionSelector::buildI32Constant(uint32_t Val, MachineInstr &I,
   return {NewReg, Result};
 }
 
+bool SPIRVInstructionSelector::selectFCanonicalize(Register ResVReg,
+                                                   const SPIRVType *ResType,
+                                                   MachineInstr &I) const {
+  // There is no native fcanonicalize instruction in SPIRV. We can lower it to:
+  // - fmin(x, x) or
+  // - fmul(x, 1.0)
+  //
+  // We use fmul(x, 1.0) here, because:
+  // - llvm-spirv translates fmin to a function call, whereas
+  //   fmul is translated to the LLVM fmul instruction.
----------------
Maetveis wrote:

> I don't think we really specify how this specific interaction of non-IEEE (denormal) fpenv with non-constrained FP works, but my general assumption was that omission of canonicalizing operations still holds in that mode.

I think this sentence from the description of "denormal-fp-math" disagrees:
> If the input mode is "preserve-sign", or "positive-zero", a floating-point operation must treat any input denormal value as zero. In some situations, if an instruction does not respect this mode, the input may need to be converted to 0 as if by @llvm.canonicalize during lowering for correctness.



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


More information about the llvm-commits mailing list