Skip to content

Commit ebafe6f

Browse files
committed
[CIR][Transforms] Simplify redundant bitcasts
1 parent 91a4e15 commit ebafe6f

File tree

5 files changed

+22
-20
lines changed

5 files changed

+22
-20
lines changed

clang/lib/CIR/Dialect/IR/CIRDialect.cpp

+10-8
Original file line numberDiff line numberDiff line change
@@ -501,16 +501,18 @@ LogicalResult CastOp::verify() {
501501
}
502502

503503
OpFoldResult CastOp::fold(FoldAdaptor adaptor) {
504-
if (getKind() != mlir::cir::CastKind::integral)
505-
return {};
506504
if (getSrc().getType() != getResult().getType())
507505
return {};
508-
// TODO: for sign differences, it's possible in certain conditions to
509-
// create a new attributes that's capable or representing the source.
510-
SmallVector<mlir::OpFoldResult, 1> foldResults;
511-
auto foldOrder = getSrc().getDefiningOp()->fold(foldResults);
512-
if (foldOrder.succeeded() && foldResults[0].is<mlir::Attribute>())
513-
return foldResults[0].get<mlir::Attribute>();
506+
if (getKind() == mlir::cir::CastKind::integral) {
507+
// TODO: for sign differences, it's possible in certain conditions to
508+
// create a new attributes that's capable or representing the source.
509+
SmallVector<mlir::OpFoldResult, 1> foldResults;
510+
auto foldOrder = getSrc().getDefiningOp()->fold(foldResults);
511+
if (foldOrder.succeeded() && foldResults[0].is<mlir::Attribute>())
512+
return foldResults[0].get<mlir::Attribute>();
513+
} else if (getKind() == mlir::cir::CastKind::bitcast) {
514+
return getSrc();
515+
}
514516
return {};
515517
}
516518

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace {
3434
/// To:
3535
/// ^bb0:
3636
/// cir.return
37-
struct RemoveRedudantBranches : public OpRewritePattern<BrOp> {
37+
struct RemoveRedundantBranches : public OpRewritePattern<BrOp> {
3838
using OpRewritePattern<BrOp>::OpRewritePattern;
3939

4040
LogicalResult matchAndRewrite(BrOp op,
@@ -101,7 +101,7 @@ struct MergeCleanupsPass : public MergeCleanupsBase<MergeCleanupsPass> {
101101
void populateMergeCleanupPatterns(RewritePatternSet &patterns) {
102102
// clang-format off
103103
patterns.add<
104-
RemoveRedudantBranches,
104+
RemoveRedundantBranches,
105105
RemoveEmptyScope,
106106
RemoveEmptySwitch
107107
>(patterns.getContext());
@@ -116,7 +116,9 @@ void MergeCleanupsPass::runOnOperation() {
116116
// Collect operations to apply patterns.
117117
SmallVector<Operation *, 16> ops;
118118
getOperation()->walk([&](Operation *op) {
119-
if (isa<BrOp, BrCondOp, ScopeOp, SwitchOp>(op))
119+
// CastOp here is to perform an manual `fold` in
120+
// applyOpPatternsAndFold
121+
if (isa<BrOp, BrCondOp, ScopeOp, SwitchOp, CastOp>(op))
120122
ops.push_back(op);
121123
});
122124

clang/test/CIR/CodeGen/no-proto-fun-ptr.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ void check_noproto_ptr() {
99
// CHECK: cir.func no_proto @check_noproto_ptr()
1010
// CHECK: [[ALLOC:%.*]] = cir.alloca !cir.ptr<!cir.func<!void ()>>, !cir.ptr<!cir.ptr<!cir.func<!void ()>>>, ["fun", init] {alignment = 8 : i64}
1111
// CHECK: [[GGO:%.*]] = cir.get_global @empty : !cir.ptr<!cir.func<!void ()>>
12-
// CHECK: [[CAST:%.*]] = cir.cast(bitcast, [[GGO]] : !cir.ptr<!cir.func<!void ()>>), !cir.ptr<!cir.func<!void ()>>
13-
// CHECK: cir.store [[CAST]], [[ALLOC]] : !cir.ptr<!cir.func<!void ()>>, !cir.ptr<!cir.ptr<!cir.func<!void ()>>>
12+
// CHECK: cir.store [[GGO]], [[ALLOC]] : !cir.ptr<!cir.func<!void ()>>, !cir.ptr<!cir.ptr<!cir.func<!void ()>>>
1413
// CHECK: cir.return
1514

1615
void empty(void) {}

clang/test/CIR/CodeGen/no-prototype.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ int noProto2();
3636
int test2(int x) {
3737
return noProto2(x);
3838
// CHECK: [[GGO:%.*]] = cir.get_global @noProto2 : !cir.ptr<!cir.func<!s32i (!s32i)>>
39-
// CHECK: [[CAST:%.*]] = cir.cast(bitcast, %3 : !cir.ptr<!cir.func<!s32i (!s32i)>>), !cir.ptr<!cir.func<!s32i (!s32i)>>
40-
// CHECK: {{.*}} = cir.call [[CAST]](%{{[0-9]+}}) : (!cir.ptr<!cir.func<!s32i (!s32i)>>, !s32i) -> !s32i
39+
// CHECK: {{.*}} = cir.call [[GGO]](%{{[0-9]+}}) : (!cir.ptr<!cir.func<!s32i (!s32i)>>, !s32i) -> !s32i
4140
}
4241
int noProto2(int x) { return x; }
4342
// CHECK: cir.func no_proto @noProto2(%arg0: !s32i {{.+}}) -> !s32i

clang/test/CIR/CodeGen/unary.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@
33

44
void valueNegation(int i, short s, long l, float f, double d) {
55
// CHECK: cir.func @valueNegation(
6-
!i;
6+
i = !i;
77
// CHECK: %[[#INT:]] = cir.load %{{[0-9]+}} : !cir.ptr<!s32i>, !s32i
88
// CHECK: %[[#INT_TO_BOOL:]] = cir.cast(int_to_bool, %[[#INT]] : !s32i), !cir.bool
99
// CHECK: = cir.unary(not, %[[#INT_TO_BOOL]]) : !cir.bool, !cir.bool
10-
!s;
10+
s = !s;
1111
// CHECK: %[[#SHORT:]] = cir.load %{{[0-9]+}} : !cir.ptr<!s16i>, !s16i
1212
// CHECK: %[[#SHORT_TO_BOOL:]] = cir.cast(int_to_bool, %[[#SHORT]] : !s16i), !cir.bool
1313
// CHECK: = cir.unary(not, %[[#SHORT_TO_BOOL]]) : !cir.bool, !cir.bool
14-
!l;
14+
l = !l;
1515
// CHECK: %[[#LONG:]] = cir.load %{{[0-9]+}} : !cir.ptr<!s64i>, !s64i
1616
// CHECK: %[[#LONG_TO_BOOL:]] = cir.cast(int_to_bool, %[[#LONG]] : !s64i), !cir.bool
1717
// CHECK: = cir.unary(not, %[[#LONG_TO_BOOL]]) : !cir.bool, !cir.bool
18-
!f;
18+
f = !f;
1919
// CHECK: %[[#FLOAT:]] = cir.load %{{[0-9]+}} : !cir.ptr<!cir.float>, !cir.float
2020
// CHECK: %[[#FLOAT_TO_BOOL:]] = cir.cast(float_to_bool, %[[#FLOAT]] : !cir.float), !cir.bool
2121
// CHECK: %[[#FLOAT_NOT:]] = cir.unary(not, %[[#FLOAT_TO_BOOL]]) : !cir.bool, !cir.bool
2222
// CHECK: = cir.cast(bool_to_int, %[[#FLOAT_NOT]] : !cir.bool), !s32i
23-
!d;
23+
d = !d;
2424
// CHECK: %[[#DOUBLE:]] = cir.load %{{[0-9]+}} : !cir.ptr<!cir.double>, !cir.double
2525
// CHECK: %[[#DOUBLE_TO_BOOL:]] = cir.cast(float_to_bool, %[[#DOUBLE]] : !cir.double), !cir.bool
2626
// CHECK: %[[#DOUBLE_NOT:]] = cir.unary(not, %[[#DOUBLE_TO_BOOL]]) : !cir.bool, !cir.bool

0 commit comments

Comments
 (0)