-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[CIR] Add transform test for cir-flatten-cfg #130861
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// RUN: cir-opt %s -cir-flatten-cfg -o - | FileCheck %s | ||
|
||
module { | ||
cir.func @foo() { | ||
cir.scope { | ||
%0 = cir.alloca !cir.int<u, 32>, !cir.ptr<!cir.int<u, 32>>, ["a", init] {alignment = 4 : i64} | ||
%1 = cir.const #cir.int<4> : !cir.int<u, 32> | ||
cir.store %1, %0 : !cir.int<u, 32>, !cir.ptr<!cir.int<u, 32>> | ||
} | ||
cir.return | ||
} | ||
// CHECK: cir.func @foo() { | ||
// CHECK: cir.br ^bb1 | ||
// CHECK: ^bb1: // pred: ^bb0 | ||
// CHECK: %0 = cir.alloca !cir.int<u, 32>, !cir.ptr<!cir.int<u, 32>>, ["a", init] {alignment = 4 : i64} | ||
// CHECK: %1 = cir.const #cir.int<4> : !cir.int<u, 32> | ||
// CHECK: cir.store %1, %0 : !cir.int<u, 32>, !cir.ptr<!cir.int<u, 32>> | ||
// CHECK: cir.br ^bb2 | ||
// CHECK: ^bb2: // pred: ^bb1 | ||
// CHECK: cir.return | ||
// CHECK: } | ||
|
||
// Should drop empty scopes. | ||
cir.func @empty_scope() { | ||
cir.scope { | ||
} | ||
cir.return | ||
} | ||
// CHECK: cir.func @empty_scope() { | ||
// CHECK: cir.return | ||
// CHECK: } | ||
|
||
cir.func @scope_with_return() -> !cir.int<u, 32> { | ||
%0 = cir.alloca !cir.int<u, 32>, !cir.ptr<!cir.int<u, 32>>, ["__retval"] {alignment = 4 : i64} | ||
cir.scope { | ||
%2 = cir.const #cir.int<0> : !cir.int<u, 32> | ||
cir.store %2, %0 : !cir.int<u, 32>, !cir.ptr<!cir.int<u, 32>> | ||
%3 = cir.load %0 : !cir.ptr<!cir.int<u, 32>>, !cir.int<u, 32> | ||
cir.return %3 : !cir.int<u, 32> | ||
} | ||
%1 = cir.load %0 : !cir.ptr<!cir.int<u, 32>>, !cir.int<u, 32> | ||
cir.return %1 : !cir.int<u, 32> | ||
} | ||
|
||
// CHECK: cir.func @scope_with_return() -> !cir.int<u, 32> { | ||
// CHECK: %0 = cir.alloca !cir.int<u, 32>, !cir.ptr<!cir.int<u, 32>>, ["__retval"] {alignment = 4 : i64} | ||
// CHECK: cir.br ^bb1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is another block that it seems to me could be removed/merged with I guess there are later passes that do these sorts of merges? Also, removing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
yes! |
||
// CHECK: ^bb1: // pred: ^bb0 | ||
// CHECK: %1 = cir.const #cir.int<0> : !cir.int<u, 32> | ||
// CHECK: cir.store %1, %0 : !cir.int<u, 32>, !cir.ptr<!cir.int<u, 32>> | ||
// CHECK: %2 = cir.load %0 : !cir.ptr<!cir.int<u, 32>>, !cir.int<u, 32> | ||
// CHECK: cir.return %2 : !cir.int<u, 32> | ||
// CHECK: ^bb2: // no predecessors | ||
// CHECK: %3 = cir.load %0 : !cir.ptr<!cir.int<u, 32>>, !cir.int<u, 32> | ||
// CHECK: cir.return %3 : !cir.int<u, 32> | ||
// CHECK: } | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This here is a touch awkward, right? I know we did a bit of work to remove 'empty' blocks in the pass. I wonder if a future version of this pass should be checking the 'exit' of a block to see if it is a single-out (that is, no decisions being done, just a single
br
, and merge the two. It seems to fit in well with the flatten.That is, this function here seems like it should/could be a single block, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the patch I am currently working on I am adding the Canonicalization pass, which merges/removes useless blocks. So this CIR will look better after that is done. (At least I think it will. The canonicalization pass runs before the flattening pass. I can't promise that it will also run after.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These checks are the same checks that are present in this test in the incubator (except for the fact that we're not abbreviating the int types upstream yet). The test is verifying the flattening pass in isolation, and since the flattening pass doesn't remove unneeded blocks, the output will stay as it is here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My question was really "should it?" to the "the flattening pass doesn't remove unneeded blocks". But TBH my understanding of what we should expect out of individual passes, or how passes are designed is lacking.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it should. The general intent is for passes to perform a specific purpose and rely on layering of passes to clean things up. Because the canonicalization pass needs to remove useless blocks anyway, there is no advantage to also doing that in the flatten pass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, removing blocks should be on a separated pass. However, if during some transformation in the flattening it's convenient/easy to delete certain blocks without extra work, we should just do it (pretty common all around), but it's really on a case basis!