summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/passes/CMakeLists.txt1
-rw-r--r--src/passes/LLVMNontrappingFPToIntLowering.cpp180
-rw-r--r--src/passes/pass.cpp4
-rw-r--r--src/passes/passes.h1
-rw-r--r--test/lit/exec/nontrapping-fptoint-lowering.wat261
-rw-r--r--test/lit/help/wasm-metadce.test5
-rw-r--r--test/lit/help/wasm-opt.test5
-rw-r--r--test/lit/help/wasm2js.test5
-rw-r--r--test/lit/passes/nontrapping-fptoint-lowering.wast208
9 files changed, 670 insertions, 0 deletions
diff --git a/src/passes/CMakeLists.txt b/src/passes/CMakeLists.txt
index c6e079079..e46163406 100644
--- a/src/passes/CMakeLists.txt
+++ b/src/passes/CMakeLists.txt
@@ -76,6 +76,7 @@ set(passes_SOURCES
NameList.cpp
NameTypes.cpp
NoInline.cpp
+ LLVMNontrappingFPToIntLowering.cpp
OnceReduction.cpp
OptimizeAddedConstants.cpp
OptimizeCasts.cpp
diff --git a/src/passes/LLVMNontrappingFPToIntLowering.cpp b/src/passes/LLVMNontrappingFPToIntLowering.cpp
new file mode 100644
index 000000000..d14e58af8
--- /dev/null
+++ b/src/passes/LLVMNontrappingFPToIntLowering.cpp
@@ -0,0 +1,180 @@
+#include "pass.h"
+#include "wasm-builder.h"
+#include "wasm.h"
+#include <limits>
+#include <memory>
+
+// By default LLVM emits nontrapping float-to-int instructions to implement its
+// fptoui/fptosi conversion instructions. This pass replaces these instructions
+// with code sequences which also implement LLVM's fptoui/fptosi, but which are
+// not semantically equivalent in wasm. This is because out-of-range inputs to
+// these instructions produce poison values. So we need only ensure that there
+// is no trap, but need not ensure any particular result. The transformation
+// in this pass is the same as the one used by LLVM to lower fptoui/fptosi
+// to wasm trapping instructions.
+
+// For example, if a conversion is guarded by a range check in the source, LLVM
+// can move the conversion before the check (and instead guard the use of the
+// result, which may be poison). This is valid in LLVM and for the nontrapping
+// wasm fptoint instructions but not for the trapping conversions. The
+// transformation in this pass is valid only if the nontrapping conversions
+// in the wasm were generated from LLVM and implement LLVM's conversion
+// semantics.
+
+namespace wasm {
+struct LLVMNonTrappingFPToIntLoweringImpl
+ : public WalkerPass<PostWalker<LLVMNonTrappingFPToIntLoweringImpl>> {
+ bool isFunctionParallel() override { return true; }
+
+ std::unique_ptr<Pass> create() override {
+ return std::make_unique<LLVMNonTrappingFPToIntLoweringImpl>();
+ }
+
+ UnaryOp getReplacementOp(UnaryOp op) {
+ switch (op) {
+ case TruncSatSFloat32ToInt32:
+ return TruncSFloat32ToInt32;
+ case TruncSatUFloat32ToInt32:
+ return TruncUFloat32ToInt32;
+ case TruncSatSFloat64ToInt32:
+ return TruncSFloat64ToInt32;
+ case TruncSatUFloat64ToInt32:
+ return TruncUFloat64ToInt32;
+ case TruncSatSFloat32ToInt64:
+ return TruncSFloat32ToInt64;
+ case TruncSatUFloat32ToInt64:
+ return TruncUFloat32ToInt64;
+ case TruncSatSFloat64ToInt64:
+ return TruncSFloat64ToInt64;
+ case TruncSatUFloat64ToInt64:
+ return TruncUFloat64ToInt64;
+ default:
+ WASM_UNREACHABLE("Unexpected opcode");
+ }
+ }
+
+ template<typename From, typename To> void replaceSigned(Unary* curr) {
+ BinaryOp ltOp;
+ UnaryOp absOp;
+ switch (curr->op) {
+ case TruncSatSFloat32ToInt32:
+ case TruncSatSFloat32ToInt64:
+ ltOp = LtFloat32;
+ absOp = AbsFloat32;
+ break;
+ case TruncSatSFloat64ToInt32:
+ case TruncSatSFloat64ToInt64:
+ ltOp = LtFloat64;
+ absOp = AbsFloat64;
+ break;
+ default:
+ WASM_UNREACHABLE("Unexpected opcode");
+ }
+
+ Builder builder(*getModule());
+ Index v = Builder::addVar(getFunction(), curr->value->type);
+ // if fabs(operand) < INT_MAX then use the trapping operation, else return
+ // INT_MIN. The altnernate value is correct for the case where the input is
+ // INT_MIN itself; otherwise it's UB so any value will do.
+ replaceCurrent(builder.makeIf(
+ builder.makeBinary(
+ ltOp,
+ builder.makeUnary(
+ absOp, builder.makeLocalTee(v, curr->value, curr->value->type)),
+ builder.makeConst(static_cast<From>(std::numeric_limits<To>::max()))),
+ builder.makeUnary(getReplacementOp(curr->op),
+ builder.makeLocalGet(v, curr->value->type)),
+ builder.makeConst(std::numeric_limits<To>::min())));
+ }
+
+ template<typename From, typename To> void replaceUnsigned(Unary* curr) {
+ BinaryOp ltOp, geOp;
+
+ switch (curr->op) {
+ case TruncSatUFloat32ToInt32:
+ case TruncSatUFloat32ToInt64:
+ ltOp = LtFloat32;
+ geOp = GeFloat32;
+ break;
+ case TruncSatUFloat64ToInt32:
+ case TruncSatUFloat64ToInt64:
+ ltOp = LtFloat64;
+ geOp = GeFloat64;
+ break;
+ default:
+ WASM_UNREACHABLE("Unexpected opcode");
+ }
+
+ Builder builder(*getModule());
+ Index v = Builder::addVar(getFunction(), curr->value->type);
+ // if op < INT_MAX and op >= 0 then use the trapping operation, else return
+ // 0
+ replaceCurrent(builder.makeIf(
+ builder.makeBinary(
+ AndInt32,
+ builder.makeBinary(
+ ltOp,
+ builder.makeLocalTee(v, curr->value, curr->value->type),
+ builder.makeConst(static_cast<From>(std::numeric_limits<To>::max()))),
+ builder.makeBinary(geOp,
+ builder.makeLocalGet(v, curr->value->type),
+ builder.makeConst(static_cast<From>(0.0)))),
+ builder.makeUnary(getReplacementOp(curr->op),
+ builder.makeLocalGet(v, curr->value->type)),
+ builder.makeConst(static_cast<To>(0))));
+ }
+
+ void visitUnary(Unary* curr) {
+ switch (curr->op) {
+ case TruncSatSFloat32ToInt32:
+ replaceSigned<float, int32_t>(curr);
+ break;
+ case TruncSatSFloat64ToInt32:
+ replaceSigned<double, int32_t>(curr);
+ break;
+ case TruncSatSFloat32ToInt64:
+ replaceSigned<float, int64_t>(curr);
+ break;
+ case TruncSatSFloat64ToInt64:
+ replaceSigned<double, int64_t>(curr);
+ break;
+ case TruncSatUFloat32ToInt32:
+ replaceUnsigned<float, uint32_t>(curr);
+ break;
+ case TruncSatUFloat64ToInt32:
+ replaceUnsigned<double, uint32_t>(curr);
+ break;
+ case TruncSatUFloat32ToInt64:
+ replaceUnsigned<float, uint64_t>(curr);
+ break;
+ case TruncSatUFloat64ToInt64:
+ replaceUnsigned<double, uint64_t>(curr);
+ break;
+ default:
+ break;
+ }
+ }
+
+ void doWalkFunction(Function* func) { Super::doWalkFunction(func); }
+};
+
+struct LLVMNonTrappingFPToIntLowering : public Pass {
+ void run(Module* module) override {
+ if (!module->features.hasTruncSat()) {
+ return;
+ }
+ PassRunner runner(module);
+ // Run the Impl pass as an inner pass in parallel. This pass updates the
+ // module features, so it can't be parallel.
+ runner.add(std::make_unique<LLVMNonTrappingFPToIntLoweringImpl>());
+ runner.setIsNested(true);
+ runner.run();
+ module->features.disable(FeatureSet::TruncSat);
+ }
+};
+
+Pass* createLLVMNonTrappingFPToIntLoweringPass() {
+ return new LLVMNonTrappingFPToIntLowering();
+}
+
+} // namespace wasm
diff --git a/src/passes/pass.cpp b/src/passes/pass.cpp
index 5cbf4a31f..4be24cebf 100644
--- a/src/passes/pass.cpp
+++ b/src/passes/pass.cpp
@@ -334,6 +334,10 @@ void PassRegistry::registerPasses() {
registerPass("no-partial-inline",
"mark functions as no-inline (for partial inlining only)",
createNoPartialInlinePass);
+ registerPass("llvm-nontrapping-fptoint-lowering",
+ "lower nontrapping float-to-int operations to wasm mvp and "
+ "disable the nontrapping fptoint feature",
+ createLLVMNonTrappingFPToIntLoweringPass);
registerPass("once-reduction",
"reduces calls to code that only runs once",
createOnceReductionPass);
diff --git a/src/passes/passes.h b/src/passes/passes.h
index 212a2b0e4..aadd26d41 100644
--- a/src/passes/passes.h
+++ b/src/passes/passes.h
@@ -119,6 +119,7 @@ Pass* createOutliningPass();
Pass* createPickLoadSignsPass();
Pass* createModAsyncifyAlwaysOnlyUnwindPass();
Pass* createModAsyncifyNeverUnwindPass();
+Pass* createLLVMNonTrappingFPToIntLoweringPass();
Pass* createPoppifyPass();
Pass* createPostEmscriptenPass();
Pass* createPrecomputePass();
diff --git a/test/lit/exec/nontrapping-fptoint-lowering.wat b/test/lit/exec/nontrapping-fptoint-lowering.wat
new file mode 100644
index 000000000..72a86bf9c
--- /dev/null
+++ b/test/lit/exec/nontrapping-fptoint-lowering.wat
@@ -0,0 +1,261 @@
+;; NOTE: Assertions have been generated by update_lit_checks.py --output=fuzz-exec and should not be edited.
+
+;; RUN: wasm-opt --enable-nontrapping-float-to-int %s --llvm-nontrapping-fptoint-lowering --fuzz-exec -q | filecheck %s
+
+(module
+
+ (func $assert_i32 (param i32 i32)
+ (if (i32.ne (local.get 1) (local.get 0))
+ (then (unreachable)))
+ )
+ (func $assert_i64 (param i64 i64)
+ (if (i64.ne (local.get 1) (local.get 0))
+ (then (unreachable)))
+ )
+
+ (func $i32.trunc_sat_f32_s (param $x f32) (result i32) (i32.trunc_sat_f32_s (local.get $x)))
+ (func $i32.trunc_sat_f32_u (param $x f32) (result i32) (i32.trunc_sat_f32_u (local.get $x)))
+ (func $i32.trunc_sat_f64_s (param $x f64) (result i32) (i32.trunc_sat_f64_s (local.get $x)))
+ (func $i32.trunc_sat_f64_u (param $x f64) (result i32) (i32.trunc_sat_f64_u (local.get $x)))
+ (func $i64.trunc_sat_f32_s (param $x f32) (result i64) (i64.trunc_sat_f32_s (local.get $x)))
+ (func $i64.trunc_sat_f32_u (param $x f32) (result i64) (i64.trunc_sat_f32_u (local.get $x)))
+ (func $i64.trunc_sat_f64_s (param $x f64) (result i64) (i64.trunc_sat_f64_s (local.get $x)))
+ (func $i64.trunc_sat_f64_u (param $x f64) (result i64) (i64.trunc_sat_f64_u (local.get $x)))
+
+ ;; CHECK: [fuzz-exec] calling f32_i32
+ (func $f32_i32 (export "f32_i32")
+ (call $assert_i32 (call $i32.trunc_sat_f32_s (f32.const 0.0)) (i32.const 0))
+ (call $assert_i32 (call $i32.trunc_sat_f32_s (f32.const -0.0)) (i32.const 0))
+ (call $assert_i32 (call $i32.trunc_sat_f32_s (f32.const 0x1p-149)) (i32.const 0))
+ (call $assert_i32 (call $i32.trunc_sat_f32_s (f32.const -0x1p-149)) (i32.const 0))
+ (call $assert_i32 (call $i32.trunc_sat_f32_s (f32.const 1.0)) (i32.const 1))
+ (call $assert_i32 (call $i32.trunc_sat_f32_s (f32.const 0x1.19999ap+0)) (i32.const 1))
+ (call $assert_i32 (call $i32.trunc_sat_f32_s (f32.const 1.5)) (i32.const 1))
+ (call $assert_i32 (call $i32.trunc_sat_f32_s (f32.const -1.0)) (i32.const -1))
+ (call $assert_i32 (call $i32.trunc_sat_f32_s (f32.const -0x1.19999ap+0)) (i32.const -1))
+ (call $assert_i32 (call $i32.trunc_sat_f32_s (f32.const -1.5)) (i32.const -1))
+ (call $assert_i32 (call $i32.trunc_sat_f32_s (f32.const -1.9)) (i32.const -1))
+ (call $assert_i32 (call $i32.trunc_sat_f32_s (f32.const -2.0)) (i32.const -2))
+ (call $assert_i32 (call $i32.trunc_sat_f32_s (f32.const 2147483520.0)) (i32.const 2147483520))
+ (call $assert_i32 (call $i32.trunc_sat_f32_s (f32.const -2147483648.0)) (i32.const -2147483648))
+ ;; For out-of-range inputs, we ensure there is no trap, but do not check the value
+ (drop (call $i32.trunc_sat_f32_s (f32.const 2147483648.0)))
+ (call $assert_i32 (call $i32.trunc_sat_f32_s (f32.const -2147483904.0)) (i32.const 0x80000000))
+ (drop (call $i32.trunc_sat_f32_s (f32.const inf)))
+ (drop (call $i32.trunc_sat_f32_s (f32.const -inf)))
+ (drop (call $i32.trunc_sat_f32_s (f32.const nan)))
+ (drop (call $i32.trunc_sat_f32_s (f32.const nan:0x200000)))
+ (drop (call $i32.trunc_sat_f32_s (f32.const -nan)))
+ (drop (call $i32.trunc_sat_f32_s (f32.const -nan:0x200000)))
+ )
+
+ ;; CHECK: [fuzz-exec] calling f32_i32_u
+ (func $f32_i32_u (export "f32_i32_u")
+ (call $assert_i32 (call $i32.trunc_sat_f32_u (f32.const 0.0)) (i32.const 0))
+ (call $assert_i32 (call $i32.trunc_sat_f32_u (f32.const -0.0)) (i32.const 0))
+ (call $assert_i32 (call $i32.trunc_sat_f32_u (f32.const 0x1p-149)) (i32.const 0))
+ (call $assert_i32 (call $i32.trunc_sat_f32_u (f32.const -0x1p-149)) (i32.const 0))
+ (call $assert_i32 (call $i32.trunc_sat_f32_u (f32.const 1.0)) (i32.const 1))
+ (call $assert_i32 (call $i32.trunc_sat_f32_u (f32.const 0x1.19999ap+0)) (i32.const 1))
+ (call $assert_i32 (call $i32.trunc_sat_f32_u (f32.const 1.5)) (i32.const 1))
+ (call $assert_i32 (call $i32.trunc_sat_f32_u (f32.const 1.9)) (i32.const 1))
+ (call $assert_i32 (call $i32.trunc_sat_f32_u (f32.const 2.0)) (i32.const 2))
+ (call $assert_i32 (call $i32.trunc_sat_f32_u (f32.const 2147483648)) (i32.const -2147483648)) ;; 0x1.00000p+31 -> 8000 0000
+ (call $assert_i32 (call $i32.trunc_sat_f32_u (f32.const 4294967040.0)) (i32.const -256))
+ (call $assert_i32 (call $i32.trunc_sat_f32_u (f32.const -0x1.ccccccp-1)) (i32.const 0))
+ (call $assert_i32 (call $i32.trunc_sat_f32_u (f32.const -0x1.fffffep-1)) (i32.const 0))
+ (drop (call $i32.trunc_sat_f32_u (f32.const 4294967296.0)))
+ (drop (call $i32.trunc_sat_f32_u (f32.const -1.0)))
+ (drop (call $i32.trunc_sat_f32_u (f32.const inf)))
+ (drop (call $i32.trunc_sat_f32_u (f32.const -inf)))
+ (drop (call $i32.trunc_sat_f32_u (f32.const nan)))
+ (drop (call $i32.trunc_sat_f32_u (f32.const nan:0x200000)))
+ (drop (call $i32.trunc_sat_f32_u (f32.const -nan)))
+ (drop (call $i32.trunc_sat_f32_u (f32.const -nan:0x200000)))
+ )
+
+ ;; CHECK: [fuzz-exec] calling f64_i32
+ (func $f64_i32 (export "f64_i32")
+ (call $assert_i32 (call $i32.trunc_sat_f64_s (f64.const 0.0)) (i32.const 0))
+ (call $assert_i32 (call $i32.trunc_sat_f64_s (f64.const -0.0)) (i32.const 0))
+ (call $assert_i32 (call $i32.trunc_sat_f64_s (f64.const 0x0.0000000000001p-1022)) (i32.const 0))
+ (call $assert_i32 (call $i32.trunc_sat_f64_s (f64.const -0x0.0000000000001p-1022)) (i32.const 0))
+ (call $assert_i32 (call $i32.trunc_sat_f64_s (f64.const 1.0)) (i32.const 1))
+ (call $assert_i32 (call $i32.trunc_sat_f64_s (f64.const 0x1.199999999999ap+0)) (i32.const 1))
+ (call $assert_i32 (call $i32.trunc_sat_f64_s (f64.const 1.5)) (i32.const 1))
+ (call $assert_i32 (call $i32.trunc_sat_f64_s (f64.const -1.0)) (i32.const -1))
+ (call $assert_i32 (call $i32.trunc_sat_f64_s (f64.const -0x1.199999999999ap+0)) (i32.const -1))
+ (call $assert_i32 (call $i32.trunc_sat_f64_s (f64.const -1.5)) (i32.const -1))
+ (call $assert_i32 (call $i32.trunc_sat_f64_s (f64.const -1.9)) (i32.const -1))
+ (call $assert_i32 (call $i32.trunc_sat_f64_s (f64.const -2.0)) (i32.const -2))
+ (call $assert_i32 (call $i32.trunc_sat_f64_s (f64.const -2147483648.0)) (i32.const -2147483648))
+ (drop (call $i32.trunc_sat_f64_s (f64.const 2147483647.0)))
+ (drop (call $i32.trunc_sat_f64_s (f64.const 2147483648.0)))
+ (drop (call $i32.trunc_sat_f64_s (f64.const -2147483649.0)))
+ (drop (call $i32.trunc_sat_f64_s (f64.const inf)))
+ (drop (call $i32.trunc_sat_f64_s (f64.const -inf)))
+ (drop (call $i32.trunc_sat_f64_s (f64.const nan)))
+ (drop (call $i32.trunc_sat_f64_s (f64.const nan:0x4000000000000)))
+ (drop (call $i32.trunc_sat_f64_s (f64.const -nan)))
+ (drop (call $i32.trunc_sat_f64_s (f64.const -nan:0x4000000000000)))
+ )
+
+ ;; CHECK: [fuzz-exec] calling f64_i32_s
+ (func $f64_i32_s (export "f64_i32_s")
+ (call $assert_i32 (call $i32.trunc_sat_f64_u (f64.const 0.0)) (i32.const 0))
+ (call $assert_i32 (call $i32.trunc_sat_f64_u (f64.const -0.0)) (i32.const 0))
+ (call $assert_i32 (call $i32.trunc_sat_f64_u (f64.const 0x0.0000000000001p-1022)) (i32.const 0))
+ (call $assert_i32 (call $i32.trunc_sat_f64_u (f64.const -0x0.0000000000001p-1022)) (i32.const 0))
+ (call $assert_i32 (call $i32.trunc_sat_f64_u (f64.const 1.0)) (i32.const 1))
+ (call $assert_i32 (call $i32.trunc_sat_f64_u (f64.const 0x1.199999999999ap+0)) (i32.const 1))
+ (call $assert_i32 (call $i32.trunc_sat_f64_u (f64.const 1.5)) (i32.const 1))
+ (call $assert_i32 (call $i32.trunc_sat_f64_u (f64.const 1.9)) (i32.const 1))
+ (call $assert_i32 (call $i32.trunc_sat_f64_u (f64.const 2.0)) (i32.const 2))
+ (call $assert_i32 (call $i32.trunc_sat_f64_u (f64.const 2147483648)) (i32.const -2147483648)) ;; 0x1.00000p+31 -> 8000 0000
+
+ (drop (call $i32.trunc_sat_f64_u (f64.const 4294967295.0)))
+ (call $assert_i32 (call $i32.trunc_sat_f64_u (f64.const -0x1.ccccccccccccdp-1)) (i32.const 0))
+ (call $assert_i32 (call $i32.trunc_sat_f64_u (f64.const -0x1.fffffffffffffp-1)) (i32.const 0))
+ (call $assert_i32 (call $i32.trunc_sat_f64_u (f64.const 1e8)) (i32.const 100000000))
+ (drop (call $i32.trunc_sat_f64_u (f64.const 4294967296.0)))
+ (call $assert_i32 (call $i32.trunc_sat_f64_u (f64.const -1.0)) (i32.const 0x00000000))
+ (drop (call $i32.trunc_sat_f64_u (f64.const 1e16)))
+ (drop (call $i32.trunc_sat_f64_u (f64.const 1e30)))
+ (drop (call $i32.trunc_sat_f64_u (f64.const 9223372036854775808)))
+ (drop (call $i32.trunc_sat_f64_u (f64.const inf)))
+ (drop (call $i32.trunc_sat_f64_u (f64.const -inf)))
+ (drop (call $i32.trunc_sat_f64_u (f64.const nan)))
+ (drop (call $i32.trunc_sat_f64_u (f64.const nan:0x4000000000000)))
+ (drop (call $i32.trunc_sat_f64_u (f64.const -nan)))
+ (drop (call $i32.trunc_sat_f64_u (f64.const -nan:0x4000000000000)))
+ )
+
+ ;; CHECK: [fuzz-exec] calling f32_i64
+ (func $f32_i64 (export "f32_i64")
+ (call $assert_i64 (call $i64.trunc_sat_f32_s (f32.const 0.0)) (i64.const 0))
+ (call $assert_i64 (call $i64.trunc_sat_f32_s (f32.const -0.0)) (i64.const 0))
+ (call $assert_i64 (call $i64.trunc_sat_f32_s (f32.const 0x1p-149)) (i64.const 0))
+ (call $assert_i64 (call $i64.trunc_sat_f32_s (f32.const -0x1p-149)) (i64.const 0))
+ (call $assert_i64 (call $i64.trunc_sat_f32_s (f32.const 1.0)) (i64.const 1))
+ (call $assert_i64 (call $i64.trunc_sat_f32_s (f32.const 0x1.19999ap+0)) (i64.const 1))
+ (call $assert_i64 (call $i64.trunc_sat_f32_s (f32.const 1.5)) (i64.const 1))
+ (call $assert_i64 (call $i64.trunc_sat_f32_s (f32.const -1.0)) (i64.const -1))
+ (call $assert_i64 (call $i64.trunc_sat_f32_s (f32.const -0x1.19999ap+0)) (i64.const -1))
+ (call $assert_i64 (call $i64.trunc_sat_f32_s (f32.const -1.5)) (i64.const -1))
+ (call $assert_i64 (call $i64.trunc_sat_f32_s (f32.const -1.9)) (i64.const -1))
+ (call $assert_i64 (call $i64.trunc_sat_f32_s (f32.const -2.0)) (i64.const -2))
+ (call $assert_i64 (call $i64.trunc_sat_f32_s (f32.const 4294967296)) (i64.const 4294967296)) ;; 0x1.00000p+32 -> 1 0000 0000
+ (call $assert_i64 (call $i64.trunc_sat_f32_s (f32.const -4294967296)) (i64.const -4294967296)) ;; -0x1.00000p+32 -> ffff ffff 0000 0000
+ (call $assert_i64 (call $i64.trunc_sat_f32_s (f32.const 9223371487098961920.0)) (i64.const 9223371487098961920))
+ (call $assert_i64 (call $i64.trunc_sat_f32_s (f32.const -9223372036854775808.0)) (i64.const -9223372036854775808))
+ (drop (call $i64.trunc_sat_f32_s (f32.const 9223372036854775808.0)))
+ (drop (call $i64.trunc_sat_f32_s (f32.const -9223373136366403584.0)))
+ (drop (call $i64.trunc_sat_f32_s (f32.const inf)))
+ (drop (call $i64.trunc_sat_f32_s (f32.const -inf)))
+ (drop (call $i64.trunc_sat_f32_s (f32.const nan)))
+ (drop (call $i64.trunc_sat_f32_s (f32.const nan:0x200000)))
+ (drop (call $i64.trunc_sat_f32_s (f32.const -nan)))
+ (drop (call $i64.trunc_sat_f32_s (f32.const -nan:0x200000)))
+ )
+
+ ;; CHECK: [fuzz-exec] calling f32_i64_u
+ (func $f32_i64_u (export "f32_i64_u")
+ (call $assert_i64 (call $i64.trunc_sat_f32_u (f32.const 0.0)) (i64.const 0))
+ (call $assert_i64 (call $i64.trunc_sat_f32_u (f32.const -0.0)) (i64.const 0))
+ (call $assert_i64 (call $i64.trunc_sat_f32_u (f32.const 0x1p-149)) (i64.const 0))
+ (call $assert_i64 (call $i64.trunc_sat_f32_u (f32.const -0x1p-149)) (i64.const 0))
+ (call $assert_i64 (call $i64.trunc_sat_f32_u (f32.const 1.0)) (i64.const 1))
+ (call $assert_i64 (call $i64.trunc_sat_f32_u (f32.const 0x1.19999ap+0)) (i64.const 1))
+ (call $assert_i64 (call $i64.trunc_sat_f32_u (f32.const 1.5)) (i64.const 1))
+ (call $assert_i64 (call $i64.trunc_sat_f32_u (f32.const 4294967296)) (i64.const 4294967296))
+ (call $assert_i64 (call $i64.trunc_sat_f32_u (f32.const 18446742974197923840.0)) (i64.const -1099511627776))
+ (call $assert_i64 (call $i64.trunc_sat_f32_u (f32.const -0x1.ccccccp-1)) (i64.const 0))
+ (call $assert_i64 (call $i64.trunc_sat_f32_u (f32.const -0x1.fffffep-1)) (i64.const 0))
+ (drop (call $i64.trunc_sat_f32_u (f32.const 18446744073709551616.0)))
+ (call $assert_i64 (call $i64.trunc_sat_f32_u (f32.const -1.0)) (i64.const 0x0000000000000000))
+ (drop (call $i64.trunc_sat_f32_u (f32.const inf)))
+ (drop (call $i64.trunc_sat_f32_u (f32.const -inf)))
+ (drop (call $i64.trunc_sat_f32_u (f32.const nan)))
+ (drop (call $i64.trunc_sat_f32_u (f32.const nan:0x200000)))
+ (drop (call $i64.trunc_sat_f32_u (f32.const -nan)))
+ (drop (call $i64.trunc_sat_f32_u (f32.const -nan:0x200000)))
+ )
+
+ ;; CHECK: [fuzz-exec] calling f64_i64
+ (func $f64_i64 (export "f64_i64")
+ (call $assert_i64 (call $i64.trunc_sat_f64_s (f64.const 0.0)) (i64.const 0))
+ (call $assert_i64 (call $i64.trunc_sat_f64_s (f64.const -0.0)) (i64.const 0))
+ (call $assert_i64 (call $i64.trunc_sat_f64_s (f64.const 0x0.0000000000001p-1022)) (i64.const 0))
+ (call $assert_i64 (call $i64.trunc_sat_f64_s (f64.const -0x0.0000000000001p-1022)) (i64.const 0))
+ (call $assert_i64 (call $i64.trunc_sat_f64_s (f64.const 1.0)) (i64.const 1))
+ (call $assert_i64 (call $i64.trunc_sat_f64_s (f64.const 0x1.199999999999ap+0)) (i64.const 1))
+ (call $assert_i64 (call $i64.trunc_sat_f64_s (f64.const 1.5)) (i64.const 1))
+ (call $assert_i64 (call $i64.trunc_sat_f64_s (f64.const -1.0)) (i64.const -1))
+ (call $assert_i64 (call $i64.trunc_sat_f64_s (f64.const -0x1.199999999999ap+0)) (i64.const -1))
+ (call $assert_i64 (call $i64.trunc_sat_f64_s (f64.const -1.5)) (i64.const -1))
+ (call $assert_i64 (call $i64.trunc_sat_f64_s (f64.const -1.9)) (i64.const -1))
+ (call $assert_i64 (call $i64.trunc_sat_f64_s (f64.const -2.0)) (i64.const -2))
+ (call $assert_i64 (call $i64.trunc_sat_f64_s (f64.const 4294967296)) (i64.const 4294967296)) ;; 0x1.00000p+32 -> 1 0000 0000
+ (call $assert_i64 (call $i64.trunc_sat_f64_s (f64.const -4294967296)) (i64.const -4294967296)) ;; -0x1.00000p+32 -> ffff ffff 0000 0000
+ (call $assert_i64 (call $i64.trunc_sat_f64_s (f64.const 9223372036854774784.0)) (i64.const 9223372036854774784))
+ (call $assert_i64 (call $i64.trunc_sat_f64_s (f64.const -9223372036854775808.0)) (i64.const -9223372036854775808))
+ (drop (call $i64.trunc_sat_f64_s (f64.const 9223372036854775808.0)))
+ (call $assert_i64 (call $i64.trunc_sat_f64_s (f64.const -9223372036854777856.0)) (i64.const 0x8000000000000000))
+ (drop (call $i64.trunc_sat_f64_s (f64.const inf)))
+ (drop (call $i64.trunc_sat_f64_s (f64.const -inf)))
+ (drop (call $i64.trunc_sat_f64_s (f64.const nan)))
+ (drop (call $i64.trunc_sat_f64_s (f64.const nan:0x4000000000000)))
+ (drop (call $i64.trunc_sat_f64_s (f64.const -nan)))
+ (drop (call $i64.trunc_sat_f64_s (f64.const -nan:0x4000000000000)))
+ )
+
+ ;; CHECK: [fuzz-exec] calling f64_i64_u
+ (func $f64_i64_u (export "f64_i64_u")
+ (call $assert_i64 (call $i64.trunc_sat_f64_u (f64.const 0.0)) (i64.const 0))
+ (call $assert_i64 (call $i64.trunc_sat_f64_u (f64.const -0.0)) (i64.const 0))
+ (call $assert_i64 (call $i64.trunc_sat_f64_u (f64.const 0x0.0000000000001p-1022)) (i64.const 0))
+ (call $assert_i64 (call $i64.trunc_sat_f64_u (f64.const -0x0.0000000000001p-1022)) (i64.const 0))
+ (call $assert_i64 (call $i64.trunc_sat_f64_u (f64.const 1.0)) (i64.const 1))
+ (call $assert_i64 (call $i64.trunc_sat_f64_u (f64.const 0x1.199999999999ap+0)) (i64.const 1))
+ (call $assert_i64 (call $i64.trunc_sat_f64_u (f64.const 1.5)) (i64.const 1))
+ (call $assert_i64 (call $i64.trunc_sat_f64_u (f64.const 4294967295)) (i64.const 0xffffffff))
+ (call $assert_i64 (call $i64.trunc_sat_f64_u (f64.const 4294967296)) (i64.const 0x100000000))
+ (call $assert_i64 (call $i64.trunc_sat_f64_u (f64.const 18446744073709549568.0)) (i64.const -2048))
+ (call $assert_i64 (call $i64.trunc_sat_f64_u (f64.const -0x1.ccccccccccccdp-1)) (i64.const 0))
+ (call $assert_i64 (call $i64.trunc_sat_f64_u (f64.const -0x1.fffffffffffffp-1)) (i64.const 0))
+ (call $assert_i64 (call $i64.trunc_sat_f64_u (f64.const 1e8)) (i64.const 100000000))
+ (call $assert_i64 (call $i64.trunc_sat_f64_u (f64.const 1e16)) (i64.const 10000000000000000))
+ (call $assert_i64 (call $i64.trunc_sat_f64_u (f64.const 9223372036854775808)) (i64.const -9223372036854775808))
+ (drop (call $i64.trunc_sat_f64_u (f64.const 18446744073709551616.0)))
+ (call $assert_i64 (call $i64.trunc_sat_f64_u (f64.const -1.0)) (i64.const 0x0000000000000000))
+ (drop (call $i64.trunc_sat_f64_u (f64.const inf)))
+ (drop (call $i64.trunc_sat_f64_u (f64.const -inf)))
+ (drop (call $i64.trunc_sat_f64_u (f64.const nan)))
+ (drop (call $i64.trunc_sat_f64_u (f64.const nan:0x4000000000000)))
+ (drop (call $i64.trunc_sat_f64_u (f64.const -nan)))
+ (drop (call $i64.trunc_sat_f64_u (f64.const -nan:0x4000000000000)))
+ )
+)
+;; CHECK: [fuzz-exec] calling f32_i32
+
+;; CHECK: [fuzz-exec] calling f32_i32_u
+
+;; CHECK: [fuzz-exec] calling f64_i32
+
+;; CHECK: [fuzz-exec] calling f64_i32_s
+
+;; CHECK: [fuzz-exec] calling f32_i64
+
+;; CHECK: [fuzz-exec] calling f32_i64_u
+
+;; CHECK: [fuzz-exec] calling f64_i64
+
+;; CHECK: [fuzz-exec] calling f64_i64_u
+;; CHECK-NEXT: [fuzz-exec] comparing f32_i32
+;; CHECK-NEXT: [fuzz-exec] comparing f32_i32_u
+;; CHECK-NEXT: [fuzz-exec] comparing f32_i64
+;; CHECK-NEXT: [fuzz-exec] comparing f32_i64_u
+;; CHECK-NEXT: [fuzz-exec] comparing f64_i32
+;; CHECK-NEXT: [fuzz-exec] comparing f64_i32_s
+;; CHECK-NEXT: [fuzz-exec] comparing f64_i64
+;; CHECK-NEXT: [fuzz-exec] comparing f64_i64_u
diff --git a/test/lit/help/wasm-metadce.test b/test/lit/help/wasm-metadce.test
index 50f71f8f5..4dc03883a 100644
--- a/test/lit/help/wasm-metadce.test
+++ b/test/lit/help/wasm-metadce.test
@@ -234,6 +234,11 @@
;; CHECK-NEXT: memory.fill to wasm mvp and
;; CHECK-NEXT: disable the bulk-memory feature.
;; CHECK-NEXT:
+;; CHECK-NEXT: --llvm-nontrapping-fptoint-lowering lower nontrapping float-to-int
+;; CHECK-NEXT: operations to wasm mvp and
+;; CHECK-NEXT: disable the nontrapping fptoint
+;; CHECK-NEXT: feature
+;; CHECK-NEXT:
;; CHECK-NEXT: --local-cse common subexpression elimination
;; CHECK-NEXT: inside basic blocks
;; CHECK-NEXT:
diff --git a/test/lit/help/wasm-opt.test b/test/lit/help/wasm-opt.test
index 1ac823fa7..62a30a770 100644
--- a/test/lit/help/wasm-opt.test
+++ b/test/lit/help/wasm-opt.test
@@ -243,6 +243,11 @@
;; CHECK-NEXT: memory.fill to wasm mvp and
;; CHECK-NEXT: disable the bulk-memory feature.
;; CHECK-NEXT:
+;; CHECK-NEXT: --llvm-nontrapping-fptoint-lowering lower nontrapping float-to-int
+;; CHECK-NEXT: operations to wasm mvp and
+;; CHECK-NEXT: disable the nontrapping fptoint
+;; CHECK-NEXT: feature
+;; CHECK-NEXT:
;; CHECK-NEXT: --local-cse common subexpression elimination
;; CHECK-NEXT: inside basic blocks
;; CHECK-NEXT:
diff --git a/test/lit/help/wasm2js.test b/test/lit/help/wasm2js.test
index 69923a064..e2a450bc8 100644
--- a/test/lit/help/wasm2js.test
+++ b/test/lit/help/wasm2js.test
@@ -197,6 +197,11 @@
;; CHECK-NEXT: memory.fill to wasm mvp and
;; CHECK-NEXT: disable the bulk-memory feature.
;; CHECK-NEXT:
+;; CHECK-NEXT: --llvm-nontrapping-fptoint-lowering lower nontrapping float-to-int
+;; CHECK-NEXT: operations to wasm mvp and
+;; CHECK-NEXT: disable the nontrapping fptoint
+;; CHECK-NEXT: feature
+;; CHECK-NEXT:
;; CHECK-NEXT: --local-cse common subexpression elimination
;; CHECK-NEXT: inside basic blocks
;; CHECK-NEXT:
diff --git a/test/lit/passes/nontrapping-fptoint-lowering.wast b/test/lit/passes/nontrapping-fptoint-lowering.wast
new file mode 100644
index 000000000..e4598dcf9
--- /dev/null
+++ b/test/lit/passes/nontrapping-fptoint-lowering.wast
@@ -0,0 +1,208 @@
+;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
+
+;; RUN: wasm-opt --enable-nontrapping-float-to-int %s --llvm-nontrapping-fptoint-lowering -S -o - | filecheck %s
+
+(module
+ ;; CHECK: (type $0 (func))
+ (type $0 (func))
+ ;; CHECK: (func $truncsat
+ ;; CHECK-NEXT: (local $0 f32)
+ ;; CHECK-NEXT: (local $1 f64)
+ ;; CHECK-NEXT: (local $2 f32)
+ ;; CHECK-NEXT: (local $3 f32)
+ ;; CHECK-NEXT: (local $4 f64)
+ ;; CHECK-NEXT: (local $5 f64)
+ ;; CHECK-NEXT: (local $6 f32)
+ ;; CHECK-NEXT: (local $7 f32)
+ ;; CHECK-NEXT: (local $8 f64)
+ ;; CHECK-NEXT: (local $9 f64)
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (if (result i32)
+ ;; CHECK-NEXT: (f32.lt
+ ;; CHECK-NEXT: (f32.abs
+ ;; CHECK-NEXT: (local.tee $2
+ ;; CHECK-NEXT: (local.get $0)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (f32.const 2147483648)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (then
+ ;; CHECK-NEXT: (i32.trunc_f32_s
+ ;; CHECK-NEXT: (local.get $2)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (else
+ ;; CHECK-NEXT: (i32.const -2147483648)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (if (result i64)
+ ;; CHECK-NEXT: (f32.lt
+ ;; CHECK-NEXT: (f32.abs
+ ;; CHECK-NEXT: (local.tee $3
+ ;; CHECK-NEXT: (local.get $0)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (f32.const 9223372036854775808)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (then
+ ;; CHECK-NEXT: (i64.trunc_f32_s
+ ;; CHECK-NEXT: (local.get $3)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (else
+ ;; CHECK-NEXT: (i64.const -9223372036854775808)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (if (result i32)
+ ;; CHECK-NEXT: (f64.lt
+ ;; CHECK-NEXT: (f64.abs
+ ;; CHECK-NEXT: (local.tee $4
+ ;; CHECK-NEXT: (local.get $1)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (f64.const 2147483647)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (then
+ ;; CHECK-NEXT: (i32.trunc_f64_s
+ ;; CHECK-NEXT: (local.get $4)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (else
+ ;; CHECK-NEXT: (i32.const -2147483648)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (if (result i64)
+ ;; CHECK-NEXT: (f64.lt
+ ;; CHECK-NEXT: (f64.abs
+ ;; CHECK-NEXT: (local.tee $5
+ ;; CHECK-NEXT: (local.get $1)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (f64.const 9223372036854775808)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (then
+ ;; CHECK-NEXT: (i64.trunc_f64_s
+ ;; CHECK-NEXT: (local.get $5)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (else
+ ;; CHECK-NEXT: (i64.const -9223372036854775808)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (if (result i32)
+ ;; CHECK-NEXT: (i32.and
+ ;; CHECK-NEXT: (f32.lt
+ ;; CHECK-NEXT: (local.tee $6
+ ;; CHECK-NEXT: (local.get $0)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (f32.const 4294967296)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (f32.ge
+ ;; CHECK-NEXT: (local.get $6)
+ ;; CHECK-NEXT: (f32.const 0)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (then
+ ;; CHECK-NEXT: (i32.trunc_f32_u
+ ;; CHECK-NEXT: (local.get $6)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (else
+ ;; CHECK-NEXT: (i32.const 0)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (if (result i64)
+ ;; CHECK-NEXT: (i32.and
+ ;; CHECK-NEXT: (f32.lt
+ ;; CHECK-NEXT: (local.tee $7
+ ;; CHECK-NEXT: (local.get $0)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (f32.const 18446744073709551615)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (f32.ge
+ ;; CHECK-NEXT: (local.get $7)
+ ;; CHECK-NEXT: (f32.const 0)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (then
+ ;; CHECK-NEXT: (i64.trunc_f32_u
+ ;; CHECK-NEXT: (local.get $7)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (else
+ ;; CHECK-NEXT: (i64.const 0)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (if (result i32)
+ ;; CHECK-NEXT: (i32.and
+ ;; CHECK-NEXT: (f64.lt
+ ;; CHECK-NEXT: (local.tee $8
+ ;; CHECK-NEXT: (local.get $1)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (f64.const 4294967295)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (f64.ge
+ ;; CHECK-NEXT: (local.get $8)
+ ;; CHECK-NEXT: (f64.const 0)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (then
+ ;; CHECK-NEXT: (i32.trunc_f64_u
+ ;; CHECK-NEXT: (local.get $8)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (else
+ ;; CHECK-NEXT: (i32.const 0)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (if (result i64)
+ ;; CHECK-NEXT: (i32.and
+ ;; CHECK-NEXT: (f64.lt
+ ;; CHECK-NEXT: (local.tee $9
+ ;; CHECK-NEXT: (local.get $1)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (f64.const 18446744073709551615)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (f64.ge
+ ;; CHECK-NEXT: (local.get $9)
+ ;; CHECK-NEXT: (f64.const 0)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (then
+ ;; CHECK-NEXT: (i64.trunc_f64_u
+ ;; CHECK-NEXT: (local.get $9)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (else
+ ;; CHECK-NEXT: (i64.const 0)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ (func $truncsat (type $0)
+ (local $0 f32)
+ (local $1 f64)
+ (drop (i32.trunc_sat_f32_s (local.get $0)))
+ (drop (i64.trunc_sat_f32_s (local.get $0)))
+ (drop (i32.trunc_sat_f64_s (local.get $1)))
+ (drop (i64.trunc_sat_f64_s (local.get $1)))
+ (drop (i32.trunc_sat_f32_u (local.get $0)))
+ (drop (i64.trunc_sat_f32_u (local.get $0)))
+ (drop (i32.trunc_sat_f64_u (local.get $1)))
+ (drop (i64.trunc_sat_f64_u (local.get $1)))
+ )
+)