https://github.com/xakep8 created https://github.com/llvm/llvm-project/pull/224899
Added fast-math flags attribute to CIR which cir.call_llvm_intrinsic now carries through DirectToLLVM lowering. CIR now preserves fast-math flags such as reassoc when lowering to llvm.call_intrinsic. Added test for the same. >From 86288722d33eb011c4fa8238ebbaf4724e3654bc Mon Sep 17 00:00:00 2001 From: Kunal Dubey <[email protected]> Date: Sun, 20 Sep 2026 14:31:07 +0530 Subject: [PATCH] [CIR] Added fast-math flags to LLVM intrinsic calls Added fast-math flags attribute to CIR which cir.call_llvm_intrinsic now carries through DirectToLLVM lowering. CIR now preserves fast-math flags such as reassoc when lowering to llvm.call_intrinsic. Added test for the same. --- .../include/clang/CIR/Dialect/IR/CIRAttrs.td | 28 +++++++++++++++++++ clang/include/clang/CIR/Dialect/IR/CIROps.td | 9 ++++-- clang/lib/CIR/CodeGen/CIRGenBuilder.h | 9 ++++++ .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 26 ++++++++++++++++- .../test/CIR/Lowering/call-llvm-intrinsic.cir | 11 ++++++++ 5 files changed, 80 insertions(+), 3 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td index c5f19592d4592..9445badd9f2a1 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td @@ -947,6 +947,34 @@ def CIR_FenvAttr : CIR_Attr<"Fenv", "fenv"> { let canHaveIllegalCXXABIType = 0; } +//===----------------------------------------------------------------------===// +// FastMathFlagsAttr +//===----------------------------------------------------------------------===// + +def CIR_FMFnone : I32BitEnumAttrCaseNone<"none">; +def CIR_FMFnnan : I32BitEnumAttrCaseBit<"nnan", 0>; +def CIR_FMFninf : I32BitEnumAttrCaseBit<"ninf", 1>; +def CIR_FMFnsz : I32BitEnumAttrCaseBit<"nsz", 2>; +def CIR_FMFarcp : I32BitEnumAttrCaseBit<"arcp", 3>; +def CIR_FMFcontract : I32BitEnumAttrCaseBit<"contract", 4>; +def CIR_FMFafn : I32BitEnumAttrCaseBit<"afn", 5>; +def CIR_FMFreassoc : I32BitEnumAttrCaseBit<"reassoc", 6>; +def CIR_FMFfast : I32BitEnumAttrCaseGroup<"fast", [ + CIR_FMFnnan, CIR_FMFninf, CIR_FMFnsz, CIR_FMFarcp, CIR_FMFcontract, + CIR_FMFafn, CIR_FMFreassoc +]>; + +def CIR_FastMathFlags : CIR_I32BitEnum< + "FastMathFlags", "fast-math flags", [ + CIR_FMFnone, CIR_FMFnnan, CIR_FMFninf, CIR_FMFnsz, CIR_FMFarcp, + CIR_FMFcontract, CIR_FMFafn, CIR_FMFreassoc, CIR_FMFfast +]> { + let separator = ", "; + let printBitEnumPrimaryGroups = 1; +} + +def CIR_FastMathFlagsAttr : CIR_EnumAttr<CIR_FastMathFlags, "fastmath">; + //===----------------------------------------------------------------------===// // GlobalViewAttr //===----------------------------------------------------------------------===// diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index f23475842ed3a..16fd5758c8fb7 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -4467,7 +4467,9 @@ def CIR_LLVMIntrinsicCallOp : CIR_Op<"call_llvm_intrinsic"> { let results = (outs Optional<CIR_AnyType>:$result); let arguments = (ins - StrAttr:$intrinsic_name, Variadic<CIR_AnyType>:$arg_ops); + StrAttr:$intrinsic_name, + OptionalAttr<CIR_FastMathFlagsAttr>:$fastmath_flags, + Variadic<CIR_AnyType>:$arg_ops); let skipDefaultBuilders = 1; @@ -4477,8 +4479,11 @@ def CIR_LLVMIntrinsicCallOp : CIR_Op<"call_llvm_intrinsic"> { let builders = [ OpBuilder<(ins "mlir::StringAttr":$intrinsic_name, "mlir::Type":$resType, - CArg<"mlir::ValueRange", "{}">:$operands), [{ + CArg<"mlir::ValueRange", "{}">:$operands, + CArg<"cir::FastMathFlagsAttr", "{}">:$fastmath), [{ $_state.addAttribute("intrinsic_name", intrinsic_name); + if (fastmath) + $_state.addAttribute("fastmath_flags", fastmath); $_state.addOperands(operands); if (resType) $_state.addTypes(resType); diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h b/clang/lib/CIR/CodeGen/CIRGenBuilder.h index 91e3b18a6b315..2f82c1d249434 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h +++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h @@ -830,6 +830,15 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy { std::forward<Operands>(op)...) .getResult(); } + + mlir::Value emitIntrinsicCallOp(mlir::Location loc, const llvm::StringRef str, + const mlir::Type &resTy, + mlir::ValueRange operands, + cir::FastMathFlagsAttr fastmath) { + return cir::LLVMIntrinsicCallOp::create( + *this, loc, this->getStringAttr(str), resTy, operands, fastmath) + .getResult(); + } }; } // namespace clang::CIRGen diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index f29fa93ee4609..2d816015c3537 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -581,6 +581,27 @@ mlir::LogicalResult lowerConstrainableFPOp( constrainedMnemonic, hasRoundingMode); } +static mlir::LLVM::FastmathFlags +convertFastMathFlags(cir::FastMathFlags cirFlags) { + mlir::LLVM::FastmathFlags llvmFlags{}; + const std::pair<cir::FastMathFlags, mlir::LLVM::FastmathFlags> flags[] = { + {cir::FastMathFlags::nnan, mlir::LLVM::FastmathFlags::nnan}, + {cir::FastMathFlags::ninf, mlir::LLVM::FastmathFlags::ninf}, + {cir::FastMathFlags::nsz, mlir::LLVM::FastmathFlags::nsz}, + {cir::FastMathFlags::arcp, mlir::LLVM::FastmathFlags::arcp}, + {cir::FastMathFlags::contract, mlir::LLVM::FastmathFlags::contract}, + {cir::FastMathFlags::afn, mlir::LLVM::FastmathFlags::afn}, + {cir::FastMathFlags::reassoc, mlir::LLVM::FastmathFlags::reassoc}, + }; + + for (auto [cirFlag, llvmFlag] : flags) { + if (bitEnumContainsAny(cirFlags, cirFlag)) + llvmFlags = llvmFlags | llvmFlag; + } + + return llvmFlags; +} + mlir::LogicalResult CIRToLLVMLLVMIntrinsicCallOpLowering::matchAndRewrite( cir::LLVMIntrinsicCallOp op, OpAdaptor adaptor, mlir::ConversionPatternRewriter &rewriter) const { @@ -593,6 +614,9 @@ mlir::LogicalResult CIRToLLVMLLVMIntrinsicCallOpLowering::matchAndRewrite( return op.emitError("expected LLVM result type"); } StringRef name = op.getIntrinsicName(); + mlir::LLVM::FastmathFlags fastmathFlags = {}; + if (std::optional<cir::FastMathFlags> fastmath = op.getFastmathFlags()) + fastmathFlags = convertFastMathFlags(*fastmath); // Some LLVM intrinsics require ElementType attribute to be attached to // the argument of pointer type. That prevents us from generating LLVM IR @@ -605,7 +629,7 @@ mlir::LogicalResult CIRToLLVMLLVMIntrinsicCallOpLowering::matchAndRewrite( // to set LLVM IR attribute. assert(!cir::MissingFeatures::intrinsicElementTypeSupport()); replaceOpWithCallLLVMIntrinsicOp(rewriter, op, "llvm." + name, llvmResTy, - adaptor.getOperands()); + adaptor.getOperands(), fastmathFlags); return mlir::success(); } diff --git a/clang/test/CIR/Lowering/call-llvm-intrinsic.cir b/clang/test/CIR/Lowering/call-llvm-intrinsic.cir index edd492aa7477c..643e4db0d7d0c 100644 --- a/clang/test/CIR/Lowering/call-llvm-intrinsic.cir +++ b/clang/test/CIR/Lowering/call-llvm-intrinsic.cir @@ -5,6 +5,8 @@ // 0-result (void) calls in addition to the single-result case. !s32i = !cir.int<s, 32> +!f32 = !cir.float +!v4f32 = !cir.vector<4 x !f32> module { // 0-result, 0-operand. @@ -24,4 +26,13 @@ module { cir.call_llvm_intrinsic "amdgcn.s.sleep" %arg0 : (!s32i) -> () cir.return } + + // Fast-math flags are preserved on the lowered LLVM intrinsic call. + // CHECK-LABEL: llvm.func @fastmath_flags + // CHECK: llvm.call_intrinsic "llvm.vector.reduce.fadd"(%{{.*}}, %{{.*}}) {fastmathFlags = #llvm.fastmath<reassoc>} : (f32, vector<4xf32>) -> f32 + // CHECK: llvm.return + cir.func @fastmath_flags(%arg0: !f32, %arg1: !v4f32) -> !f32 { + %0 = cir.call_llvm_intrinsic "vector.reduce.fadd" %arg0, %arg1 : (!f32, !v4f32) -> !f32 {fastmath_flags = #cir.fastmath<reassoc>} + cir.return %0 : !f32 + } } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
