-
Notifications
You must be signed in to change notification settings - Fork 192
[CIR][CodeGen] Implement popNullFixups and unreachable code null fixups #2108
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
Draft
bruteforceboy
wants to merge
2
commits into
llvm:main
Choose a base branch
from
bruteforceboy:null-fixups
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+184
−43
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| #include "std-cxx.h" | ||
|
|
||
| // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -I%S/../Inputs -fclangir -emit-cir %s -o %t.cir | ||
| // RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s | ||
|
|
||
| // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -I%S/../Inputs -fclangir -emit-llvm %s -o %t.ll | ||
| // RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s | ||
|
|
||
| // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -I%S/../Inputs -emit-llvm %s -o %t.og.ll | ||
| // RUN: FileCheck --check-prefix=OGCG --input-file=%t.og.ll %s | ||
|
|
||
| // CIR-LABEL: cir.func {{.*}} @_ZNK1d1eEv | ||
| // CIR-NEXT: %[[V0:.*]] = cir.alloca !cir.ptr<!rec_d>, !cir.ptr<!cir.ptr<!rec_d>>, ["this", init] | ||
| // CIR-NEXT: %[[V1:.*]] = cir.alloca !rec_a3A3Ac, !cir.ptr<!rec_a3A3Ac>, ["__retval"] | ||
| // CIR-NEXT: %[[V2:.*]] = cir.alloca !rec_aj, !cir.ptr<!rec_aj>, ["ao"] | ||
| // CIR-NEXT: cir.store %arg0, %[[V0]] : !cir.ptr<!rec_d>, !cir.ptr<!cir.ptr<!rec_d>> | ||
| // CIR-NEXT: %[[V3:.*]] = cir.load %[[V0]] : !cir.ptr<!cir.ptr<!rec_d>>, !cir.ptr<!rec_d> | ||
| // CIR-NEXT: cir.scope { | ||
| // CIR-NEXT: %[[V5:.*]] = cir.alloca !rec_aj, !cir.ptr<!rec_aj>, ["agg.tmp0"] | ||
| // CIR-NEXT: %[[V6:.*]] = cir.get_global @an : !cir.ptr<!rec_aj> | ||
| // CIR-NEXT: cir.copy %[[V6]] to %[[V5]] : !cir.ptr<!rec_aj> | ||
| // CIR-NEXT: %[[V7:.*]] = cir.load align(1) %[[V5]] : !cir.ptr<!rec_aj>, !rec_aj | ||
| // CIR-NEXT: cir.call @_ZN1a1cC1I2ajEET_(%[[V1]], %[[V7]]) : (!cir.ptr<!rec_a3A3Ac>, !rec_aj) -> () | ||
| // CIR-NEXT: cir.call @_ZN2ajD1Ev(%[[V5]]) : (!cir.ptr<!rec_aj>) -> () | ||
| // CIR-NEXT: } | ||
| // CIR-NEXT: cir.call @_ZN2ajD1Ev(%[[V2]]) : (!cir.ptr<!rec_aj>) -> () | ||
| // CIR-NEXT: %[[V4:.*]] = cir.load align(1) %[[V1]] : !cir.ptr<!rec_a3A3Ac>, !rec_a3A3Ac | ||
| // CIR-NEXT: cir.return %[[V4]] : !rec_a3A3Ac | ||
| // CIR-NEXT: } | ||
|
|
||
| // LLVM-LABEL: {{.*}} @_ZNK1d1eEv(ptr {{.*}}) | ||
| // LLVM-NEXT: %[[V2:.*]] = alloca %class.aj, i64 1, align 1 | ||
| // LLVM-NEXT: %[[V3:.*]] = alloca ptr, i64 1, align 8 | ||
| // LLVM-NEXT: %[[V4:.*]] = alloca %"class.a::c", i64 1, align 1 | ||
| // LLVM-NEXT: %[[V5:.*]] = alloca %class.aj, i64 1, align 1 | ||
| // LLVM-NEXT: store ptr {{.*}}, ptr %[[V3]], align 8 | ||
| // LLVM-NEXT: %[[V6:.*]] = load ptr, ptr %[[V3]], align 8 | ||
| // LLVM-NEXT: br label %[[B7:.*]] | ||
| // LLVM: [[B7]]: | ||
| // LLVM-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr %[[V2]], ptr @an, i32 1, i1 false) | ||
| // LLVM-NEXT: %[[V8:.*]] = load %class.aj, ptr %[[V2]], align 1 | ||
| // LLVM-NEXT: call void @_ZN1a1cC1I2ajEET_(ptr %[[V4]], %class.aj %[[V8]]) | ||
| // LLVM-NEXT: call void @_ZN2ajD1Ev(ptr %[[V2]]) | ||
| // LLVM-NEXT: br label %[[B9:.*]] | ||
| // LLVM: [[B9]]: | ||
| // LLVM-NEXT: call void @_ZN2ajD1Ev(ptr %[[V5]]) | ||
| // LLVM-NEXT: %[[V10:.*]] = load %"class.a::c", ptr %[[V4]], align 1 | ||
| // LLVM-NEXT: ret %"class.a::c" %[[V10]] | ||
| // LLVM-NEXT: } | ||
|
|
||
| // OGCG-LABEL: {{.*}} @_ZNK1d1eEv(ptr {{.*}}) | ||
| // OGCG: %{{.*}} = alloca ptr, align 8 | ||
| // OGCG-NEXT: %{{.*}} = alloca ptr, align 8 | ||
| // OGCG-NEXT: %{{.*}} = alloca %{{.*}}, align 1 | ||
| // OGCG-NEXT: %{{.*}} = alloca %{{.*}}, align 1 | ||
| // OGCG-NEXT: store ptr %{{.*}}, ptr %{{.*}}, align 8 | ||
| // OGCG-NEXT: store ptr %{{.*}}, ptr %{{.*}}, align 8 | ||
| // OGCG-NEXT: %{{.*}} = load ptr, ptr %{{.*}}, align 8 | ||
| // OGCG-NEXT: call void @_ZN1a1cC1I2ajEET_(ptr noundef nonnull align 1 dereferenceable(1) %{{.*}}, ptr noundef %{{.*}}) | ||
| // OGCG-NEXT: call void @_ZN2ajD1Ev(ptr noundef nonnull align 1 dereferenceable(1) %{{.*}}) | ||
| // OGCG-NEXT: call void @_ZN2ajD1Ev(ptr noundef nonnull align 1 dereferenceable(1) %{{.*}}) | ||
| // OGCG-NEXT: ret void | ||
| // OGCG-NEXT: } | ||
|
|
||
| inline namespace a { | ||
| class c { | ||
| public: | ||
| template <typename b> c(b); | ||
| ~c(); | ||
| }; | ||
| } // namespace a | ||
| class d { | ||
| c e() const; | ||
| }; | ||
| class aj { | ||
| public: | ||
| ~aj(); | ||
| } an; | ||
| c d::e() const { | ||
| aj ao; | ||
| return an; | ||
| c(0); | ||
| } | ||
|
|
||
| // CIR-LABEL: cir.func {{.*}} @_Z3foov | ||
| // CIR-NEXT: %[[V0:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["__retval"] {alignment = 4 : i64} | ||
| // CIR-NEXT: %[[V1:.*]] = cir.alloca !rec_std3A3Abasic_string3Cchar3E, !cir.ptr<!rec_std3A3Abasic_string3Cchar3E>, ["a", init] | ||
| // CIR-NEXT: %[[V2:.*]] = cir.alloca !rec_std3A3Abasic_string3Cchar3E, !cir.ptr<!rec_std3A3Abasic_string3Cchar3E>, ["b"] | ||
| // CIR-NEXT: cir.call @_ZNSbIcEC1Ev(%[[V1]]) : (!cir.ptr<!rec_std3A3Abasic_string3Cchar3E>) -> () | ||
| // CIR-NEXT: %[[V3:.*]] = cir.const #cir.int<0> : !s32i | ||
| // CIR-NEXT: cir.store align(4) %[[V3]], %[[V0]] : !s32i, !cir.ptr<!s32i> | ||
| // CIR-NEXT: cir.call @_ZNSbIcED1Ev(%[[V1]]) : (!cir.ptr<!rec_std3A3Abasic_string3Cchar3E>) -> () extra(#fn_attr) | ||
| // CIR-NEXT: %[[V4:.*]] = cir.load align(4) %[[V0]] : !cir.ptr<!s32i>, !s32i | ||
| // CIR-NEXT: cir.return %[[V4]] : !s32i | ||
| // CIR-NEXT: } | ||
|
|
||
| // LLVM-LABEL: {{.*}} @_Z3foov() | ||
| // LLVM-NEXT: %[[V1:.*]] = alloca i32, i64 1, align 4 | ||
| // LLVM-NEXT: %[[V2:.*]] = alloca %"class.std::basic_string<char>", i64 1, align 1 | ||
| // LLVM-NEXT: %[[V3:.*]] = alloca %"class.std::basic_string<char>", i64 1, align 1 | ||
| // LLVM-NEXT: call void @_ZNSbIcEC1Ev(ptr %[[V2]]) | ||
| // LLVM-NEXT: store i32 0, ptr %[[V1]], align 4 | ||
| // LLVM-NEXT: call void @_ZNSbIcED1Ev(ptr %[[V2]]) | ||
| // LLVM-NEXT: %[[V4:.*]] = load i32, ptr %[[V1]], align 4 | ||
| // LLVM-NEXT: ret i32 %[[V4]] | ||
| // LLVM-NEXT: } | ||
|
|
||
| // OGCG-LABEL: {{.*}} @_Z3foov() | ||
| // OGCG: %[[a:.*]] = alloca %"class.std::basic_string", align 1 | ||
| // OGCG-NEXT: %[[b:.*]] = alloca %"class.std::basic_string", align 1 | ||
| // OGCG-NEXT: call void @_ZNSbIcEC1Ev(ptr noundef nonnull align 1 dereferenceable(1) %[[a]]) | ||
| // OGCG-NEXT: call void @_ZNSbIcED1Ev(ptr noundef nonnull align 1 dereferenceable(1) %[[a]]) | ||
| // OGCG-NEXT: ret i32 0 | ||
| // OGCG-NEXT: } | ||
|
|
||
| int foo() { | ||
| std::string a; | ||
| return 0; | ||
| std::string b; | ||
| } | ||
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Hmmm, back to the trap being omitted here
Do you get the llvm.trap if you use
-disable-llvm-passes? We need to make sure we understand why it's different before accepting a different outcome.Uh oh!
There was an error while loading. Please reload this page.
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.
We don't get
llvm.trapeven when we use-disable-llvm-passes, because it is not emitted at all by the OG CodeGen in this. I'll explain my findings in more detail, hopefully without boring you with details.I did some digging to understand how the OG handles return statements in scopes. For my experiment, I used:
This reproduces the same null-fixup crash using the standard library, which was easier to reason about. The AST for the function body looks roughly like:
The OG IR output eventually looks something like:
Notice the ctor & dtor for
%bare omitted, and this is with the passes disabled.The statements in the function are emitted through
EmitFunctionBody. The firstDeclStmtis emitted just fine, then at the end ofCodeGenFunction::EmitReturnStmtfor the second statement, we have:cleanupScope.ForceCleanup(); EmitBranchThroughCleanup(ReturnBlock);We force the cleanup for the current scope and then emit a branch through the cleanup. In
EmitBranchThroughCleanup, we clear the insertion point (Builder.ClearInsertionPoint()) at the end. This signals to the rest of the codegen paths that they should not emit statements further in this scope.So, when emitting the last
DeclStmtthroughCodeGenFunction::EmitStmt, we reach this inCGDeclfrom my example:and both
EmitAutoVarDeclandEmitAutoVarCleanupsdo not emit anything if the insertion point isn't set [1], [2].PS: There are some caveats, such as cases with labels, that insert a trap, but that's not very relevant here.
In CIR, as I understand, our cleanups aren't mature enough [3], [4], [5], [6], [7], and we don't clear insertion points at the moment or properly mimic the unreachable code behavior.
So, with my PR our llvm output for this example looks like:
This is why I added the
TrapOpfor blocks that are unreachable, as a starting point, and mentioned handling such blocks in the future. As CIR matures, these traps should become redundant without affecting correctness.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 understand what you are doing now, thanks for attempting a solution. I don't see how this brings us any closer to improve the situation though, fixing issues and making cleanup more mature is the desired direction to go.
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.
That makes sense. I will rework this PR in a bit to use the null insertion point logic from the OG, and I'll get rid of the traps.