This is an archive of the discontinued LLVM Phabricator instance.

[WebAssembly] Add prototype relaxed float to int trunc instructions
ClosedPublic

Authored by ngzhian on Oct 20 2021, 2:54 PM.

Details

Summary

Add i32x4.relaxed_trunc_f32x4_s, i32x4.relaxed_trunc_f32x4_u,
i32x4.relaxed_trunc_f64x2_s_zero, i32x4.relaxed_trunc_f64x2_u_zero.

These are only exposed as builtins, and require user opt-in.

Diff Detail

Event Timeline

ngzhian created this revision.Oct 20 2021, 2:54 PM
ngzhian requested review of this revision.Oct 20 2021, 2:54 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptOct 20 2021, 2:54 PM
ngzhian added inline comments.
clang/lib/CodeGen/CGBuiltin.cpp
18304–18309

@tlively I'm having trouble with this, getting this stack trace

WidenVectorResult #0: t4: v2i32 = llvm.wasm.relaxed.trunc.zero.signed TargetConstant:i32<9112>, t2

Do not know how to widen the result of this operator!
UNREACHABLE executed at /usr/local/google/home/zhin/src/llvm-project/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp:3035!
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /usr/local/google/home/zhin/ssd2/llvm-project/build-wasm/bin/llc -asm-verbose=false -verify-machineinstrs -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -mattr=+simd128,+relaxed-simd -debug
1.      Running pass 'Function Pass Manager' on module '<stdin>'.
2.      Running pass 'WebAssembly Instruction Selection' on function '@relaxed_trunc_zero_s_v4i32'
 #0 0x00007f3012db05bb llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /usr/local/google/home/zhin/src/llvm-project/llvm/lib/Support/Unix/Signals.inc:565:22
 #1 0x00007f3012db0672 PrintStackTraceSignalHandler(void*) /usr/local/google/home/zhin/src/llvm-project/llvm/lib/Support/Unix/Signals.inc:632:1
 #2 0x00007f3012dae668 llvm::sys::RunSignalHandlers() /usr/local/google/home/zhin/src/llvm-project/llvm/lib/Support/Signals.cpp:97:20
 #3 0x00007f3012db000e SignalHandler(int) /usr/local/google/home/zhin/src/llvm-project/llvm/lib/Support/Unix/Signals.inc:407:1
 #4 0x00007f301276fef0 (/lib/x86_64-linux-gnu/libc.so.6+0x3cef0)
 #5 0x00007f301276fe71 raise ./signal/../sysdeps/unix/sysv/linux/raise.c:50:1
 #6 0x00007f3012759536 abort ./stdlib/abort.c:81:7
 #7 0x00007f3012c5a974 bindingsErrorHandler(void*, char const*, bool) /usr/local/google/home/zhin/src/llvm-project/llvm/lib/Support/ErrorHandling.cpp:218:55
 #8 0x00007f3016e7856c llvm::DAGTypeLegalizer::WidenVectorResult(llvm::SDNode*, unsigned int) /usr/local/google/home/zhin/src/llvm-project/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp:3037:71
 #9 0x00007f3016e4a338 llvm::DAGTypeLegalizer::run() /usr/local/google/home/zhin/src/llvm-project/llvm/lib/CodeGen/SelectionDAG/LegalizeTypes.cpp:280:17
#10 0x00007f3016e4e34f llvm::SelectionDAG::LegalizeTypes() /usr/local/google/home/zhin/src/llvm-project/llvm/lib/CodeGen/SelectionDAG/LegalizeTypes.cpp:1055:37

Do I need to add some stuff to LegalizeTypes?

tlively added inline comments.Oct 26 2021, 10:23 AM
clang/lib/CodeGen/CGBuiltin.cpp
18304–18309

Sorry for the delay in review! The issue is that the code below generates the intrinsic calls with return type <2 x i32>, which is not a legal type, as well as a shuffle to get back to <4 x i32>. For the existing builtins, we use target-independent LLVM intrinsics, so LLVM already knows how to expand them, but it doesn't know how to expand these new target-specific intrinsics. Rather than piggybacking on the existing logic here, it would be good to generate intrinsics that return the expected <4 x i32> type (and no shuffle) so there is nothing to expand.

ngzhian updated this revision to Diff 382451.Oct 26 2021, 1:41 PM

No shuffles

ngzhian added inline comments.
clang/lib/CodeGen/CGBuiltin.cpp
18304–18309

Got it, thanks! Updated in the latest diff, ptal.

tlively added inline comments.Oct 26 2021, 6:06 PM
clang/lib/CodeGen/CGBuiltin.cpp
18417–18418

This can be ConvertType(E->getType()) to calculate the LLVM type from the clang type for the call. However, it would be even better to not make these intrinsics polymorphic.

llvm/include/llvm/IR/IntrinsicsWebAssembly.td
218–219

Since these intrinsics only need to work with a single set of types each, it would be simpler to specify those types rather than make the intrinsics polymorphic. See the definition of int_wasm_q15mulr_sat_signed for an example. That would allow you to drop the type arguments from CGM.getIntrinsic() in CGBuiltin.cpp.

ngzhian updated this revision to Diff 382717.Oct 27 2021, 10:39 AM

Make intrinsics monomorphic

ngzhian marked 2 inline comments as done.Oct 27 2021, 10:40 AM

Good suggestion, done.

tlively accepted this revision.Oct 28 2021, 1:44 PM

LGTM, thanks!

This revision is now accepted and ready to land.Oct 28 2021, 1:44 PM