-
Notifications
You must be signed in to change notification settings - Fork 843
[multibyte] Add multibyte array load instructions. #8504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1147,6 +1147,15 @@ class EffectAnalyzer { | |
| parent.implicitTrap = true; | ||
| writesArray(curr->ref->type.getHeapType(), curr->order); | ||
| } | ||
| void visitArrayLoad(ArrayLoad* curr) { | ||
| if (curr->ref->type.isNull()) { | ||
| parent.trap = true; | ||
| return; | ||
| } | ||
|
Comment on lines
+1151
to
+1154
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can use if (trapOnNull(curr->ref)) {
return;
}which also handles the unreachable case. I guess the below statement setting
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to add a comment about the possibility of OOB access here. |
||
| parent.implicitTrap = true; | ||
| readsArray(curr->ref->type.getHeapType(), MemoryOrder::Unordered); | ||
| } | ||
|
|
||
| void visitArrayStore(ArrayStore* curr) { | ||
| if (curr->ref->type.isNull()) { | ||
| parent.trap = true; | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -1105,6 +1105,13 @@ struct InfoCollector | |||||||
| addChildParentLink(curr->ref, curr); | ||||||||
| addChildParentLink(curr->value, curr); | ||||||||
| } | ||||||||
| void visitArrayLoad(ArrayLoad* curr) { | ||||||||
| if (!isRelevant(curr->ref)) { | ||||||||
| addRoot(curr); | ||||||||
| return; | ||||||||
| } | ||||||||
| addChildParentLink(curr->ref, curr); | ||||||||
| } | ||||||||
| void visitArrayStore(ArrayStore* curr) { | ||||||||
| if (curr->ref->type == Type::unreachable) { | ||||||||
| return; | ||||||||
|
|
@@ -1755,6 +1762,7 @@ void TNHOracle::scan(Function* func, | |||||||
| } | ||||||||
| void visitArrayGet(ArrayGet* curr) { notePossibleTrap(curr->ref); } | ||||||||
| void visitArraySet(ArraySet* curr) { notePossibleTrap(curr->ref); } | ||||||||
| void visitArrayLoad(ArrayLoad* curr) { notePossibleTrap(curr->ref); } | ||||||||
| void visitArrayStore(ArrayStore* curr) { notePossibleTrap(curr->ref); } | ||||||||
| void visitArrayLen(ArrayLen* curr) { notePossibleTrap(curr->ref); } | ||||||||
| void visitArrayCopy(ArrayCopy* curr) { | ||||||||
|
|
@@ -2865,6 +2873,9 @@ void Flower::flowAfterUpdate(LocationIndex locationIndex) { | |||||||
| } else if (auto* set = parent->dynCast<ArraySet>()) { | ||||||||
| assert(set->ref == child || set->value == child); | ||||||||
| writeToData(set->ref, set->value, 0); | ||||||||
| } else if (auto* load = parent->dynCast<ArrayLoad>()) { | ||||||||
| assert(load->ref == child); | ||||||||
| readFromData(load->ref->type, 0, contents, load); | ||||||||
| } else if (auto* store = parent->dynCast<ArrayStore>()) { | ||||||||
| assert(store->ref == child || store->value == child); | ||||||||
| // TODO: model the stored value, and handle different but equal values in | ||||||||
|
|
@@ -3108,15 +3119,25 @@ void Flower::filterPackedDataReads(PossibleContents& contents, | |||||||
| auto signed_ = false; | ||||||||
| Expression* ref; | ||||||||
| Index index; | ||||||||
| unsigned bytes = 0; | ||||||||
| Type resultType = Type::none; | ||||||||
| if (auto* get = expr->dynCast<StructGet>()) { | ||||||||
| signed_ = get->signed_; | ||||||||
| ref = get->ref; | ||||||||
| index = get->index; | ||||||||
| resultType = get->type; | ||||||||
| } else if (auto* get = expr->dynCast<ArrayGet>()) { | ||||||||
| signed_ = get->signed_; | ||||||||
| ref = get->ref; | ||||||||
| // Arrays are treated as having a single field. | ||||||||
| index = 0; | ||||||||
| resultType = get->type; | ||||||||
| } else if (auto* load = expr->dynCast<ArrayLoad>()) { | ||||||||
| signed_ = load->signed_; | ||||||||
| ref = load->ref; | ||||||||
| index = 0; | ||||||||
| bytes = load->bytes; | ||||||||
| resultType = load->type; | ||||||||
| } else { | ||||||||
| WASM_UNREACHABLE("bad packed read"); | ||||||||
| } | ||||||||
|
|
@@ -3135,13 +3156,20 @@ void Flower::filterPackedDataReads(PossibleContents& contents, | |||||||
| assert(ref->type.isRef()); | ||||||||
| auto field = GCTypeUtils::getField(ref->type.getHeapType(), index); | ||||||||
| assert(field); | ||||||||
| if (!field->isPacked()) { | ||||||||
| return; | ||||||||
| if (!bytes) { | ||||||||
| if (!field->isPacked()) { | ||||||||
| return; | ||||||||
| } | ||||||||
| bytes = field->getByteSize(); | ||||||||
| } | ||||||||
|
|
||||||||
| if (contents.isLiteral()) { | ||||||||
| // This is a constant. We can sign-extend it and use that value. | ||||||||
| auto shifts = Literal(int32_t(32 - field->getByteSize() * 8)); | ||||||||
| unsigned bits = resultType.getByteSize() == 8 ? 64 : 32; | ||||||||
| if (bits <= bytes * 8) { | ||||||||
| return; // No need to sign-extend for full size | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We generally avoid inline comments.
Suggested change
|
||||||||
| } | ||||||||
| auto shifts = Literal(int32_t(bits - bytes * 8)); | ||||||||
| auto lit = contents.getLiteral(); | ||||||||
| lit = lit.shl(shifts); | ||||||||
| lit = lit.shrS(shifts); | ||||||||
|
|
||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2378,6 +2378,51 @@ class ExpressionRunner : public OverriddenVisitor<SubType, Flow> { | |
| data->values[i] = truncateForPacking(value.getSingleValue(), field); | ||
| return Flow(); | ||
| } | ||
| Flow visitArrayLoad(ArrayLoad* curr) { | ||
| VISIT(ref, curr->ref) | ||
| VISIT(index, curr->index) | ||
| auto data = ref.getSingleValue().getGCData(); | ||
| if (!data) { | ||
| trap("null ref"); | ||
| } | ||
| Index i = index.getSingleValue().geti32(); | ||
| size_t size = data->values.size(); | ||
| if (i >= size || curr->bytes > (size - i)) { | ||
| trap("array oob"); | ||
| } | ||
| uint64_t val = 0; | ||
| for (unsigned b = 0; b < curr->bytes; ++b) { | ||
| val |= static_cast<uint64_t>(data->values[i + b].geti32()) << (b * 8); | ||
| } | ||
| switch (curr->type.getBasic()) { | ||
| case Type::i32: { | ||
| int32_t sval = static_cast<int32_t>(val); | ||
| if (curr->signed_) { | ||
| if (curr->bytes == 1) sval = int32_t(int8_t(sval)); | ||
| else if (curr->bytes == 2) sval = int32_t(int16_t(sval)); | ||
|
Comment on lines
+2401
to
+2402
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We always use brackets around if bodies, even for one-liners. (Here and below.) |
||
| } | ||
| return Literal(sval); | ||
| } | ||
| case Type::i64: { | ||
| int64_t sval = static_cast<int64_t>(val); | ||
| if (curr->signed_) { | ||
| if (curr->bytes == 1) sval = int64_t(int8_t(sval)); | ||
| else if (curr->bytes == 2) sval = int64_t(int16_t(sval)); | ||
| else if (curr->bytes == 4) sval = int64_t(int32_t(sval)); | ||
|
Comment on lines
+2409
to
+2411
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use C++ style casts or bit operators instead of C-style casts? e.g. sval & 0xFF |
||
| } | ||
| return Literal(sval); | ||
| } | ||
| case Type::f32: { | ||
| return Literal(bit_cast<float>(static_cast<int32_t>(val))); | ||
| } | ||
| case Type::f64: { | ||
| return Literal(bit_cast<double>(static_cast<int64_t>(val))); | ||
| } | ||
| default: | ||
| WASM_UNREACHABLE("invalid type"); | ||
| } | ||
| } | ||
|
|
||
| Flow visitArrayStore(ArrayStore* curr) { | ||
| VISIT(ref, curr->ref) | ||
| VISIT(index, curr->index) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the 1 for here? Can we name this constant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use lots of unnamed constants for base costs, so I think considering this separately would be fine.