-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[MIPS][ISel] Fix musttail #161860
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
Open
djtodoro
wants to merge
9
commits into
llvm:main
Choose a base branch
from
djtodoro:pr/fix-mips-musttail
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.
Open
[MIPS][ISel] Fix musttail #161860
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ac9497e
[MIPS][ISel] Fix musttail
djtodoro 9a77adf
Address comments
djtodoro 1e26120
Apply clang-format
djtodoro 20d634f
address comments
djtodoro eaa5678
Clean up and improve tests
djtodoro 54412fe
apply clang-format
djtodoro b9e145d
apply clang-format on Mips16ISelLowering.h
djtodoro e4a3991
Align with PPC solution
djtodoro a4b960c
Apply clang-format and simplify code for err reporting
djtodoro 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1561,16 +1561,32 @@ void CodeGenModule::Release() { | |
| setVisibilityFromDLLStorageClass(LangOpts, getModule()); | ||
|
|
||
| // Check the tail call symbols are truly undefined. | ||
| if (getTriple().isPPC() && !MustTailCallUndefinedGlobals.empty()) { | ||
| for (auto &I : MustTailCallUndefinedGlobals) { | ||
| if (!I.first->isDefined()) | ||
| getDiags().Report(I.second, diag::err_ppc_impossible_musttail) << 2; | ||
| else { | ||
| StringRef MangledName = getMangledName(GlobalDecl(I.first)); | ||
| llvm::GlobalValue *Entry = GetGlobalValue(MangledName); | ||
| if (!Entry || Entry->isWeakForLinker() || | ||
| Entry->isDeclarationForLinker()) | ||
| if (!MustTailCallUndefinedGlobals.empty()) { | ||
| if (getTriple().isPPC()) { | ||
| for (auto &I : MustTailCallUndefinedGlobals) { | ||
| if (!I.first->isDefined()) | ||
| getDiags().Report(I.second, diag::err_ppc_impossible_musttail) << 2; | ||
| else { | ||
| StringRef MangledName = getMangledName(GlobalDecl(I.first)); | ||
| llvm::GlobalValue *Entry = GetGlobalValue(MangledName); | ||
| if (!Entry || Entry->isWeakForLinker() || | ||
| Entry->isDeclarationForLinker()) | ||
| getDiags().Report(I.second, diag::err_ppc_impossible_musttail) << 2; | ||
| } | ||
| } | ||
| } else if (getTriple().isMIPS()) { | ||
| for (auto &I : MustTailCallUndefinedGlobals) { | ||
| const FunctionDecl *FD = I.first; | ||
| const FunctionDecl *Definition = FD->getDefinition(); | ||
| if (!Definition) { | ||
| getDiags().Report(I.second, diag::err_mips_impossible_musttail) << 1; | ||
| continue; | ||
| } | ||
| llvm::GlobalValue::LinkageTypes Linkage = | ||
|
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. As in |
||
| getFunctionLinkage(GlobalDecl(Definition)); | ||
| if (!llvm::GlobalValue::isLocalLinkage(Linkage) && | ||
| Definition->isExternallyVisible()) | ||
| getDiags().Report(I.second, diag::err_mips_impossible_musttail) << 1; | ||
| } | ||
| } | ||
| } | ||
|
|
||
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,19 @@ | ||
| // RUN: not %clang_cc1 %s -triple mipsel-unknown-linux-gnu -emit-llvm 2>&1 \ | ||
| // RUN: | FileCheck %s | ||
| // RUN: not %clang_cc1 %s -triple mipsel-unknown-linux-gnu -target-feature +mips16 \ | ||
| // RUN: -emit-llvm 2>&1 | FileCheck %s --check-prefix=CHECK-MIPS16 | ||
|
|
||
| // CHECK: error: 'musttail' attribute for this call is impossible because calls outside the current linkage unit cannot be tail called on MIPS | ||
| // CHECK-MIPS16: error: 'musttail' attribute for this call is impossible because the MIPS16 ABI does not support tail calls | ||
|
|
||
| static int local(int x) { return x; } | ||
|
|
||
| int call_local(int x) { | ||
| [[clang::musttail]] return local(x); | ||
| } | ||
|
|
||
| extern int external(int x); | ||
|
|
||
| int call_external(int x) { | ||
| [[clang::musttail]] return external(x); | ||
| } |
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
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 @@ | ||
| ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py | ||
| ; RUN: llc -mtriple=mips-unknown-linux-gnu < %s | FileCheck %s --check-prefix=MIPS32 | ||
| ; RUN: llc -mtriple=mips64-unknown-linux-gnu < %s | FileCheck %s --check-prefix=MIPS64 | ||
|
|
||
| ; Test musttail support for MIPS | ||
|
|
||
| define dso_local i32 @callee_args(i32 %a, i32 %b, i32 %c) { | ||
| ret i32 %a; | ||
| } | ||
|
|
||
| define i32 @test_musttail_args(i32 %x, i32 %y, i32 %z) { | ||
| ; MIPS32-LABEL: test_musttail_args: | ||
| ; MIPS32: # %bb.0: | ||
| ; MIPS32-NEXT: j callee_args | ||
| ; MIPS32-NEXT: nop | ||
| ; | ||
| ; MIPS64-LABEL: test_musttail_args: | ||
| ; MIPS64: # %bb.0: | ||
| ; MIPS64-NEXT: j callee_args | ||
| ; MIPS64-NEXT: nop | ||
| %ret = musttail call i32 @callee_args(i32 %x, i32 %y, i32 %z) | ||
| ret i32 %ret | ||
| } | ||
|
|
||
| ; Test musttail with many arguments that spill to stack (involves memory) | ||
| ; MIPS O32 ABI: first 4 args in $a0-$a3, rest on stack | ||
| define hidden i32 @many_args_callee(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e, i32 %f, i32 %g, i32 %h) { | ||
| ret i32 %a | ||
| } | ||
|
|
||
| define i32 @test_musttail_many_args(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e, i32 %f, i32 %g, i32 %h) { | ||
| ; MIPS32-LABEL: test_musttail_many_args: | ||
| ; MIPS32: # %bb.0: | ||
| ; MIPS32-NEXT: lw $1, 24($sp) | ||
| ; MIPS32-NEXT: lw $2, 20($sp) | ||
| ; MIPS32-NEXT: lw $3, 16($sp) | ||
| ; MIPS32-NEXT: sw $3, 16($sp) | ||
| ; MIPS32-NEXT: sw $2, 20($sp) | ||
| ; MIPS32-NEXT: sw $1, 24($sp) | ||
| ; MIPS32-NEXT: lw $1, 28($sp) | ||
| ; MIPS32-NEXT: j many_args_callee | ||
| ; MIPS32-NEXT: sw $1, 28($sp) | ||
| ; | ||
| ; MIPS64-LABEL: test_musttail_many_args: | ||
| ; MIPS64: # %bb.0: | ||
| ; MIPS64-NEXT: j many_args_callee | ||
| ; MIPS64-NEXT: nop | ||
| %ret = musttail call i32 @many_args_callee(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e, i32 %f, i32 %g, i32 %h) | ||
| ret i32 %ret | ||
| } | ||
|
|
||
| ; Test musttail with large struct passed by value (involves memory) | ||
| %struct.large = type { i32, i32, i32, i32, i32, i32, i32, i32 } | ||
|
|
||
| define hidden i32 @callee_with_struct(%struct.large %s, i32 %x) { | ||
| ret i32 %x | ||
| } | ||
|
|
||
| define i32 @test_musttail_struct(%struct.large %s, i32 %x) { | ||
| ; MIPS32-LABEL: test_musttail_struct: | ||
| ; MIPS32: # %bb.0: | ||
| ; MIPS32-NEXT: lw $1, 28($sp) | ||
| ; MIPS32-NEXT: lw $2, 24($sp) | ||
| ; MIPS32-NEXT: lw $3, 20($sp) | ||
| ; MIPS32-NEXT: lw $8, 16($sp) | ||
| ; MIPS32-NEXT: sw $8, 16($sp) | ||
| ; MIPS32-NEXT: sw $3, 20($sp) | ||
| ; MIPS32-NEXT: sw $2, 24($sp) | ||
| ; MIPS32-NEXT: sw $1, 28($sp) | ||
| ; MIPS32-NEXT: lw $1, 32($sp) | ||
| ; MIPS32-NEXT: j callee_with_struct | ||
| ; MIPS32-NEXT: sw $1, 32($sp) | ||
| ; | ||
| ; MIPS64-LABEL: test_musttail_struct: | ||
| ; MIPS64: # %bb.0: | ||
| ; MIPS64-NEXT: ld $1, 0($sp) | ||
| ; MIPS64-NEXT: j callee_with_struct | ||
| ; MIPS64-NEXT: sd $1, 0($sp) | ||
| %ret = musttail call i32 @callee_with_struct(%struct.large %s, i32 %x) | ||
| ret i32 %ret | ||
| } | ||
|
|
||
| ; Test musttail with mixed int and float arguments that use stack | ||
| define hidden float @mixed_args_callee(i32 %a, float %b, i32 %c, float %d, i32 %e, float %f) { | ||
| ret float %b | ||
| } | ||
|
|
||
| define float @test_musttail_mixed_args(i32 %a, float %b, i32 %c, float %d, i32 %e, float %f) { | ||
| ; MIPS32-LABEL: test_musttail_mixed_args: | ||
| ; MIPS32: # %bb.0: | ||
| ; MIPS32-NEXT: lw $1, 16($sp) | ||
| ; MIPS32-NEXT: sw $1, 16($sp) | ||
| ; MIPS32-NEXT: lwc1 $f0, 20($sp) | ||
| ; MIPS32-NEXT: j mixed_args_callee | ||
| ; MIPS32-NEXT: swc1 $f0, 20($sp) | ||
| ; | ||
| ; MIPS64-LABEL: test_musttail_mixed_args: | ||
| ; MIPS64: # %bb.0: | ||
| ; MIPS64-NEXT: j mixed_args_callee | ||
| ; MIPS64-NEXT: nop | ||
| %ret = musttail call float @mixed_args_callee(i32 %a, float %b, i32 %c, float %d, i32 %e, float %f) | ||
| ret float %ret | ||
| } | ||
|
|
||
| ; Test musttail with indirect call | ||
| define i32 @test_musttail_fptr(ptr %fptr, i32 %x) { | ||
| ; MIPS32-LABEL: test_musttail_fptr: | ||
| ; MIPS32: # %bb.0: | ||
| ; MIPS32-NEXT: move $25, $4 | ||
| ; MIPS32-NEXT: jr $25 | ||
| ; MIPS32-NEXT: nop | ||
| ; | ||
| ; MIPS64-LABEL: test_musttail_fptr: | ||
| ; MIPS64: # %bb.0: | ||
| ; MIPS64-NEXT: move $25, $4 | ||
| ; MIPS64-NEXT: jr $25 | ||
| ; MIPS64-NEXT: nop | ||
| %ret = musttail call i32 %fptr(ptr %fptr, i32 %x) | ||
| ret i32 %ret | ||
| } |
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.
The conditions used here differ from the conditions used in MipsISelLowering. This causes it to be inconsistent and results in the error I provided in conversations.
Locally, I used this which still isn't correct but seems to work more consistently: