[clang] [CIR][NFC] Add a CIR_FenvOp base class (PR #245984)
Henrich Lauko via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 25 04:11:05 PST 2026
https://github.com/xlauko created https://github.com/llvm/llvm-project/pull/245984
Each operation with the fenv traits repeated them and the optional `fenv` attribute in its own definition. This adds a `CIR_FenvOp` base class that carries both, and the operations prepend their own arguments to it. `CIR_FPBinaryOp` derives from `CIR_BinaryOp`, so it keeps the traits and now appends its attributes to the `CIR_BinaryOp` arguments instead of rebuilding the list. `cir.complex.fadd` and `cir.complex.fsub` take the attribute without the traits and are not changed here. The generated operation classes are unchanged.
Assisted-by: Claude Code (Fable 5.2).
>From 8c0487d39fc64cd59dfe821c34a032fc29a6341b Mon Sep 17 00:00:00 2001
From: Henrich Lauko <hlauko at nvidia.com>
Date: Wed, 25 Nov 2026 10:01:48 +0000
Subject: [PATCH] [CIR][NFC] Add a CIR_FenvOp base class
Each operation with the fenv traits repeated them and the optional
`fenv` attribute in its own definition. This adds a `CIR_FenvOp` base
class that carries both, and the operations prepend their own arguments
to it. `CIR_FPBinaryOp` derives from `CIR_BinaryOp`, so it keeps the
traits and now appends its attributes to the `CIR_BinaryOp` arguments
instead of rebuilding the list. `cir.complex.fadd` and
`cir.complex.fsub` take the attribute without the traits and are not
changed here.
The generated operation classes are unchanged.
Assisted-by: Claude Code (Fable 5.2).
---
clang/include/clang/CIR/Dialect/IR/CIROps.td | 90 +++++++++-----------
1 file changed, 40 insertions(+), 50 deletions(-)
diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index 01e4d9bfb6d20..3b76244741b93 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -225,6 +225,14 @@ defvar CIR_FenvOpTraits = [
CIR_FenvOpTrait
];
+// Base class for operations constrained by an optional `fenv` attribute.
+// Subclasses prepend their own arguments.
+class CIR_FenvOp<string mnemonic, list<Trait> traits = []>
+ : CIR_Op<mnemonic, traits> {
+ let append traits = CIR_FenvOpTraits;
+ let arguments = (ins OptionalAttr<CIR_FenvAttr>:$fenv);
+}
+
// Passed as CIR_BinaryOp's hasNoMemoryEffect: fenv ops compute effects
// dynamically via FenvOpTrait, so they must not inherit NoMemoryEffect.
defvar CIR_DynamicMemoryEffects = 0;
@@ -319,9 +327,9 @@ def CIR_CastKind : CIR_I32Enum<"CastKind", "cast kind", [
def CIR_CastKindAttr : CIR_EnumAttr<CIR_CastKind, "cast">;
-def CIR_CastOp : CIR_Op<"cast", !listconcat([
+def CIR_CastOp : CIR_FenvOp<"cast", [
DeclareOpInterfaceMethods<PromotableOpInterface>
-], CIR_FenvOpTraits)> {
+]> {
let summary = "Conversion between values of different types";
let description = [{
Apply the usual C/C++ conversion rules between values. This operation models
@@ -375,8 +383,7 @@ def CIR_CastOp : CIR_Op<"cast", !listconcat([
```
}];
- let arguments = (ins CIR_CastKindAttr:$kind, CIR_AnyType:$src,
- OptionalAttr<CIR_FenvAttr>:$fenv);
+ let prepend arguments = (ins CIR_CastKindAttr:$kind, CIR_AnyType:$src);
let results = (outs CIR_AnyType:$result);
let assemblyFormat = [{
@@ -2697,8 +2704,7 @@ def CIR_CmpOpKind : CIR_I32Enum<"CmpOpKind", "compare operation kind", [
def CIR_CmpOpKindAttr : CIR_EnumAttr<CIR_CmpOpKind, "cmp">;
-def CIR_CmpOp : CIR_Op<"cmp",
- !listconcat([SameTypeOperands], CIR_FenvOpTraits)> {
+def CIR_CmpOp : CIR_FenvOp<"cmp", [SameTypeOperands]> {
let summary = "Compare two values and produce a boolean result";
let description = [{
The `cir.cmp` operation compares two operands of the same type and produces
@@ -2733,11 +2739,10 @@ def CIR_CmpOp : CIR_Op<"cmp",
```
}];
- let arguments = (ins
+ let prepend arguments = (ins
CIR_CmpOpKindAttr:$kind,
CIR_ComparableType:$lhs,
- CIR_ComparableType:$rhs,
- OptionalAttr<CIR_FenvAttr>:$fenv
+ CIR_ComparableType:$rhs
);
let results = (outs CIR_BoolType:$result);
@@ -2788,8 +2793,7 @@ def CIR_FCmpOpKind : CIR_I32Enum<"FCmpOpKind", "floating compare operation kind"
def CIR_FCmpOpKindAttr : CIR_EnumAttr<CIR_FCmpOpKind, "fcmp">;
-def CIR_FCmpOp : CIR_Op<"fcmp",
- !listconcat([SameTypeOperands], CIR_FenvOpTraits)> {
+def CIR_FCmpOp : CIR_FenvOp<"fcmp", [SameTypeOperands]> {
let summary = "Compare two floating point values and produce a boolean result";
let description = [{
The `cir.fcmp` operation compares two operands of the same floating type and
@@ -2827,12 +2831,11 @@ def CIR_FCmpOp : CIR_Op<"fcmp",
```
}];
- let arguments = (ins
+ let prepend arguments = (ins
CIR_FCmpOpKindAttr:$kind,
CIR_AnyFloatType:$lhs,
CIR_AnyFloatType:$rhs,
- UnitAttr:$signaling,
- OptionalAttr<CIR_FenvAttr>:$fenv
+ UnitAttr:$signaling
);
let results = (outs CIR_BoolType:$result);
@@ -2963,13 +2966,11 @@ class CIR_BinaryOp<string mnemonic, Type type, list<Trait> traits = [],
DeclareOpInterfaceMethods<CIR_BinaryOpInterface>
], traits)>
{
- dag commonArgs = (ins
+ let arguments = (ins
type:$lhs,
type:$rhs
);
- let arguments = commonArgs;
-
let results = (outs type:$result);
let assemblyFormat = [{
@@ -3182,9 +3183,10 @@ class CIR_FPBinaryOp<string mnemonic, list<Trait> traits = [],
: CIR_BinaryOp<mnemonic, type,
!listconcat(CIR_FenvOpTraits, traits),
CIR_DynamicMemoryEffects> {
- let arguments = !con(commonArgs, (ins
- OptionalAttr<CIR_FenvAttr>:$fenv,
- OptionalAttr<CIR_FastMathFlagsAttr>:$fastmath_flags));
+ let append arguments = (ins
+ OptionalAttr<CIR_FenvAttr>:$fenv,
+ OptionalAttr<CIR_FastMathFlagsAttr>:$fastmath_flags
+ );
let assemblyFormat = [{
$lhs `,` $rhs `:` type($lhs) (`fenv` `` $fenv^)? prop-dict attr-dict
}];
@@ -7003,8 +7005,7 @@ def CIR_VecCmpOp : CIR_Op<"vec.cmp", [Pure, SameTypeOperands]> {
// VecFCmpOp
//===----------------------------------------------------------------------===//
-def CIR_VecFCmpOp : CIR_Op<"vec.fcmp",
- !listconcat([SameTypeOperands], CIR_FenvOpTraits)> {
+def CIR_VecFCmpOp : CIR_FenvOp<"vec.fcmp", [SameTypeOperands]> {
let summary = "Compare two floating point vectors";
let description = [{
The `cir.vec.fcmp` operation does an element-wise comparison of two vectors
@@ -7025,12 +7026,11 @@ def CIR_VecFCmpOp : CIR_Op<"vec.fcmp",
```
}];
- let arguments = (ins
+ let prepend arguments = (ins
CIR_FCmpOpKindAttr:$kind,
CIR_VectorOfFloatType:$lhs,
CIR_VectorOfFloatType:$rhs,
- UnitAttr:$signaling,
- OptionalAttr<CIR_FenvAttr>:$fenv
+ UnitAttr:$signaling
);
let results = (outs CIR_VectorType:$result);
@@ -8828,11 +8828,8 @@ def CIR_PtrDiffOp : CIR_Op<"ptr_diff", [Pure, SameTypeOperands]> {
// The optional `fenv` attribute describes constraints on the floating-point
// handling of the operation.
class CIR_UnaryFPToFPBuiltinOp<string mnemonic, string llvmOpName>
- : CIR_Op<mnemonic,
- !listconcat([SameOperandsAndResultType], CIR_FenvOpTraits)>
-{
- let arguments = (ins CIR_AnyFloatOrVecOfFloatType:$src,
- OptionalAttr<CIR_FenvAttr>:$fenv);
+ : CIR_FenvOp<mnemonic, [SameOperandsAndResultType]> {
+ let prepend arguments = (ins CIR_AnyFloatOrVecOfFloatType:$src);
let results = (outs CIR_AnyFloatOrVecOfFloatType:$result);
let assemblyFormat = "$src `:` type($src) (`fenv` `` $fenv^)? attr-dict";
@@ -9154,10 +9151,8 @@ def CIR_FloorOp : CIR_UnaryFPToFPBuiltinOp<"floor", "FFloorOp"> {
}
class CIR_UnaryFPToIntBuiltinOp<string mnemonic, string llvmOpName>
- : CIR_Op<mnemonic, CIR_FenvOpTraits>
-{
- let arguments = (ins CIR_AnyFloatType:$src,
- OptionalAttr<CIR_FenvAttr>:$fenv);
+ : CIR_FenvOp<mnemonic> {
+ let prepend arguments = (ins CIR_AnyFloatType:$src);
let results = (outs CIR_IntType:$result);
let summary = [{
@@ -9218,16 +9213,14 @@ def CIR_LlrintOp : CIR_UnaryFPToIntBuiltinOp<"llrint", "LlrintOp"> {
}
class CIR_BinaryFPToFPBuiltinOp<string mnemonic, string llvmOpName>
- : CIR_Op<mnemonic,
- !listconcat([SameOperandsAndResultType], CIR_FenvOpTraits)> {
+ : CIR_FenvOp<mnemonic, [SameOperandsAndResultType]> {
let summary = [{
libc builtin equivalent ignoring floating-point exceptions and errno.
}];
- let arguments = (ins
+ let prepend arguments = (ins
CIR_AnyFloatOrVecOfFloatType:$lhs,
- CIR_AnyFloatOrVecOfFloatType:$rhs,
- OptionalAttr<CIR_FenvAttr>:$fenv
+ CIR_AnyFloatOrVecOfFloatType:$rhs
);
let results = (outs CIR_AnyFloatOrVecOfFloatType:$result);
@@ -9330,8 +9323,7 @@ def CIR_ATan2Op : CIR_BinaryFPToFPBuiltinOp<"atan2", "ATan2Op"> {
let constrainedLLVMIntrinsic = "atan2";
}
-def CIR_LdexpOp : CIR_Op<"ldexp",
- !listconcat([AllTypesMatch<["src", "result"]>], CIR_FenvOpTraits)> {
+def CIR_LdexpOp : CIR_FenvOp<"ldexp", [AllTypesMatch<["src", "result"]>]> {
let summary = "Multiplies a floating-point value by an integral power of two";
let description = [{
`cir.ldexp` computes `src * 2^exp`.
@@ -9343,10 +9335,9 @@ def CIR_LdexpOp : CIR_Op<"ldexp",
%r = cir.ldexp %x, %e : !cir.float, !s32i
}];
- let arguments = (ins
+ let prepend arguments = (ins
CIR_AnyFloatOrVecOfFloatType:$src,
- CIR_AnyIntOrVecOfIntType:$exp,
- OptionalAttr<CIR_FenvAttr>:$fenv
+ CIR_AnyIntOrVecOfIntType:$exp
);
let results = (outs CIR_AnyFloatOrVecOfFloatType:$result);
@@ -9412,14 +9403,13 @@ def CIR_ModfOp : CIR_Op<"modf", [Pure]> {
// The optional `fastmath_flags` attribute holds the fast-math flags of the
// operation.
class CIR_TernaryFPToFPBuiltinOp<string mnemonic, string llvmOpName>
- : CIR_Op<mnemonic,
- !listconcat([SameOperandsAndResultType], CIR_FenvOpTraits)>
-{
- let arguments = (ins
+ : CIR_FenvOp<mnemonic, [SameOperandsAndResultType]> {
+ let prepend arguments = (ins
CIR_AnyFloatOrVecOfFloatType:$a,
CIR_AnyFloatOrVecOfFloatType:$b,
- CIR_AnyFloatOrVecOfFloatType:$c,
- OptionalAttr<CIR_FenvAttr>:$fenv,
+ CIR_AnyFloatOrVecOfFloatType:$c
+ );
+ let append arguments = (ins
OptionalAttr<CIR_FastMathFlagsAttr>:$fastmath_flags
);
More information about the cfe-commits
mailing list