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

Conversation

sunfishcode
Copy link
Member

@sunfishcode sunfishcode commented Oct 11, 2024

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 Trail1 proposal if "trail1" is a supersetsubset of "generic". Edit: except for extended-const though.

@llvmbot llvmbot added clang Clang issues not falling into any other category backend:WebAssembly clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Oct 11, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 11, 2024

@llvm/pr-subscribers-lld-wasm
@llvm/pr-subscribers-lld
@llvm/pr-subscribers-mc

@llvm/pr-subscribers-backend-webassembly

Author: Dan Gohman (sunfishcode)

Changes

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 Trail1 proposal if "trail1" is a superset of "generic".


Full diff: https://github.com/llvm/llvm-project/pull/112049.diff

4 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+9)
  • (modified) clang/lib/Basic/Targets/WebAssembly.cpp (+2)
  • (modified) llvm/docs/ReleaseNotes.md (+9)
  • (modified) llvm/lib/Target/WebAssembly/WebAssembly.td (+2-1)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index e74dd1a5fb32da..aec48e32dc85a8 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -627,6 +627,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
 ^^^^^^^^^^^
 
diff --git a/clang/lib/Basic/Targets/WebAssembly.cpp b/clang/lib/Basic/Targets/WebAssembly.cpp
index 5ac9421663adea..1ac8f14f8cb175 100644
--- a/clang/lib/Basic/Targets/WebAssembly.cpp
+++ b/clang/lib/Basic/Targets/WebAssembly.cpp
@@ -151,8 +151,10 @@ bool WebAssemblyTargetInfo::initFeatureMap(
     llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
     const std::vector<std::string> &FeaturesVec) const {
   auto addGenericFeatures = [&]() {
+    Features["bulk-memory"] = true;
     Features["multivalue"] = true;
     Features["mutable-globals"] = true;
+    Features["nontrapping-fptoint"] = true;
     Features["reference-types"] = true;
     Features["sign-ext"] = true;
   };
diff --git a/llvm/docs/ReleaseNotes.md b/llvm/docs/ReleaseNotes.md
index dcdd7a25c7fbee..1d4197f25f4422 100644
--- a/llvm/docs/ReleaseNotes.md
+++ b/llvm/docs/ReleaseNotes.md
@@ -164,6 +164,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
 -----------------------------
 
diff --git a/llvm/lib/Target/WebAssembly/WebAssembly.td b/llvm/lib/Target/WebAssembly/WebAssembly.td
index c632d4a74355d8..1e22707db23e91 100644
--- a/llvm/lib/Target/WebAssembly/WebAssembly.td
+++ b/llvm/lib/Target/WebAssembly/WebAssembly.td
@@ -110,7 +110,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!

@llvmbot
Copy link
Member

llvmbot commented Oct 11, 2024

@llvm/pr-subscribers-clang

Author: Dan Gohman (sunfishcode)

Changes

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 Trail1 proposal if "trail1" is a superset of "generic".


Full diff: https://github.com/llvm/llvm-project/pull/112049.diff

4 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+9)
  • (modified) clang/lib/Basic/Targets/WebAssembly.cpp (+2)
  • (modified) llvm/docs/ReleaseNotes.md (+9)
  • (modified) llvm/lib/Target/WebAssembly/WebAssembly.td (+2-1)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index e74dd1a5fb32da..aec48e32dc85a8 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -627,6 +627,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
 ^^^^^^^^^^^
 
diff --git a/clang/lib/Basic/Targets/WebAssembly.cpp b/clang/lib/Basic/Targets/WebAssembly.cpp
index 5ac9421663adea..1ac8f14f8cb175 100644
--- a/clang/lib/Basic/Targets/WebAssembly.cpp
+++ b/clang/lib/Basic/Targets/WebAssembly.cpp
@@ -151,8 +151,10 @@ bool WebAssemblyTargetInfo::initFeatureMap(
     llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
     const std::vector<std::string> &FeaturesVec) const {
   auto addGenericFeatures = [&]() {
+    Features["bulk-memory"] = true;
     Features["multivalue"] = true;
     Features["mutable-globals"] = true;
+    Features["nontrapping-fptoint"] = true;
     Features["reference-types"] = true;
     Features["sign-ext"] = true;
   };
diff --git a/llvm/docs/ReleaseNotes.md b/llvm/docs/ReleaseNotes.md
index dcdd7a25c7fbee..1d4197f25f4422 100644
--- a/llvm/docs/ReleaseNotes.md
+++ b/llvm/docs/ReleaseNotes.md
@@ -164,6 +164,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
 -----------------------------
 
diff --git a/llvm/lib/Target/WebAssembly/WebAssembly.td b/llvm/lib/Target/WebAssembly/WebAssembly.td
index c632d4a74355d8..1e22707db23e91 100644
--- a/llvm/lib/Target/WebAssembly/WebAssembly.td
+++ b/llvm/lib/Target/WebAssembly/WebAssembly.td
@@ -110,7 +110,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!

clang/lib/Basic/Targets/WebAssembly.cpp Show resolved Hide resolved
clang/test/Preprocessor/wasm-target-features.c Outdated Show resolved Hide resolved
@@ -1,4 +1,4 @@
; RUN: llc -filetype=obj %s -o %t.o
; RUN: llc -filetype=obj -mattr=-bulk-memory %s -o %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.

Copy link
Member

@aheejin aheejin left a comment

Choose a reason for hiding this comment

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

This LGTM, but I guess we need to wait until Emscripten disables them by default, because there are some failing Emscripten tests and Binaryen doesn't yet have lowering passes for these. I don't think we need to block this on all these; we would just need to disable these two feature in Emscripten by default in the meantime.

@nikic
Copy link
Contributor

nikic commented Oct 16, 2024

I'd like #63755 to be fixed before this lands. Rust will likely crash and burn otherwise.

@sunfishcode
Copy link
Member Author

I've now added a patch which fixes #63755.

Copy link

github-actions bot commented Oct 16, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@aheejin
Copy link
Member

aheejin commented Oct 16, 2024

I've now added a patch which fixes #63755.

Can you split this patch into a separate PR?

@sunfishcode
Copy link
Member Author

Can you split this patch into a separate PR?

Sure, I've now created #112617 with just the that patch.

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
[Trail1 proposal] if "trail1" is a superset of "generic".

[back in February]: WebAssembly/tool-conventions#158 (comment)
[Trail1 proposal]: llvm#112035
dschuff added a commit to emscripten-core/emscripten that referenced this pull request Oct 25, 2024
When clang starts enabling features by default that Emscripten's minimum
browser versions want disabled, Emscripten will need to explicitly
disable them. This PR does that; once clang rolls, we can update the
default versions and tests in a single commit if needed.

See also llvm/llvm-project#112049
…bled.

Fix disabling "bulk-memory" via function attributes.

 - Fix FixFunctionBitcasts to add attributes to the generated wrapper
   functions so that they don't pick up default target features.

 - Fix `coalesceFeatures` to only consider defined functions.

 - Fix `coalesceFeatures` to combine the union starting from the empty
   set, rather than starting from the target machine set, so that if
   some feature is explicitly disabled in all functions, it's disabled
   in the result.

This fixes the wasm/lto/libcall-archive.ll test once bulk-memory is
enabled, as that test depends on being able to disable bulk-memory
via function attributes.
Copy link
Member

@dschuff dschuff left a comment

Choose a reason for hiding this comment

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

I think Emscripten is ready for this now, thanks for working on this!

@sunfishcode
Copy link
Member Author

Thanks!

@sunfishcode sunfishcode merged commit 1bc2cd9 into llvm:main Oct 25, 2024
9 checks passed
@sunfishcode sunfishcode deleted the sunfishcode/features branch October 25, 2024 20:52
winner245 pushed a commit to winner245/llvm-project that referenced this pull request Oct 26, 2024
…lvm#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]: llvm#112035
NoumanAmir657 pushed a commit to NoumanAmir657/llvm-project that referenced this pull request Nov 4, 2024
…lvm#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]: llvm#112035
aheejin added a commit to aheejin/llvm-project that referenced this pull request Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:WebAssembly clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category lld:wasm lld mc Machine (object) code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants