Skip to content

Initial implementation of "Memory64" proposal #3130

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

Merged
merged 1 commit into from
Sep 18, 2020
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
3 changes: 3 additions & 0 deletions scripts/test/wasm2js.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
from scripts.test import support

tests = shared.get_tests(shared.options.binaryen_test)
# memory64 is not supported in wasm2js yet (but may be with BigInt eventually).
tests = [t for t in tests if '64.wast' not in t]
spec_tests = shared.options.spec_tests
spec_tests = [t for t in spec_tests if '.fail' not in t]
spec_tests = [t for t in spec_tests if '64.wast' not in t]
wasm2js_tests = shared.get_tests(shared.get_test_dir('wasm2js'), ['.wast'])
assert_tests = ['wasm2js.wast.asserts']
# These tests exercise functionality not supported by wasm2js
Expand Down
2 changes: 1 addition & 1 deletion src/binaryen-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3262,7 +3262,7 @@ void BinaryenSetMemory(BinaryenModuleRef module,
uint8_t shared) {
auto* wasm = (Module*)module;
wasm->memory.initial = initial;
wasm->memory.max = maximum;
wasm->memory.max = int32_t(maximum); // Make sure -1 extends.
wasm->memory.exists = true;
wasm->memory.shared = shared;
if (exportName) {
Expand Down
4 changes: 1 addition & 3 deletions src/ir/ExpressionAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,13 +475,11 @@ size_t ExpressionAnalyzer::hash(Expression* curr) {
void visitLiteral(Literal curr) { rehash(digest, curr); }
void visitType(Type curr) { rehash(digest, curr.getID()); }
void visitIndex(Index curr) {
static_assert(sizeof(Index) == sizeof(int32_t),
static_assert(sizeof(Index) == sizeof(uint32_t),
"wasm64 will need changes here");
rehash(digest, curr);
}
void visitAddress(Address curr) {
static_assert(sizeof(Address) == sizeof(int32_t),
"wasm64 will need changes here");
rehash(digest, curr.addr);
}
};
Expand Down
15 changes: 15 additions & 0 deletions src/literal.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,21 @@ class Literal {
}
}

static Literal makeFromUInt64(uint64_t x, Type type) {
switch (type.getBasic()) {
case Type::i32:
return Literal(int32_t(x));
case Type::i64:
return Literal(int64_t(x));
case Type::f32:
return Literal(float(x));
case Type::f64:
return Literal(double(x));
default:
WASM_UNREACHABLE("unexpected type");
}
}

static Literals makeZero(Type type);
static Literal makeSingleZero(Type type);

Expand Down
52 changes: 28 additions & 24 deletions src/passes/AlignmentLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> {
if (curr->align == 0 || curr->align == curr->bytes) {
return curr;
}
auto indexType = getModule()->memory.indexType;
Builder builder(*getModule());
assert(curr->type == Type::i32);
auto temp = builder.addVar(getFunction(), Type::i32);
auto temp = builder.addVar(getFunction(), indexType);
Expression* ret;
if (curr->bytes == 2) {
ret = builder.makeBinary(
Expand All @@ -45,15 +46,15 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> {
false,
curr->offset,
1,
builder.makeLocalGet(temp, Type::i32),
builder.makeLocalGet(temp, indexType),
Type::i32),
builder.makeBinary(
ShlInt32,
builder.makeLoad(1,
false,
curr->offset + 1,
1,
builder.makeLocalGet(temp, Type::i32),
builder.makeLocalGet(temp, indexType),
Type::i32),
builder.makeConst(int32_t(8))));
if (curr->signed_) {
Expand All @@ -69,15 +70,15 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> {
false,
curr->offset,
1,
builder.makeLocalGet(temp, Type::i32),
builder.makeLocalGet(temp, indexType),
Type::i32),
builder.makeBinary(
ShlInt32,
builder.makeLoad(1,
false,
curr->offset + 1,
1,
builder.makeLocalGet(temp, Type::i32),
builder.makeLocalGet(temp, indexType),
Type::i32),
builder.makeConst(int32_t(8)))),
builder.makeBinary(
Expand All @@ -88,7 +89,7 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> {
false,
curr->offset + 2,
1,
builder.makeLocalGet(temp, Type::i32),
builder.makeLocalGet(temp, indexType),
Type::i32),
builder.makeConst(int32_t(16))),
builder.makeBinary(
Expand All @@ -97,7 +98,7 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> {
false,
curr->offset + 3,
1,
builder.makeLocalGet(temp, Type::i32),
builder.makeLocalGet(temp, indexType),
Type::i32),
builder.makeConst(int32_t(24)))));
} else if (curr->align == 2) {
Expand All @@ -107,15 +108,15 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> {
false,
curr->offset,
2,
builder.makeLocalGet(temp, Type::i32),
builder.makeLocalGet(temp, indexType),
Type::i32),
builder.makeBinary(
ShlInt32,
builder.makeLoad(2,
false,
curr->offset + 2,
2,
builder.makeLocalGet(temp, Type::i32),
builder.makeLocalGet(temp, indexType),
Type::i32),
builder.makeConst(int32_t(16))));
} else {
Expand All @@ -134,7 +135,8 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> {
}
Builder builder(*getModule());
assert(curr->value->type == Type::i32);
auto tempPtr = builder.addVar(getFunction(), Type::i32);
auto indexType = getModule()->memory.indexType;
auto tempPtr = builder.addVar(getFunction(), indexType);
auto tempValue = builder.addVar(getFunction(), Type::i32);
auto* block =
builder.makeBlock({builder.makeLocalSet(tempPtr, curr->ptr),
Expand All @@ -144,14 +146,14 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> {
builder.makeStore(1,
curr->offset,
1,
builder.makeLocalGet(tempPtr, Type::i32),
builder.makeLocalGet(tempPtr, indexType),
builder.makeLocalGet(tempValue, Type::i32),
Type::i32));
block->list.push_back(builder.makeStore(
1,
curr->offset + 1,
1,
builder.makeLocalGet(tempPtr, Type::i32),
builder.makeLocalGet(tempPtr, indexType),
builder.makeBinary(ShrUInt32,
builder.makeLocalGet(tempValue, Type::i32),
builder.makeConst(int32_t(8))),
Expand All @@ -162,14 +164,14 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> {
builder.makeStore(1,
curr->offset,
1,
builder.makeLocalGet(tempPtr, Type::i32),
builder.makeLocalGet(tempPtr, indexType),
builder.makeLocalGet(tempValue, Type::i32),
Type::i32));
block->list.push_back(builder.makeStore(
1,
curr->offset + 1,
1,
builder.makeLocalGet(tempPtr, Type::i32),
builder.makeLocalGet(tempPtr, indexType),
builder.makeBinary(ShrUInt32,
builder.makeLocalGet(tempValue, Type::i32),
builder.makeConst(int32_t(8))),
Expand All @@ -178,7 +180,7 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> {
1,
curr->offset + 2,
1,
builder.makeLocalGet(tempPtr, Type::i32),
builder.makeLocalGet(tempPtr, indexType),
builder.makeBinary(ShrUInt32,
builder.makeLocalGet(tempValue, Type::i32),
builder.makeConst(int32_t(16))),
Expand All @@ -187,7 +189,7 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> {
1,
curr->offset + 3,
1,
builder.makeLocalGet(tempPtr, Type::i32),
builder.makeLocalGet(tempPtr, indexType),
builder.makeBinary(ShrUInt32,
builder.makeLocalGet(tempValue, Type::i32),
builder.makeConst(int32_t(24))),
Expand All @@ -197,14 +199,14 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> {
builder.makeStore(2,
curr->offset,
2,
builder.makeLocalGet(tempPtr, Type::i32),
builder.makeLocalGet(tempPtr, indexType),
builder.makeLocalGet(tempValue, Type::i32),
Type::i32));
block->list.push_back(builder.makeStore(
2,
curr->offset + 2,
2,
builder.makeLocalGet(tempPtr, Type::i32),
builder.makeLocalGet(tempPtr, indexType),
builder.makeBinary(ShrUInt32,
builder.makeLocalGet(tempValue, Type::i32),
builder.makeConst(int32_t(16))),
Expand Down Expand Up @@ -254,14 +256,15 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> {
break;
}
// Load two 32-bit pieces, and combine them.
auto temp = builder.addVar(getFunction(), Type::i32);
auto indexType = getModule()->memory.indexType;
auto temp = builder.addVar(getFunction(), indexType);
auto* set = builder.makeLocalSet(temp, curr->ptr);
Expression* low =
lowerLoadI32(builder.makeLoad(4,
false,
curr->offset,
curr->align,
builder.makeLocalGet(temp, Type::i32),
builder.makeLocalGet(temp, indexType),
Type::i32));
low = builder.makeUnary(ExtendUInt32, low);
// Note that the alignment is assumed to be the same here, even though
Expand All @@ -273,7 +276,7 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> {
false,
curr->offset + 4,
curr->align,
builder.makeLocalGet(temp, Type::i32),
builder.makeLocalGet(temp, indexType),
Type::i32));
high = builder.makeUnary(ExtendUInt32, high);
high =
Expand Down Expand Up @@ -332,7 +335,8 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> {
value = builder.makeUnary(ReinterpretFloat64, value);
}
// Store as two 32-bit pieces.
auto tempPtr = builder.addVar(getFunction(), Type::i32);
auto indexType = getModule()->memory.indexType;
auto tempPtr = builder.addVar(getFunction(), indexType);
auto* setPtr = builder.makeLocalSet(tempPtr, curr->ptr);
auto tempValue = builder.addVar(getFunction(), Type::i64);
auto* setValue = builder.makeLocalSet(tempValue, value);
Expand All @@ -342,7 +346,7 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> {
builder.makeStore(4,
curr->offset,
curr->align,
builder.makeLocalGet(tempPtr, Type::i32),
builder.makeLocalGet(tempPtr, indexType),
low,
Type::i32));
Expression* high =
Expand All @@ -358,7 +362,7 @@ struct AlignmentLowering : public WalkerPass<PostWalker<AlignmentLowering>> {
builder.makeStore(4,
curr->offset + 4,
curr->align,
builder.makeLocalGet(tempPtr, Type::i32),
builder.makeLocalGet(tempPtr, indexType),
high,
Type::i32));
replacement = builder.makeBlock({setPtr, setValue, low, high});
Expand Down
9 changes: 6 additions & 3 deletions src/passes/AvoidReinterprets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,13 @@ struct AvoidReinterprets : public WalkerPass<PostWalker<AvoidReinterprets>> {

void optimize(Function* func) {
std::set<Load*> unoptimizables;
auto indexType = getModule()->memory.indexType;
for (auto& pair : infos) {
auto* load = pair.first;
auto& info = pair.second;
if (info.reinterpreted && canReplaceWithReinterpret(load)) {
// We should use another load here, to avoid reinterprets.
info.ptrLocal = Builder::addVar(func, Type::i32);
info.ptrLocal = Builder::addVar(func, indexType);
info.reinterpretedLocal =
Builder::addVar(func, load->type.reinterpret());
} else {
Expand Down Expand Up @@ -176,7 +177,8 @@ struct AvoidReinterprets : public WalkerPass<PostWalker<AvoidReinterprets>> {
auto& info = iter->second;
Builder builder(*module);
auto* ptr = curr->ptr;
curr->ptr = builder.makeLocalGet(info.ptrLocal, Type::i32);
auto indexType = getModule()->memory.indexType;
curr->ptr = builder.makeLocalGet(info.ptrLocal, indexType);
// Note that the other load can have its sign set to false - if the
// original were an integer, the other is a float anyhow; and if
// original were a float, we don't know what sign to use.
Expand All @@ -185,7 +187,7 @@ struct AvoidReinterprets : public WalkerPass<PostWalker<AvoidReinterprets>> {
builder.makeLocalSet(
info.reinterpretedLocal,
makeReinterpretedLoad(
curr, builder.makeLocalGet(info.ptrLocal, Type::i32))),
curr, builder.makeLocalGet(info.ptrLocal, indexType))),
curr}));
}
}
Expand All @@ -201,6 +203,7 @@ struct AvoidReinterprets : public WalkerPass<PostWalker<AvoidReinterprets>> {
}
} finalOptimizer(infos, localGraph, getModule(), getPassOptions());

finalOptimizer.setModule(getModule());
finalOptimizer.walk(func->body);
}
};
Expand Down
17 changes: 11 additions & 6 deletions src/passes/InstrumentMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ struct InstrumentMemory : public WalkerPass<PostWalker<InstrumentMemory>> {
void visitLoad(Load* curr) {
id++;
Builder builder(*getModule());
auto indexType = getModule()->memory.indexType;
auto offset = builder.makeConstPtr(curr->offset.addr);
curr->ptr = builder.makeCall(load_ptr,
{builder.makeConst(int32_t(id)),
builder.makeConst(int32_t(curr->bytes)),
builder.makeConst(int32_t(curr->offset.addr)),
offset,
curr->ptr},
Type::i32);
indexType);
Name target;
switch (curr->type.getBasic()) {
case Type::i32:
Expand All @@ -108,12 +110,14 @@ struct InstrumentMemory : public WalkerPass<PostWalker<InstrumentMemory>> {
void visitStore(Store* curr) {
id++;
Builder builder(*getModule());
auto indexType = getModule()->memory.indexType;
auto offset = builder.makeConstPtr(curr->offset.addr);
curr->ptr = builder.makeCall(store_ptr,
{builder.makeConst(int32_t(id)),
builder.makeConst(int32_t(curr->bytes)),
builder.makeConst(int32_t(curr->offset.addr)),
offset,
curr->ptr},
Type::i32);
indexType);
Name target;
switch (curr->value->type.getBasic()) {
case Type::i32:
Expand All @@ -136,14 +140,15 @@ struct InstrumentMemory : public WalkerPass<PostWalker<InstrumentMemory>> {
}

void visitModule(Module* curr) {
auto indexType = curr->memory.indexType;
addImport(
curr, load_ptr, {Type::i32, Type::i32, Type::i32, Type::i32}, Type::i32);
curr, load_ptr, {Type::i32, Type::i32, indexType, indexType}, indexType);
addImport(curr, load_val_i32, {Type::i32, Type::i32}, Type::i32);
addImport(curr, load_val_i64, {Type::i32, Type::i64}, Type::i64);
addImport(curr, load_val_f32, {Type::i32, Type::f32}, Type::f32);
addImport(curr, load_val_f64, {Type::i32, Type::f64}, Type::f64);
addImport(
curr, store_ptr, {Type::i32, Type::i32, Type::i32, Type::i32}, Type::i32);
curr, store_ptr, {Type::i32, Type::i32, indexType, indexType}, indexType);
addImport(curr, store_val_i32, {Type::i32, Type::i32}, Type::i32);
addImport(curr, store_val_i64, {Type::i32, Type::i64}, Type::i64);
addImport(curr, store_val_f32, {Type::i32, Type::f32}, Type::f32);
Expand Down
Loading