Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WebAssembly] Enable nontrapping-fptoint and bulk-memory by default. #112049

Merged
merged 10 commits into from
Oct 25, 2024
9 changes: 9 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,15 @@ NetBSD Support
WebAssembly Support
^^^^^^^^^^^^^^^^^^^

The default target CPU, "generic", now enables the `-mnontrapping-fptoint`
and `-mbulk-memory` flags, which correspond to the [Bulk Memory Operations]
and [Non-trapping float-to-int Conversions] language features, which are
[widely implemented in engines].

[Bulk Memory Operations]: https://github.com/WebAssembly/bulk-memory-operations/blob/master/proposals/bulk-memory-operations/Overview.md
[Non-trapping float-to-int Conversions]: https://github.com/WebAssembly/spec/blob/master/proposals/nontrapping-float-to-int-conversion/Overview.md
[widely implemented in engines]: https://webassembly.org/features/

AVR Support
^^^^^^^^^^^

Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Basic/Targets/WebAssembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,20 @@ bool WebAssemblyTargetInfo::initFeatureMap(
llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
const std::vector<std::string> &FeaturesVec) const {
auto addGenericFeatures = [&]() {
Features["bulk-memory"] = true;
sunfishcode marked this conversation as resolved.
Show resolved Hide resolved
Features["multivalue"] = true;
Features["mutable-globals"] = true;
Features["nontrapping-fptoint"] = true;
Features["reference-types"] = true;
Features["sign-ext"] = true;
};
auto addBleedingEdgeFeatures = [&]() {
addGenericFeatures();
Features["atomics"] = true;
Features["bulk-memory"] = true;
Features["exception-handling"] = true;
Features["extended-const"] = true;
Features["fp16"] = true;
Features["multimemory"] = true;
Features["nontrapping-fptoint"] = true;
Features["tail-call"] = true;
Features["wide-arithmetic"] = true;
setSIMDLevel(Features, RelaxedSIMD, true);
Expand Down
4 changes: 2 additions & 2 deletions clang/test/Preprocessor/wasm-target-features.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,10 @@
// RUN: -target wasm64-unknown-unknown -mcpu=generic \
// RUN: | FileCheck %s -check-prefix=GENERIC-INCLUDE
//
// GENERIC-INCLUDE-DAG: #define __wasm_bulk_memory__ 1{{$}}
// GENERIC-INCLUDE-DAG: #define __wasm_multivalue__ 1{{$}}
// GENERIC-INCLUDE-DAG: #define __wasm_mutable_globals__ 1{{$}}
// GENERIC-INCLUDE-DAG: #define __wasm_nontrapping_fptoint__ 1{{$}}
// GENERIC-INCLUDE-DAG: #define __wasm_reference_types__ 1{{$}}
// GENERIC-INCLUDE-DAG: #define __wasm_sign_ext__ 1{{$}}
//
Expand All @@ -176,12 +178,10 @@
// RUN: | FileCheck %s -check-prefix=GENERIC
//
// GENERIC-NOT: #define __wasm_atomics__ 1{{$}}
// GENERIC-NOT: #define __wasm_bulk_memory__ 1{{$}}
// GENERIC-NOT: #define __wasm_exception_handling__ 1{{$}}
// GENERIC-NOT: #define __wasm_extended_const__ 1{{$}}
// GENERIC-NOT: #define __wasm__fp16__ 1{{$}}
// GENERIC-NOT: #define __wasm_multimemory__ 1{{$}}
// GENERIC-NOT: #define __wasm_nontrapping_fptoint__ 1{{$}}
// GENERIC-NOT: #define __wasm_relaxed_simd__ 1{{$}}
// GENERIC-NOT: #define __wasm_simd128__ 1{{$}}
// GENERIC-NOT: #define __wasm_tail_call__ 1{{$}}
Expand Down
2 changes: 1 addition & 1 deletion lld/test/wasm/custom-section-name.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: llc -filetype=obj %s -o %t.o
; RUN: llc -filetype=obj -mattr=-bulk-memory %s -o %t.o
; RUN: wasm-ld -no-gc-sections --no-entry -o %t.wasm %t.o
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to disable bulk memory here and in many other files?

Copy link
Member Author

@sunfishcode sunfishcode Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In lld/test/wasm/custom-section-name.ll, it's because bulk-memory makes the target_features section bigger and thus changes some of the section offsets and it was easier to just disable bulk-memory than to figure out the new section offsets. I can update the section offsets if you prefer though. See my comment below.

In lld/test/wasm/data-segments.ll, it's because the test is written to test both bulk-memory and non-bulk-memory, and to keep it doing that, it's now necessary to explicitly disable bulk-memory for the non-bulk-memory case.

In lld/test/wasm/lto/stub-library-libcall.s it's because the test is testing the use of a memcpy stub function, and with bulk-memory it gets inlined.

llvm/test/MC/WebAssembly/extern-functype-intrinsic.ll is similarly testing memset calls.

llvm/test/MC/WebAssembly/libcall.ll is similarly testing memcpy calls.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've now investigated custom-section-name.ll more.

With bulk-memory, we get a __wasm_init_memory function:

(module $custom-section-name.bss.wasm
  (type (;0;) (func))
  (import "env" "memory" (memory (;0;) 2))
  (func $__wasm_call_ctors (;0;) (type 0))
  (func $__wasm_init_memory (;1;) (type 0)
    i32.const 1036
    i32.const 0
    i32.const 4
    memory.fill
  )
  (global $__stack_pointer (;0;) (mut i32) i32.const 66576)
  (start $__wasm_init_memory)
  (data $WowZero! (;0;) (i32.const 1024) "\00\00\00\00")
  (data $MyAwesomeSection (;1;) (i32.const 1028) "*\00\00\00")
  (data $AnotherGreatSection (;2;) (i32.const 1032) "\07\00\00\00")
  (@custom "target_features" (after data) "\06+\0bbulk-memory+\16call-indirect-overlong+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext")
)

Without bulk-memory we get a .bss section:

(module $custom-section-name.bss.wasm
  (type (;0;) (func))
  (import "env" "memory" (memory (;0;) 2))
  (func $__wasm_call_ctors (;0;) (type 0))
  (global $__stack_pointer (;0;) (mut i32) i32.const 66576)
  (data $WowZero! (;0;) (i32.const 1024) "\00\00\00\00")
  (data $MyAwesomeSection (;1;) (i32.const 1028) "*\00\00\00")
  (data $AnotherGreatSection (;2;) (i32.const 1032) "\07\00\00\00")
  (data $.bss (;3;) (i32.const 1036) "\00\00\00\00")
  (@custom "target_features" (after data) "\05+\16call-indirect-overlong+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext")
)

The test is intended to test the use of the .bss section, and we only get a .bss section in this test if we disable bulk-memory.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, interesting, so we are using passive segments anytime bulk memory is enabled. That makes sense since this was all meant for threads, but is this actually an improvement in the non-threads case? i.e. maybe we don't want the extra explicit initialization code if we can help it?

edit: i just checked this out but I'll leave it here FTR: it looks like after linking we actually don't use passive segments unless threads is also enabled. so this seems like not a problem.

; RUN: obj2yaml %t.wasm | FileCheck %s --check-prefixes=CHECK,NO-BSS
; RUN: wasm-ld -no-gc-sections --no-entry --import-memory -o %t.bss.wasm %t.o
Expand Down
2 changes: 1 addition & 1 deletion lld/test/wasm/data-segments.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: llc --mtriple=wasm32-unknown-unknown -filetype=obj %s -o %t.atomics.o -mattr=+atomics
; RUN: llc --mtriple=wasm32-unknown-unknown -filetype=obj %s -o %t.atomics.o -mattr=+atomics,-bulk-memory
; RUN: llc --mtriple=wasm32-unknown-unknown -filetype=obj %s -o %t.bulk-mem.o -mattr=+bulk-memory
; RUN: llc --mtriple=wasm64-unknown-unknown -filetype=obj %s -o %t.bulk-mem64.o -mattr=+bulk-memory
; RUN: llc --mtriple=wasm32-unknown-unknown -filetype=obj %s -o %t.atomics.bulk-mem.o -mattr=+atomics,+bulk-memory
Expand Down
4 changes: 3 additions & 1 deletion lld/test/wasm/lto/Inputs/libcall-archive.ll
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
target datalayout = "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-n32:64-S128"
target triple = "wasm32-unknown-unknown"

define void @memcpy() {
define void @memcpy() #0 {
ret void
}

attributes #0 = { "target-features"="-bulk-memory" }
4 changes: 3 additions & 1 deletion lld/test/wasm/lto/libcall-archive.ll
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
target datalayout = "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-n32:64-S128"
target triple = "wasm32-unknown-unknown"

define void @_start(ptr %a, ptr %b) {
define void @_start(ptr %a, ptr %b) #0 {
entry:
call void @llvm.memcpy.p0.p0.i64(ptr %a, ptr %b, i64 1024, i1 false)
ret void
}

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

attributes #0 = { "target-features"="-bulk-memory" }

; CHECK: - Type: CUSTOM
; CHECK-NEXT: Name: name
; CHECK-NEXT: FunctionNames:
Expand Down
4 changes: 2 additions & 2 deletions lld/test/wasm/lto/stub-library-libcall.s
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t_main.o %t/main.s
# RUN: llvm-as %S/Inputs/foo.ll -o %t_foo.o
# RUN: llvm-as %S/Inputs/libcall.ll -o %t_libcall.o
# RUN: wasm-ld %t_main.o %t_libcall.o %t_foo.o %p/Inputs/stub.so -o %t.wasm
# RUN: wasm-ld -mllvm -mattr=-bulk-memory %t_main.o %t_libcall.o %t_foo.o %p/Inputs/stub.so -o %t.wasm
# RUN: obj2yaml %t.wasm | FileCheck %s

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

# RUN: not wasm-ld %t_main.o %t_libcall.o %p/Inputs/stub.so -o %t.wasm 2>&1 | FileCheck --check-prefix=MISSING %s
# 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
# MISSING: stub.so: undefined symbol: foo. Required by memcpy

#--- main.s
Expand Down
9 changes: 9 additions & 0 deletions llvm/docs/ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ Changes to the RISC-V Backend
Changes to the WebAssembly Backend
----------------------------------

The default target CPU, "generic", now enables the `-mnontrapping-fptoint`
and `-mbulk-memory` flags, which correspond to the [Bulk Memory Operations]
and [Non-trapping float-to-int Conversions] language features, which are
[widely implemented in engines].

[Bulk Memory Operations]: https://github.com/WebAssembly/bulk-memory-operations/blob/master/proposals/bulk-memory-operations/Overview.md
[Non-trapping float-to-int Conversions]: https://github.com/WebAssembly/spec/blob/master/proposals/nontrapping-float-to-int-conversion/Overview.md
[widely implemented in engines]: https://webassembly.org/features/

Changes to the Windows Target
-----------------------------

Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Target/WebAssembly/WebAssembly.td
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ def : ProcessorModel<"mvp", NoSchedModel, []>;
// consideration given to available support in relevant engines and tools, and
// the importance of the features.
def : ProcessorModel<"generic", NoSchedModel,
[FeatureMultivalue, FeatureMutableGlobals,
[FeatureBulkMemory, FeatureMultivalue,
FeatureMutableGlobals, FeatureNontrappingFPToInt,
FeatureReferenceTypes, FeatureSignExt]>;

// Latest and greatest experimental version of WebAssembly. Bugs included!
Expand Down
10 changes: 5 additions & 5 deletions llvm/test/CodeGen/WebAssembly/cfg-stackify-eh-legacy.ll
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
; REQUIRES: asserts
; 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
; 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
; 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
; 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
; 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
; 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
sunfishcode marked this conversation as resolved.
Show resolved Hide resolved
; 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
; 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
; 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
; 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

target triple = "wasm32-unknown-unknown"

Expand Down
8 changes: 7 additions & 1 deletion llvm/test/CodeGen/WebAssembly/target-features-cpus.ll
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@ target triple = "wasm32-unknown-unknown"

; generic: +multivalue, +mutable-globals, +reference-types, +sign-ext
; GENERIC-LABEL: .custom_section.target_features,"",@
; GENERIC-NEXT: .int8 4
; GENERIC-NEXT: .int8 6
; GENERIC-NEXT: .int8 43
; GENERIC-NEXT: .int8 11
; GENERIC-NEXT: .ascii "bulk-memory"
; GENERIC-NEXT: .int8 43
; GENERIC-NEXT: .int8 10
; GENERIC-NEXT: .ascii "multivalue"
; GENERIC-NEXT: .int8 43
; GENERIC-NEXT: .int8 15
; GENERIC-NEXT: .ascii "mutable-globals"
; GENERIC-NEXT: .int8 43
; GENERIC-NEXT: .int8 19
; GENERIC-NEXT: .ascii "nontrapping-fptoint"
; GENERIC-NEXT: .int8 43
; GENERIC-NEXT: .int8 15
; GENERIC-NEXT: .ascii "reference-types"
; GENERIC-NEXT: .int8 43
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/MC/WebAssembly/extern-functype-intrinsic.ll
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
; RUN: llc %s -o - | FileCheck %s
; RUN: llc %s -o - | llvm-mc -triple=wasm32-unknown-unknown | FileCheck %s
; RUN: llc %s -mattr=-bulk-memory -o - | FileCheck %s
; RUN: llc %s -mattr=-bulk-memory -o - | llvm-mc -triple=wasm32-unknown-unknown | FileCheck %s

; ModuleID = 'test.c'
source_filename = "test.c"
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/MC/WebAssembly/libcall.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: llc -filetype=obj %s -o - | obj2yaml | FileCheck %s
; RUN: llc -filetype=obj -mattr=-bulk-memory %s -o - | obj2yaml | FileCheck %s

target triple = "wasm32-unknown-unknown"

Expand Down
Loading