Skip to content

Commit c07e838

Browse files
committed
[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
1 parent 0cd27cd commit c07e838

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

lld/test/wasm/mutable-global-exports.s

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@
33
# Should fail without mutable globals feature enabled.
44
# RUN: not wasm-ld --export-all %t.o -o %t.wasm 2>&1 | FileCheck -check-prefix=CHECK-ERR %s
55
# RUN: not wasm-ld --export=foo_global %t.o -o %t.wasm 2>&1 | FileCheck -check-prefix=CHECK-ERR %s
6-
#
7-
# RUN: wasm-ld --features=mutable-globals --export=foo_global %t.o -o %t.wasm
6+
7+
# RUN: wasm-ld --extra-features=mutable-globals --export=foo_global %t.o -o %t.wasm
88
# RUN: obj2yaml %t.wasm | FileCheck %s
99

1010
# Explcitly check that __stack_pointer can be exported
11-
# RUN: wasm-ld --features=mutable-globals --export=__stack_pointer %t.o -o %t.wasm
11+
# RUN: wasm-ld --extra-features=mutable-globals --export=__stack_pointer %t.o -o %t.wasm
1212
# RUN: obj2yaml %t.wasm | FileCheck -check-prefix=CHECK-SP %s
1313

14-
# RUN: wasm-ld --features=mutable-globals --export-all %t.o -o %t.wasm
14+
# RUN: wasm-ld --extra-features=mutable-globals --export-all %t.o -o %t.wasm
1515
# RUN: obj2yaml %t.wasm | FileCheck -check-prefix=CHECK-ALL %s
1616

17-
1817
.globl _start
1918
.globl foo_global
2019

@@ -25,6 +24,14 @@ _start:
2524
.functype _start () -> ()
2625
end_function
2726

27+
# Add a target feature and ensure that it is preserved when --extra-features is
28+
# used above.
29+
.section .custom_section.target_features,"",@
30+
.int8 1
31+
.int8 43
32+
.int8 7
33+
.ascii "atomics"
34+
2835
# CHECK-ERR: mutable global exported but 'mutable-globals' feature not present in inputs: `foo_global`. Use --no-check-features to suppress
2936

3037
# CHECK: - Type: EXPORT
@@ -86,3 +93,12 @@ _start:
8693
# CHECK-ALL-NEXT: Kind: GLOBAL
8794
# CHECK-ALL-NEXT: Index: 7
8895
# CHECK-ALL-NEXT: - Type: CODE
96+
97+
# CHECK-ALL: Name: target_features
98+
# CHECK-ALL-NEXT: Features:
99+
# CHECK-ALL-NEXT: - Prefix: USED
100+
# CHECK-ALL-NEXT: Name: atomics
101+
# CHECK-ALL-NEXT: - Prefix: USED
102+
# CHECK-ALL-NEXT: Name: mutable-globals
103+
# CHECK-ALL-NEXT: ...
104+

lld/wasm/Config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ struct Configuration {
7777
llvm::SmallVector<llvm::StringRef, 0> searchPaths;
7878
llvm::CachePruningPolicy thinLTOCachePolicy;
7979
llvm::Optional<std::vector<std::string>> features;
80+
llvm::Optional<std::vector<std::string>> extraFeatures;
8081

8182
// The following config options do not directly correspond to any
8283
// particular command line options.

lld/wasm/Driver.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,13 @@ static void readConfigs(opt::InputArgList &args) {
469469
config->features->push_back(std::string(s));
470470
}
471471

472+
if (auto *arg = args.getLastArg(OPT_extra_features)) {
473+
config->extraFeatures =
474+
llvm::Optional<std::vector<std::string>>(std::vector<std::string>());
475+
for (StringRef s : arg->getValues())
476+
config->extraFeatures->push_back(std::string(s));
477+
}
478+
472479
// Legacy --allow-undefined flag which is equivalent to
473480
// --unresolve-symbols=ignore + --import-undefined
474481
if (args.hasArg(OPT_allow_undefined)) {

lld/wasm/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ defm check_features: BB<"check-features",
220220
def features: CommaJoined<["--", "-"], "features=">,
221221
HelpText<"Comma-separated used features, inferred from input objects by default.">;
222222

223+
def extra_features: CommaJoined<["--", "-"], "extra-features=">,
224+
HelpText<"Comma-separated list of features to add to the default set of features inferred from input objects.">;
225+
223226
// Aliases
224227
def: JoinedOrSeparate<["-"], "e">, Alias<entry>;
225228
def: J<"entry=">, Alias<entry>;

lld/wasm/Writer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,11 @@ void Writer::populateTargetFeatures() {
445445
allowed.insert("mutable-globals");
446446
}
447447

448+
if (config->extraFeatures.has_value()) {
449+
auto &extraFeatures = config->extraFeatures.value();
450+
allowed.insert(extraFeatures.begin(), extraFeatures.end());
451+
}
452+
448453
// Only infer used features if user did not specify features
449454
bool inferFeatures = !config->features.has_value();
450455

0 commit comments

Comments
 (0)