[clang] [CIR] Fix unary op folding (PR #132269)

Andy Kaylor via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 20 11:30:45 PDT 2025


https://github.com/andykaylor updated https://github.com/llvm/llvm-project/pull/132269

>From bee4e73e125c5643eb321ffef7b2b8e25f881267 Mon Sep 17 00:00:00 2001
From: Andy Kaylor <akaylor at nvidia.com>
Date: Thu, 20 Mar 2025 11:23:02 -0700
Subject: [PATCH 1/2] [CIR] Fix unary op folding

Unary ops had previously been omitted from the list of ops handled in the
CIRCanonicalizePass. Although the incubator code doesn't use them directly,
the mlir folding code does.

This change enables folding of unary ops by adding them to the list.
---
 clang/include/clang/CIR/MissingFeatures.h            | 1 -
 clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp | 7 +++----
 clang/test/CIR/Transforms/canonicalize.cir           | 8 ++++++++
 3 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/clang/include/clang/CIR/MissingFeatures.h b/clang/include/clang/CIR/MissingFeatures.h
index d276af5686995..46780775dd4bd 100644
--- a/clang/include/clang/CIR/MissingFeatures.h
+++ b/clang/include/clang/CIR/MissingFeatures.h
@@ -110,7 +110,6 @@ struct MissingFeatures {
   static bool brCondOp() { return false; }
   static bool switchOp() { return false; }
   static bool tryOp() { return false; }
-  static bool unaryOp() { return false; }
   static bool selectOp() { return false; }
   static bool complexCreateOp() { return false; }
   static bool complexRealOp() { return false; }
diff --git a/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp b/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp
index d32691dba1473..6cbaf26a0bf33 100644
--- a/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp
@@ -121,15 +121,14 @@ void CIRCanonicalizePass::runOnOperation() {
     assert(!cir::MissingFeatures::brCondOp());
     assert(!cir::MissingFeatures::switchOp());
     assert(!cir::MissingFeatures::tryOp());
-    assert(!cir::MissingFeatures::unaryOp());
     assert(!cir::MissingFeatures::selectOp());
     assert(!cir::MissingFeatures::complexCreateOp());
     assert(!cir::MissingFeatures::complexRealOp());
     assert(!cir::MissingFeatures::complexImagOp());
     assert(!cir::MissingFeatures::callOp());
-    // CastOp here is to perform a manual `fold` in
-    // applyOpPatternsGreedily
-    if (isa<BrOp, ScopeOp, CastOp>(op))
+    // CastOp and UnaryOp are here to perform a manual `fold` in
+    // applyOpPatternsGreedily.
+    if (isa<BrOp, ScopeOp, CastOp, UnaryOp>(op))
       ops.push_back(op);
   });
 
diff --git a/clang/test/CIR/Transforms/canonicalize.cir b/clang/test/CIR/Transforms/canonicalize.cir
index d61991aef6f01..c0c32627a1096 100644
--- a/clang/test/CIR/Transforms/canonicalize.cir
+++ b/clang/test/CIR/Transforms/canonicalize.cir
@@ -31,6 +31,14 @@ module {
   // CHECK-NEXT:   cir.return
   // CHECK-NEXT: }
 
+  cir.func @unary_not(%arg0: !cir.bool) -> !cir.bool {
+    %0 = cir.unary(not, %arg0) : !cir.bool, !cir.bool
+    %1 = cir.unary(not, %0) : !cir.bool, !cir.bool
+    cir.return %1 : !cir.bool
+  }
+  // CHECK:  cir.func @unary_not(%arg0: !cir.bool) -> !cir.bool
+  // CHECK:     cir.return %arg0 : !cir.bool
+
   cir.func @cast1(%arg0: !cir.bool) -> !cir.bool {
     %0 = cir.cast(bool_to_int, %arg0 : !cir.bool), !s32i
     %1 = cir.cast(int_to_bool, %0 : !s32i), !cir.bool

>From 9640f595adf3ea63f746418a6798bf2970b4ddf5 Mon Sep 17 00:00:00 2001
From: Andy Kaylor <akaylor at nvidia.com>
Date: Thu, 20 Mar 2025 11:30:13 -0700
Subject: [PATCH 2/2] Test update

---
 clang/test/CIR/Transforms/canonicalize.cir | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/test/CIR/Transforms/canonicalize.cir b/clang/test/CIR/Transforms/canonicalize.cir
index c0c32627a1096..164d231db7bb4 100644
--- a/clang/test/CIR/Transforms/canonicalize.cir
+++ b/clang/test/CIR/Transforms/canonicalize.cir
@@ -36,8 +36,8 @@ module {
     %1 = cir.unary(not, %0) : !cir.bool, !cir.bool
     cir.return %1 : !cir.bool
   }
-  // CHECK:  cir.func @unary_not(%arg0: !cir.bool) -> !cir.bool
-  // CHECK:     cir.return %arg0 : !cir.bool
+  // CHECK:      cir.func @unary_not(%arg0: !cir.bool) -> !cir.bool
+  // CHECK-NEXT:   cir.return %arg0 : !cir.bool
 
   cir.func @cast1(%arg0: !cir.bool) -> !cir.bool {
     %0 = cir.cast(bool_to_int, %arg0 : !cir.bool), !s32i



More information about the cfe-commits mailing list