c3c icon indicating copy to clipboard operation
c3c copied to clipboard

wasm: is target_features+mutable-globals+sign-ext required?

Open brentharts opened this issue 1 year ago • 6 comments

python script here to test wasm output sizes of C3 vs Zig. https://github.com/brentharts/c3_vs_zig

I noticed in the simple hello world test the C3 wasm has at the end: \x00,\x0ftarget_features\x02+\x0fmutable-globals+\x08sign-ext

I'm using this python function to strip this away before calling wasm-opt -Oz ... My other tests are still working, so it appears that removing target_features+mutable-globals+sign-ext is safe (tested only in FireFox), when will it break? The Zig wasm does not have this.

When stripped and after wasm-opt, C3 wasm is a few bytes smaller than Zig. Screenshot from 2024-10-25 13-09-03

Screenshot from 2024-10-25 13-11-28

brentharts avatar Oct 25 '24 20:10 brentharts

I don't pass those arguments to LLVM, so it's likely that they form some kind of default? What does Clang output?

lerno avatar Oct 25 '24 21:10 lerno

With emcc -Oz the output wasm does not have: target_features+mutable-globals+sign-ext

brentharts avatar Oct 26 '24 17:10 brentharts

And Zig did not have these either? I see that Zig explicitly adds those to the functions. Can you perhaps run emcc and output some LLVM for the same example and I could look at that one to determine the optimal output. 🙏

lerno avatar Nov 16 '24 01:11 lerno

Screenshot from 2024-11-18 06-48-42

brentharts avatar Nov 18 '24 14:11 brentharts

Screenshot from 2024-11-18 08-38-07

confirmed that zig 0.13 and 0.13 and emcc 3.1.6 do not add target_features+mutable-globals+sign-ext

looking online i found: https://reviews.llvm.org/D88506 """ wasm-ld --features=mutable-globals --export-all

Note that this will not export linker-generated mutable globals unless the resulting binaryen already includes the 'mutable-globals' features since that would otherwise create and invalid binaryen. """

https://clang.llvm.org/doxygen/Basic_2Targets_2WebAssembly_8cpp_source.html '''c++ bool WebAssemblyTargetInfo::hasFeature(StringRef Feature) const { return llvm::StringSwitch(Feature) .Case("atomics", HasAtomics) .Case("bulk-memory", HasBulkMemory) .Case("exception-handling", HasExceptionHandling) .Case("extended-const", HasExtendedConst) .Case("half-precision", HasHalfPrecision) .Case("multimemory", HasMultiMemory) .Case("multivalue", HasMultivalue) .Case("mutable-globals", HasMutableGlobals) .Case("nontrapping-fptoint", HasNontrappingFPToInt) .Case("reference-types", HasReferenceTypes) .Case("relaxed-simd", SIMDLevel >= RelaxedSIMD) .Case("sign-ext", HasSignExt) .Case("simd128", SIMDLevel >= SIMD128) .Case("tail-call", HasTailCall) .Default(false); } '''

https://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20190408/643578.html

brentharts avatar Nov 18 '24 16:11 brentharts

It looks like if mutable-globals and sign-ext isn't added to all functions it's added during linking then?

lerno avatar Nov 18 '24 21:11 lerno