[lld] c07e838 - [lld][WebAssembly] Add `--extra-features` flag to add addional features

Sam Clegg via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 13 09:36:58 PDT 2022


Author: Sam Clegg
Date: 2022-10-13T09:25:02-07:00
New Revision: c07e83813009dca9d7549991b3f5162b676059f7

URL: https://github.com/llvm/llvm-project/commit/c07e83813009dca9d7549991b3f5162b676059f7
DIFF: https://github.com/llvm/llvm-project/commit/c07e83813009dca9d7549991b3f5162b676059f7.diff

LOG: [lld][WebAssembly] Add `--extra-features` flag to add addional features

This flag acts just like the existing `--features` flag but instead
of replacing the set of inferred features it adds to it.

This is useful for example if you want to `--export` a mutable global
but none of the input of object were built with mutable global support.
In that case you can do `--extra-features=mutable-globals` to avoid the
linker error that would otherwise be generated in this case:

wasm-ld: error: mutable global exported but 'mutable-globals' feature not present in inputs: `__stack_pointer`. Use --no-check-features to suppress.

Differential Revision: https://reviews.llvm.org/D135831

Added: 
    

Modified: 
    lld/test/wasm/mutable-global-exports.s
    lld/wasm/Config.h
    lld/wasm/Driver.cpp
    lld/wasm/Options.td
    lld/wasm/Writer.cpp

Removed: 
    


################################################################################
diff  --git a/lld/test/wasm/mutable-global-exports.s b/lld/test/wasm/mutable-global-exports.s
index e2e45ff93a4bc..278be3a2e3b19 100644
--- a/lld/test/wasm/mutable-global-exports.s
+++ b/lld/test/wasm/mutable-global-exports.s
@@ -3,18 +3,17 @@
 # Should fail without mutable globals feature enabled.
 # RUN: not wasm-ld --export-all %t.o -o %t.wasm 2>&1 | FileCheck -check-prefix=CHECK-ERR %s
 # RUN: not wasm-ld --export=foo_global %t.o -o %t.wasm 2>&1 | FileCheck -check-prefix=CHECK-ERR %s
-#
-# RUN: wasm-ld --features=mutable-globals --export=foo_global %t.o -o %t.wasm
+
+# RUN: wasm-ld --extra-features=mutable-globals --export=foo_global %t.o -o %t.wasm
 # RUN: obj2yaml %t.wasm | FileCheck %s
 
 # Explcitly check that __stack_pointer can be exported
-# RUN: wasm-ld --features=mutable-globals --export=__stack_pointer %t.o -o %t.wasm
+# RUN: wasm-ld --extra-features=mutable-globals --export=__stack_pointer %t.o -o %t.wasm
 # RUN: obj2yaml %t.wasm | FileCheck -check-prefix=CHECK-SP %s
 
-# RUN: wasm-ld --features=mutable-globals --export-all %t.o -o %t.wasm
+# RUN: wasm-ld --extra-features=mutable-globals --export-all %t.o -o %t.wasm
 # RUN: obj2yaml %t.wasm | FileCheck -check-prefix=CHECK-ALL %s
 
-
 .globl _start
 .globl foo_global
 
@@ -25,6 +24,14 @@ _start:
   .functype _start () -> ()
   end_function
 
+# Add a target feature and ensure that it is preserved when --extra-features is
+# used above.
+.section  .custom_section.target_features,"",@
+  .int8 1
+  .int8 43
+  .int8 7
+  .ascii  "atomics"
+
 # CHECK-ERR: mutable global exported but 'mutable-globals' feature not present in inputs: `foo_global`. Use --no-check-features to suppress
 
 #      CHECK:  - Type:            EXPORT
@@ -86,3 +93,12 @@ _start:
 # CHECK-ALL-NEXT:        Kind:            GLOBAL
 # CHECK-ALL-NEXT:        Index:           7
 # CHECK-ALL-NEXT:  - Type:            CODE
+
+# CHECK-ALL:         Name:            target_features
+# CHECK-ALL-NEXT:    Features:
+# CHECK-ALL-NEXT:      - Prefix:          USED
+# CHECK-ALL-NEXT:        Name:            atomics
+# CHECK-ALL-NEXT:      - Prefix:          USED
+# CHECK-ALL-NEXT:        Name:            mutable-globals
+# CHECK-ALL-NEXT: ...
+

diff  --git a/lld/wasm/Config.h b/lld/wasm/Config.h
index fbc195e974f0b..35171b5afd8f1 100644
--- a/lld/wasm/Config.h
+++ b/lld/wasm/Config.h
@@ -77,6 +77,7 @@ struct Configuration {
   llvm::SmallVector<llvm::StringRef, 0> searchPaths;
   llvm::CachePruningPolicy thinLTOCachePolicy;
   llvm::Optional<std::vector<std::string>> features;
+  llvm::Optional<std::vector<std::string>> extraFeatures;
 
   // The following config options do not directly correspond to any
   // particular command line options.

diff  --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp
index e6b2239e7a159..62f90d7eaa7b4 100644
--- a/lld/wasm/Driver.cpp
+++ b/lld/wasm/Driver.cpp
@@ -469,6 +469,13 @@ static void readConfigs(opt::InputArgList &args) {
       config->features->push_back(std::string(s));
   }
 
+  if (auto *arg = args.getLastArg(OPT_extra_features)) {
+    config->extraFeatures =
+        llvm::Optional<std::vector<std::string>>(std::vector<std::string>());
+    for (StringRef s : arg->getValues())
+      config->extraFeatures->push_back(std::string(s));
+  }
+
   // Legacy --allow-undefined flag which is equivalent to
   // --unresolve-symbols=ignore + --import-undefined
   if (args.hasArg(OPT_allow_undefined)) {

diff  --git a/lld/wasm/Options.td b/lld/wasm/Options.td
index 7a6192433ec85..ec452a38ae53e 100644
--- a/lld/wasm/Options.td
+++ b/lld/wasm/Options.td
@@ -220,6 +220,9 @@ defm check_features: BB<"check-features",
 def features: CommaJoined<["--", "-"], "features=">,
   HelpText<"Comma-separated used features, inferred from input objects by default.">;
 
+def extra_features: CommaJoined<["--", "-"], "extra-features=">,
+  HelpText<"Comma-separated list of features to add to the default set of features inferred from input objects.">;
+
 // Aliases
 def: JoinedOrSeparate<["-"], "e">, Alias<entry>;
 def: J<"entry=">, Alias<entry>;

diff  --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp
index 14dc82d0284a8..c8377dd0f972c 100644
--- a/lld/wasm/Writer.cpp
+++ b/lld/wasm/Writer.cpp
@@ -445,6 +445,11 @@ void Writer::populateTargetFeatures() {
     allowed.insert("mutable-globals");
   }
 
+  if (config->extraFeatures.has_value()) {
+    auto &extraFeatures = config->extraFeatures.value();
+    allowed.insert(extraFeatures.begin(), extraFeatures.end());
+  }
+
   // Only infer used features if user did not specify features
   bool inferFeatures = !config->features.has_value();
 


        


More information about the llvm-commits mailing list