Skip to content

Commit

Permalink
WIP remove type immediates
Browse files Browse the repository at this point in the history
  • Loading branch information
dhil committed Nov 21, 2024
1 parent b1d65ce commit 82d683a
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 124 deletions.
29 changes: 20 additions & 9 deletions src/ir/child-typer.h
Original file line number Diff line number Diff line change
Expand Up @@ -1050,11 +1050,17 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
note(&curr->end, Type::i32);
}

void visitContNew(ContNew* curr) { note(&curr->func, curr->type); }
void visitContNew(ContNew* curr) {
note(&curr->func,
Type(curr->type.getHeapType().getContinuation().type, Nullable));
}

void visitContBind(ContBind* curr) {
auto sourceParams =
curr->sourceType.getContinuation().type.getSignature().params;
note(&curr->cont, curr->cont->type);
auto sourceParams = curr->cont->type.getHeapType()
.getContinuation()
.type.getSignature()
.params;
auto targetParams =
curr->type.getHeapType().getContinuation().type.getSignature().params;
assert(sourceParams.size() >= targetParams.size());
Expand All @@ -1063,7 +1069,6 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
for (size_t i = 0; i < n; ++i) {
note(&curr->operands[i], sourceParams[i]);
}
note(&curr->cont, Type(curr->sourceType, Nullable));
}

void visitSuspend(Suspend* curr) {
Expand All @@ -1075,31 +1080,37 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
}

void visitResume(Resume* curr) {
auto params = curr->contType.getContinuation().type.getSignature().params;
note(&curr->cont, curr->cont->type);
auto params = curr->cont->type.getHeapType()
.getContinuation()
.type.getSignature()
.params;
assert(params.size() == curr->operands.size());
for (size_t i = 0; i < params.size(); ++i) {
note(&curr->operands[i], params[i]);
}
note(&curr->cont, Type(curr->contType, Nullable));
}

void visitResumeThrow(ResumeThrow* curr) {
note(&curr->cont, curr->cont->type);
auto params = wasm.getTag(curr->tag)->sig.params;
assert(params.size() == curr->operands.size());
for (size_t i = 0; i < params.size(); ++i) {
note(&curr->operands[i], params[i]);
}
note(&curr->cont, Type(curr->contType, Nullable));
}

void visitStackSwitch(StackSwitch* curr) {
auto params = curr->contType.getContinuation().type.getSignature().params;
note(&curr->cont, curr->cont->type);
auto params = curr->cont->type.getHeapType()
.getContinuation()
.type.getSignature()
.params;
assert(params.size() >= 1 &&
((params.size() - 1) == curr->operands.size()));
for (size_t i = 0; i < params.size() - 1; ++i) {
note(&curr->operands[i], params[i]);
}
note(&curr->cont, Type(curr->contType, Nullable));
}
};

Expand Down
8 changes: 4 additions & 4 deletions src/ir/module-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,18 +460,18 @@ struct CodeScanner
} else if (auto* set = curr->dynCast<ArraySet>()) {
info.note(set->ref->type);
} else if (auto* contBind = curr->dynCast<ContBind>()) {
info.note(contBind->sourceType);
info.note(contBind->cont->type);
info.note(contBind->type);
} 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
20 changes: 8 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 @@ -2466,7 +2462,7 @@ struct PrintExpressionContents
}
void visitContBind(ContBind* curr) {
printMedium(o, "cont.bind ");
printHeapType(curr->sourceType);
printHeapType(curr->cont->type.getHeapType());
o << ' ';
printHeapType(curr->type.getHeapType());
}
Expand Down Expand Up @@ -2495,15 +2491,15 @@ struct PrintExpressionContents
printMedium(o, "resume");

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

handleResumeTable(o, curr);
}
void visitResumeThrow(ResumeThrow* curr) {
printMedium(o, "resume_throw");

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

Expand All @@ -2513,7 +2509,7 @@ struct PrintExpressionContents
printMedium(o, "switch");

o << ' ';
printHeapType(curr->contType);
printHeapType(curr->cont->type.getHeapType());
o << ' ';
curr->tag.print(o);
}
Expand Down
32 changes: 16 additions & 16 deletions src/wasm-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -1168,76 +1168,76 @@ class Builder {
ret->finalize();
return ret;
}
ContNew* makeContNew(HeapType type, Expression* func) {
ContNew* makeContNew(Type type, Expression* func) {
auto* ret = wasm.allocator.alloc<ContNew>();
ret->type = Type(type, NonNullable);
ret->type = type;
ret->func = func;
ret->finalize();
return ret;
}
ContBind* makeContBind(HeapType sourceType,
HeapType targetType,
ContBind* makeContBind(Type type,
const std::vector<Expression*>& operands,
Expression* cont) {
auto* ret = wasm.allocator.alloc<ContBind>();
ret->sourceType = sourceType;
ret->type = Type(targetType, NonNullable);
ret->type = type;
ret->operands.set(operands);
ret->cont = cont;
ret->finalize();
return ret;
}
Suspend* makeSuspend(Name tag, const std::vector<Expression*>& args) {
Suspend*
makeSuspend(Type type, Name tag, const std::vector<Expression*>& args) {
auto* ret = wasm.allocator.alloc<Suspend>();
ret->type = type;
ret->tag = tag;
ret->operands.set(args);
ret->finalize(&wasm);
return ret;
}
Resume* makeResume(HeapType contType,
Resume* makeResume(Type type,
const std::vector<Name>& handlerTags,
const std::vector<Name>& handlerBlocks,
const std::vector<Type>& sentTypes,
const std::vector<Expression*>& operands,
Expression* cont) {
auto* ret = wasm.allocator.alloc<Resume>();
ret->contType = contType;
ret->type = type;
ret->handlerTags.set(handlerTags);
ret->handlerBlocks.set(handlerBlocks);
ret->sentTypes.set(sentTypes);
ret->operands.set(operands);
ret->cont = cont;
ret->finalize(&wasm);
ret->finalize();
return ret;
}
ResumeThrow* makeResumeThrow(HeapType contType,
ResumeThrow* makeResumeThrow(Type type,
Name tag,
const std::vector<Name>& handlerTags,
const std::vector<Name>& handlerBlocks,
const std::vector<Type>& sentTypes,
const std::vector<Expression*>& operands,
Expression* cont) {
auto* ret = wasm.allocator.alloc<ResumeThrow>();
ret->contType = contType;
ret->type = type;
ret->tag = tag;
ret->handlerTags.set(handlerTags);
ret->handlerBlocks.set(handlerBlocks);
ret->sentTypes.set(sentTypes);
ret->operands.set(operands);
ret->cont = cont;
ret->finalize(&wasm);
ret->finalize();
return ret;
}
StackSwitch* makeStackSwitch(HeapType contType,
StackSwitch* makeStackSwitch(Type type,
Name tag,
const std::vector<Expression*>& operands,
Expression* cont) {
auto* ret = wasm.allocator.alloc<StackSwitch>();
ret->contType = contType;
ret->type = type;
ret->tag = tag;
ret->operands.set(operands);
ret->cont = cont;
ret->finalize(&wasm);
ret->finalize();
return ret;
}

Expand Down
4 changes: 0 additions & 4 deletions src/wasm-delegations-fields.def
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,6 @@ DELEGATE_FIELD_CASE_END(ContNew)
DELEGATE_FIELD_CASE_START(ContBind)
DELEGATE_FIELD_CHILD(ContBind, cont)
DELEGATE_FIELD_CHILD_VECTOR(ContBind, operands)
DELEGATE_FIELD_HEAPTYPE(ContBind, sourceType)
DELEGATE_FIELD_CASE_END(ContBind)

DELEGATE_FIELD_CASE_START(Suspend)
Expand All @@ -786,7 +785,6 @@ DELEGATE_FIELD_CHILD(Resume, cont)
DELEGATE_FIELD_CHILD_VECTOR(Resume, operands)
DELEGATE_FIELD_SCOPE_NAME_USE_VECTOR(Resume, handlerBlocks)
DELEGATE_FIELD_NAME_KIND_VECTOR(Resume, handlerTags, ModuleItemKind::Tag)
DELEGATE_FIELD_HEAPTYPE(Resume, contType)
DELEGATE_FIELD_CASE_END(Resume)

DELEGATE_FIELD_CASE_START(ResumeThrow)
Expand All @@ -795,14 +793,12 @@ DELEGATE_FIELD_CHILD(ResumeThrow, cont)
DELEGATE_FIELD_CHILD_VECTOR(ResumeThrow, operands)
DELEGATE_FIELD_SCOPE_NAME_USE_VECTOR(ResumeThrow, handlerBlocks)
DELEGATE_FIELD_NAME_KIND_VECTOR(ResumeThrow, handlerTags, ModuleItemKind::Tag)
DELEGATE_FIELD_HEAPTYPE(ResumeThrow, contType)
DELEGATE_FIELD_NAME_KIND(ResumeThrow, tag, ModuleItemKind::Tag)
DELEGATE_FIELD_CASE_END(ResumeThrow)

DELEGATE_FIELD_CASE_START(StackSwitch)
DELEGATE_FIELD_CHILD(StackSwitch, cont)
DELEGATE_FIELD_CHILD_VECTOR(StackSwitch, operands)
DELEGATE_FIELD_HEAPTYPE(StackSwitch, contType)
DELEGATE_FIELD_NAME_KIND(StackSwitch, tag, ModuleItemKind::Tag)
DELEGATE_FIELD_CASE_END(StackSwitch)

Expand Down
8 changes: 4 additions & 4 deletions src/wasm-ir-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,17 +213,17 @@ class IRBuilder : public UnifiedExpressionVisitor<IRBuilder, Result<>> {
Result<> makeStringWTF16Get();
Result<> makeStringIterNext();
Result<> makeStringSliceWTF();
Result<> makeContNew(HeapType ct);
Result<> makeContNew(HeapType type);
Result<> makeContBind(HeapType sourceType, HeapType targetType);
Result<> makeSuspend(Name tag);
Result<> makeResume(HeapType ct,
Result<> makeResume(HeapType type,
const std::vector<Name>& tags,
const std::vector<std::optional<Index>>& labels);
Result<> makeResumeThrow(HeapType ct,
Result<> makeResumeThrow(HeapType type,
Name tag,
const std::vector<Name>& tags,
const std::vector<std::optional<Index>>& labels);
Result<> makeStackSwitch(HeapType ct, Name tag);
Result<> makeStackSwitch(HeapType type, Name tag);

// Private functions that must be public for technical reasons.
Result<> visitExpression(Expression*);
Expand Down
10 changes: 3 additions & 7 deletions src/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -1942,7 +1942,6 @@ class ContBind : public SpecificExpression<Expression::ContBindId> {

// Syntax: cont.bind $src $dst
// We store $src here, and $dst in the inherited `type` member.
HeapType sourceType;
ExpressionList operands;
Expression* cont;

Expand All @@ -1968,7 +1967,6 @@ class Resume : public SpecificExpression<Expression::ResumeId> {
: handlerTags(allocator), handlerBlocks(allocator), operands(allocator),
sentTypes(allocator) {}

HeapType contType;
// The following two vectors are to be understood together
// pointwise. That is, the ith component of each vector together
// classifies an on-clause `(on $tag $label)` or `(on $tag
Expand All @@ -1986,7 +1984,7 @@ class Resume : public SpecificExpression<Expression::ResumeId> {
// 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 @@ -2002,7 +2000,6 @@ class ResumeThrow : public SpecificExpression<Expression::ResumeThrowId> {
: handlerTags(allocator), handlerBlocks(allocator), operands(allocator),
sentTypes(allocator) {}

HeapType contType;
Name tag;
// See the comment on `Resume` above.
ArenaVector<Name> handlerTags;
Expand All @@ -2014,7 +2011,7 @@ class ResumeThrow : public SpecificExpression<Expression::ResumeThrowId> {
// 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 @@ -2028,14 +2025,13 @@ class StackSwitch : public SpecificExpression<Expression::StackSwitchId> {
public:
StackSwitch(MixedArena& allocator) : operands(allocator) {}

HeapType contType;
Name tag;

ExpressionList operands;
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
Loading

0 comments on commit 82d683a

Please sign in to comment.