[llvm-branch-commits] [cfe-branch] r353834 - [WebAssembly] Backport custom import name changes for clang to 8.0.

Hans Wennborg via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Feb 12 04:26:10 PST 2019


Author: hans
Date: Tue Feb 12 04:26:10 2019
New Revision: 353834

URL: http://llvm.org/viewvc/llvm-project?rev=353834&view=rev
Log:
[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!

Added:
    cfe/branches/release_80/test/CodeGen/wasm-import-module.c
    cfe/branches/release_80/test/CodeGen/wasm-import-name.c
Modified:
    cfe/branches/release_80/include/clang/Basic/Attr.td
    cfe/branches/release_80/include/clang/Basic/AttrDocs.td
    cfe/branches/release_80/lib/CodeGen/TargetInfo.cpp
    cfe/branches/release_80/lib/Sema/SemaDeclAttr.cpp
    cfe/branches/release_80/test/Misc/pragma-attribute-supported-attributes-list.test

Modified: cfe/branches/release_80/include/clang/Basic/Attr.td
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_80/include/clang/Basic/Attr.td?rev=353834&r1=353833&r2=353834&view=diff
==============================================================================
--- cfe/branches/release_80/include/clang/Basic/Attr.td (original)
+++ cfe/branches/release_80/include/clang/Basic/Attr.td Tue Feb 12 04:26:10 2019
@@ -329,6 +329,7 @@ def TargetMSP430 : TargetArch<["msp430"]
 def TargetRISCV : TargetArch<["riscv32", "riscv64"]>;
 def TargetX86 : TargetArch<["x86"]>;
 def TargetAnyX86 : TargetArch<["x86", "x86_64"]>;
+def TargetWebAssembly : TargetArch<["wasm32", "wasm64"]>;
 def TargetWindows : TargetArch<["x86", "x86_64", "arm", "thumb", "aarch64"]> {
   let OSes = ["Win32"];
 }
@@ -1500,6 +1501,22 @@ def AMDGPUNumVGPR : InheritableAttr {
   let Subjects = SubjectList<[Function], ErrorDiag, "kernel functions">;
 }
 
+def WebAssemblyImportModule : InheritableAttr,
+                              TargetSpecificAttr<TargetWebAssembly> {
+  let Spellings = [Clang<"import_module">];
+  let Args = [StringArgument<"ImportModule">];
+  let Documentation = [WebAssemblyImportModuleDocs];
+  let Subjects = SubjectList<[Function], ErrorDiag>;
+}
+
+def WebAssemblyImportName : InheritableAttr,
+                            TargetSpecificAttr<TargetWebAssembly> {
+  let Spellings = [Clang<"import_name">];
+  let Args = [StringArgument<"ImportName">];
+  let Documentation = [WebAssemblyImportNameDocs];
+  let Subjects = SubjectList<[Function], ErrorDiag>;
+}
+
 def NoSplitStack : InheritableAttr {
   let Spellings = [GCC<"no_split_stack">];
   let Subjects = SubjectList<[Function], ErrorDiag>;

Modified: cfe/branches/release_80/include/clang/Basic/AttrDocs.td
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_80/include/clang/Basic/AttrDocs.td?rev=353834&r1=353833&r2=353834&view=diff
==============================================================================
--- cfe/branches/release_80/include/clang/Basic/AttrDocs.td (original)
+++ cfe/branches/release_80/include/clang/Basic/AttrDocs.td Tue Feb 12 04:26:10 2019
@@ -3652,7 +3652,40 @@ definition (
 For more information see
 `gcc documentation <https://gcc.gnu.org/onlinedocs/gcc-7.2.0/gcc/Microsoft-Windows-Variable-Attributes.html>`_
 or `msvc documentation <https://docs.microsoft.com/pl-pl/cpp/cpp/selectany>`_.
-}];
+}]; }
+
+def WebAssemblyImportModuleDocs : Documentation {
+  let Category = DocCatFunction;
+  let Content = [{
+Clang supports the ``__attribute__((import_module(<module_name>)))`` 
+attribute for the WebAssembly target. This attribute may be attached to a
+function declaration, where it modifies how the symbol is to be imported
+within the WebAssembly linking environment.
+
+WebAssembly imports use a two-level namespace scheme, consisting of a module
+name, which typically identifies a module from which to import, and a field
+name, which typically identifies a field from that module to import. By
+default, module names for C/C++ symbols are assigned automatically by the
+linker. This attribute can be used to override the default behavior, and
+reuqest a specific module name be used instead.
+  }];
+}
+
+def WebAssemblyImportNameDocs : Documentation {
+  let Category = DocCatFunction;
+  let Content = [{
+Clang supports the ``__attribute__((import_name(<name>)))`` 
+attribute for the WebAssembly target. This attribute may be attached to a
+function declaration, where it modifies how the symbol is to be imported
+within the WebAssembly linking environment.
+
+WebAssembly imports use a two-level namespace scheme, consisting of a module
+name, which typically identifies a module from which to import, and a field
+name, which typically identifies a field from that module to import. By
+default, field names for C/C++ symbols are the same as their C/C++ symbol
+names. This attribute can be used to override the default behavior, and
+reuqest a specific field name be used instead.
+  }];
 }
 
 def ArtificialDocs : Documentation {

Modified: cfe/branches/release_80/lib/CodeGen/TargetInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_80/lib/CodeGen/TargetInfo.cpp?rev=353834&r1=353833&r2=353834&view=diff
==============================================================================
--- cfe/branches/release_80/lib/CodeGen/TargetInfo.cpp (original)
+++ cfe/branches/release_80/lib/CodeGen/TargetInfo.cpp Tue Feb 12 04:26:10 2019
@@ -761,6 +761,22 @@ public:
 
   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
                            CodeGen::CodeGenModule &CGM) const override {
+    TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
+    if (const auto *FD = dyn_cast_or_null<FunctionDecl>(D)) {
+      if (const auto *Attr = FD->getAttr<WebAssemblyImportModuleAttr>()) {
+        llvm::Function *Fn = cast<llvm::Function>(GV);
+        llvm::AttrBuilder B;
+        B.addAttribute("wasm-import-module", Attr->getImportModule());
+        Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
+      }
+      if (const auto *Attr = FD->getAttr<WebAssemblyImportNameAttr>()) {
+        llvm::Function *Fn = cast<llvm::Function>(GV);
+        llvm::AttrBuilder B;
+        B.addAttribute("wasm-import-name", Attr->getImportName());
+        Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
+      }
+    }
+
     if (auto *FD = dyn_cast_or_null<FunctionDecl>(D)) {
       llvm::Function *Fn = cast<llvm::Function>(GV);
       if (!FD->doesThisDeclarationHaveABody() && !FD->hasPrototype())

Modified: cfe/branches/release_80/lib/Sema/SemaDeclAttr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_80/lib/Sema/SemaDeclAttr.cpp?rev=353834&r1=353833&r2=353834&view=diff
==============================================================================
--- cfe/branches/release_80/lib/Sema/SemaDeclAttr.cpp (original)
+++ cfe/branches/release_80/lib/Sema/SemaDeclAttr.cpp Tue Feb 12 04:26:10 2019
@@ -5577,6 +5577,51 @@ static void handleAVRSignalAttr(Sema &S,
   handleSimpleAttribute<AVRSignalAttr>(S, D, AL);
 }
 
+static void handleWebAssemblyImportModuleAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
+  if (!isFunctionOrMethod(D)) {
+    S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
+        << "'import_module'" << ExpectedFunction;
+    return;
+  }
+
+  auto *FD = cast<FunctionDecl>(D);
+  if (FD->isThisDeclarationADefinition()) {
+    S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0;
+    return;
+  }
+
+  StringRef Str;
+  SourceLocation ArgLoc;
+  if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
+    return;
+
+  FD->addAttr(::new (S.Context) WebAssemblyImportModuleAttr(
+      AL.getRange(), S.Context, Str,
+      AL.getAttributeSpellingListIndex()));
+}
+
+static void handleWebAssemblyImportNameAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
+  if (!isFunctionOrMethod(D)) {
+    S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type)
+        << "'import_name'" << ExpectedFunction;
+    return;
+  }
+
+  auto *FD = cast<FunctionDecl>(D);
+  if (FD->isThisDeclarationADefinition()) {
+    S.Diag(D->getLocation(), diag::err_alias_is_definition) << FD << 0;
+    return;
+  }
+
+  StringRef Str;
+  SourceLocation ArgLoc;
+  if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &ArgLoc))
+    return;
+
+  FD->addAttr(::new (S.Context) WebAssemblyImportNameAttr(
+      AL.getRange(), S.Context, Str,
+      AL.getAttributeSpellingListIndex()));
+}
 
 static void handleRISCVInterruptAttr(Sema &S, Decl *D,
                                      const ParsedAttr &AL) {
@@ -6330,6 +6375,12 @@ static void ProcessDeclAttribute(Sema &S
   case ParsedAttr::AT_AVRSignal:
     handleAVRSignalAttr(S, D, AL);
     break;
+  case ParsedAttr::AT_WebAssemblyImportModule:
+    handleWebAssemblyImportModuleAttr(S, D, AL);
+    break;
+  case ParsedAttr::AT_WebAssemblyImportName:
+    handleWebAssemblyImportNameAttr(S, D, AL);
+    break;
   case ParsedAttr::AT_IBAction:
     handleSimpleAttribute<IBActionAttr>(S, D, AL);
     break;

Added: cfe/branches/release_80/test/CodeGen/wasm-import-module.c
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_80/test/CodeGen/wasm-import-module.c?rev=353834&view=auto
==============================================================================
--- cfe/branches/release_80/test/CodeGen/wasm-import-module.c (added)
+++ cfe/branches/release_80/test/CodeGen/wasm-import-module.c Tue Feb 12 04:26:10 2019
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -triple wasm32-unknown-unknown-wasm -emit-llvm -o - %s | FileCheck %s
+
+void __attribute__((import_module("bar"))) foo(void);
+
+void call(void) {
+  foo();
+}
+
+// CHECK: declare void @foo() [[A:#[0-9]+]]
+
+// CHECK: attributes [[A]] = {{{.*}} "wasm-import-module"="bar" {{.*}}}

Added: cfe/branches/release_80/test/CodeGen/wasm-import-name.c
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_80/test/CodeGen/wasm-import-name.c?rev=353834&view=auto
==============================================================================
--- cfe/branches/release_80/test/CodeGen/wasm-import-name.c (added)
+++ cfe/branches/release_80/test/CodeGen/wasm-import-name.c Tue Feb 12 04:26:10 2019
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -triple wasm32-unknown-unknown-wasm -emit-llvm -o - %s | FileCheck %s
+
+void __attribute__((import_name("bar"))) foo(void);
+
+void call(void) {
+  foo();
+}
+
+// CHECK: declare void @foo() [[A:#[0-9]+]]
+
+// CHECK: attributes [[A]] = {{{.*}} "wasm-import-name"="bar" {{.*}}}

Modified: cfe/branches/release_80/test/Misc/pragma-attribute-supported-attributes-list.test
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_80/test/Misc/pragma-attribute-supported-attributes-list.test?rev=353834&r1=353833&r2=353834&view=diff
==============================================================================
--- cfe/branches/release_80/test/Misc/pragma-attribute-supported-attributes-list.test (original)
+++ cfe/branches/release_80/test/Misc/pragma-attribute-supported-attributes-list.test Tue Feb 12 04:26:10 2019
@@ -136,6 +136,8 @@
 // CHECK-NEXT: WarnUnusedResult (SubjectMatchRule_objc_method, SubjectMatchRule_enum, SubjectMatchRule_record, SubjectMatchRule_hasType_functionType)
 // CHECK-NEXT: Weak (SubjectMatchRule_variable, SubjectMatchRule_function, SubjectMatchRule_record)
 // CHECK-NEXT: WeakRef (SubjectMatchRule_variable, SubjectMatchRule_function)
+// CHECK-NEXT: WebAssemblyImportModule (SubjectMatchRule_function)
+// CHECK-NEXT: WebAssemblyImportName (SubjectMatchRule_function)
 // CHECK-NEXT: WorkGroupSizeHint (SubjectMatchRule_function)
 // CHECK-NEXT: XRayInstrument (SubjectMatchRule_function, SubjectMatchRule_objc_method)
 // CHECK-NEXT: XRayLogArgs (SubjectMatchRule_function, SubjectMatchRule_objc_method)




More information about the llvm-branch-commits mailing list