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 multivalue and reference-types in generic CPU config #80923

Merged
merged 10 commits into from
Apr 29, 2024
5 changes: 5 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,11 @@ AIX Support
WebAssembly Support
^^^^^^^^^^^^^^^^^^^

The -mcpu=generic configuration now enables multivalue and reference-types.These
proposals are standardized and available in all major engines. Enabling
multivalue here only enables the language feature but does not turn on the
multivalue ABI.
aheejin marked this conversation as resolved.
Show resolved Hide resolved

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

Expand Down
20 changes: 13 additions & 7 deletions clang/lib/Basic/Targets/WebAssembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,25 @@ void WebAssemblyTargetInfo::setFeatureEnabled(llvm::StringMap<bool> &Features,
bool WebAssemblyTargetInfo::initFeatureMap(
llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
const std::vector<std::string> &FeaturesVec) const {
if (CPU == "bleeding-edge") {
Features["nontrapping-fptoint"] = true;
auto addGenericFeatures = [&]() {
Features["sign-ext"] = true;
Features["mutable-globals"] = true;
Features["reference-types"] = true;
Features["multivalue"] = true;
};
auto addBleedingEdgeFeatures = [&]() {
addGenericFeatures();
Features["nontrapping-fptoint"] = true;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like this was enabled in Safari in September 2021. Should we enable this by default as well?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh, I see, if we think we would need a Binaryen lowering pass first, then proceeding without this SGTM.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm a bit sad not to see this added, since this is the one that can actually improve codegen. If we know we want to enable to this, is there any downside to doing two different rounds (i.e. are there advantages to waiting on this until we have the lowering pass in place?)

Copy link
Member

@dschuff dschuff Apr 23, 2024

Choose a reason for hiding this comment

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

MV and reftypes are required to use exnref, that's the thing that's on the critical path here. So yes, we definitely want to land these two without blocking on nontrapping-fptoint.

Relatedly (to nontrapping-fptoint, but unrelatedly to this PR), I prototyped a Binaryen pass to lower away LLVM's use of nontrapping-fptoint* which is what would allow us to improve codegen. I think it works, but I discovered that some of emscripten's tests fail when we enable nontrapping fptoint, and I haven't yet looked at why that's the case (either a bug in emscripten's tests, or a bug in V8's implementation of nontrapping fptoint). We'd need to also fix that to unblock enabling this.

** (This isn't exactly the same as a full lowering of nontrapping-fptoint as spec'ed, because it takes advantage of knowledge of the semantics of LLVM's fptosi intrinsics)

Features["bulk-memory"] = true;
Features["atomics"] = true;
Features["mutable-globals"] = true;
Features["tail-call"] = true;
Features["reference-types"] = true;
Features["multimemory"] = true;
setSIMDLevel(Features, SIMD128, true);
} else if (CPU == "generic") {
Features["sign-ext"] = true;
Features["mutable-globals"] = true;
};
if (CPU == "generic") {
addGenericFeatures();
} else if (CPU == "bleeding-edge") {
addBleedingEdgeFeatures();
}

return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
Expand Down
7 changes: 4 additions & 3 deletions clang/test/Preprocessor/wasm-target-features.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,15 @@
//
// GENERIC-DAG:#define __wasm_sign_ext__ 1{{$}}
// GENERIC-DAG:#define __wasm_mutable_globals__ 1{{$}}
// GENERIC-DAG:#define __wasm_multivalue__ 1{{$}}
// GENERIC-DAG:#define __wasm_reference_types__ 1{{$}}
// GENERIC-NOT:#define __wasm_nontrapping_fptoint__ 1{{$}}
// GENERIC-NOT:#define __wasm_bulk_memory__ 1{{$}}
// GENERIC-NOT:#define __wasm_simd128__ 1{{$}}
// GENERIC-NOT:#define __wasm_atomics__ 1{{$}}
// GENERIC-NOT:#define __wasm_tail_call__ 1{{$}}
// GENERIC-NOT:#define __wasm_multimemory__ 1{{$}}
// GENERIC-NOT:#define __wasm_exception_handling__ 1{{$}}
// GENERIC-NOT:#define __wasm_multivalue__ 1{{$}}
// GENERIC-NOT:#define __wasm_reference_types__ 1{{$}}
// GENERIC-NOT:#define __wasm_extended_const__ 1{{$}}

// RUN: %clang -E -dM %s -o - 2>&1 \
Expand All @@ -181,9 +181,10 @@
// BLEEDING-EDGE-DAG:#define __wasm_mutable_globals__ 1{{$}}
// BLEEDING-EDGE-DAG:#define __wasm_tail_call__ 1{{$}}
// BLEEDING-EDGE-DAG:#define __wasm_multimemory__ 1{{$}}
// BLEEDING-EDGE-DAG:#define __wasm_multivalue__ 1{{$}}
// BLEEDING-EDGE-DAG:#define __wasm_reference_types__ 1{{$}}
// BLEEDING-EDGE-NOT:#define __wasm_exception_handling__ 1{{$}}
// BLEEDING-EDGE-NOT:#define __wasm_multivalue__ 1{{$}}
// BLEEDING-EDGE-NOT:#define __wasm_reference_types__ 1{{$}}
// BLEEDING-EDGE-NOT:#define __wasm_extended_const__ 1{{$}}
// BLEEDING-EDGE-NOT:#define __wasm_relaxed_simd__ 1{{$}}

Expand Down
Loading