Skip to content

Commit 5f3d4bf

Browse files
badumbatishahatanaka
authored andcommitted
[WebAssembly] Enable musttail only when tail-call is enabled (llvm#163618)
Fixes llvm#163256
1 parent 51fcfee commit 5f3d4bf

File tree

6 files changed

+34
-1
lines changed

6 files changed

+34
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,8 @@ NetBSD Support
11971197
WebAssembly Support
11981198
^^^^^^^^^^^^^^^^^^^
11991199

1200+
- Fix a bug so that ``__has_attribute(musttail)`` is no longer true when WebAssembly's tail-call is not enabled. (#GH163256)
1201+
12001202
AVR Support
12011203
^^^^^^^^^^^
12021204

clang/include/clang/Basic/Attr.td

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,10 @@ def TargetMicrosoftRecordLayout : TargetArch<["x86", "x86_64", "arm", "thumb",
527527
let CustomCode = [{ Target.hasMicrosoftRecordLayout() }];
528528
}
529529

530+
def TargetMustTailAvaiable: TargetSpec {
531+
let CustomCode = [{ Target.hasMustTail() }];
532+
}
533+
530534
def TargetELF : TargetSpec {
531535
let ObjectFormats = ["ELF"];
532536
}
@@ -1914,7 +1918,7 @@ def NoMerge : DeclOrStmtAttr {
19141918
"functions, statements and variables">;
19151919
}
19161920

1917-
def MustTail : StmtAttr {
1921+
def MustTail : StmtAttr, TargetSpecificAttr<TargetMustTailAvaiable> {
19181922
let Spellings = [Clang<"musttail">];
19191923
let Documentation = [MustTailDocs];
19201924
let Subjects = SubjectList<[ReturnStmt], ErrorDiag, "return statements">;

clang/include/clang/Basic/TargetInfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ class TargetInfo : public TransferrableTargetInfo,
229229
protected:
230230
// Target values set by the ctor of the actual target implementation. Default
231231
// values are specified by the TargetInfo constructor.
232+
bool HasMustTail;
232233
bool BigEndian;
233234
bool TLSSupported;
234235
bool VLASupported;
@@ -674,6 +675,8 @@ class TargetInfo : public TransferrableTargetInfo,
674675
: getLongFractScale() + 1;
675676
}
676677

678+
virtual bool hasMustTail() const { return HasMustTail; }
679+
677680
/// Determine whether the __int128 type is supported on this target.
678681
virtual bool hasInt128Type() const {
679682
return (getPointerWidth(LangAS::Default) >= 64) ||

clang/lib/Basic/TargetInfo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ static const LangASMap FakeAddrSpaceMap = {
5858
TargetInfo::TargetInfo(const llvm::Triple &T) : Triple(T) {
5959
// Set defaults. Defaults are set for a 32-bit RISC platform, like PPC or
6060
// SPARC. These should be overridden by concrete targets as needed.
61+
HasMustTail = true;
6162
BigEndian = !T.isLittleEndian();
6263
TLSSupported = true;
6364
VLASupported = true;

clang/lib/Basic/Targets/WebAssembly.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ bool WebAssemblyTargetInfo::initFeatureMap(
209209

210210
bool WebAssemblyTargetInfo::handleTargetFeatures(
211211
std::vector<std::string> &Features, DiagnosticsEngine &Diags) {
212+
HasMustTail = false;
212213
for (const auto &Feature : Features) {
213214
if (Feature == "+atomics") {
214215
HasAtomics = true;
@@ -333,10 +334,12 @@ bool WebAssemblyTargetInfo::handleTargetFeatures(
333334
}
334335
if (Feature == "+tail-call") {
335336
HasTailCall = true;
337+
HasMustTail = true;
336338
continue;
337339
}
338340
if (Feature == "-tail-call") {
339341
HasTailCall = false;
342+
HasMustTail = false;
340343
continue;
341344
}
342345
if (Feature == "+wide-arithmetic") {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %clang_cc1 %s -triple wasm32-unknown-unknown -target-feature +tail-call -o /dev/null -emit-llvm -verify=tail
2+
// RUN: %clang_cc1 %s -triple wasm32-unknown-unknown -o /dev/null -emit-llvm -verify=notail
3+
4+
int foo(int x) {
5+
return x;
6+
}
7+
8+
#if __has_attribute(musttail)
9+
// tail-warning@+1 {{HAS IT}}
10+
#warning HAS IT
11+
#else
12+
// notail-warning@+1 {{DOES NOT HAVE}}
13+
#warning DOES NOT HAVE
14+
#endif
15+
16+
int bar(int x)
17+
{
18+
// notail-warning@+1 {{unknown attribute 'clang::musttail' ignored}}
19+
[[clang::musttail]] return foo(1);
20+
}

0 commit comments

Comments
 (0)