Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/ir/type-updating.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,9 @@ Type GlobalTypeRewriter::getTempTupleType(Tuple tuple) {

namespace TypeUpdating {

void handleNonDefaultableLocals(Function* func, Module& wasm) {
void handleNonDefaultableLocals(Function* func,
Module& wasm,
Index firstLocal) {
if (!wasm.features.hasReferenceTypes()) {
// No references, so no non-nullable ones at all.
return;
Expand All @@ -524,6 +526,9 @@ void handleNonDefaultableLocals(Function* func, Module& wasm) {
func, wasm, LocalStructuralDominance::NonNullableOnly);
std::unordered_set<Index> badIndexes;
for (auto index : info.nonDominatingIndices) {
if (index < firstLocal) {
continue;
}
badIndexes.insert(index);

// LocalStructuralDominance should have only looked at non-nullable indexes
Expand Down
5 changes: 4 additions & 1 deletion src/ir/type-updating.h
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,10 @@ namespace TypeUpdating {
// them. Atm this turns them into nullable ones, and adds ref.as_non_null on
// their uses (which keeps the type of the users identical).
// This may also handle other types of nondefaultable locals in the future.
void handleNonDefaultableLocals(Function* func, Module& wasm);
// Ignore locals before firstLocal, if specified.
void handleNonDefaultableLocals(Function* func,
Module& wasm,
Index firstLocal = 0);

// Returns the type that a local should be, after handling of non-
// defaultability.
Expand Down
5 changes: 5 additions & 0 deletions src/parser/parse-5-defs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include "wat-parser-internal.h"

#include "ir/type-updating.h"

namespace wasm::WATParser {

Result<> parseDefinitions(
Expand Down Expand Up @@ -46,6 +48,7 @@ Result<> parseDefinitions(
WithPosition with(ctx, decls.funcDefs[i].pos);
ctx.setSrcLoc(decls.funcDefs[i].annotations);
ctx.in.setAnnotations(std::move(decls.funcDefs[i].annotations));
auto firstScratchLocal = f->getNumLocals();
if (!f->imported()) {
CHECK_ERR(ctx.visitFunctionStart(f));
}
Expand All @@ -63,6 +66,8 @@ Result<> parseDefinitions(
if (auto* err = end.getErr()) {
return ctx.in.err(decls.funcDefs[i].pos, err->msg);
}
TypeUpdating::handleNonDefaultableLocals(
f, decls.wasm, firstScratchLocal);
Comment on lines +69 to +70

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's not worry about the firstScratchLocal optimization and just call handleNonDefaultableLocals as-is. It's ok if it fixes locals that were used invalidly in the input; there are a few similar cases where we end up parsing and accepting invalid modules.

}
}

Expand Down
2 changes: 1 addition & 1 deletion src/wasm/wasm-ir-builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2302,7 +2302,7 @@ Result<> IRBuilder::makeBrOn(Index label,
case BrOnCast:
case BrOnCastDescEq:
if (out->isNullable()) {
resultType = Type(in->getHeapType(), NonNullable);
resultType = in->with(NonNullable);
} else {
resultType = *in;
}
Expand Down
100 changes: 100 additions & 0 deletions test/lit/basic/br-on-cast-extra-values.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
;; RUN: wasm-opt %s -all --roundtrip -S -o - | filecheck %s

(module
;; CHECK: (type $s (struct))
(type $s (struct))
;; CHECK: (func $extra-values (type $1) (param $s (ref $s)) (result (ref $s) (ref $s))
;; CHECK-NEXT: (local $scratch (ref $s))
;; CHECK-NEXT: (local $scratch_3 (ref $s))
;; CHECK-NEXT: (local $scratch_4 (ref $s))
;; CHECK-NEXT: (local $scratch_5 (ref $s))
;; CHECK-NEXT: (local $scratch_2 (ref null $s))
;; CHECK-NEXT: (block $block1 (type $3) (result (ref $s) (ref $s))
;; CHECK-NEXT: (local.set $scratch_5
;; CHECK-NEXT: (block $block (result (ref $s))
;; CHECK-NEXT: (local.set $scratch_3
;; CHECK-NEXT: (local.get $s)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $scratch
;; CHECK-NEXT: (local.get $s)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $scratch_2
;; CHECK-NEXT: (local.get $scratch_3)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $scratch_4
;; CHECK-NEXT: (br_on_cast $block (ref $s) (ref $s)
;; CHECK-NEXT: (local.get $scratch)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (br $block1
;; CHECK-NEXT: (tuple.make 2
;; CHECK-NEXT: (ref.as_non_null
;; CHECK-NEXT: (local.get $scratch_2)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.get $scratch_4)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (tuple.make 2
;; CHECK-NEXT: (ref.as_non_null
;; CHECK-NEXT: (local.get $scratch_2)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.get $scratch_5)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $extra-values (param $s (ref $s)) (result (ref $s) (ref $s))
local.get $s
local.get $s
br_on_cast 0 (ref $s) (ref $s)
)
;; CHECK: (func $exact (type $2) (param $s (ref (exact $s))) (result (ref (exact $s)) (ref null (exact $s)))
;; CHECK-NEXT: (local $scratch (ref (exact $s)))
;; CHECK-NEXT: (local $scratch_3 (ref (exact $s)))
;; CHECK-NEXT: (local $scratch_4 (ref (exact $s)))
;; CHECK-NEXT: (local $scratch_2 (ref null (exact $s)))
;; CHECK-NEXT: (local $scratch_5 (ref null (exact $s)))
;; CHECK-NEXT: (block $block1 (type $4) (result (ref (exact $s)) (ref null (exact $s)))
;; CHECK-NEXT: (local.set $scratch_5
;; CHECK-NEXT: (block $block (result (ref null (exact $s)))
;; CHECK-NEXT: (local.set $scratch_3
;; CHECK-NEXT: (local.get $s)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $scratch
;; CHECK-NEXT: (local.get $s)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $scratch_2
;; CHECK-NEXT: (local.get $scratch_3)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $scratch_4
;; CHECK-NEXT: (br_on_cast $block (ref (exact $s)) (ref (exact $s))
;; CHECK-NEXT: (local.get $scratch)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (br $block1
;; CHECK-NEXT: (tuple.make 2
;; CHECK-NEXT: (ref.as_non_null
;; CHECK-NEXT: (local.get $scratch_2)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.get $scratch_4)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (tuple.make 2
;; CHECK-NEXT: (ref.as_non_null
;; CHECK-NEXT: (local.get $scratch_2)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.get $scratch_5)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $exact (param $s (ref (exact $s)))
(result (ref (exact $s)) (ref null (exact $s)))
local.get $s
local.get $s
br_on_cast 0 (ref (exact $s)) (ref null (exact $s))
)
)