Skip to content

Commit 16b7c08

Browse files
committed
[WebAssembly] Backport custom import name changes for clang to 8.0.
Specifically, this backports r352106, r352108, r352930, and r352936 to the 8.0 branch. The trunk patches don't apply cleanly to 8.0 due to some contemporaneous mass-rename and mass-clang-tidy patches, so this merges them to simplify rebasing. r352106 [WebAssembly] Add an import_module function attribute r352108 [WebAssembly] Add WebAssemblyImportModule to pragma-attribute-supported-attributes-list.test r352930 [WebAssembly] Add an import_field function attribute r352936 [WebAssembly] Fix ImportName's position in this test. By Dan Gohman! llvm-svn: 353834
1 parent ef182d5 commit 16b7c08

File tree

7 files changed

+142
-1
lines changed

7 files changed

+142
-1
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ def TargetMSP430 : TargetArch<["msp430"]>;
329329
def TargetRISCV : TargetArch<["riscv32", "riscv64"]>;
330330
def TargetX86 : TargetArch<["x86"]>;
331331
def TargetAnyX86 : TargetArch<["x86", "x86_64"]>;
332+
def TargetWebAssembly : TargetArch<["wasm32", "wasm64"]>;
332333
def TargetWindows : TargetArch<["x86", "x86_64", "arm", "thumb", "aarch64"]> {
333334
let OSes = ["Win32"];
334335
}
@@ -1500,6 +1501,22 @@ def AMDGPUNumVGPR : InheritableAttr {
15001501
let Subjects = SubjectList<[Function], ErrorDiag, "kernel functions">;
15011502
}
15021503

1504+
def WebAssemblyImportModule : InheritableAttr,
1505+
TargetSpecificAttr<TargetWebAssembly> {
1506+
let Spellings = [Clang<"import_module">];
1507+
let Args = [StringArgument<"ImportModule">];
1508+
let Documentation = [WebAssemblyImportModuleDocs];
1509+
let Subjects = SubjectList<[Function], ErrorDiag>;
1510+
}
1511+
1512+
def WebAssemblyImportName : InheritableAttr,
1513+
TargetSpecificAttr<TargetWebAssembly> {
1514+
let Spellings = [Clang<"import_name">];
1515+
let Args = [StringArgument<"ImportName">];
1516+
let Documentation = [WebAssemblyImportNameDocs];
1517+
let Subjects = SubjectList<[Function], ErrorDiag>;
1518+
}
1519+
15031520
def NoSplitStack : InheritableAttr {
15041521
let Spellings = [GCC<"no_split_stack">];
15051522
let Subjects = SubjectList<[Function], ErrorDiag>;

clang/include/clang/Basic/AttrDocs.td

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3652,7 +3652,40 @@ definition (
36523652
For more information see
36533653
`gcc documentation <https://gcc.gnu.org/onlinedocs/gcc-7.2.0/gcc/Microsoft-Windows-Variable-Attributes.html>`_
36543654
or `msvc documentation <https://docs.microsoft.com/pl-pl/cpp/cpp/selectany>`_.
3655-
}];
3655+
}]; }
3656+
3657+
def WebAssemblyImportModuleDocs : Documentation {
3658+
let Category = DocCatFunction;
3659+
let Content = [{
3660+
Clang supports the ``__attribute__((import_module(<module_name>)))``
3661+
attribute for the WebAssembly target. This attribute may be attached to a
3662+
function declaration, where it modifies how the symbol is to be imported
3663+
within the WebAssembly linking environment.
3664+
3665+
WebAssembly imports use a two-level namespace scheme, consisting of a module
3666+
name, which typically identifies a module from which to import, and a field
3667+
name, which typically identifies a field from that module to import. By
3668+
default, module names for C/C++ symbols are assigned automatically by the
3669+
linker. This attribute can be used to override the default behavior, and
3670+
reuqest a specific module name be used instead.
3671+
}];
3672+
}
3673+
3674+
def WebAssemblyImportNameDocs : Documentation {
3675+
let Category = DocCatFunction;
3676+
let Content = [{
3677+
Clang supports the ``__attribute__((import_name(<name>)))``
3678+
attribute for the WebAssembly target. This attribute may be attached to a
3679+
function declaration, where it modifies how the symbol is to be imported
3680+
within the WebAssembly linking environment.
3681+
3682+
WebAssembly imports use a two-level namespace scheme, consisting of a module
3683+
name, which typically identifies a module from which to import, and a field
3684+
name, which typically identifies a field from that module to import. By
3685+
default, field names for C/C++ symbols are the same as their C/C++ symbol
3686+
names. This attribute can be used to override the default behavior, and
3687+
reuqest a specific field name be used instead.
3688+
}];
36563689
}
36573690

36583691
def ArtificialDocs : Documentation {

clang/lib/CodeGen/TargetInfo.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,22 @@ class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo {
761761

762762
void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
763763
CodeGen::CodeGenModule &CGM) const override {
764+
TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
765+
if (const auto *FD = dyn_cast_or_null<FunctionDecl>(D)) {
766+
if (const auto *Attr = FD->getAttr<WebAssemblyImportModuleAttr>()) {
767+
llvm::Function *Fn = cast<llvm::Function>(GV);
768+
llvm::AttrBuilder B;
769+
B.addAttribute("wasm-import-module", Attr->getImportModule());
770+
Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
771+
}
772+
if (const auto *Attr = FD->getAttr<WebAssemblyImportNameAttr>()) {
773+
llvm::Function *Fn = cast<llvm::Function>(GV);
774+
llvm::AttrBuilder B;
775+
B.addAttribute("wasm-import-name", Attr->getImportName());
776+
Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
777+
}
778+
}
779+
764780
if (auto *FD = dyn_cast_or_null<FunctionDecl>(D)) {
765781
llvm::Function *Fn = cast<llvm::Function>(GV);
766782
if (!FD->doesThisDeclarationHaveABody() && !FD->hasPrototype())

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5577,6 +5577,51 @@ static void handleAVRSignalAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
55775577
handleSimpleAttribute<AVRSignalAttr>(S, D, AL);
55785578
}
55795579

5580+
static void handleWebAssemblyImportModuleAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5581+
if (!isFunctionOrMethod(D)) {
5582+
S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5583+
<< "'import_module'" << ExpectedFunction;
5584+
return;
5585+
}
5586+
5587+
auto *FD = cast<FunctionDecl>(D);
5588+
if (FD->isThisDeclarationADefinition()) {
5589+
S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0;
5590+
return;
5591+
}
5592+
5593+
StringRef Str;
5594+
SourceLocation ArgLoc;
5595+
if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
5596+
return;
5597+
5598+
FD->addAttr(::new (S.Context) WebAssemblyImportModuleAttr(
5599+
AL.getRange(), S.Context, Str,
5600+
AL.getAttributeSpellingListIndex()));
5601+
}
5602+
5603+
static void handleWebAssemblyImportNameAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5604+
if (!isFunctionOrMethod(D)) {
5605+
S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
5606+
<< "'import_name'" << ExpectedFunction;
5607+
return;
5608+
}
5609+
5610+
auto *FD = cast<FunctionDecl>(D);
5611+
if (FD->isThisDeclarationADefinition()) {
5612+
S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0;
5613+
return;
5614+
}
5615+
5616+
StringRef Str;
5617+
SourceLocation ArgLoc;
5618+
if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
5619+
return;
5620+
5621+
FD->addAttr(::new (S.Context) WebAssemblyImportNameAttr(
5622+
AL.getRange(), S.Context, Str,
5623+
AL.getAttributeSpellingListIndex()));
5624+
}
55805625

55815626
static void handleRISCVInterruptAttr(Sema &S, Decl *D,
55825627
const ParsedAttr &AL) {
@@ -6330,6 +6375,12 @@ static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
63306375
case ParsedAttr::AT_AVRSignal:
63316376
handleAVRSignalAttr(S, D, AL);
63326377
break;
6378+
case ParsedAttr::AT_WebAssemblyImportModule:
6379+
handleWebAssemblyImportModuleAttr(S, D, AL);
6380+
break;
6381+
case ParsedAttr::AT_WebAssemblyImportName:
6382+
handleWebAssemblyImportNameAttr(S, D, AL);
6383+
break;
63336384
case ParsedAttr::AT_IBAction:
63346385
handleSimpleAttribute<IBActionAttr>(S, D, AL);
63356386
break;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %clang_cc1 -triple wasm32-unknown-unknown-wasm -emit-llvm -o - %s | FileCheck %s
2+
3+
void __attribute__((import_module("bar"))) foo(void);
4+
5+
void call(void) {
6+
foo();
7+
}
8+
9+
// CHECK: declare void @foo() [[A:#[0-9]+]]
10+
11+
// CHECK: attributes [[A]] = {{{.*}} "wasm-import-module"="bar" {{.*}}}

clang/test/CodeGen/wasm-import-name.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %clang_cc1 -triple wasm32-unknown-unknown-wasm -emit-llvm -o - %s | FileCheck %s
2+
3+
void __attribute__((import_name("bar"))) foo(void);
4+
5+
void call(void) {
6+
foo();
7+
}
8+
9+
// CHECK: declare void @foo() [[A:#[0-9]+]]
10+
11+
// CHECK: attributes [[A]] = {{{.*}} "wasm-import-name"="bar" {{.*}}}

clang/test/Misc/pragma-attribute-supported-attributes-list.test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@
136136
// CHECK-NEXT: WarnUnusedResult (SubjectMatchRule_objc_method, SubjectMatchRule_enum, SubjectMatchRule_record, SubjectMatchRule_hasType_functionType)
137137
// CHECK-NEXT: Weak (SubjectMatchRule_variable, SubjectMatchRule_function, SubjectMatchRule_record)
138138
// CHECK-NEXT: WeakRef (SubjectMatchRule_variable, SubjectMatchRule_function)
139+
// CHECK-NEXT: WebAssemblyImportModule (SubjectMatchRule_function)
140+
// CHECK-NEXT: WebAssemblyImportName (SubjectMatchRule_function)
139141
// CHECK-NEXT: WorkGroupSizeHint (SubjectMatchRule_function)
140142
// CHECK-NEXT: XRayInstrument (SubjectMatchRule_function, SubjectMatchRule_objc_method)
141143
// CHECK-NEXT: XRayLogArgs (SubjectMatchRule_function, SubjectMatchRule_objc_method)

0 commit comments

Comments
 (0)