Skip to content

Commit 20e10f0

Browse files
author
Chen Zheng
committed
address comments
1 parent 720b71c commit 20e10f0

File tree

12 files changed

+114
-59
lines changed

12 files changed

+114
-59
lines changed

clang/include/clang/Basic/DiagnosticCommonKinds.td

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,15 @@ def warn_target_unrecognized_env : Warning<
367367
def err_target_unsupported_abi_with_fpu : Error<
368368
"'%0' ABI is not supported with FPU">;
369369

370+
def err_ppc_impossible_musttail: Error<
371+
"'musttail' attribute for this call is impossible because %select{"
372+
"long calls can not be tail called on PPC|"
373+
"indirect calls can not be tail called on PPC|"
374+
"external calls can not be tail called on PPC}0"
375+
>;
376+
def err_aix_musttail_unsupported: Error<
377+
"'musttail' attribute is not supported on AIX">;
378+
370379
// Source manager
371380
def err_cannot_open_file : Error<"cannot open file '%0': %1">, DefaultFatal;
372381
def err_file_modified : Error<

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3864,14 +3864,6 @@ def note_cannot_use_trivial_abi_reason : Note<
38643864
"it is polymorphic|"
38653865
"it has a base of a non-trivial class type|it has a virtual base|"
38663866
"it has a __weak field|it has a field of a non-trivial class type}1">;
3867-
def err_ppc_impossible_musttail: Error<
3868-
"'musttail' attribute for this call is impossible because %select{"
3869-
"long calls can not be tail called|"
3870-
"indirect calls can not be tail called|"
3871-
"external calls can not be tail called}0"
3872-
>;
3873-
def err_aix_musttail_unsupported: Error<
3874-
"'musttail' attribute is not supported on AIX">;
38753867

38763868
// Availability attribute
38773869
def warn_availability_unknown_platform : Warning<

clang/lib/CodeGen/CGCall.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5751,8 +5751,29 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
57515751
if (llvm::CallInst *Call = dyn_cast<llvm::CallInst>(CI)) {
57525752
if (TargetDecl && TargetDecl->hasAttr<NotTailCalledAttr>())
57535753
Call->setTailCallKind(llvm::CallInst::TCK_NoTail);
5754-
else if (IsMustTail)
5754+
else if (IsMustTail) {
5755+
if (getTarget().getTriple().isPPC()) {
5756+
if (getTarget().getTriple().isOSAIX())
5757+
CGM.getDiags().Report(Loc, diag::err_aix_musttail_unsupported);
5758+
else if (!getTarget().hasFeature("pcrelative-memops")) {
5759+
if (getTarget().hasFeature("longcall"))
5760+
CGM.getDiags().Report(Loc, diag::err_ppc_impossible_musttail) << 0;
5761+
else if (Call->isIndirectCall())
5762+
CGM.getDiags().Report(Loc, diag::err_ppc_impossible_musttail) << 1;
5763+
else if (isa_and_nonnull<FunctionDecl>(TargetDecl) &&
5764+
cast<FunctionDecl>(TargetDecl)->isWeak())
5765+
CGM.getDiags().Report(Loc, diag::err_ppc_impossible_musttail) << 2;
5766+
else if (isa_and_nonnull<FunctionDecl>(TargetDecl) &&
5767+
!cast<FunctionDecl>(TargetDecl)->isDefined())
5768+
// The undefined callee may be a forward declaration. Without
5769+
// knowning all symbols in the module, we won't know the symbol is
5770+
// defined or not. Collect all these symbols for later diagnosing.
5771+
CGM.addUndefinedGlobalForTailCall(
5772+
{cast<FunctionDecl>(TargetDecl), Loc});
5773+
}
5774+
}
57555775
Call->setTailCallKind(llvm::CallInst::TCK_MustTail);
5776+
}
57565777
}
57575778

57585779
// Add metadata for calls to MSAllocator functions

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,14 @@ void CodeGenModule::Release() {
13941394
// that might affect the DLL storage class or the visibility, and
13951395
// before anything that might act on these.
13961396
setVisibilityFromDLLStorageClass(LangOpts, getModule());
1397+
1398+
// Check the tail call symbols are truly undefined.
1399+
if (getTriple().isPPC() && !MustTailCallUndefinedGlobals.empty()) {
1400+
for (auto &I : MustTailCallUndefinedGlobals) {
1401+
if (!I.first->isDefined())
1402+
getDiags().Report(I.second, diag::err_ppc_impossible_musttail) << 2;
1403+
}
1404+
}
13971405
}
13981406

13991407
void CodeGenModule::EmitOpenCLMetadata() {

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,14 @@ class CodeGenModule : public CodeGenTypeCache {
485485
typedef std::pair<OrderGlobalInitsOrStermFinalizers, llvm::Function *>
486486
GlobalInitData;
487487

488+
// When a tail call is performed on an "undefined" symbol, on PPC without pc
489+
// relative feature, the tail call is not allowed. In "EmitCall" for such
490+
// tail calls, the "undefined" symbols may be forward declarations, their
491+
// definitions are provided in the module but after the callsites. For such
492+
// tail calls, diagnose message should be not emitted.
493+
llvm::SmallSetVector<std::pair<const FunctionDecl *, SourceLocation>, 4>
494+
MustTailCallUndefinedGlobals;
495+
488496
struct GlobalInitPriorityCmp {
489497
bool operator()(const GlobalInitData &LHS,
490498
const GlobalInitData &RHS) const {
@@ -1647,6 +1655,11 @@ class CodeGenModule : public CodeGenTypeCache {
16471655
return getTriple().isSPIRVLogical();
16481656
}
16491657

1658+
void addUndefinedGlobalForTailCall(
1659+
std::pair<const FunctionDecl *, SourceLocation> Global) {
1660+
MustTailCallUndefinedGlobals.insert(Global);
1661+
}
1662+
16501663
private:
16511664
bool shouldDropDLLAttribute(const Decl *D, const llvm::GlobalValue *GV) const;
16521665

clang/lib/Sema/SemaStmt.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -743,20 +743,6 @@ bool Sema::checkMustTailAttr(const Stmt *St, const Attr &MTA) {
743743
CallerType.Func = CallerDecl->getType()->getAs<FunctionProtoType>();
744744
}
745745

746-
if (Context.getTargetInfo().getTriple().isPPC()) {
747-
if (Context.getTargetInfo().getTriple().isOSAIX())
748-
return Diag(St->getBeginLoc(), diag::err_aix_musttail_unsupported);
749-
else if (!Context.getTargetInfo().hasFeature("pcrelative-memops")) {
750-
if (Context.getTargetInfo().hasFeature("longcall"))
751-
return Diag(St->getBeginLoc(), diag::err_ppc_impossible_musttail) << 0;
752-
else if (!CE->getDirectCallee())
753-
return Diag(St->getBeginLoc(), diag::err_ppc_impossible_musttail) << 1;
754-
else if (isa_and_nonnull<FunctionDecl>(CE->getCalleeDecl()) &&
755-
!cast<FunctionDecl>(CE->getCalleeDecl())->isDefined())
756-
return Diag(St->getBeginLoc(), diag::err_ppc_impossible_musttail) << 2;
757-
}
758-
}
759-
760746
const Expr *CalleeExpr = CE->getCallee()->IgnoreParens();
761747
const auto *CalleeBinOp = dyn_cast<BinaryOperator>(CalleeExpr);
762748
SourceLocation CalleeLoc = CE->getCalleeDecl()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %clang_cc1 %s -triple powerpc64le-unknown-linux-gnu -o /dev/null -emit-llvm -verify=good
2+
// RUN: %clang_cc1 %s -triple powerpc64-unknown-linux-gnu -o /dev/null -emit-llvm -verify=good
3+
4+
int func2(int i);
5+
int external_call2(int i) {
6+
// good-no-diagnostics
7+
[[clang::musttail]] return func2(i);
8+
}
9+
int func2(int i) {
10+
return 0;
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %clang_cc1 %s -triple powerpc64-unknown-linux-gnu -o /dev/null -emit-llvm -verify
2+
// RUN: %clang_cc1 %s -triple powerpc-unknown-linux-gnu -o /dev/null -emit-llvm -verify
3+
4+
void name(int *params) {
5+
auto fn = (void (*)(int *))1;
6+
// expected-error@+1 {{'musttail' attribute for this call is impossible because indirect calls can not be tail called on PPC}}
7+
[[clang::musttail]] return fn(params);
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %clang_cc1 %s -triple powerpc64-unknown-linux-gnu -o /dev/null -emit-llvm -verify
2+
// RUN: %clang_cc1 %s -triple powerpc64le-unknown-linux-gnu -o /dev/null -emit-llvm -verify
3+
4+
int foo(int x);
5+
6+
int bar(int x)
7+
{
8+
// expected-error@+1 {{'musttail' attribute for this call is impossible because external calls can not be tail called on PPC}}
9+
[[clang::musttail]] return foo(x);
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %clang_cc1 %s -triple powerpc64-ibm-aix-xcoff -o /dev/null -emit-llvm -verify=aix
2+
// RUN: %clang_cc1 %s -triple powerpc-ibm-aix-xcoff -o /dev/null -emit-llvm -verify=aix
3+
// RUN: %clang_cc1 %s -triple powerpc64-unknown-linux-gnu -o /dev/null -emit-llvm -verify=linux
4+
// RUN: %clang_cc1 %s -triple powerpc64le-unknown-linux-gnu -o /dev/null -emit-llvm -verify=linux
5+
6+
__attribute__((weak)) int func2(int i) {
7+
return 0;
8+
}
9+
int external_call2(int i) {
10+
// linux-error@+2 {{'musttail' attribute for this call is impossible because external calls can not be tail called on PPC}}
11+
// aix-error@+1 {{'musttail' attribute is not supported on AIX}}
12+
[[clang::musttail]] return func2(i);
13+
}

clang/test/CodeGen/PowerPC/musttail.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %clang_cc1 %s -triple powerpc64-ibm-aix-xcoff -o /dev/null -emit-llvm -verify=aix
2+
// RUN: %clang_cc1 %s -triple powerpc-ibm-aix-xcoff -o /dev/null -emit-llvm -verify=aix
3+
// RUN: %clang_cc1 %s -triple powerpc64-unknown-linux-gnu -o /dev/null -emit-llvm -verify=good
4+
// RUN: %clang_cc1 %s -triple powerpc-unknown-linux-gnu -o /dev/null -emit-llvm -verify=good
5+
// RUN: %clang_cc1 %s -triple powerpc64le-unknown-linux-gnu -o /dev/null -emit-llvm -verify=good
6+
// RUN: %clang_cc1 %s -triple powerpc64le-unknown-linux-gnu -target-feature +pcrelative-memops -o /dev/null -emit-llvm -verify=good
7+
// RUN: %clang_cc1 %s -triple powerpc64le-unknown-linux-gnu -target-feature +longcall -o /dev/null -emit-llvm -verify=longcall
8+
// RUN: %clang_cc1 %s -triple powerpc64le-unknown-linux-gnu -target-feature +pcrelative-memops -target-feature +longcall -o /dev/null -emit-llvm -verify=good
9+
10+
int foo(int x) {
11+
return x;
12+
}
13+
14+
int bar(int x)
15+
{
16+
// good-no-diagnostics
17+
// longcall-error@+2 {{'musttail' attribute for this call is impossible because long calls can not be tail called on PPC}}
18+
// aix-error@+1 {{'musttail' attribute is not supported on AIX}}
19+
[[clang::musttail]] return foo(1);
20+
}

clang/test/SemaCXX/attr-musttail-ppc.cpp

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)