Skip to content

Commit 1bc2cd9

Browse files
authored
[WebAssembly] Enable nontrapping-fptoint and bulk-memory by default. (#112049)
We were prepared to enable these features [back in February], but they got pulled for what appear to be unrelated reasons. So let's have another try at enabling them! Another motivation here is that it'd be convenient for the [Lime1 proposal] if "lime1" is close to a subset of "generic" (missing only for extended-const). [back in February]: WebAssembly/tool-conventions#158 (comment) [Lime1 proposal]: #112035
1 parent cfde4fb commit 1bc2cd9

16 files changed

+74
-26
lines changed

clang/docs/ReleaseNotes.rst

+9
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,15 @@ NetBSD Support
690690
WebAssembly Support
691691
^^^^^^^^^^^^^^^^^^^
692692

693+
The default target CPU, "generic", now enables the `-mnontrapping-fptoint`
694+
and `-mbulk-memory` flags, which correspond to the [Bulk Memory Operations]
695+
and [Non-trapping float-to-int Conversions] language features, which are
696+
[widely implemented in engines].
697+
698+
[Bulk Memory Operations]: https://github.com/WebAssembly/bulk-memory-operations/blob/master/proposals/bulk-memory-operations/Overview.md
699+
[Non-trapping float-to-int Conversions]: https://github.com/WebAssembly/spec/blob/master/proposals/nontrapping-float-to-int-conversion/Overview.md
700+
[widely implemented in engines]: https://webassembly.org/features/
701+
693702
AVR Support
694703
^^^^^^^^^^^
695704

clang/lib/Basic/Targets/WebAssembly.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -154,20 +154,20 @@ bool WebAssemblyTargetInfo::initFeatureMap(
154154
llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
155155
const std::vector<std::string> &FeaturesVec) const {
156156
auto addGenericFeatures = [&]() {
157+
Features["bulk-memory"] = true;
157158
Features["multivalue"] = true;
158159
Features["mutable-globals"] = true;
160+
Features["nontrapping-fptoint"] = true;
159161
Features["reference-types"] = true;
160162
Features["sign-ext"] = true;
161163
};
162164
auto addBleedingEdgeFeatures = [&]() {
163165
addGenericFeatures();
164166
Features["atomics"] = true;
165-
Features["bulk-memory"] = true;
166167
Features["exception-handling"] = true;
167168
Features["extended-const"] = true;
168169
Features["fp16"] = true;
169170
Features["multimemory"] = true;
170-
Features["nontrapping-fptoint"] = true;
171171
Features["tail-call"] = true;
172172
Features["wide-arithmetic"] = true;
173173
setSIMDLevel(Features, RelaxedSIMD, true);

clang/test/Preprocessor/wasm-target-features.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,10 @@
163163
// RUN: -target wasm64-unknown-unknown -mcpu=generic \
164164
// RUN: | FileCheck %s -check-prefix=GENERIC-INCLUDE
165165
//
166+
// GENERIC-INCLUDE-DAG: #define __wasm_bulk_memory__ 1{{$}}
166167
// GENERIC-INCLUDE-DAG: #define __wasm_multivalue__ 1{{$}}
167168
// GENERIC-INCLUDE-DAG: #define __wasm_mutable_globals__ 1{{$}}
169+
// GENERIC-INCLUDE-DAG: #define __wasm_nontrapping_fptoint__ 1{{$}}
168170
// GENERIC-INCLUDE-DAG: #define __wasm_reference_types__ 1{{$}}
169171
// GENERIC-INCLUDE-DAG: #define __wasm_sign_ext__ 1{{$}}
170172
//
@@ -176,12 +178,10 @@
176178
// RUN: | FileCheck %s -check-prefix=GENERIC
177179
//
178180
// GENERIC-NOT: #define __wasm_atomics__ 1{{$}}
179-
// GENERIC-NOT: #define __wasm_bulk_memory__ 1{{$}}
180181
// GENERIC-NOT: #define __wasm_exception_handling__ 1{{$}}
181182
// GENERIC-NOT: #define __wasm_extended_const__ 1{{$}}
182183
// GENERIC-NOT: #define __wasm__fp16__ 1{{$}}
183184
// GENERIC-NOT: #define __wasm_multimemory__ 1{{$}}
184-
// GENERIC-NOT: #define __wasm_nontrapping_fptoint__ 1{{$}}
185185
// GENERIC-NOT: #define __wasm_relaxed_simd__ 1{{$}}
186186
// GENERIC-NOT: #define __wasm_simd128__ 1{{$}}
187187
// GENERIC-NOT: #define __wasm_tail_call__ 1{{$}}

lld/test/wasm/custom-section-name.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: llc -filetype=obj %s -o %t.o
1+
; RUN: llc -filetype=obj -mattr=-bulk-memory %s -o %t.o
22
; RUN: wasm-ld -no-gc-sections --no-entry -o %t.wasm %t.o
33
; RUN: obj2yaml %t.wasm | FileCheck %s --check-prefixes=CHECK,NO-BSS
44
; RUN: wasm-ld -no-gc-sections --no-entry --import-memory -o %t.bss.wasm %t.o

lld/test/wasm/data-segments.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: llc --mtriple=wasm32-unknown-unknown -filetype=obj %s -o %t.atomics.o -mattr=+atomics
1+
; RUN: llc --mtriple=wasm32-unknown-unknown -filetype=obj %s -o %t.atomics.o -mattr=+atomics,-bulk-memory
22
; RUN: llc --mtriple=wasm32-unknown-unknown -filetype=obj %s -o %t.bulk-mem.o -mattr=+bulk-memory
33
; RUN: llc --mtriple=wasm64-unknown-unknown -filetype=obj %s -o %t.bulk-mem64.o -mattr=+bulk-memory
44
; RUN: llc --mtriple=wasm32-unknown-unknown -filetype=obj %s -o %t.atomics.bulk-mem.o -mattr=+atomics,+bulk-memory
+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
target datalayout = "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20"
22
target triple = "wasm32-unknown-unknown"
33

4-
define void @memcpy() {
4+
define void @memcpy() #0 {
55
ret void
66
}
7+
8+
attributes #0 = { "target-features"="-bulk-memory" }

lld/test/wasm/lto/libcall-archive.ll

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@
88
target datalayout = "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20"
99
target triple = "wasm32-unknown-unknown"
1010

11-
define void @_start(ptr %a, ptr %b) {
11+
define void @_start(ptr %a, ptr %b) #0 {
1212
entry:
1313
call void @llvm.memcpy.p0.p0.i64(ptr %a, ptr %b, i64 1024, i1 false)
1414
ret void
1515
}
1616

1717
declare void @llvm.memcpy.p0.p0.i64(ptr nocapture, ptr nocapture, i64, i1)
1818

19+
attributes #0 = { "target-features"="-bulk-memory" }
20+
1921
; CHECK: - Type: CUSTOM
2022
; CHECK-NEXT: Name: name
2123
; CHECK-NEXT: FunctionNames:

lld/test/wasm/lto/stub-library-libcall.s

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t_main.o %t/main.s
33
# RUN: llvm-as %S/Inputs/foo.ll -o %t_foo.o
44
# RUN: llvm-as %S/Inputs/libcall.ll -o %t_libcall.o
5-
# RUN: wasm-ld %t_main.o %t_libcall.o %t_foo.o %p/Inputs/stub.so -o %t.wasm
5+
# RUN: wasm-ld -mllvm -mattr=-bulk-memory %t_main.o %t_libcall.o %t_foo.o %p/Inputs/stub.so -o %t.wasm
66
# RUN: obj2yaml %t.wasm | FileCheck %s
77

88
# The function `func_with_libcall` will generate an undefined reference to
@@ -12,7 +12,7 @@
1212
# If %t_foo.o is not included in the link we get an undefined symbol reported
1313
# to the dependency of memcpy on the foo export:
1414

15-
# RUN: not wasm-ld %t_main.o %t_libcall.o %p/Inputs/stub.so -o %t.wasm 2>&1 | FileCheck --check-prefix=MISSING %s
15+
# RUN: not wasm-ld -mllvm -mattr=-bulk-memory %t_main.o %t_libcall.o %p/Inputs/stub.so -o %t.wasm 2>&1 | FileCheck --check-prefix=MISSING %s
1616
# MISSING: stub.so: undefined symbol: foo. Required by memcpy
1717

1818
#--- main.s

llvm/docs/ReleaseNotes.md

+9
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,15 @@ Changes to the RISC-V Backend
180180
Changes to the WebAssembly Backend
181181
----------------------------------
182182

183+
The default target CPU, "generic", now enables the `-mnontrapping-fptoint`
184+
and `-mbulk-memory` flags, which correspond to the [Bulk Memory Operations]
185+
and [Non-trapping float-to-int Conversions] language features, which are
186+
[widely implemented in engines].
187+
188+
[Bulk Memory Operations]: https://github.com/WebAssembly/bulk-memory-operations/blob/master/proposals/bulk-memory-operations/Overview.md
189+
[Non-trapping float-to-int Conversions]: https://github.com/WebAssembly/spec/blob/master/proposals/nontrapping-float-to-int-conversion/Overview.md
190+
[widely implemented in engines]: https://webassembly.org/features/
191+
183192
Changes to the Windows Target
184193
-----------------------------
185194

llvm/lib/Target/WebAssembly/WebAssembly.td

+2-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ def : ProcessorModel<"mvp", NoSchedModel, []>;
114114
// consideration given to available support in relevant engines and tools, and
115115
// the importance of the features.
116116
def : ProcessorModel<"generic", NoSchedModel,
117-
[FeatureMultivalue, FeatureMutableGlobals,
117+
[FeatureBulkMemory, FeatureMultivalue,
118+
FeatureMutableGlobals, FeatureNontrappingFPToInt,
118119
FeatureReferenceTypes, FeatureSignExt]>;
119120

120121
// Latest and greatest experimental version of WebAssembly. Bugs included!

llvm/lib/Target/WebAssembly/WebAssemblyFixFunctionBitcasts.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ static Function *createWrapper(Function *F, FunctionType *Ty) {
111111

112112
Function *Wrapper = Function::Create(Ty, Function::PrivateLinkage,
113113
F->getName() + "_bitcast", M);
114+
Wrapper->setAttributes(F->getAttributes());
114115
BasicBlock *BB = BasicBlock::Create(M->getContext(), "body", Wrapper);
115116
const DataLayout &DL = BB->getDataLayout();
116117

@@ -201,6 +202,7 @@ static Function *createWrapper(Function *F, FunctionType *Ty) {
201202
Wrapper->eraseFromParent();
202203
Wrapper = Function::Create(Ty, Function::PrivateLinkage,
203204
F->getName() + "_bitcast_invalid", M);
205+
Wrapper->setAttributes(F->getAttributes());
204206
BasicBlock *BB = BasicBlock::Create(M->getContext(), "body", Wrapper);
205207
new UnreachableInst(M->getContext(), BB);
206208
Wrapper->setName(F->getName() + "_bitcast_invalid");

llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp

+23-6
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,30 @@ class CoalesceFeaturesAndStripAtomics final : public ModulePass {
233233

234234
private:
235235
FeatureBitset coalesceFeatures(const Module &M) {
236-
FeatureBitset Features =
237-
WasmTM
238-
->getSubtargetImpl(std::string(WasmTM->getTargetCPU()),
239-
std::string(WasmTM->getTargetFeatureString()))
240-
->getFeatureBits();
241-
for (auto &F : M)
236+
// Union the features of all defined functions. Start with an empty set, so
237+
// that if a feature is disabled in every function, we'll compute it as
238+
// disabled. If any function lacks a target-features attribute, it'll
239+
// default to the target CPU from the `TargetMachine`.
240+
FeatureBitset Features;
241+
bool AnyDefinedFuncs = false;
242+
for (auto &F : M) {
243+
if (F.isDeclaration())
244+
continue;
245+
242246
Features |= WasmTM->getSubtargetImpl(F)->getFeatureBits();
247+
AnyDefinedFuncs = true;
248+
}
249+
250+
// If we have no defined functions, use the target CPU from the
251+
// `TargetMachine`.
252+
if (!AnyDefinedFuncs) {
253+
Features =
254+
WasmTM
255+
->getSubtargetImpl(std::string(WasmTM->getTargetCPU()),
256+
std::string(WasmTM->getTargetFeatureString()))
257+
->getFeatureBits();
258+
}
259+
243260
return Features;
244261
}
245262

llvm/test/CodeGen/WebAssembly/cfg-stackify-eh-legacy.ll

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
; REQUIRES: asserts
2-
; RUN: llc < %s -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -disable-block-placement -verify-machineinstrs -fast-isel=false -machine-sink-split-probability-threshold=0 -cgp-freq-ratio-to-skip-merge=1000 -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling | FileCheck %s
3-
; RUN: llc < %s -disable-wasm-fallthrough-return-opt -disable-block-placement -verify-machineinstrs -fast-isel=false -machine-sink-split-probability-threshold=0 -cgp-freq-ratio-to-skip-merge=1000 -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling
4-
; RUN: llc < %s -O0 -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -verify-machineinstrs -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling | FileCheck %s --check-prefix=NOOPT
5-
; RUN: llc < %s -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -disable-block-placement -verify-machineinstrs -fast-isel=false -machine-sink-split-probability-threshold=0 -cgp-freq-ratio-to-skip-merge=1000 -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling -wasm-disable-ehpad-sort -stats 2>&1 | FileCheck %s --check-prefix=NOSORT
6-
; RUN: llc < %s -disable-wasm-fallthrough-return-opt -disable-block-placement -verify-machineinstrs -fast-isel=false -machine-sink-split-probability-threshold=0 -cgp-freq-ratio-to-skip-merge=1000 -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling -wasm-disable-ehpad-sort | FileCheck %s --check-prefix=NOSORT-LOCALS
2+
; RUN: llc < %s -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -disable-block-placement -verify-machineinstrs -fast-isel=false -machine-sink-split-probability-threshold=0 -cgp-freq-ratio-to-skip-merge=1000 -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling,bulk-memory | FileCheck %s
3+
; RUN: llc < %s -disable-wasm-fallthrough-return-opt -disable-block-placement -verify-machineinstrs -fast-isel=false -machine-sink-split-probability-threshold=0 -cgp-freq-ratio-to-skip-merge=1000 -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling,bulk-memory
4+
; RUN: llc < %s -O0 -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -verify-machineinstrs -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling,-bulk-memory | FileCheck %s --check-prefix=NOOPT
5+
; RUN: llc < %s -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -disable-block-placement -verify-machineinstrs -fast-isel=false -machine-sink-split-probability-threshold=0 -cgp-freq-ratio-to-skip-merge=1000 -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling,-bulk-memory -wasm-disable-ehpad-sort -stats 2>&1 | FileCheck %s --check-prefix=NOSORT
6+
; RUN: llc < %s -disable-wasm-fallthrough-return-opt -disable-block-placement -verify-machineinstrs -fast-isel=false -machine-sink-split-probability-threshold=0 -cgp-freq-ratio-to-skip-merge=1000 -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling,-bulk-memory -wasm-disable-ehpad-sort | FileCheck %s --check-prefix=NOSORT-LOCALS
77

88
target triple = "wasm32-unknown-unknown"
99

llvm/test/CodeGen/WebAssembly/target-features-cpus.ll

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,20 @@ target triple = "wasm32-unknown-unknown"
1313

1414
; generic: +multivalue, +mutable-globals, +reference-types, +sign-ext
1515
; GENERIC-LABEL: .custom_section.target_features,"",@
16-
; GENERIC-NEXT: .int8 4
16+
; GENERIC-NEXT: .int8 6
17+
; GENERIC-NEXT: .int8 43
18+
; GENERIC-NEXT: .int8 11
19+
; GENERIC-NEXT: .ascii "bulk-memory"
1720
; GENERIC-NEXT: .int8 43
1821
; GENERIC-NEXT: .int8 10
1922
; GENERIC-NEXT: .ascii "multivalue"
2023
; GENERIC-NEXT: .int8 43
2124
; GENERIC-NEXT: .int8 15
2225
; GENERIC-NEXT: .ascii "mutable-globals"
2326
; GENERIC-NEXT: .int8 43
27+
; GENERIC-NEXT: .int8 19
28+
; GENERIC-NEXT: .ascii "nontrapping-fptoint"
29+
; GENERIC-NEXT: .int8 43
2430
; GENERIC-NEXT: .int8 15
2531
; GENERIC-NEXT: .ascii "reference-types"
2632
; GENERIC-NEXT: .int8 43

llvm/test/MC/WebAssembly/extern-functype-intrinsic.ll

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
; RUN: llc %s -o - | FileCheck %s
2-
; RUN: llc %s -o - | llvm-mc -triple=wasm32-unknown-unknown | FileCheck %s
1+
; RUN: llc %s -mattr=-bulk-memory -o - | FileCheck %s
2+
; RUN: llc %s -mattr=-bulk-memory -o - | llvm-mc -triple=wasm32-unknown-unknown | FileCheck %s
33

44
; ModuleID = 'test.c'
55
source_filename = "test.c"

llvm/test/MC/WebAssembly/libcall.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: llc -filetype=obj %s -o - | obj2yaml | FileCheck %s
1+
; RUN: llc -filetype=obj -mattr=-bulk-memory %s -o - | obj2yaml | FileCheck %s
22

33
target triple = "wasm32-unknown-unknown"
44

0 commit comments

Comments
 (0)