[clang] [CIR] Add Commutative/Idempotent traits to binary ops (PR #185163)
Henrich Lauko via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 6 23:46:39 PST 2026
https://github.com/xlauko created https://github.com/llvm/llvm-project/pull/185163
Add missing MLIR traits to CIR binary operations, matching the arith
dialect conventions:
- AndOp, OrOp: Commutative, Idempotent (fixes FIXME)
- AddOp, MulOp, XorOp, MaxOp: Commutative
Add these ops to the CIRCanonicalize pass op list so trait-based
folding is exercised by applyOpPatternsGreedily.
Update testFloatingPointBinOps in binop.cpp to use computed values,
preventing DCE of the now-canonicalized ops.
>From cf4d3060b394d0616a758b98daf45244187ab72b Mon Sep 17 00:00:00 2001
From: xlauko <xlauko at mail.muni.cz>
Date: Sat, 7 Mar 2026 08:42:26 +0100
Subject: [PATCH] [CIR] Add Commutative/Idempotent traits to binary ops
Add missing MLIR traits to CIR binary operations, matching the arith
dialect conventions:
- AndOp, OrOp: Commutative, Idempotent (fixes FIXME)
- AddOp, MulOp, XorOp, MaxOp: Commutative
Add these ops to the CIRCanonicalize pass op list so trait-based
folding is exercised by applyOpPatternsGreedily.
Update testFloatingPointBinOps in binop.cpp to use computed values,
preventing DCE of the now-canonicalized ops.
---
clang/include/clang/CIR/Dialect/IR/CIROps.td | 20 +++---
.../Dialect/Transforms/CIRCanonicalize.cpp | 11 ++--
clang/test/CIR/CodeGen/binop.cpp | 46 ++++++++-----
clang/test/CIR/Transforms/binop-traits.cir | 65 +++++++++++++++++++
4 files changed, 111 insertions(+), 31 deletions(-)
create mode 100644 clang/test/CIR/Transforms/binop-traits.cir
diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index 79eef71229192..9e0d4e6732021 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -2186,7 +2186,9 @@ class CIR_BinaryOpWithOverflowFlags<string mnemonic, Type type,
// AddOp
//===----------------------------------------------------------------------===//
-def CIR_AddOp : CIR_BinaryOpWithOverflowFlags<"add", CIR_AnyArithType> {
+def CIR_AddOp : CIR_BinaryOpWithOverflowFlags<"add", CIR_AnyArithType, [
+ Commutative
+]> {
let summary = "Integer or floating-point addition";
let description = [{
The `cir.add` operation performs addition on integer or floating-point
@@ -2241,7 +2243,7 @@ def CIR_SubOp : CIR_BinaryOpWithOverflowFlags<"sub", CIR_AnyArithType> {
// MulOp
//===----------------------------------------------------------------------===//
-def CIR_MulOp : CIR_BinaryOp<"mul", CIR_AnyArithType> {
+def CIR_MulOp : CIR_BinaryOp<"mul", CIR_AnyArithType, [Commutative]> {
let summary = "Integer or floating-point multiplication";
let description = [{
The `cir.mul` operation performs multiplication on integer or floating-point
@@ -2321,8 +2323,9 @@ def CIR_RemOp : CIR_BinaryOp<"rem", CIR_AnyArithType> {
// AndOp
//===----------------------------------------------------------------------===//
-// FIXME: Commutative, Idempotent traits
-def CIR_AndOp : CIR_BinaryOp<"and", CIR_AnyBitwiseType> {
+def CIR_AndOp : CIR_BinaryOp<"and", CIR_AnyBitwiseType, [
+ Commutative, Idempotent
+]> {
let summary = "Bitwise AND";
let description = [{
The `cir.and` operation performs a bitwise AND on integer operands.
@@ -2341,8 +2344,9 @@ def CIR_AndOp : CIR_BinaryOp<"and", CIR_AnyBitwiseType> {
// OrOp
//===----------------------------------------------------------------------===//
-// FIXME: Commutative, Idempotent traits
-def CIR_OrOp : CIR_BinaryOp<"or", CIR_AnyBitwiseType> {
+def CIR_OrOp : CIR_BinaryOp<"or", CIR_AnyBitwiseType, [
+ Commutative, Idempotent
+]> {
let summary = "Bitwise OR";
let description = [{
The `cir.or` operation performs a bitwise OR on integer operands.
@@ -2361,7 +2365,7 @@ def CIR_OrOp : CIR_BinaryOp<"or", CIR_AnyBitwiseType> {
// XorOp
//===----------------------------------------------------------------------===//
-def CIR_XorOp : CIR_BinaryOp<"xor", CIR_AnyBitwiseType> {
+def CIR_XorOp : CIR_BinaryOp<"xor", CIR_AnyBitwiseType, [Commutative]> {
let summary = "Bitwise XOR";
let description = [{
The `cir.xor` operation performs a bitwise XOR on integer operands.
@@ -2380,7 +2384,7 @@ def CIR_XorOp : CIR_BinaryOp<"xor", CIR_AnyBitwiseType> {
// MaxOp
//===----------------------------------------------------------------------===//
-def CIR_MaxOp : CIR_BinaryOp<"max", CIR_AnyIntOrVecOfIntType> {
+def CIR_MaxOp : CIR_BinaryOp<"max", CIR_AnyIntOrVecOfIntType, [Commutative]> {
let summary = "Integer maximum";
let description = [{
The `cir.max` operation computes the maximum of two integer operands.
diff --git a/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp b/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp
index e9b825606a04e..46a8c011b320b 100644
--- a/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp
@@ -70,11 +70,12 @@ void CIRCanonicalizePass::runOnOperation() {
// Many operations are here to perform a manual `fold` in
// applyOpPatternsGreedily.
- if (isa<BrOp, BrCondOp, CastOp, ScopeOp, SwitchOp, SelectOp, UnaryOp,
- ComplexCreateOp, ComplexImagOp, ComplexRealOp, VecCmpOp,
- VecCreateOp, VecExtractOp, VecShuffleOp, VecShuffleDynamicOp,
- VecTernaryOp, BitClrsbOp, BitClzOp, BitCtzOp, BitFfsOp, BitParityOp,
- BitPopcountOp, BitReverseOp, ByteSwapOp, RotateOp, ConstantOp>(op))
+ if (isa<BrOp, BrCondOp, CastOp, ScopeOp, SwitchOp, SelectOp, UnaryOp, AddOp,
+ MulOp, AndOp, OrOp, XorOp, MaxOp, ComplexCreateOp, ComplexImagOp,
+ ComplexRealOp, VecCmpOp, VecCreateOp, VecExtractOp, VecShuffleOp,
+ VecShuffleDynamicOp, VecTernaryOp, BitClrsbOp, BitClzOp, BitCtzOp,
+ BitFfsOp, BitParityOp, BitPopcountOp, BitReverseOp, ByteSwapOp,
+ RotateOp, ConstantOp>(op))
ops.push_back(op);
});
diff --git a/clang/test/CIR/CodeGen/binop.cpp b/clang/test/CIR/CodeGen/binop.cpp
index 3d883f14acdc9..33498b90e3a42 100644
--- a/clang/test/CIR/CodeGen/binop.cpp
+++ b/clang/test/CIR/CodeGen/binop.cpp
@@ -127,10 +127,10 @@ void b0(int a, int b) {
// OGCG: ret void
void testFloatingPointBinOps(float a, float b) {
- a * b;
- a / b;
- a + b;
- a - b;
+ float x = a * b;
+ x = x / b;
+ x = x + b;
+ x = x - b;
}
// CIR-LABEL: cir.func{{.*}} @_Z23testFloatingPointBinOpsff(
@@ -144,48 +144,58 @@ void testFloatingPointBinOps(float a, float b) {
// LLVM-SAME: float {{.*}} %[[A:.*]], float {{.*}} %[[B:.*]])
// LLVM: %[[A_ADDR:.*]] = alloca float, i64 1
// LLVM: %[[B_ADDR:.*]] = alloca float, i64 1
+// LLVM: %[[X_ADDR:.*]] = alloca float, i64 1
// LLVM: store float %[[A]], ptr %[[A_ADDR]]
// LLVM: store float %[[B]], ptr %[[B_ADDR]]
// LLVM: %[[A1:.*]] = load float, ptr %[[A_ADDR]]
// LLVM: %[[B1:.*]] = load float, ptr %[[B_ADDR]]
-// LLVM: fmul float %[[A1]], %[[B1]]
+// LLVM: %[[MUL:.*]] = fmul float %[[A1]], %[[B1]]
+// LLVM: store float %[[MUL]], ptr %[[X_ADDR]]
-// LLVM: %[[A2:.*]] = load float, ptr %[[A_ADDR]]
+// LLVM: %[[X1:.*]] = load float, ptr %[[X_ADDR]]
// LLVM: %[[B2:.*]] = load float, ptr %[[B_ADDR]]
-// LLVM: fdiv float %[[A2]], %[[B2]]
+// LLVM: %[[DIV:.*]] = fdiv float %[[X1]], %[[B2]]
+// LLVM: store float %[[DIV]], ptr %[[X_ADDR]]
-// LLVM: %[[A3:.*]] = load float, ptr %[[A_ADDR]]
+// LLVM: %[[X2:.*]] = load float, ptr %[[X_ADDR]]
// LLVM: %[[B3:.*]] = load float, ptr %[[B_ADDR]]
-// LLVM: fadd float %[[A3]], %[[B3]]
+// LLVM: %[[ADD:.*]] = fadd float %[[X2]], %[[B3]]
+// LLVM: store float %[[ADD]], ptr %[[X_ADDR]]
-// LLVM: %[[A4:.*]] = load float, ptr %[[A_ADDR]]
+// LLVM: %[[X3:.*]] = load float, ptr %[[X_ADDR]]
// LLVM: %[[B4:.*]] = load float, ptr %[[B_ADDR]]
-// LLVM: fsub float %[[A4]], %[[B4]]
+// LLVM: %[[SUB:.*]] = fsub float %[[X3]], %[[B4]]
+// LLVM: store float %[[SUB]], ptr %[[X_ADDR]]
// LLVM: ret void
// OGCG-LABEL: define{{.*}} void @_Z23testFloatingPointBinOpsff(float {{.*}} %a, float {{.*}} %b)
// OGCG: %a.addr = alloca float
// OGCG: %b.addr = alloca float
+// OGCG: %x = alloca float
// OGCG: store float %a, ptr %a.addr
// OGCG: store float %b, ptr %b.addr
// OGCG: %[[A1:.*]] = load float, ptr %a.addr
// OGCG: %[[B1:.*]] = load float, ptr %b.addr
-// OGCG: fmul float %[[A1]], %[[B1]]
+// OGCG: %[[MUL:.*]] = fmul float %[[A1]], %[[B1]]
+// OGCG: store float %[[MUL]], ptr %x
-// OGCG: %[[A2:.*]] = load float, ptr %a.addr
+// OGCG: %[[X1:.*]] = load float, ptr %x
// OGCG: %[[B2:.*]] = load float, ptr %b.addr
-// OGCG: fdiv float %[[A2]], %[[B2]]
+// OGCG: %[[DIV:.*]] = fdiv float %[[X1]], %[[B2]]
+// OGCG: store float %[[DIV]], ptr %x
-// OGCG: %[[A3:.*]] = load float, ptr %a.addr
+// OGCG: %[[X2:.*]] = load float, ptr %x
// OGCG: %[[B3:.*]] = load float, ptr %b.addr
-// OGCG: fadd float %[[A3]], %[[B3]]
+// OGCG: %[[ADD:.*]] = fadd float %[[X2]], %[[B3]]
+// OGCG: store float %[[ADD]], ptr %x
-// OGCG: %[[A4:.*]] = load float, ptr %a.addr
+// OGCG: %[[X3:.*]] = load float, ptr %x
// OGCG: %[[B4:.*]] = load float, ptr %b.addr
-// OGCG: fsub float %[[A4]], %[[B4]]
+// OGCG: %[[SUB:.*]] = fsub float %[[X3]], %[[B4]]
+// OGCG: store float %[[SUB]], ptr %x
// OGCG: ret void
diff --git a/clang/test/CIR/Transforms/binop-traits.cir b/clang/test/CIR/Transforms/binop-traits.cir
new file mode 100644
index 0000000000000..df2b12cf9f178
--- /dev/null
+++ b/clang/test/CIR/Transforms/binop-traits.cir
@@ -0,0 +1,65 @@
+// RUN: cir-opt %s -cir-canonicalize -o - | FileCheck %s
+
+!s32i = !cir.int<s, 32>
+
+// CHECK-LABEL: @and_idempotent
+// CHECK-NEXT: cir.return %arg0
+cir.func @and_idempotent(%arg0 : !s32i) -> !s32i {
+ %0 = cir.and %arg0, %arg0 : !s32i
+ cir.return %0 : !s32i
+}
+
+// CHECK-LABEL: @or_idempotent
+// CHECK-NEXT: cir.return %arg0
+cir.func @or_idempotent(%arg0 : !s32i) -> !s32i {
+ %0 = cir.or %arg0, %arg0 : !s32i
+ cir.return %0 : !s32i
+}
+
+// CHECK-LABEL: @and_commutative
+// CHECK: cir.and %arg0, %{{.*}} : !s32i
+cir.func @and_commutative(%arg0 : !s32i) -> !s32i {
+ %0 = cir.const #cir.int<42> : !s32i
+ %1 = cir.and %0, %arg0 : !s32i
+ cir.return %1 : !s32i
+}
+
+// CHECK-LABEL: @or_commutative
+// CHECK: cir.or %arg0, %{{.*}} : !s32i
+cir.func @or_commutative(%arg0 : !s32i) -> !s32i {
+ %0 = cir.const #cir.int<42> : !s32i
+ %1 = cir.or %0, %arg0 : !s32i
+ cir.return %1 : !s32i
+}
+
+// CHECK-LABEL: @xor_commutative
+// CHECK: cir.xor %arg0, %{{.*}} : !s32i
+cir.func @xor_commutative(%arg0 : !s32i) -> !s32i {
+ %0 = cir.const #cir.int<42> : !s32i
+ %1 = cir.xor %0, %arg0 : !s32i
+ cir.return %1 : !s32i
+}
+
+// CHECK-LABEL: @add_commutative
+// CHECK: cir.add %arg0, %{{.*}} : !s32i
+cir.func @add_commutative(%arg0 : !s32i) -> !s32i {
+ %0 = cir.const #cir.int<42> : !s32i
+ %1 = cir.add %0, %arg0 : !s32i
+ cir.return %1 : !s32i
+}
+
+// CHECK-LABEL: @mul_commutative
+// CHECK: cir.mul %arg0, %{{.*}} : !s32i
+cir.func @mul_commutative(%arg0 : !s32i) -> !s32i {
+ %0 = cir.const #cir.int<42> : !s32i
+ %1 = cir.mul %0, %arg0 : !s32i
+ cir.return %1 : !s32i
+}
+
+// CHECK-LABEL: @max_commutative
+// CHECK: cir.max %arg0, %{{.*}} : !s32i
+cir.func @max_commutative(%arg0 : !s32i) -> !s32i {
+ %0 = cir.const #cir.int<42> : !s32i
+ %1 = cir.max %0, %arg0 : !s32i
+ cir.return %1 : !s32i
+}
More information about the cfe-commits
mailing list