[llvm] [RISCV]: Implemented softening of `FCANONICALIZE` (PR #169234)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 3 09:55:30 PST 2025


================
@@ -311,6 +313,26 @@ SDValue DAGTypeLegalizer::SoftenFloatRes_FABS(SDNode *N) {
   return DAG.getNode(ISD::AND, SDLoc(N), NVT, Op, Mask);
 }
 
+SDValue DAGTypeLegalizer::SoftenFloatRes_FCANONICALIZE(SDNode *N) {
----------------
topperc wrote:

This code feels like a series of hacks to reuse existing Softening functions.

Can we do this instead. It's the code from LegalizeDAG with a BitConvertToInteger at the end.

```
SDValue DAGTypeLegalizer::SoftenFloatRes_FCANONICALIZE(SDNode *N) {                                           
  SDLoc dl(N);                                                                                                
                                                                                                              
  // This implements llvm.canonicalize.f* by multiplication with 1.0, as
  // suggested in
  // https://llvm.org/docs/LangRef.html#llvm-canonicalize-intrinsic.
  // It uses strict_fp operations even outside a strict_fp context in order
  // to guarantee that the canonicalization is not optimized away by later
  // passes. The result chain introduced by that is intentionally ignored
  // since no ordering requirement is intended here.

  // Create strict multiplication by 1.0.
  SDValue Operand = N->getOperand(0);
  EVT VT = Operand.getValueType();
  SDValue One = DAG.getConstantFP(1.0, dl, VT);
  SDValue Chain = DAG.getEntryNode();
  // Propagate existing flags on canonicalize, and additionally set
  // NoFPExcept.
  SDNodeFlags CanonicalizeFlags = N->getFlags();
  CanonicalizeFlags.setNoFPExcept(true);
  SDValue Mul = DAG.getNode(ISD::STRICT_FMUL, dl, {VT, MVT::Other},
                            {Chain, Operand, One}, CanonicalizeFlags);
  return BitConvertToInteger(Mul);
}
```

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


More information about the llvm-commits mailing list