From 9c0e2764f8f7d89cc5747cd45016926bf9c91374 Mon Sep 17 00:00:00 2001 From: dcode Date: Sat, 12 Sep 2020 10:27:47 +0200 Subject: [PATCH 1/3] Fix RefNull issues --- src/ir/ExpressionAnalyzer.cpp | 2 +- src/passes/Precompute.cpp | 9 ++++++--- src/tools/execution-results.h | 5 +++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/ir/ExpressionAnalyzer.cpp b/src/ir/ExpressionAnalyzer.cpp index cdbcd9ae0b7..d2f9611f9f3 100644 --- a/src/ir/ExpressionAnalyzer.cpp +++ b/src/ir/ExpressionAnalyzer.cpp @@ -219,7 +219,7 @@ template void visitImmediates(Expression* curr, T& visitor) { visitor.visitInt(curr->op); visitor.visitNonScopeName(curr->nameOperand); } - void visitRefNull(RefNull* curr) {} + void visitRefNull(RefNull* curr) { visitor.visitType(curr->type); } void visitRefIsNull(RefIsNull* curr) {} void visitRefFunc(RefFunc* curr) { visitor.visitNonScopeName(curr->func); } void visitTry(Try* curr) {} diff --git a/src/passes/Precompute.cpp b/src/passes/Precompute.cpp index 0b67a25aa03..d8a1fa9fc1e 100644 --- a/src/passes/Precompute.cpp +++ b/src/passes/Precompute.cpp @@ -134,9 +134,12 @@ struct Precompute curr->finalize(); return; } - } else if (singleValue.isNull() && - curr->value->template is()) { - return; + } else if (singleValue.isNull()) { + if (auto* n = curr->value->template dynCast()) { + n->finalize(singleValue.type); + curr->finalize(); + return; + } } else if (singleValue.type == Type::funcref) { if (auto* r = curr->value->template dynCast()) { r->func = singleValue.getFunc(); diff --git a/src/tools/execution-results.h b/src/tools/execution-results.h index c41b0c208fc..0881163f1fc 100644 --- a/src/tools/execution-results.h +++ b/src/tools/execution-results.h @@ -73,7 +73,7 @@ struct ExecutionResults { // change (after duplicate function elimination or roundtripping) // while the function contents are still the same for (Literal& val : ret) { - if (val.type == Type::funcref) { + if (val.type == Type::funcref && !val.isNull()) { val = Literal::makeFunc(Name("funcref")); } } @@ -112,7 +112,8 @@ struct ExecutionResults { } std::cout << "[fuzz-exec] comparing " << name << '\n'; if (results[name] != other.results[name]) { - std::cout << "not identical!\n"; + std::cout << "not identical! " << results[name] + << " != " << other.results[name] << "\n"; return false; } } From b0c0dbc74bd44d5f8b65769b6f2c9800a9578476 Mon Sep 17 00:00:00 2001 From: dcode Date: Sat, 12 Sep 2020 10:41:21 +0200 Subject: [PATCH 2/3] update affected tests --- test/passes/optimize-instructions_all-features.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/passes/optimize-instructions_all-features.txt b/test/passes/optimize-instructions_all-features.txt index f223cb838c4..26b522fe4dc 100644 --- a/test/passes/optimize-instructions_all-features.txt +++ b/test/passes/optimize-instructions_all-features.txt @@ -3713,7 +3713,11 @@ ) ) (func $if-arms-subtype (result anyref) - (ref.null extern) + (if (result anyref) + (i32.const 0) + (ref.null extern) + (ref.null func) + ) ) (func $optimize-boolean-context (param $x i32) (param $y i32) (if From 9a0c8960d6407df4c3c227fabe616d0c56689312 Mon Sep 17 00:00:00 2001 From: dcode Date: Sun, 13 Sep 2020 02:48:13 +0200 Subject: [PATCH 3/3] test cases of 'if' folding conditions involving subtypes, update comment --- test/passes/optimize-instructions_all-features.txt | 7 +++++-- test/passes/optimize-instructions_all-features.wast | 13 +++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/test/passes/optimize-instructions_all-features.txt b/test/passes/optimize-instructions_all-features.txt index 26b522fe4dc..31c1afbfcfe 100644 --- a/test/passes/optimize-instructions_all-features.txt +++ b/test/passes/optimize-instructions_all-features.txt @@ -10,11 +10,11 @@ (type $i64_=>_i64 (func (param i64) (result i64))) (type $i32_i64_f32_=>_none (func (param i32 i64 f32))) (type $i32_i64_f32_f64_=>_none (func (param i32 i64 f32 f64))) + (type $none_=>_anyref (func (result anyref))) (type $i32_i32_i32_=>_none (func (param i32 i32 i32))) (type $i32_i32_f64_f64_=>_none (func (param i32 i32 f64 f64))) (type $i32_i64_f64_i32_=>_none (func (param i32 i64 f64 i32))) (type $none_=>_f64 (func (result f64))) - (type $none_=>_anyref (func (result anyref))) (memory $0 0) (export "load-off-2" (func $load-off-2)) (func $f (param $i1 i32) (param $i2 i64) @@ -3712,7 +3712,10 @@ (unreachable) ) ) - (func $if-arms-subtype (result anyref) + (func $if-arms-subtype-fold (result anyref) + (ref.null extern) + ) + (func $if-arms-subtype-nofold (result anyref) (if (result anyref) (i32.const 0) (ref.null extern) diff --git a/test/passes/optimize-instructions_all-features.wast b/test/passes/optimize-instructions_all-features.wast index 6e106ad1b08..c2fa16e35a6 100644 --- a/test/passes/optimize-instructions_all-features.wast +++ b/test/passes/optimize-instructions_all-features.wast @@ -4207,8 +4207,17 @@ (unreachable) ) ) - ;; Tests when if arms are subtype of if's type - (func $if-arms-subtype (result anyref) + ;; These functions test if an `if` with subtyped arms is correctly folded + ;; 1. if its `ifTrue` and `ifFalse` arms are identical (can fold) + (func $if-arms-subtype-fold (result anyref) + (if (result anyref) + (i32.const 0) + (ref.null extern) + (ref.null extern) + ) + ) + ;; 2. if its `ifTrue` and `ifFalse` arms are not identical (cannot fold) + (func $if-arms-subtype-nofold (result anyref) (if (result anyref) (i32.const 0) (ref.null extern)