Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CIR][Transforms] Simplify redundant bitcasts #591

Merged
merged 5 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,17 +501,24 @@ LogicalResult CastOp::verify() {
}

OpFoldResult CastOp::fold(FoldAdaptor adaptor) {
if (getKind() != mlir::cir::CastKind::integral)
return {};
if (getSrc().getType() != getResult().getType())
return {};
// TODO: for sign differences, it's possible in certain conditions to
// create a new attributes that's capable or representing the source.
SmallVector<mlir::OpFoldResult, 1> foldResults;
auto foldOrder = getSrc().getDefiningOp()->fold(foldResults);
if (foldOrder.succeeded() && foldResults[0].is<mlir::Attribute>())
return foldResults[0].get<mlir::Attribute>();
return {};
switch (getKind()) {
case mlir::cir::CastKind::integral: {
// TODO: for sign differences, it's possible in certain conditions to
// create a new attribute that's capable of representing the source.
SmallVector<mlir::OpFoldResult, 1> foldResults;
auto foldOrder = getSrc().getDefiningOp()->fold(foldResults);
if (foldOrder.succeeded() && foldResults[0].is<mlir::Attribute>())
return foldResults[0].get<mlir::Attribute>();
return {};
}
case mlir::cir::CastKind::bitcast: {
return getSrc();
}
default:
return {};
}
}

//===----------------------------------------------------------------------===//
Expand Down
8 changes: 5 additions & 3 deletions clang/lib/CIR/Dialect/Transforms/MergeCleanups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace {
/// To:
/// ^bb0:
/// cir.return
struct RemoveRedudantBranches : public OpRewritePattern<BrOp> {
struct RemoveRedundantBranches : public OpRewritePattern<BrOp> {
using OpRewritePattern<BrOp>::OpRewritePattern;

LogicalResult matchAndRewrite(BrOp op,
Expand Down Expand Up @@ -101,7 +101,7 @@ struct MergeCleanupsPass : public MergeCleanupsBase<MergeCleanupsPass> {
void populateMergeCleanupPatterns(RewritePatternSet &patterns) {
// clang-format off
patterns.add<
RemoveRedudantBranches,
RemoveRedundantBranches,
RemoveEmptyScope,
RemoveEmptySwitch
>(patterns.getContext());
Expand All @@ -116,7 +116,9 @@ void MergeCleanupsPass::runOnOperation() {
// Collect operations to apply patterns.
SmallVector<Operation *, 16> ops;
getOperation()->walk([&](Operation *op) {
if (isa<BrOp, BrCondOp, ScopeOp, SwitchOp>(op))
// CastOp here is to perform a manual `fold` in
// applyOpPatternsAndFold
if (isa<BrOp, BrCondOp, ScopeOp, SwitchOp, CastOp>(op))
ops.push_back(op);
});

Expand Down
3 changes: 1 addition & 2 deletions clang/test/CIR/CodeGen/no-proto-fun-ptr.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ void check_noproto_ptr() {
// CHECK: cir.func no_proto @check_noproto_ptr()
// CHECK: [[ALLOC:%.*]] = cir.alloca !cir.ptr<!cir.func<!void ()>>, !cir.ptr<!cir.ptr<!cir.func<!void ()>>>, ["fun", init] {alignment = 8 : i64}
// CHECK: [[GGO:%.*]] = cir.get_global @empty : !cir.ptr<!cir.func<!void ()>>
// CHECK: [[CAST:%.*]] = cir.cast(bitcast, [[GGO]] : !cir.ptr<!cir.func<!void ()>>), !cir.ptr<!cir.func<!void ()>>
// CHECK: cir.store [[CAST]], [[ALLOC]] : !cir.ptr<!cir.func<!void ()>>, !cir.ptr<!cir.ptr<!cir.func<!void ()>>>
// CHECK: cir.store [[GGO]], [[ALLOC]] : !cir.ptr<!cir.func<!void ()>>, !cir.ptr<!cir.ptr<!cir.func<!void ()>>>
// CHECK: cir.return

void empty(void) {}
Expand Down
3 changes: 1 addition & 2 deletions clang/test/CIR/CodeGen/no-prototype.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ int noProto2();
int test2(int x) {
return noProto2(x);
// CHECK: [[GGO:%.*]] = cir.get_global @noProto2 : !cir.ptr<!cir.func<!s32i (!s32i)>>
// CHECK: [[CAST:%.*]] = cir.cast(bitcast, %3 : !cir.ptr<!cir.func<!s32i (!s32i)>>), !cir.ptr<!cir.func<!s32i (!s32i)>>
// CHECK: {{.*}} = cir.call [[CAST]](%{{[0-9]+}}) : (!cir.ptr<!cir.func<!s32i (!s32i)>>, !s32i) -> !s32i
// CHECK: {{.*}} = cir.call [[GGO]](%{{[0-9]+}}) : (!cir.ptr<!cir.func<!s32i (!s32i)>>, !s32i) -> !s32i
}
int noProto2(int x) { return x; }
// CHECK: cir.func no_proto @noProto2(%arg0: !s32i {{.+}}) -> !s32i
Expand Down
30 changes: 23 additions & 7 deletions clang/test/CIR/CodeGen/unary.c
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -Wno-unused-value -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s

void valueNegation(int i, short s, long l, float f, double d) {
// CHECK: cir.func @valueNegation(
!i;
int valueNegationInt(int i) {
// CHECK: cir.func @valueNegationInt(
return !i;
// CHECK: %[[#INT:]] = cir.load %{{[0-9]+}} : !cir.ptr<!s32i>, !s32i
// CHECK: %[[#INT_TO_BOOL:]] = cir.cast(int_to_bool, %[[#INT]] : !s32i), !cir.bool
// CHECK: = cir.unary(not, %[[#INT_TO_BOOL]]) : !cir.bool, !cir.bool
!s;
}

short valueNegationShort(short s) {
// CHECK: cir.func @valueNegationShort(
return !s;
// CHECK: %[[#SHORT:]] = cir.load %{{[0-9]+}} : !cir.ptr<!s16i>, !s16i
// CHECK: %[[#SHORT_TO_BOOL:]] = cir.cast(int_to_bool, %[[#SHORT]] : !s16i), !cir.bool
// CHECK: = cir.unary(not, %[[#SHORT_TO_BOOL]]) : !cir.bool, !cir.bool
!l;
}

long valueNegationLong(long l) {
// CHECK: cir.func @valueNegationLong(
return !l;
// CHECK: %[[#LONG:]] = cir.load %{{[0-9]+}} : !cir.ptr<!s64i>, !s64i
// CHECK: %[[#LONG_TO_BOOL:]] = cir.cast(int_to_bool, %[[#LONG]] : !s64i), !cir.bool
// CHECK: = cir.unary(not, %[[#LONG_TO_BOOL]]) : !cir.bool, !cir.bool
!f;
}

float valueNegationFloat(float f) {
// CHECK: cir.func @valueNegationFloat(
return !f;
// CHECK: %[[#FLOAT:]] = cir.load %{{[0-9]+}} : !cir.ptr<!cir.float>, !cir.float
// CHECK: %[[#FLOAT_TO_BOOL:]] = cir.cast(float_to_bool, %[[#FLOAT]] : !cir.float), !cir.bool
// CHECK: %[[#FLOAT_NOT:]] = cir.unary(not, %[[#FLOAT_TO_BOOL]]) : !cir.bool, !cir.bool
// CHECK: = cir.cast(bool_to_int, %[[#FLOAT_NOT]] : !cir.bool), !s32i
!d;
}

double valueNegationDouble(double d) {
// CHECK: cir.func @valueNegationDouble(
return !d;
// CHECK: %[[#DOUBLE:]] = cir.load %{{[0-9]+}} : !cir.ptr<!cir.double>, !cir.double
// CHECK: %[[#DOUBLE_TO_BOOL:]] = cir.cast(float_to_bool, %[[#DOUBLE]] : !cir.double), !cir.bool
// CHECK: %[[#DOUBLE_NOT:]] = cir.unary(not, %[[#DOUBLE_TO_BOOL]]) : !cir.bool, !cir.bool
Expand Down
9 changes: 9 additions & 0 deletions clang/test/CIR/Transforms/merge-cleanups.cir
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,13 @@ module {
// CHECK: cir.return
}

// Should remove redundant bitcasts.
// CHECK-LABEL: @ptrbitcastfold
// CHECK: %[[ARG0:.+]]: !cir.ptr<!s32i>
// CHECK: cir.return %[[ARG0]] : !cir.ptr<!s32i>
cir.func @ptrbitcastfold(%arg0: !cir.ptr<!s32i>) -> !cir.ptr<!s32i> {
%0 = cir.cast(bitcast, %arg0: !cir.ptr<!s32i>), !cir.ptr<!s32i>
cir.return %0 : !cir.ptr<!s32i>
}

}
Loading