Skip to content

Commit e1d74ef

Browse files
authored
Add GC feature flag (#3135)
Adds the `--enable-gc` feature flag, so far enabling the `anyref` type incl. subtyping, and removes the temporary `--enable-anyref` feature flag that it replaces.
1 parent a96e831 commit e1d74ef

22 files changed

+64
-148
lines changed

scripts/fuzz_opt.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ def can_run(self, wasm):
404404
if random.random() < 0.5:
405405
return False
406406
# wasm2c doesn't support most features
407-
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']])
407+
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-gc']])
408408

409409
def run(self, wasm):
410410
run([in_bin('wasm-opt'), wasm, '--emit-wasm2c-wrapper=main.c'] + FEATURE_OPTS)
@@ -504,7 +504,7 @@ def compare_before_and_after(self, before, after):
504504
compare(before[vm], after[vm], 'CompareVMs between before and after: ' + vm.name)
505505

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

509509

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

637637
def can_run_on_feature_opts(self, feature_opts):
638-
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']])
638+
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-gc']])
639639

640640

641641
class Asyncify(TestCaseHandler):
@@ -689,7 +689,7 @@ def do_asyncify(wasm):
689689
compare(before, after_asyncify, 'Asyncify (before/after_asyncify)')
690690

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

694694

695695
# The global list of all test case handlers
@@ -875,7 +875,7 @@ def randomize_opt_flags():
875875
# some features depend on other features, so if a required feature is
876876
# disabled, its dependent features need to be disabled as well.
877877
IMPLIED_FEATURE_OPTS = {
878-
'--disable-reference-types': ['--disable-exception-handling', '--disable-anyref']
878+
'--disable-reference-types': ['--disable-exception-handling', '--disable-gc']
879879
}
880880

881881
if __name__ == '__main__':

src/binaryen-c.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,8 @@ BinaryenFeatures BinaryenFeatureReferenceTypes(void) {
327327
BinaryenFeatures BinaryenFeatureMultivalue(void) {
328328
return static_cast<BinaryenFeatures>(FeatureSet::Multivalue);
329329
}
330-
BinaryenFeatures BinaryenFeatureAnyref(void) {
331-
return static_cast<BinaryenFeatures>(FeatureSet::Anyref);
330+
BinaryenFeatures BinaryenFeatureGC(void) {
331+
return static_cast<BinaryenFeatures>(FeatureSet::GC);
332332
}
333333
BinaryenFeatures BinaryenFeatureAll(void) {
334334
return static_cast<BinaryenFeatures>(FeatureSet::All);

src/binaryen-c.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ BINARYEN_API BinaryenFeatures BinaryenFeatureExceptionHandling(void);
197197
BINARYEN_API BinaryenFeatures BinaryenFeatureTailCall(void);
198198
BINARYEN_API BinaryenFeatures BinaryenFeatureReferenceTypes(void);
199199
BINARYEN_API BinaryenFeatures BinaryenFeatureMultivalue(void);
200-
BINARYEN_API BinaryenFeatures BinaryenFeatureAnyref(void);
200+
BINARYEN_API BinaryenFeatures BinaryenFeatureGC(void);
201201
BINARYEN_API BinaryenFeatures BinaryenFeatureAll(void);
202202

203203
// Modules

src/js/binaryen.js-post.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ function initializeConstants() {
121121
'TailCall',
122122
'ReferenceTypes',
123123
'Multivalue',
124-
'Anyref',
124+
'GC',
125125
'All'
126126
].forEach(name => {
127127
Module['Features'][name] = Module['_BinaryenFeature' + name]();

src/passes/InstrumentLocals.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ struct InstrumentLocals : public WalkerPass<PostWalker<InstrumentLocals>> {
198198
addImport(
199199
curr, set_exnref, {Type::i32, Type::i32, Type::exnref}, Type::exnref);
200200
}
201-
if (curr->features.hasAnyref()) {
201+
if (curr->features.hasGC()) {
202202
addImport(
203203
curr, get_anyref, {Type::i32, Type::i32, Type::anyref}, Type::anyref);
204204
addImport(

src/tools/fuzzing.h

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -848,8 +848,7 @@ class TranslateToFuzzReader {
848848
}
849849

850850
Expression* _makeConcrete(Type type) {
851-
bool canMakeControlFlow =
852-
!type.isTuple() || wasm.features.has(FeatureSet::Multivalue);
851+
bool canMakeControlFlow = !type.isTuple() || wasm.features.hasMultivalue();
853852
using Self = TranslateToFuzzReader;
854853
FeatureOptions<Expression* (Self::*)(Type)> options;
855854
using WeightedOption = decltype(options)::WeightedOption;
@@ -2609,16 +2608,7 @@ class TranslateToFuzzReader {
26092608
Expression* makeRefIsNull(Type type) {
26102609
assert(type == Type::i32);
26112610
assert(wasm.features.hasReferenceTypes());
2612-
SmallVector<Type, 2> options;
2613-
options.push_back(Type::externref);
2614-
options.push_back(Type::funcref);
2615-
if (wasm.features.hasExceptionHandling()) {
2616-
options.push_back(Type::exnref);
2617-
}
2618-
if (wasm.features.hasAnyref()) {
2619-
options.push_back(Type::anyref);
2620-
}
2621-
return builder.makeRefIsNull(make(pick(options)));
2611+
return builder.makeRefIsNull(make(getReferenceType()));
26222612
}
26232613

26242614
Expression* makeMemoryInit() {
@@ -2684,11 +2674,22 @@ class TranslateToFuzzReader {
26842674
.add(FeatureSet::ReferenceTypes, Type::funcref, Type::externref)
26852675
.add(FeatureSet::ReferenceTypes | FeatureSet::ExceptionHandling,
26862676
Type::exnref)
2687-
.add(FeatureSet::ReferenceTypes | FeatureSet::Anyref, Type::anyref));
2677+
.add(FeatureSet::ReferenceTypes | FeatureSet::GC, Type::anyref));
26882678
}
26892679

26902680
Type getSingleConcreteType() { return pick(getSingleConcreteTypes()); }
26912681

2682+
std::vector<Type> getReferenceTypes() {
2683+
return items(
2684+
FeatureOptions<Type>()
2685+
.add(FeatureSet::ReferenceTypes, Type::funcref, Type::externref)
2686+
.add(FeatureSet::ReferenceTypes | FeatureSet::ExceptionHandling,
2687+
Type::exnref)
2688+
.add(FeatureSet::ReferenceTypes | FeatureSet::GC, Type::anyref));
2689+
}
2690+
2691+
Type getReferenceType() { return pick(getReferenceTypes()); }
2692+
26922693
Type getTupleType() {
26932694
std::vector<Type> elements;
26942695
size_t numElements = 2 + upTo(MAX_TUPLE_SIZE - 1);

src/tools/tool-options.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ struct ToolOptions : public Options {
8787
.addFeature(FeatureSet::TailCall, "tail call operations")
8888
.addFeature(FeatureSet::ReferenceTypes, "reference types")
8989
.addFeature(FeatureSet::Multivalue, "multivalue functions")
90-
.addFeature(FeatureSet::Anyref, "anyref type (without GC)")
90+
.addFeature(FeatureSet::GC, "garbage collection")
9191
.add("--no-validation",
9292
"-n",
9393
"Disables validation, assumes inputs are correct",

src/wasm-binary.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ extern const char* ExceptionHandlingFeature;
383383
extern const char* TailCallFeature;
384384
extern const char* ReferenceTypesFeature;
385385
extern const char* MultivalueFeature;
386-
extern const char* AnyrefFeature;
386+
extern const char* GCFeature;
387387

388388
enum Subsection {
389389
NameModule = 0,

src/wasm-features.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct FeatureSet {
3636
TailCall = 1 << 7,
3737
ReferenceTypes = 1 << 8,
3838
Multivalue = 1 << 9,
39-
Anyref = 1 << 10,
39+
GC = 1 << 10,
4040
All = (1 << 11) - 1
4141
};
4242

@@ -62,8 +62,8 @@ struct FeatureSet {
6262
return "reference-types";
6363
case Multivalue:
6464
return "multivalue";
65-
case Anyref:
66-
return "anyref";
65+
case GC:
66+
return "gc";
6767
default:
6868
WASM_UNREACHABLE("unexpected feature");
6969
}
@@ -87,7 +87,7 @@ struct FeatureSet {
8787
bool hasTailCall() const { return (features & TailCall) != 0; }
8888
bool hasReferenceTypes() const { return (features & ReferenceTypes) != 0; }
8989
bool hasMultivalue() const { return (features & Multivalue) != 0; }
90-
bool hasAnyref() const { return (features & Anyref) != 0; }
90+
bool hasGC() const { return (features & GC) != 0; }
9191
bool hasAll() const { return (features & All) != 0; }
9292

9393
void makeMVP() { features = MVP; }
@@ -104,7 +104,7 @@ struct FeatureSet {
104104
void setTailCall(bool v = true) { set(TailCall, v); }
105105
void setReferenceTypes(bool v = true) { set(ReferenceTypes, v); }
106106
void setMultivalue(bool v = true) { set(Multivalue, v); }
107-
void setAnyref(bool v = true) { set(Anyref, v); }
107+
void setGC(bool v = true) { set(GC, v); }
108108
void setAll(bool v = true) { features = v ? All : MVP; }
109109

110110
void enable(const FeatureSet& other) { features |= other.features; }

src/wasm/wasm-binary.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -750,8 +750,8 @@ void WasmBinaryWriter::writeFeaturesSection() {
750750
return BinaryConsts::UserSections::ReferenceTypesFeature;
751751
case FeatureSet::Multivalue:
752752
return BinaryConsts::UserSections::MultivalueFeature;
753-
case FeatureSet::Anyref:
754-
return BinaryConsts::UserSections::AnyrefFeature;
753+
case FeatureSet::GC:
754+
return BinaryConsts::UserSections::GCFeature;
755755
default:
756756
WASM_UNREACHABLE("unexpected feature flag");
757757
}
@@ -2307,8 +2307,8 @@ void WasmBinaryBuilder::readFeatures(size_t payloadLen) {
23072307
wasm.features.setReferenceTypes();
23082308
} else if (name == BinaryConsts::UserSections::MultivalueFeature) {
23092309
wasm.features.setMultivalue();
2310-
} else if (name == BinaryConsts::UserSections::AnyrefFeature) {
2311-
wasm.features.setAnyref();
2310+
} else if (name == BinaryConsts::UserSections::GCFeature) {
2311+
wasm.features.setGC();
23122312
}
23132313
}
23142314
}

0 commit comments

Comments
 (0)