Skip to content

Commit f25185b

Browse files
authored
[CIR] Fix unary op folding (llvm#132269)
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.
1 parent 6038077 commit f25185b

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

clang/include/clang/CIR/MissingFeatures.h

-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ struct MissingFeatures {
110110
static bool brCondOp() { return false; }
111111
static bool switchOp() { return false; }
112112
static bool tryOp() { return false; }
113-
static bool unaryOp() { return false; }
114113
static bool selectOp() { return false; }
115114
static bool complexCreateOp() { return false; }
116115
static bool complexRealOp() { return false; }

clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,14 @@ void CIRCanonicalizePass::runOnOperation() {
121121
assert(!cir::MissingFeatures::brCondOp());
122122
assert(!cir::MissingFeatures::switchOp());
123123
assert(!cir::MissingFeatures::tryOp());
124-
assert(!cir::MissingFeatures::unaryOp());
125124
assert(!cir::MissingFeatures::selectOp());
126125
assert(!cir::MissingFeatures::complexCreateOp());
127126
assert(!cir::MissingFeatures::complexRealOp());
128127
assert(!cir::MissingFeatures::complexImagOp());
129128
assert(!cir::MissingFeatures::callOp());
130-
// CastOp here is to perform a manual `fold` in
131-
// applyOpPatternsGreedily
132-
if (isa<BrOp, ScopeOp, CastOp>(op))
129+
// CastOp and UnaryOp are here to perform a manual `fold` in
130+
// applyOpPatternsGreedily.
131+
if (isa<BrOp, ScopeOp, CastOp, UnaryOp>(op))
133132
ops.push_back(op);
134133
});
135134

clang/test/CIR/Transforms/canonicalize.cir

+8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ module {
3131
// CHECK-NEXT: cir.return
3232
// CHECK-NEXT: }
3333

34+
cir.func @unary_not(%arg0: !cir.bool) -> !cir.bool {
35+
%0 = cir.unary(not, %arg0) : !cir.bool, !cir.bool
36+
%1 = cir.unary(not, %0) : !cir.bool, !cir.bool
37+
cir.return %1 : !cir.bool
38+
}
39+
// CHECK: cir.func @unary_not(%arg0: !cir.bool) -> !cir.bool
40+
// CHECK-NEXT: cir.return %arg0 : !cir.bool
41+
3442
cir.func @cast1(%arg0: !cir.bool) -> !cir.bool {
3543
%0 = cir.cast(bool_to_int, %arg0 : !cir.bool), !s32i
3644
%1 = cir.cast(int_to_bool, %0 : !s32i), !cir.bool

0 commit comments

Comments
 (0)