Skip to content
Merged
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
1 change: 1 addition & 0 deletions scripts/test/fuzzing.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
'type-merging-desc.wast',
'heap2local-desc.wast',
'minimize-rec-groups-desc.wast',
'precompute-desc.wast',
# TODO: fix split_wast() on tricky escaping situations like a string ending
# in \\" (the " is not escaped - there is an escaped \ before it)
'string-lifting-section.wast',
Expand Down
2 changes: 1 addition & 1 deletion src/ir/child-typer.h
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
}
auto desc = curr->type.getHeapType().getDescriptorType();
if (desc) {
note(&curr->descriptor, Type(*desc, NonNullable, Exact));
note(&curr->desc, Type(*desc, NonNullable, Exact));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ir/cost.h
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ struct CostAnalyzer : public OverriddenVisitor<CostAnalyzer, CostType> {
for (auto* child : curr->operands) {
ret += visit(child);
}
ret += maybeVisit(curr->descriptor);
ret += maybeVisit(curr->desc);
return ret;
}
CostType visitStructGet(StructGet* curr) {
Expand Down
27 changes: 13 additions & 14 deletions src/passes/Heap2Local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,9 +612,8 @@ struct Struct2Local : PostWalker<Struct2Local> {
for (auto field : fields) {
localIndexes.push_back(builder.addVar(func, field.type));
}
if (allocation->descriptor) {
localIndexes.push_back(
builder.addVar(func, allocation->descriptor->type));
if (allocation->desc) {
localIndexes.push_back(builder.addVar(func, allocation->desc->type));
}

// Replace the things we need to using the visit* methods.
Expand Down Expand Up @@ -732,7 +731,7 @@ struct Struct2Local : PostWalker<Struct2Local> {
// computed do we copy them into the locals representing the fields.
std::vector<Index> tempIndexes;
Index numTemps =
(curr->isWithDefault() ? 0 : fields.size()) + bool(curr->descriptor);
(curr->isWithDefault() ? 0 : fields.size()) + bool(curr->desc);
tempIndexes.reserve(numTemps);

// Create the temp variables.
Expand All @@ -741,8 +740,8 @@ struct Struct2Local : PostWalker<Struct2Local> {
tempIndexes.push_back(builder.addVar(func, field.type));
}
}
if (curr->descriptor) {
tempIndexes.push_back(builder.addVar(func, curr->descriptor->type));
if (curr->desc) {
tempIndexes.push_back(builder.addVar(func, curr->desc->type));
}

// Store the initial values into the temp locals.
Expand All @@ -752,9 +751,9 @@ struct Struct2Local : PostWalker<Struct2Local> {
builder.makeLocalSet(tempIndexes[i], curr->operands[i]));
}
}
if (curr->descriptor) {
if (curr->desc) {
contents.push_back(
builder.makeLocalSet(tempIndexes[numTemps - 1], curr->descriptor));
builder.makeLocalSet(tempIndexes[numTemps - 1], curr->desc));
}

// Store the values into the locals representing the fields.
Expand All @@ -765,9 +764,9 @@ struct Struct2Local : PostWalker<Struct2Local> {
: builder.makeLocalGet(tempIndexes[i], fields[i].type);
contents.push_back(builder.makeLocalSet(localIndexes[i], val));
}
if (curr->descriptor) {
if (curr->desc) {
auto* val =
builder.makeLocalGet(tempIndexes[numTemps - 1], curr->descriptor->type);
builder.makeLocalGet(tempIndexes[numTemps - 1], curr->desc->type);
contents.push_back(
builder.makeLocalSet(localIndexes[fields.size()], val));
}
Expand Down Expand Up @@ -851,8 +850,8 @@ struct Struct2Local : PostWalker<Struct2Local> {
// also know the cast must fail if the optimized allocation flows in as
// the descriptor, since it cannot possibly have been used in the
// allocation of the cast value without having been considered to escape.
if (!allocation->descriptor || analyzer.getInteraction(curr->desc) ==
ParentChildInteraction::Flows) {
if (!allocation->desc || analyzer.getInteraction(curr->desc) ==
ParentChildInteraction::Flows) {
// The allocation does not have a descriptor, so there is no way for the
// cast to succeed.
replaceCurrent(builder.blockify(builder.makeDrop(curr->ref),
Expand All @@ -861,7 +860,7 @@ struct Struct2Local : PostWalker<Struct2Local> {
} else {
// The cast succeeds iff the optimized allocation's descriptor is the
// same as the given descriptor and traps otherwise.
auto type = allocation->descriptor->type;
auto type = allocation->desc->type;
replaceCurrent(builder.blockify(
builder.makeDrop(curr->ref),
builder.makeIf(
Expand Down Expand Up @@ -897,7 +896,7 @@ struct Struct2Local : PostWalker<Struct2Local> {
return;
}

auto type = allocation->descriptor->type;
auto type = allocation->desc->type;
if (type != curr->type) {
// We know exactly the allocation that flows into this expression, so we
// know the exact type of the descriptor. This type may be more precise
Expand Down
6 changes: 0 additions & 6 deletions src/passes/Precompute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,6 @@ class PrecomputingExpressionRunner
// string.encode_wtf16_array anyhow.)
return Flow(NONCONSTANT_FLOW);
}

Flow visitRefGetDesc(RefGetDesc* curr) {
// TODO: Implement this. For now, return nonconstant so that we skip it and
// do not error.
return Flow(NONCONSTANT_FLOW);
}
};

struct Precompute
Expand Down
4 changes: 2 additions & 2 deletions src/passes/RemoveUnusedModuleElements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,8 @@ struct Analyzer {

// Use the descriptor right now, normally. (We only have special
// optimization for struct.new operands, below.)
if (new_->descriptor) {
use(new_->descriptor);
if (new_->desc) {
use(new_->desc);
}

auto type = new_->type.getHeapType();
Expand Down
6 changes: 3 additions & 3 deletions src/wasm-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ class Builder {
Expression* descriptor = nullptr) {
auto* ret = wasm.allocator.alloc<StructNew>();
ret->operands.set(args);
ret->descriptor = descriptor;
ret->desc = descriptor;
ret->type = Type(type, NonNullable, Exact);
ret->finalize();
return ret;
Expand All @@ -941,7 +941,7 @@ class Builder {
Expression* descriptor = nullptr) {
auto* ret = wasm.allocator.alloc<StructNew>();
ret->operands = std::move(args);
ret->descriptor = descriptor;
ret->desc = descriptor;
ret->type = Type(type, NonNullable, Exact);
ret->finalize();
return ret;
Expand All @@ -952,7 +952,7 @@ class Builder {
Expression* descriptor = nullptr) {
auto* ret = wasm.allocator.alloc<StructNew>();
ret->operands.set(args);
ret->descriptor = descriptor;
ret->desc = descriptor;
ret->type = Type(type, NonNullable, Exact);
ret->finalize();
return ret;
Expand Down
2 changes: 1 addition & 1 deletion src/wasm-delegations-fields.def
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ DELEGATE_FIELD_CHILD(BrOn, ref)
DELEGATE_FIELD_CASE_END(BrOn)

DELEGATE_FIELD_CASE_START(StructNew)
DELEGATE_FIELD_OPTIONAL_CHILD(StructNew, descriptor)
DELEGATE_FIELD_OPTIONAL_CHILD(StructNew, desc)
DELEGATE_FIELD_CHILD_VECTOR(StructNew, operands)
DELEGATE_FIELD_CASE_END(StructNew)

Expand Down
8 changes: 4 additions & 4 deletions src/wasm-interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -1781,8 +1781,8 @@ class ExpressionRunner : public OverriddenVisitor<SubType, Flow> {
return value;
}
}
if (curr->descriptor) {
auto value = self()->visit(curr->descriptor);
if (curr->desc) {
auto value = self()->visit(curr->desc);
if (value.breaking()) {
return value;
}
Expand All @@ -1804,10 +1804,10 @@ class ExpressionRunner : public OverriddenVisitor<SubType, Flow> {
data[i] = truncateForPacking(value.getSingleValue(), field);
}
}
if (!curr->descriptor) {
if (!curr->desc) {
return makeGCData(std::move(data), curr->type);
}
auto desc = self()->visit(curr->descriptor);
auto desc = self()->visit(curr->desc);
if (desc.breaking()) {
return desc;
}
Expand Down
2 changes: 1 addition & 1 deletion src/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -1683,7 +1683,7 @@ class StructNew : public SpecificExpression<Expression::StructNewId> {
// case, and binaryen doesn't guarantee roundtripping binaries anyhow.
ExpressionList operands;

Expression* descriptor = nullptr;
Expression* desc = nullptr;

bool isWithDefault() { return operands.empty(); }

Expand Down
4 changes: 2 additions & 2 deletions src/wasm/wasm-ir-builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2230,15 +2230,15 @@ Result<> IRBuilder::makeStructNew(HeapType type) {
curr.type = Type(type, NonNullable, Exact);
curr.operands.resize(type.getStruct().fields.size());
CHECK_ERR(visitStructNew(&curr));
push(builder.makeStructNew(type, std::move(curr.operands), curr.descriptor));
push(builder.makeStructNew(type, std::move(curr.operands), curr.desc));
return Ok{};
}

Result<> IRBuilder::makeStructNewDefault(HeapType type) {
StructNew curr(wasm.allocator);
curr.type = Type(type, NonNullable, Exact);
CHECK_ERR(visitStructNew(&curr));
push(builder.makeStructNew(type, {}, curr.descriptor));
push(builder.makeStructNew(type, {}, curr.desc));
return Ok{};
}

Expand Down
4 changes: 2 additions & 2 deletions src/wasm/wasm-validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3159,11 +3159,11 @@ void FunctionValidator::visitStructNew(StructNew* curr) {

auto descType = curr->type.getHeapType().getDescriptorType();
if (!descType) {
shouldBeFalse(curr->descriptor,
shouldBeFalse(curr->desc,
curr,
"struct.new of type without descriptor should lack one");
} else {
shouldBeSubType(curr->descriptor->type,
shouldBeSubType(curr->desc->type,
Type(*descType, Nullable, Exact),
curr,
"struct.new descriptor operand should have proper type");
Expand Down
2 changes: 1 addition & 1 deletion src/wasm/wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,7 @@ void StructNew::finalize() {
if (handleUnreachableOperands(this)) {
return;
}
if (descriptor && descriptor->type == Type::unreachable) {
if (desc && desc->type == Type::unreachable) {
type = Type::unreachable;
}
}
Expand Down
85 changes: 85 additions & 0 deletions test/lit/passes/precompute-desc.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.

;; RUN: wasm-opt %s --remove-unused-names --precompute-propagate --fuzz-exec -all -S -o - \
;; RUN: | filecheck %s

(module
(rec
;; CHECK: (rec
;; CHECK-NEXT: (type $struct (descriptor $desc (struct)))
(type $struct (descriptor $desc (struct)))
;; CHECK: (type $desc (describes $struct (struct)))
(type $desc (describes $struct (struct)))
)

;; CHECK: (global $desc (ref (exact $desc)) (struct.new_default $desc))
(global $desc (ref (exact $desc)) (struct.new $desc))
;; CHECK: (global $struct (ref $struct) (struct.new_default $struct
;; CHECK-NEXT: (global.get $desc)
;; CHECK-NEXT: ))
(global $struct (ref $struct) (struct.new $struct (global.get $desc)))

;; CHECK: (func $eq-descs (type $0) (result i32)
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
(func $eq-descs (result i32)
(ref.eq
(ref.get_desc $struct
(struct.new $struct
(global.get $desc)
)
)
(global.get $desc)
)
)

;; CHECK: (func $different-descs (type $0) (result i32)
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
(func $different-descs (result i32)
(ref.eq
(ref.get_desc $struct
(struct.new $struct
(struct.new $desc)
)
)
(global.get $desc)
)
)

;; CHECK: (func $br-on-cast-desc (type $0) (result i32)
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
(func $br-on-cast-desc (result i32)
(ref.eq
(block $l (result eqref)
(drop
(br_on_cast_desc $l eqref (ref $struct)
(global.get $struct)
(global.get $desc)
)
)
(ref.null none)
)
(global.get $struct)
)
)

;; CHECK: (func $br-on-cast-desc-fail (type $0) (result i32)
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
(func $br-on-cast-desc-fail (result i32)
(ref.eq
(block $l (result eqref)
(drop
(br_on_cast_desc_fail $l eqref (ref $struct)
(global.get $struct)
(global.get $desc)
)
)
(ref.null none)
)
(global.get $struct)
)
)
)
Loading