Skip to content

Commit

Permalink
[WIP] Eliminate type immediate members.
Browse files Browse the repository at this point in the history
This commit is a partial solution to removing the type immediate
members from `cont.new,cont.bind,resume,resume_throw,switch`. However,
when I fully remove the type immediates then I observe a crash in
`child-ir.h` on
`visitContBind,visitResume,visitResumeThrow,visitStackSwitch`. It seems that `curr->cont->type` is sometimes not a continuation type...
  • Loading branch information
dhil committed Nov 28, 2024
1 parent b1d65ce commit c549cd7
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 68 deletions.
6 changes: 3 additions & 3 deletions src/ir/module-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,13 +465,13 @@ struct CodeScanner
} else if (auto* contNew = curr->dynCast<ContNew>()) {
info.note(contNew->type);
} else if (auto* resume = curr->dynCast<Resume>()) {
info.note(resume->contType);
info.note(resume->cont->type);
info.note(resume->type);
} else if (auto* resumeThrow = curr->dynCast<ResumeThrow>()) {
info.note(resumeThrow->contType);
info.note(resumeThrow->cont->type);
info.note(resumeThrow->type);
} else if (auto* switch_ = curr->dynCast<StackSwitch>()) {
info.note(switch_->contType);
info.note(switch_->cont->type);
info.note(switch_->type);
} else if (Properties::isControlFlowStructure(curr)) {
info.noteControlFlow(Signature(Type::none, curr->type));
Expand Down
25 changes: 13 additions & 12 deletions src/passes/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,29 +386,25 @@ struct PrintSExpression : public UnifiedExpressionVisitor<PrintSExpression> {
}
}
void visitContBind(ContBind* curr) {
if (!maybePrintUnreachableOrNullReplacement(
curr, Type(curr->sourceType, Nullable)) &&
if (!maybePrintUnreachableOrNullReplacement(curr, curr->cont->type) &&
!maybePrintUnreachableOrNullReplacement(curr, curr->type)) {
visitExpression(curr);
}
}
void visitResume(Resume* curr) {
if (!maybePrintUnreachableOrNullReplacement(
curr, Type(curr->contType, Nullable)) &&
if (!maybePrintUnreachableOrNullReplacement(curr, curr->cont->type) &&
!maybePrintUnreachableOrNullReplacement(curr, curr->type)) {
visitExpression(curr);
}
}
void visitResumeThrow(ResumeThrow* curr) {
if (!maybePrintUnreachableOrNullReplacement(
curr, Type(curr->contType, Nullable)) &&
if (!maybePrintUnreachableOrNullReplacement(curr, curr->cont->type) &&
!maybePrintUnreachableOrNullReplacement(curr, curr->type)) {
visitExpression(curr);
}
}
void visitStackSwitch(StackSwitch* curr) {
if (!maybePrintUnreachableOrNullReplacement(
curr, Type(curr->contType, Nullable)) &&
if (!maybePrintUnreachableOrNullReplacement(curr, curr->cont->type) &&
!maybePrintUnreachableOrNullReplacement(curr, curr->type)) {
visitExpression(curr);
}
Expand Down Expand Up @@ -2461,12 +2457,14 @@ struct PrintExpressionContents
printMedium(o, "stringview_wtf16.slice");
}
void visitContNew(ContNew* curr) {
assert(curr->type.isContinuation());
printMedium(o, "cont.new ");
printHeapType(curr->type.getHeapType());
}
void visitContBind(ContBind* curr) {
assert(curr->cont->type.isContinuation() && curr->type.isContinuation());
printMedium(o, "cont.bind ");
printHeapType(curr->sourceType);
printHeapType(curr->cont->type.getHeapType());
o << ' ';
printHeapType(curr->type.getHeapType());
}
Expand All @@ -2492,28 +2490,31 @@ struct PrintExpressionContents
}
}
void visitResume(Resume* curr) {
assert(curr->cont->type.isContinuation());
printMedium(o, "resume");

o << ' ';
printHeapType(curr->contType);
printHeapType(curr->cont->type.getHeapType());

handleResumeTable(o, curr);
}
void visitResumeThrow(ResumeThrow* curr) {
assert(curr->cont->type.isContinuation());
printMedium(o, "resume_throw");

o << ' ';
printHeapType(curr->contType);
printHeapType(curr->cont->type.getHeapType());
o << ' ';
curr->tag.print(o);

handleResumeTable(o, curr);
}
void visitStackSwitch(StackSwitch* curr) {
assert(curr->cont->type.isContinuation());
printMedium(o, "switch");

o << ' ';
printHeapType(curr->contType);
printHeapType(curr->cont->type.getHeapType());
o << ' ';
curr->tag.print(o);
}
Expand Down
6 changes: 3 additions & 3 deletions src/wasm-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,7 @@ class Builder {
ret->sentTypes.set(sentTypes);
ret->operands.set(operands);
ret->cont = cont;
ret->finalize(&wasm);
ret->finalize();
return ret;
}
ResumeThrow* makeResumeThrow(HeapType contType,
Expand All @@ -1225,7 +1225,7 @@ class Builder {
ret->sentTypes.set(sentTypes);
ret->operands.set(operands);
ret->cont = cont;
ret->finalize(&wasm);
ret->finalize();
return ret;
}
StackSwitch* makeStackSwitch(HeapType contType,
Expand All @@ -1237,7 +1237,7 @@ class Builder {
ret->tag = tag;
ret->operands.set(operands);
ret->cont = cont;
ret->finalize(&wasm);
ret->finalize();
return ret;
}

Expand Down
12 changes: 3 additions & 9 deletions src/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -1983,10 +1983,7 @@ class Resume : public SpecificExpression<Expression::ResumeId> {
ExpressionList operands;
Expression* cont;

// When 'Module*' parameter is given, we populate the 'sentTypes' array, so
// that the types can be accessed in other analyses without accessing the
// module.
void finalize(Module* wasm = nullptr);
void finalize();

// sentTypes[i] contains the type of the values that will be sent to the block
// handlerBlocks[i] if suspending with tag handlerTags[i]. Not part of the
Expand All @@ -2011,10 +2008,7 @@ class ResumeThrow : public SpecificExpression<Expression::ResumeThrowId> {
ExpressionList operands;
Expression* cont;

// When 'Module*' parameter is given, we populate the 'sentTypes' array, so
// that the types can be accessed in other analyses without accessing the
// module.
void finalize(Module* wasm = nullptr);
void finalize();

// sentTypes[i] contains the type of the values that will be sent to the block
// handlerBlocks[i] if suspending with tag handlerTags[i]. Not part of the
Expand All @@ -2035,7 +2029,7 @@ class StackSwitch : public SpecificExpression<Expression::StackSwitchId> {
Expression* cont;

// We need access to the module to obtain the signature of the tag.
void finalize(Module* wasm = nullptr);
void finalize();
};

// Globals
Expand Down
7 changes: 3 additions & 4 deletions src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8019,7 +8019,6 @@ static void readResumeTable(WasmBinaryReader* reader, ResumeType* curr) {
}

void WasmBinaryReader::visitResume(Resume* curr) {

curr->contType = getIndexedHeapType();
if (!curr->contType.isContinuation()) {
throwError("non-continuation type in resume instruction " +
Expand All @@ -8037,7 +8036,7 @@ void WasmBinaryReader::visitResume(Resume* curr) {
curr->operands[numArgs - i - 1] = popNonVoidExpression();
}

curr->finalize(&wasm);
curr->finalize();
}

void WasmBinaryReader::visitResumeThrow(ResumeThrow* curr) {
Expand All @@ -8061,7 +8060,7 @@ void WasmBinaryReader::visitResumeThrow(ResumeThrow* curr) {
curr->operands[numArgs - i - 1] = popNonVoidExpression();
}

curr->finalize(&wasm);
curr->finalize();
}

void WasmBinaryReader::visitStackSwitch(StackSwitch* curr) {
Expand All @@ -8086,7 +8085,7 @@ void WasmBinaryReader::visitStackSwitch(StackSwitch* curr) {
curr->operands[numArgs - i - 1] = popNonVoidExpression();
}

curr->finalize(&wasm);
curr->finalize();
}

void WasmBinaryReader::throwError(std::string text) {
Expand Down
12 changes: 8 additions & 4 deletions src/wasm/wasm-stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2618,14 +2618,16 @@ void BinaryInstWriter::visitSuspend(Suspend* curr) {
}

void BinaryInstWriter::visitContBind(ContBind* curr) {
assert(curr->cont->type.isContinuation() && curr->type.isContinuation());
o << int8_t(BinaryConsts::ContBind);
parent.writeIndexedHeapType(curr->sourceType);
parent.writeIndexedHeapType(curr->cont->type.getHeapType());
parent.writeIndexedHeapType(curr->type.getHeapType());
}

void BinaryInstWriter::visitResume(Resume* curr) {
assert(curr->cont->type.isContinuation());
o << int8_t(BinaryConsts::Resume);
parent.writeIndexedHeapType(curr->contType);
parent.writeIndexedHeapType(curr->cont->type.getHeapType());

size_t handlerNum = curr->handlerTags.size();
o << U32LEB(handlerNum);
Expand All @@ -2644,8 +2646,9 @@ void BinaryInstWriter::visitResume(Resume* curr) {
}

void BinaryInstWriter::visitResumeThrow(ResumeThrow* curr) {
assert(curr->cont->type.isContinuation());
o << int8_t(BinaryConsts::ResumeThrow);
parent.writeIndexedHeapType(curr->contType);
parent.writeIndexedHeapType(curr->cont->type.getHeapType());
o << U32LEB(parent.getTagIndex(curr->tag));

size_t handlerNum = curr->handlerTags.size();
Expand All @@ -2665,8 +2668,9 @@ void BinaryInstWriter::visitResumeThrow(ResumeThrow* curr) {
}

void BinaryInstWriter::visitStackSwitch(StackSwitch* curr) {
assert(curr->cont->type.isContinuation());
o << int8_t(BinaryConsts::Switch);
parent.writeIndexedHeapType(curr->contType);
parent.writeIndexedHeapType(curr->cont->type.getHeapType());
o << U32LEB(parent.getTagIndex(curr->tag));
}

Expand Down
39 changes: 21 additions & 18 deletions src/wasm/wasm-validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3502,9 +3502,9 @@ void FunctionValidator::visitContBind(ContBind* curr) {
"cont.bind requires stack-switching [--enable-stack-switching]");

shouldBeTrue(
(curr->sourceType.isContinuation() &&
curr->sourceType.getContinuation().type.isSignature()) ||
Type(curr->sourceType, Nullable) == Type::unreachable,
(curr->cont->type.isContinuation() &&
curr->cont->type.getHeapType().getContinuation().type.isSignature()) ||
curr->cont->type == Type::unreachable,
curr,
"the first type annotation on cont.bind must be a continuation type");

Expand Down Expand Up @@ -3534,11 +3534,12 @@ void FunctionValidator::visitResume(Resume* curr) {
curr,
"sentTypes cache in resume instruction has not been initialized");

shouldBeTrue((curr->contType.isContinuation() &&
curr->contType.getContinuation().type.isSignature()) ||
curr->type == Type::unreachable,
curr,
"resume must be annotated with a continuation type");
shouldBeTrue(
(curr->cont->type.isContinuation() &&
curr->cont->type.getHeapType().getContinuation().type.isSignature()) ||
curr->type == Type::unreachable,
curr,
"resume must be annotated with a continuation type");
}

void FunctionValidator::visitResumeThrow(ResumeThrow* curr) {
Expand All @@ -3555,11 +3556,12 @@ void FunctionValidator::visitResumeThrow(ResumeThrow* curr) {
curr,
"sentTypes cache in resume_throw instruction has not been initialized");

shouldBeTrue((curr->contType.isContinuation() &&
curr->contType.getContinuation().type.isSignature()) ||
curr->type == Type::unreachable,
curr,
"resume_throw must be annotated with a continuation type");
shouldBeTrue(
(curr->cont->type.isContinuation() &&
curr->cont->type.getHeapType().getContinuation().type.isSignature()) ||
curr->type == Type::unreachable,
curr,
"resume_throw must be annotated with a continuation type");

auto* tag = getModule()->getTagOrNull(curr->tag);
if (!shouldBeTrue(!!tag, curr, "resume_throw must be annotated with a tag")) {
Expand All @@ -3573,11 +3575,12 @@ void FunctionValidator::visitStackSwitch(StackSwitch* curr) {
curr,
"switch requires stack-switching [--enable-stack-switching]");

shouldBeTrue((curr->contType.isContinuation() &&
curr->contType.getContinuation().type.isSignature()) ||
curr->type == Type::unreachable,
curr,
"switch must be annotated with a continuation type");
shouldBeTrue(
(curr->cont->type.isContinuation() &&
curr->cont->type.getHeapType().getContinuation().type.isSignature()) ||
curr->type == Type::unreachable,
curr,
"switch must be annotated with a continuation type");

auto* tag = getModule()->getTagOrNull(curr->tag);
if (!shouldBeTrue(!!tag, curr, "switch must be annotated with a tag")) {
Expand Down
45 changes: 31 additions & 14 deletions src/wasm/wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1375,8 +1375,12 @@ void ContNew::finalize() {
}

void ContBind::finalize() {
if (handleUnreachableOperands(this)) {
if (cont->type == Type::unreachable) {
type = Type::unreachable;
return;
}
if (handleUnreachableOperands(this)) {
return;
}
}

Expand All @@ -1387,32 +1391,45 @@ void Suspend::finalize(Module* wasm) {
}
}

void Resume::finalize(Module* wasm) {
void Resume::finalize() {
if (cont->type == Type::unreachable) {
type = Type::unreachable;
} else if (!handleUnreachableOperands(this)) {
const Signature& contSig =
this->contType.getContinuation().type.getSignature();
type = contSig.results;
return;
}
if (handleUnreachableOperands(this)) {
return;
}

const Signature& contSig =
this->cont->type.getHeapType().getContinuation().type.getSignature();
type = contSig.results;
}

void ResumeThrow::finalize(Module* wasm) {
void ResumeThrow::finalize() {
if (cont->type == Type::unreachable) {
type = Type::unreachable;
} else if (!handleUnreachableOperands(this)) {
const Signature& contSig =
this->contType.getContinuation().type.getSignature();
type = contSig.results;
return;
}
if (handleUnreachableOperands(this)) {
return;
}

const Signature& contSig =
this->cont->type.getHeapType().getContinuation().type.getSignature();
type = contSig.results;
}

void StackSwitch::finalize(Module* wasm) {
void StackSwitch::finalize() {
if (cont->type == Type::unreachable) {
type = Type::unreachable;
} else if (!handleUnreachableOperands(this) && wasm) {
type = this->contType.getContinuation().type.getSignature().params;
return;
}
if (handleUnreachableOperands(this)) {
return;
}

type =
this->cont->type.getHeapType().getContinuation().type.getSignature().params;
}

size_t Function::getNumParams() { return getParams().size(); }
Expand Down
5 changes: 4 additions & 1 deletion test/lit/basic/stack_switching_contbind.wast
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@
)

;; CHECK-TEXT: (func $k (type $6) (result (ref $ct1))
;; CHECK-TEXT-NEXT: (cont.bind $ct1 $ct1
;; CHECK-TEXT-NEXT: (block ;; (replaces unreachable ContBind we can't emit)
;; CHECK-TEXT-NEXT: (drop
;; CHECK-TEXT-NEXT: (unreachable)
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: (unreachable)
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
Expand Down

0 comments on commit c549cd7

Please sign in to comment.