Skip to content
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

Add anyref feature and type #3109

Merged
merged 14 commits into from
Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 4 additions & 4 deletions scripts/fuzz_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ def can_run(self, wasm):
if random.random() < 0.5:
return False
# wasm2c doesn't support most features
return all([x in FEATURE_OPTS for x in ['--disable-exception-handling', '--disable-simd', '--disable-threads', '--disable-bulk-memory', '--disable-nontrapping-float-to-int', '--disable-tail-call', '--disable-sign-ext', '--disable-reference-types', '--disable-multivalue']])
return all([x in FEATURE_OPTS for x in ['--disable-exception-handling', '--disable-simd', '--disable-threads', '--disable-bulk-memory', '--disable-nontrapping-float-to-int', '--disable-tail-call', '--disable-sign-ext', '--disable-reference-types', '--disable-multivalue', '--disable-anyref']])

def run(self, wasm):
run([in_bin('wasm-opt'), wasm, '--emit-wasm2c-wrapper=main.c'] + FEATURE_OPTS)
Expand Down Expand Up @@ -502,7 +502,7 @@ def compare_before_and_after(self, before, after):
compare(before[vm], after[vm], 'CompareVMs between before and after: ' + vm.name)

def can_run_on_feature_opts(self, feature_opts):
return all([x in feature_opts for x in ['--disable-simd', '--disable-reference-types', '--disable-exception-handling', '--disable-multivalue']])
return all([x in feature_opts for x in ['--disable-simd', '--disable-reference-types', '--disable-exception-handling', '--disable-multivalue', '--disable-anyref']])


# Check for determinism - the same command must have the same output.
Expand Down Expand Up @@ -633,7 +633,7 @@ def run(self, wasm):
return run_vm([shared.NODEJS, js_file, 'a.wasm'])

def can_run_on_feature_opts(self, feature_opts):
return all([x in feature_opts for x in ['--disable-exception-handling', '--disable-simd', '--disable-threads', '--disable-bulk-memory', '--disable-nontrapping-float-to-int', '--disable-tail-call', '--disable-sign-ext', '--disable-reference-types', '--disable-multivalue']])
return all([x in feature_opts for x in ['--disable-exception-handling', '--disable-simd', '--disable-threads', '--disable-bulk-memory', '--disable-nontrapping-float-to-int', '--disable-tail-call', '--disable-sign-ext', '--disable-reference-types', '--disable-multivalue', '--disable-anyref']])


class Asyncify(TestCaseHandler):
Expand Down Expand Up @@ -687,7 +687,7 @@ def do_asyncify(wasm):
compare(before, after_asyncify, 'Asyncify (before/after_asyncify)')

def can_run_on_feature_opts(self, feature_opts):
return all([x in feature_opts for x in ['--disable-exception-handling', '--disable-simd', '--disable-tail-call', '--disable-reference-types', '--disable-multivalue']])
return all([x in feature_opts for x in ['--disable-exception-handling', '--disable-simd', '--disable-tail-call', '--disable-reference-types', '--disable-multivalue', '--disable-anyref']])


# The global list of all test case handlers
Expand Down
1 change: 1 addition & 0 deletions scripts/gen-s-parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
("funcref.pop", "makePop(Type::funcref)"),
("externref.pop", "makePop(Type::externref)"),
("exnref.pop", "makePop(Type::exnref)"),
("anyref.pop", "makePop(Type::anyref)"),
("i32.load", "makeLoad(s, Type::i32, /*isAtomic=*/false)"),
("i64.load", "makeLoad(s, Type::i64, /*isAtomic=*/false)"),
("f32.load", "makeLoad(s, Type::f32, /*isAtomic=*/false)"),
Expand Down
3 changes: 3 additions & 0 deletions src/asmjs/asm_v_wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ AsmType wasmToAsmType(Type type) {
case Type::funcref:
case Type::externref:
case Type::exnref:
case Type::anyref:
assert(false && "reference types are not supported by asm2wasm");
case Type::none:
return ASM_NONE;
Expand Down Expand Up @@ -85,6 +86,8 @@ char getSig(Type type) {
return 'X';
case Type::exnref:
return 'E';
case Type::anyref:
return 'A';
case Type::none:
return 'v';
case Type::unreachable:
Expand Down
6 changes: 6 additions & 0 deletions src/binaryen-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ BinaryenLiteral toBinaryenLiteral(Literal x) {
break;
case Type::externref:
case Type::exnref:
case Type::anyref:
assert(x.isNull());
break;
case Type::none:
Expand All @@ -98,6 +99,7 @@ Literal fromBinaryenLiteral(BinaryenLiteral x) {
return Literal::makeFunc(x.func);
case Type::externref:
case Type::exnref:
case Type::anyref:
return Literal::makeNull(Type(x.type));
case Type::none:
case Type::unreachable:
Expand Down Expand Up @@ -133,6 +135,7 @@ BinaryenType BinaryenTypeVec128(void) { return Type::v128; }
BinaryenType BinaryenTypeFuncref(void) { return Type::funcref; }
BinaryenType BinaryenTypeExternref(void) { return Type::externref; }
BinaryenType BinaryenTypeExnref(void) { return Type::exnref; }
BinaryenType BinaryenTypeAnyref(void) { return Type::anyref; }
BinaryenType BinaryenTypeUnreachable(void) { return Type::unreachable; }
BinaryenType BinaryenTypeAuto(void) { return uintptr_t(-1); }

Expand Down Expand Up @@ -324,6 +327,9 @@ BinaryenFeatures BinaryenFeatureReferenceTypes(void) {
BinaryenFeatures BinaryenFeatureMultivalue(void) {
return static_cast<BinaryenFeatures>(FeatureSet::Multivalue);
}
BinaryenFeatures BinaryenFeatureAnyref(void) {
return static_cast<BinaryenFeatures>(FeatureSet::Anyref);
}
BinaryenFeatures BinaryenFeatureAll(void) {
return static_cast<BinaryenFeatures>(FeatureSet::All);
}
Expand Down
2 changes: 2 additions & 0 deletions src/binaryen-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ BINARYEN_API BinaryenType BinaryenTypeVec128(void);
BINARYEN_API BinaryenType BinaryenTypeFuncref(void);
BINARYEN_API BinaryenType BinaryenTypeExternref(void);
BINARYEN_API BinaryenType BinaryenTypeExnref(void);
BINARYEN_API BinaryenType BinaryenTypeAnyref(void);
BINARYEN_API BinaryenType BinaryenTypeUnreachable(void);
// Not a real type. Used as the last parameter to BinaryenBlock to let
// the API figure out the type instead of providing one.
Expand Down Expand Up @@ -196,6 +197,7 @@ BINARYEN_API BinaryenFeatures BinaryenFeatureExceptionHandling(void);
BINARYEN_API BinaryenFeatures BinaryenFeatureTailCall(void);
BINARYEN_API BinaryenFeatures BinaryenFeatureReferenceTypes(void);
BINARYEN_API BinaryenFeatures BinaryenFeatureMultivalue(void);
BINARYEN_API BinaryenFeatures BinaryenFeatureAnyref(void);
BINARYEN_API BinaryenFeatures BinaryenFeatureAll(void);

// Modules
Expand Down
18 changes: 13 additions & 5 deletions src/gen-s-parser.inc
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@ char op[27] = {'\0'};
strncpy(op, s[0]->c_str(), 26);
switch (op[0]) {
case 'a': {
switch (op[7]) {
case 'f':
if (strcmp(op, "atomic.fence") == 0) { return makeAtomicFence(s); }
goto parse_error;
switch (op[1]) {
case 'n':
if (strcmp(op, "atomic.notify") == 0) { return makeAtomicNotify(s); }
if (strcmp(op, "anyref.pop") == 0) { return makePop(Type::anyref); }
goto parse_error;
case 't': {
switch (op[7]) {
case 'f':
if (strcmp(op, "atomic.fence") == 0) { return makeAtomicFence(s); }
goto parse_error;
case 'n':
if (strcmp(op, "atomic.notify") == 0) { return makeAtomicNotify(s); }
goto parse_error;
default: goto parse_error;
}
}
default: goto parse_error;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/ir/abstract.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ inline UnaryOp getUnary(Type type, Op op) {
case Type::funcref:
case Type::externref:
case Type::exnref:
case Type::anyref:
case Type::none:
case Type::unreachable: {
return InvalidUnary;
Expand Down Expand Up @@ -268,6 +269,7 @@ inline BinaryOp getBinary(Type type, Op op) {
case Type::funcref:
case Type::externref:
case Type::exnref:
case Type::anyref:
case Type::none:
case Type::unreachable: {
return InvalidBinary;
Expand Down
8 changes: 8 additions & 0 deletions src/js/binaryen.js-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function initializeConstants() {
['funcref', 'Funcref'],
['externref', 'Externref'],
['exnref', 'Exnref'],
['anyref', 'Anyref'],
['unreachable', 'Unreachable'],
['auto', 'Auto']
].forEach(entry => {
Expand Down Expand Up @@ -120,6 +121,7 @@ function initializeConstants() {
'TailCall',
'ReferenceTypes',
'Multivalue',
'Anyref',
'All'
].forEach(name => {
Module['Features'][name] = Module['_BinaryenFeature' + name]();
Expand Down Expand Up @@ -2063,6 +2065,12 @@ function wrapModule(module, self = {}) {
}
};

self['anyref'] = {
'pop'() {
return Module['_BinaryenPop'](module, Module['anyref']);
}
};

self['ref'] = {
'null'(type) {
return Module['_BinaryenRefNull'](module, type);
Expand Down
1 change: 1 addition & 0 deletions src/literal.h
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ template<> struct less<wasm::Literal> {
case wasm::Type::funcref:
case wasm::Type::externref:
case wasm::Type::exnref:
case wasm::Type::anyref:
case wasm::Type::none:
case wasm::Type::unreachable:
return false;
Expand Down
1 change: 1 addition & 0 deletions src/parsing.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ parseConst(cashew::IString s, Type type, MixedArena& allocator) {
case Type::funcref:
case Type::externref:
case Type::exnref:
case Type::anyref:
WASM_UNREACHABLE("unexpected const type");
case Type::none:
case Type::unreachable: {
Expand Down
3 changes: 2 additions & 1 deletion src/passes/ConstHoisting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ struct ConstHoisting : public WalkerPass<PostWalker<ConstHoisting>> {
case Type::v128:
case Type::funcref:
case Type::externref:
case Type::exnref: {
case Type::exnref:
case Type::anyref: {
return false;
}
case Type::none:
Expand Down
6 changes: 4 additions & 2 deletions src/passes/FuncCastEmulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ static Expression* toABI(Expression* value, Module* module) {
}
case Type::funcref:
case Type::externref:
case Type::exnref: {
case Type::exnref:
case Type::anyref: {
WASM_UNREACHABLE("reference types cannot be converted to i64");
}
case Type::none: {
Expand Down Expand Up @@ -110,7 +111,8 @@ static Expression* fromABI(Expression* value, Type type, Module* module) {
}
case Type::funcref:
case Type::externref:
case Type::exnref: {
case Type::exnref:
case Type::anyref: {
WASM_UNREACHABLE("reference types cannot be converted from i64");
}
case Type::none: {
Expand Down
14 changes: 14 additions & 0 deletions src/passes/InstrumentLocals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Name get_f64("get_f64");
Name get_funcref("get_funcref");
Name get_externref("get_externref");
Name get_exnref("get_exnref");
Name get_anyref("get_anyref");
Name get_v128("get_v128");

Name set_i32("set_i32");
Expand All @@ -68,6 +69,7 @@ Name set_f64("set_f64");
Name set_funcref("set_funcref");
Name set_externref("set_externref");
Name set_exnref("set_exnref");
Name set_anyref("set_anyref");
Name set_v128("set_v128");

struct InstrumentLocals : public WalkerPass<PostWalker<InstrumentLocals>> {
Expand Down Expand Up @@ -99,6 +101,9 @@ struct InstrumentLocals : public WalkerPass<PostWalker<InstrumentLocals>> {
case Type::exnref:
import = get_exnref;
break;
case Type::anyref:
import = get_anyref;
break;
case Type::none:
case Type::unreachable:
WASM_UNREACHABLE("unexpected type");
Expand Down Expand Up @@ -145,6 +150,9 @@ struct InstrumentLocals : public WalkerPass<PostWalker<InstrumentLocals>> {
case Type::exnref:
import = set_exnref;
break;
case Type::anyref:
import = set_anyref;
break;
case Type::unreachable:
return; // nothing to do here
case Type::none:
Expand Down Expand Up @@ -191,6 +199,12 @@ struct InstrumentLocals : public WalkerPass<PostWalker<InstrumentLocals>> {
addImport(
curr, set_exnref, {Type::i32, Type::i32, Type::exnref}, Type::exnref);
}
if (curr->features.hasAnyref()) {
addImport(
curr, get_anyref, {Type::i32, Type::i32, Type::anyref}, Type::anyref);
addImport(
curr, set_anyref, {Type::i32, Type::i32, Type::anyref}, Type::anyref);
}
if (curr->features.hasSIMD()) {
addImport(curr, get_v128, {Type::i32, Type::i32, Type::v128}, Type::v128);
addImport(curr, set_v128, {Type::i32, Type::i32, Type::v128}, Type::v128);
Expand Down
1 change: 1 addition & 0 deletions src/shell-interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ struct ShellExternalInterface : ModuleInstance::ExternalInterface {
case Type::funcref:
case Type::externref:
case Type::exnref:
case Type::anyref:
globals[import->name] = {Literal::makeNull(import->type)};
break;
case Type::none:
Expand Down
Loading