Skip to content

EM_JS emits an odd data section to compiled code #12778

Closed

Description

@juj

STR:

emcc tests\minimal_hello.c -o a.html -s MINIMAL_RUNTIME=1 -s ENVIRONMENT=web -s SUPPORT_ERRNO=0 -s MINIFY_HTML=0 -s TEXTDECODER=2 -O3 -g1 --closure 1 --profiling-funcs

emits

(module
 (type $none_=>_none (func))
 (type $i32_=>_none (func (param i32)))
 (type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
 (import "a" "a" (memory $1 256 256))
 (data (i32.const 1024) "(char* str)<::>{ console.log(UTF8ToString(str)); }\00minimal hello!")
 (import "a" "b" (func $console_log (param i32)))
 (table $0 1 1 funcref)
 (export "c" (table $0))
 (export "d" (func $__wasm_call_ctors))
 (export "e" (func $main))
 (func $main (; 1 ;) (param $0 i32) (param $1 i32) (result i32)
  (call $console_log
   (i32.const 1075)
  )
  (i32.const 0)
 )
 (func $__wasm_call_ctors (; 2 ;)
  (nop)
 )
)
var c;
c || (c = Module);
var d = new TextDecoder("utf8");
function e(a) {
  if (!a) {
    return "";
  }
  for (var k = a + NaN, b = a; !(b >= k) && f[b];) {
    ++b;
  }
  return d.decode(f.subarray(a, b));
}
var g = new WebAssembly.Memory({initial:256, maximum:256}), f = new Uint8Array(g.buffer), h;
WebAssembly.instantiate(c.wasm, {a:{b:function(a) {
  console.log(e(a));
}, a:g}}).then(function(a) {
  a = a.instance.exports;
  h = a.e;
  a.d();
  h();
});

Note how the EM_JS function is properly imported as a JS function (b:function(a) { console.log(e(a)); }, but there is also this

 (data (i32.const 1024) "(char* str)<::>{ console.log(UTF8ToString(str)); }\00minimal hello!")

That seems to be accidental?

Compare tests/minimal_hello.c to

void console_log(const char *str);
int main()
{
	console_log("minimal hello!");
}
mergeInto(LibraryManager.library, {
	console_log: function(str) {
		console.log(UTF8ToString(str));
	}
});

which with

emcc a.c --js-library library_a.js -o a.html -s MINIMAL_RUNTIME=1 -s ENVIRONMENT=web -s SUPPORT_ERRNO=0 -s MINIFY_HTML=0 -s TEXTDECODER=2 -O3 -g1 --closure 1 --profiling-funcs

generates a cleaner

(module
 (type $none_=>_none (func))
 (type $i32_=>_none (func (param i32)))
 (type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
 (import "a" "a" (memory $1 256 256))
 (data (i32.const 1024) "minimal hello!")
 (import "a" "b" (func $console_log (param i32)))
 (table $0 1 1 funcref)
 (export "c" (table $0))
 (export "d" (func $__wasm_call_ctors))
 (export "e" (func $main))
 (func $main (; 1 ;) (param $0 i32) (param $1 i32) (result i32)
  (call $console_log
   (i32.const 1024)
  )
  (i32.const 0)
 )
 (func $__wasm_call_ctors (; 2 ;)
  (nop)
 )
)

without the excess "(char* str)<::>{ console.log(UTF8ToString(str)); }\00"

Activity

  1. sbc100 commented on Nov 14, 2020

    @sbc100
    Collaborator

    This is just how EM_JS and EM_ASM have always worked in the wasm world I believe.

    The problem stems from the fact that EM_JS/EM_ASM are basically just static data that is part of the wasm binary and binaryen has no ability to remove of DCE static data in any way.

    I thought we had an open bug to look into this bug I didn't find it on using a quick search.

    Jacob and I talked about trying to find a way to do this, but its pretty tricky. As of today EM_JS/EM_ASM strings are just normal C strings that combined with all other static data into a single data segment by wasm-ld. The first step in working on this I think would be at least to put all EM_ASM/EM_JS data in its own segment, and have have the linker leave that section separate. Ideally wasm-emscripten-finalize, when it extracts the strings and passes them back to the JS compiler would somehow then be able to DCE the strings. However the data layout of the wasm memory is basically finalized at wasm-ld time. We don't have any relocation information available after that point so it not really possible to move data around. One option is that we can at least drop the data segment itself which would at least save on file size. Ideally we could also adjust the stack and heap pointer downwards when we do this.that

  2. kripken commented on Nov 14, 2020

    @kripken
    Member

    @sbc100 If we used globals for data segments as you've recently suggested (and relaxing them in binaryen) could this be easier to do?

  3. sbc100 commented on Nov 15, 2020

    @sbc100
    Collaborator

    I found the older issue: #9366

    Can we close this one as a duplicate of #9366?

  4. sbc100 commented on Feb 8, 2021

    @sbc100
    Collaborator

    Closing in favor of #9366... I'm also working a fix that looks promising.

  5. juj commented on Dec 13, 2021

    @juj
    CollaboratorAuthor

    Closing as fixed in #13443.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions