Skip to content

Commit a5547a5

Browse files
authored
Clean up loose ends in feature handling (#2203)
Fix and test mutable globals support, replace string literals with constants, and add a pass to emit the target features section.
1 parent 23d8497 commit a5547a5

File tree

8 files changed

+44
-9
lines changed

8 files changed

+44
-9
lines changed

src/passes/StripTargetFeatures.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919
namespace wasm {
2020

2121
struct StripTargetFeatures : public Pass {
22+
bool isStripped = false;
23+
StripTargetFeatures(bool isStripped) : isStripped(isStripped) {}
2224
void run(PassRunner* runner, Module* module) override {
23-
module->hasFeaturesSection = false;
25+
module->hasFeaturesSection = !isStripped;
2426
}
2527
};
2628

27-
Pass* createStripTargetFeaturesPass() { return new StripTargetFeatures(); }
29+
Pass* createStripTargetFeaturesPass() { return new StripTargetFeatures(true); }
30+
Pass* createEmitTargetFeaturesPass() { return new StripTargetFeatures(false); }
2831

2932
} // namespace wasm

src/passes/pass.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ void PassRegistry::registerPasses() {
109109
registerPass("duplicate-function-elimination",
110110
"removes duplicate functions",
111111
createDuplicateFunctionEliminationPass);
112+
registerPass("emit-target-features",
113+
"emit the target features section in the output",
114+
createEmitTargetFeaturesPass);
112115
registerPass("extract-function",
113116
"leaves just one function (useful for debugging)",
114117
createExtractFunctionPass);

src/passes/passes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Pass* createDataFlowOptsPass();
3636
Pass* createDeadCodeEliminationPass();
3737
Pass* createDirectizePass();
3838
Pass* createDuplicateFunctionEliminationPass();
39+
Pass* createEmitTargetFeaturesPass();
3940
Pass* createExtractFunctionPass();
4041
Pass* createFlattenPass();
4142
Pass* createFuncCastEmulationPass();

src/wasm-binary.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ extern const char* TargetFeatures;
400400
extern const char* AtomicsFeature;
401401
extern const char* BulkMemoryFeature;
402402
extern const char* ExceptionHandlingFeature;
403+
extern const char* MutableGlobalsFeature;
403404
extern const char* TruncSatFeature;
404405
extern const char* SignExtFeature;
405406
extern const char* SIMD128Feature;

src/wasm/wasm-binary.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -666,19 +666,19 @@ void WasmBinaryWriter::writeFeaturesSection() {
666666
auto toString = [](FeatureSet::Feature f) {
667667
switch (f) {
668668
case FeatureSet::Atomics:
669-
return "atomics";
669+
return BinaryConsts::UserSections::AtomicsFeature;
670670
case FeatureSet::MutableGlobals:
671-
return "mutable-globals";
671+
return BinaryConsts::UserSections::MutableGlobalsFeature;
672672
case FeatureSet::TruncSat:
673-
return "nontrapping-fptoint";
673+
return BinaryConsts::UserSections::TruncSatFeature;
674674
case FeatureSet::SIMD:
675-
return "simd128";
675+
return BinaryConsts::UserSections::SIMD128Feature;
676676
case FeatureSet::BulkMemory:
677-
return "bulk-memory";
677+
return BinaryConsts::UserSections::BulkMemoryFeature;
678678
case FeatureSet::SignExt:
679-
return "sign-ext";
679+
return BinaryConsts::UserSections::SignExtFeature;
680680
case FeatureSet::ExceptionHandling:
681-
return "exception-handling";
681+
return BinaryConsts::UserSections::ExceptionHandlingFeature;
682682
default:
683683
WASM_UNREACHABLE();
684684
}
@@ -2154,6 +2154,8 @@ void WasmBinaryBuilder::readFeatures(size_t payloadLen) {
21542154
wasm.features.setBulkMemory();
21552155
} else if (name == BinaryConsts::UserSections::ExceptionHandlingFeature) {
21562156
wasm.features.setExceptionHandling();
2157+
} else if (name == BinaryConsts::UserSections::MutableGlobalsFeature) {
2158+
wasm.features.setMutableGlobals();
21572159
} else if (name == BinaryConsts::UserSections::TruncSatFeature) {
21582160
wasm.features.setTruncSat();
21592161
} else if (name == BinaryConsts::UserSections::SignExtFeature) {

src/wasm/wasm.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const char* TargetFeatures = "target_features";
3636
const char* AtomicsFeature = "atomics";
3737
const char* BulkMemoryFeature = "bulk-memory";
3838
const char* ExceptionHandlingFeature = "exception-handling";
39+
const char* MutableGlobalsFeature = "mutable-globals";
3940
const char* TruncSatFeature = "nontrapping-fptoint";
4041
const char* SignExtFeature = "sign-ext";
4142
const char* SIMD128Feature = "simd128";
101 Bytes
Binary file not shown.

test/unit/test_features.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ def test_nontrapping_fptoint(self):
137137
self.check_features(filename, ['nontrapping-float-to-int'])
138138
self.assertIn('i32.trunc_sat_f32_u', self.disassemble(filename))
139139

140+
def test_mutable_globals(self):
141+
filename = 'mutable_globals_target_feature.wasm'
142+
self.roundtrip(filename)
143+
self.check_features(filename, ['mutable-globals'])
144+
self.assertIn('(import "env" "global-mut" (global $gimport$0 (mut i32)))',
145+
self.disassemble(filename))
146+
140147
def test_sign_ext(self):
141148
filename = 'signext_target_feature.wasm'
142149
self.roundtrip(filename)
@@ -173,3 +180,20 @@ def test_incompatible_features_forced(self):
173180
def test_explicit_detect_features(self):
174181
self.check_features('signext_target_feature.wasm', ['sign-ext', 'simd'],
175182
opts=['-mvp', '--detect-features', '--enable-simd'])
183+
184+
def test_emit_all_features(self):
185+
p = run_process(WASM_OPT + ['--emit-target-features', '-all', '-o', '-'],
186+
input="(module)", check=False, capture_output=True)
187+
self.assertEqual(p.returncode, 0)
188+
p2 = run_process(WASM_OPT + ['--print-features', '-o', os.devnull],
189+
input=p.stdout, check=False, capture_output=True)
190+
self.assertEqual(p2.returncode, 0)
191+
self.assertEqual(p2.stdout.split(), [
192+
'--enable-threads',
193+
'--enable-bulk-memory',
194+
'--enable-exception-handling',
195+
'--enable-mutable-globals',
196+
'--enable-nontrapping-float-to-int',
197+
'--enable-sign-ext',
198+
'--enable-simd',
199+
])

0 commit comments

Comments
 (0)