Clang driver: can target wasm32 but can’t look up symbols?

28 views
Skip to first unread message

Alan Garny

unread,
Jun 8, 2023, 8:03:27 PM6/8/23
to emscripten-discuss
Hi,

I have a C++ library that compiles some code on the fly using an ORC JIT and it all works fine when my library is built for X86 or AArch64, but not when it is built for wasm32Ā (usingĀ wasm32-unknown-unknown-elf as the target triple). ForĀ wasm32, it looks like the code gets compiled but I can't look up its symbolsĀ and therefore can't run that code.

In my browser’s console, I am getting:
libopencor.js:8 >>> Compiler::function("initialiseVariables")
libopencor.js:8 Ā  Ā  Ā - Looking up 'initialiseVariables'...
libopencor.js:8 LLVM ERROR: section already has a defining function: .text.initialiseVariables
put_char @ libopencor.js:8
libopencor.js:8 Aborted()
abort @ libopencor.js:8
libopencor.js:8 Uncaught (in promise) RuntimeError: Aborted(). Build with -sASSERTIONS for more info.
Ā  Ā  at abort (libopencor.js:8:6013)
Ā  Ā  at _abort (libopencor.js:8:133020)
Ā  Ā  at libopencor.wasm:0x16693f
Ā  Ā  at libopencor.wasm:0x2589f4e
Ā  Ā  at libopencor.wasm:0xd66725
Ā  Ā  at libopencor.wasm:0x25782f9
Ā  Ā  at libopencor.wasm:0x1c426bf
Ā  Ā  at libopencor.wasm:0x25c0cce
Ā  Ā  at libopencor.wasm:0x296f2b6
Ā  Ā  at libopencor.wasm:0x9d071b

I imagine that I must be doing something wrong (when compiling to wasm32), but I can’t tell what it is. I tried to google for some sample code that does what I am after, but to no avail.

FWIW, the wasm32 version of my library is built using Emscripten to generate an ES6 module (using EXPORT_ES6), making it possible to use my library from both Node.js and from a Web browser. It all works fine, except for the fact that I cannot look up symbols and therefore run code that was compiled on the fly.

So, would anyone have any idea of what I might be doing wrong? The fact that I am able to do what I want when targetingĀ X86Ā orĀ AArch64Ā tells me that I should be able to do the same when targeting wasm32...?

Cheers, Alan.

Sam Clegg

unread,
Jun 8, 2023, 8:38:42 PM6/8/23
to emscripte...@googlegroups.com
When you build for non-wasm platforms are you JIT'ing non-wasm code?Ā  Ā 

This looks like a codegen issue that only affects the wasm backend.Ā  Ā The errorĀ in question occurs if a code sectionĀ contains more than one function symbol.Ā  In the wasm backend we expect every function to get its own section (i.e. -ffunction-sections is always on).Ā  Ā SeeĀ https://github.com/llvm/llvm-project/blob/6ebf7cd7ed1b6502b3b18985f06b213a53381097/llvm/lib/MC/WasmObjectWriter.cpp#L469-L481

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/3f08dae1-570e-4cdf-ab37-72b5122c49dfn%40googlegroups.com.
Message has been deleted

Alan Garny

unread,
Jun 8, 2023, 9:23:07 PM6/8/23
to emscripten-discuss
Hi,

Correct, when I build my library for X86 or AArch64, I JIT X86 and AArch64 code. So, building my library for wasm32, I want to JIT wasm32 code.

Regarding your link, yes, I have seen that piece of code when looking for the error message I am getting. So, how can I ensure that a code section contains only one function symbol inĀ wasm32Ā or am I just out of luck? (I tried to pass -ffunction-sectionsĀ to the Clang driver, but to no avail.)

Alan

Thomas Lively

unread,
Jun 8, 2023, 9:39:28 PM6/8/23
to emscripte...@googlegroups.com
I’m general I would not expect the standard JIT setup in LLVM to work with Wasm, even if we solved this particular problem. Even if you could look up a symbol, you wouldn’t be able to execute it without calling out to the embedder to first compile and instantiate a Wasm module containing the code. You would also have to arrange for the function to be placed into the table so you can get a function pointer to it since Wasm is a Harvard architecture. This is all possible in principle to implement, but it definitely will not work out of the box.

Alan Garny

unread,
Jun 8, 2023, 10:04:21 PM6/8/23
to emscripten-discuss
Ok, thanks for the clarifications. I am after an out-of-the-box solution, so I guess I am out of luck here, unfortunately. :(

Alan

Reply all
Reply to author
Forward
0 new messages