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
132 changes: 129 additions & 3 deletions src/passes/ConstraintAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
#include "cfg/wto.h"
#include "ir/constraint.h"
#include "ir/drop.h"
#include "ir/find_all.h"
#include "ir/literal-utils.h"
#include "ir/local-graph.h"
#include "ir/properties.h"
Expand Down Expand Up @@ -100,7 +101,7 @@
bool inQueue;
Index index;

// All relevant operations: local gets and sets and uses of them.
// All relevant operations that we optimize with.
std::vector<Expression**> actions;

// The branching instruction at the end of the block (or nullptr if there is
Expand Down Expand Up @@ -199,6 +200,117 @@
void visitRefEq(RefEq* curr) { addAction(); }
void visitRefIsNull(RefIsNull* curr) { addAction(); }

// Various instructions trap on a null input, and therefore prove that the
// input is not null, after them. We represent all such actions using a
// RefAsNonNull in our actions (i.e. a StructGet shows up as a RefAsNonNull,
// which reflects the only important behavior of the StructGet, for us). As
// actions store an Expression**, we allocate a "box" for each, with a pointer
// to the RefAs, so that we can refer to that referring pointer as an
// Expression**.
struct RefAsBox {
RefAs refAs;
Expression* referrer;

RefAsBox(Expression* value) {
refAs.op = RefAsNonNull;
refAs.value = value;
referrer = &refAs;
}
};
std::vector<std::unique_ptr<RefAsBox>> refAsBoxes;

// Receives the instruction and its ref that could cause a trap.
void trapOnNull(Expression* curr, Expression* ref) {
if (!ref) {
// No value (e.g. StructNew's desc when it doesn't have one).
return;
}

if (!currBasicBlock) {

Check warning on line 229 in src/passes/ConstraintAnalysis.cpp

View workflow job for this annotation

GitHub Actions / coverage

One-sided branch in diff

Branch `!currBasicBlock` -> missing TRUE branch (True: 0x, False: 200x)
// Nowhere to store anything.
return;
}

Check warning on line 232 in src/passes/ConstraintAnalysis.cpp

View workflow job for this annotation

GitHub Actions / coverage

Uncovered diff lines

Lines 231-232 not executed by tests (0x)

// The ref must be a local, so we know what is constrained.
// TODO: local.tee, fallthrough, etc.
auto* get = ref->dynCast<LocalGet>();
if (!get) {
return;
}

// If it is non-nullable then the type proves non-nullability anyhow, and we
// are not needed.
if (!ref->type.isNullableRef()) {
return;
}

// Check for changes in later children that alter the local:
//
// (struct.set
// (local.get $x)
// (local.tee $x ..)
// )
//
// In this example, at the time that the struct.set executes, $x has been
// set to a different value, so we cannot infer anything. Any set of the
// local that is read in `ref` is dangerous, if it appears after `ref`.
auto seenRef = false;
for (auto* child : ChildIterator(curr)) {
if (child == ref) {
seenRef = true;
} else if (seenRef) {
for (auto* set : FindAll<LocalSet>(child).list) {
if (set->index == get->index) {
return;
}
}
}
}

// Valid to optimize. Add a normal action for a RefAs, and otherwise
// generate a boxed RefAs to represent this not-RefAs-but-behaving-like-a-
// RefAs in our actions.
if (curr->is<RefAs>()) {
addAction();
} else {
refAsBoxes.emplace_back(std::make_unique<RefAsBox>(ref));
currBasicBlock->contents.actions.push_back(&refAsBoxes.back()->referrer);
}
}

void visitRefAs(RefAs* curr) {
if (curr->op == RefAsNonNull) {
trapOnNull(curr, curr->value);
}
}
// TODO: This list overlaps with OptimizeInstructions trapOnNull logic, and
// can maybe be deduplicated?
void visitStructNew(StructNew* curr) { trapOnNull(curr, curr->desc); }
void visitStructGet(StructGet* curr) { trapOnNull(curr, curr->ref); }
void visitStructSet(StructSet* curr) { trapOnNull(curr, curr->ref); }
void visitStructRMW(StructRMW* curr) { trapOnNull(curr, curr->ref); }
void visitStructCmpxchg(StructCmpxchg* curr) { trapOnNull(curr, curr->ref); }
void visitArrayGet(ArrayGet* curr) { trapOnNull(curr, curr->ref); }
void visitArraySet(ArraySet* curr) { trapOnNull(curr, curr->ref); }
void visitArrayLen(ArrayLen* curr) { trapOnNull(curr, curr->ref); }
void visitArrayCopy(ArrayCopy* curr) {
trapOnNull(curr, curr->destRef);
trapOnNull(curr, curr->srcRef);
}
void visitArrayRMW(ArrayRMW* curr) { trapOnNull(curr, curr->ref); }
void visitArrayCmpxchg(ArrayCmpxchg* curr) { trapOnNull(curr, curr->ref); }
void visitRefCast(RefCast* curr) {
// Casts to non-null, trap on null.
if (curr->type.isNonNullableRef()) {
trapOnNull(curr, curr->ref);
}
trapOnNull(curr, curr->desc);
}
void visitBrOn(BrOn* curr) { trapOnNull(curr, curr->desc); }
void visitRefGetDesc(RefGetDesc* curr) { trapOnNull(curr, curr->ref); }
void visitCallRef(CallRef* curr) { trapOnNull(curr, curr->target); }
void visitResume(Resume* curr) { trapOnNull(curr, curr->cont); }

static void doStartIfTrue(ConstraintAnalysis* self, Expression** currp) {
// We are right after the condition, so we are in the block before the If's
// branching. Mark the If as the brancher (unless in unreachable code).
Expand Down Expand Up @@ -328,7 +440,6 @@

// After inferring all we can, apply it to optimize the code.
void optimize() {

// If we find local.gets that we can optimize, we queue those changes here.
// This order is useful for the following reason:
//
Expand Down Expand Up @@ -631,13 +742,28 @@
//
// (local.set $x (ref.as_non_null (..)))
//
// TODO: infer non-nullability from e.g. the ref of a struct.get
auto localType = getFunction()->getLocalType(set->index);
if (localType.isNullableRef() && set->value->type.isNonNullableRef()) {
auto zero = Literal::makeZero(localType);
auto c = Constraint{Abstract::Ne, {zero}};
constraints.approximateAnd(set->index, c);
}

return;
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@aheejin You suggested an early return for the LocalSet case here, but I think after this refactor it would break the symmetry of the new code after it... so I left it as is. But open to suggestions here.

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.

Yeah early return doesn't make sense if the function handles more things than local.set


// Some constraints can be inferred without a local.set (which was handled
// above). ref.as_non_null of a local.get proves the local is not null, as
// if it were we would trap and not continue.
if (auto* refAs = curr->dynCast<RefAs>()) {
// We only put an optimizable RefAs in our actions.
assert(refAs->op == RefAsNonNull && refAs->value->type.isNullableRef());

Check warning on line 760 in src/passes/ConstraintAnalysis.cpp

View workflow job for this annotation

GitHub Actions / coverage

Uncovered MC/DC condition

Decision `assert(refAs->op == RefAsNonNull && refAs->value->type.isNullableRef());` -> missing MC/DC independence pair for C1, C2
auto* get = refAs->value->cast<LocalGet>();
if (relevantLocals[get->index]) {

Check warning on line 762 in src/passes/ConstraintAnalysis.cpp

View workflow job for this annotation

GitHub Actions / coverage

One-sided branch in diff

Branch `relevantLocals[get->index]` -> missing FALSE branch (True: 196x, False: 0x)
auto zero = Literal::makeZero(get->type);
auto constraint = Constraint{Abstract::Ne, {zero}};
constraints.approximateAnd(get->index, constraint);
}
}
}

Expand Down
150 changes: 149 additions & 1 deletion test/lit/passes/constraint-analysis-cont.wast
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
(type $func (func))
;; CHECK: (type $cont (cont $func))
(type $cont (cont $func))
;; CHECK: (type $func-i32 (func (param i32)))
(type $func-i32 (func (param i32)))
;; CHECK: (type $cont-i32 (cont $func-i32))
(type $cont-i32 (cont $func-i32))

;; CHECK: (tag $tag (type $func))
(tag $tag (type $func))

;; CHECK: (func $func (type $2) (param $cont (ref $cont))
;; CHECK: (func $func (type $5) (param $cont (ref $cont))
;; CHECK-NEXT: (local $x i32)
;; CHECK-NEXT: (block $out
;; CHECK-NEXT: (drop
Expand Down Expand Up @@ -72,5 +76,149 @@
)
)
)

;; CHECK: (func $resume-trap-on-null (type $2) (param $cont (ref null $cont)) (result i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block $h (result (ref $cont))
;; CHECK-NEXT: (resume $cont (on $tag $h)
;; CHECK-NEXT: (local.get $cont)
;; CHECK-NEXT: )
;; CHECK-NEXT: (return
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
(func $resume-trap-on-null (param $cont (ref null $cont)) (result i32)
;; resume traps on a null continuation before resuming, so both the
;; fallthrough and the suspend handler know $cont is not null.
(drop
(block $h (result (ref $cont))
(resume $cont (on $tag $h)
(local.get $cont)
)
(return
(ref.is_null
(local.get $cont)
)
)
)
)
(ref.is_null
(local.get $cont)
)
)

;; CHECK: (func $resume-trap-on-null-eh (type $2) (param $cont (ref null $cont)) (result i32)
;; CHECK-NEXT: (block $catch
;; CHECK-NEXT: (try_table (catch_all $catch)
;; CHECK-NEXT: (resume $cont
;; CHECK-NEXT: (local.get $cont)
;; CHECK-NEXT: )
;; CHECK-NEXT: (return
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
(func $resume-trap-on-null-eh (param $cont (ref null $cont)) (result i32)
;; Null continuation traps before resuming, so even a catch handler knows
;; $cont is not null.
(block $catch
(try_table (catch_all $catch)
(resume $cont
(local.get $cont)
)
(return
(ref.is_null
(local.get $cont)
)
)
)
)
(ref.is_null
(local.get $cont)
)
)

;; CHECK: (func $resume-modify-in-operand (type $6) (param $cont (ref null $cont-i32)) (param $other (ref null $cont-i32)) (result i32)
;; CHECK-NEXT: (resume $cont-i32
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (local.set $cont
;; CHECK-NEXT: (local.get $other)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.get $cont)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
(func $resume-modify-in-operand (param $cont (ref null $cont-i32)) (param $other (ref null $cont-i32)) (result i32)
;; Operands execute before the continuation target, so modifying $cont in an
;; operand does not prevent inferring $cont != null from the subsequent
;; local.get $cont.
(resume $cont-i32
(block (result i32)
(local.set $cont
(local.get $other)
)
(i32.const 1)
)
(local.get $cont)
)
(ref.is_null
(local.get $cont)
)
)

;; CHECK: (func $resume-known-null (type $2) (param $cont (ref null $cont)) (result i32)
;; CHECK-NEXT: (local.set $cont
;; CHECK-NEXT: (ref.null nocont)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block $h (result (ref $cont))
;; CHECK-NEXT: (block ;; (replaces unreachable Resume we can't emit)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (ref.null nocont)
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: (return
;; CHECK-NEXT: (block
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (block
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $resume-known-null (param $cont (ref null $cont)) (result i32)
;; Resuming a known-null continuation unconditionally traps, making both the
;; fallthrough and the handler unreachable.
(local.set $cont
(ref.null $cont)
)
(drop
(block $h (result (ref $cont))
(resume $cont (on $tag $h)
(local.get $cont)
)
(return
(ref.is_null
(local.get $cont)
)
)
)
)
(ref.is_null
(local.get $cont)
)
)
)

Loading
Loading