Question: NativeAOT-LLVM: For WebAssembly imports, DllImport Value misuse? (original) (raw)

When importing functions from other modules in WebAssembly using the proposed WebAssembly canonical ABI, https://github.com/bytecodealliance/witx-bindgen, , the final Wasm/Wat looks like

  (import "hellowasi" "reverse" (func $reverse (type 4)))

This imports the reverse function from the hellowasi module. In order to generate this from LLVM, the LLVM has to look like:

declare void @reverse(i8*, i32, i8*) #2
...
attributes #2 = { "wasm-import-module"="hellowasi" "wasm-import-name"="reverse" }

For information, the C equivalent for emscripte/wasm-ld would be

__attribute__((import_module("hellowasi"), import_name("reverse"))) void reverse();

In c# this starts with currently

        [DllImport("*")]
        private static extern void reverse(byte* utf8Bytes, int len, IntPtr returnArea);

Not all DllImports are going to be Wasm imports, some like e.g. printf are supplied by the Emscripten runtime. A rule could be created:

DllImportAttribute.Value == "*" - treat as now.
DllImportAttribute.Value != "*"- assume its a Wasm import from another module and add the LLVM attributes - the Value property goes to the wasm-import-module attribute.

So this example would be

Would that be acceptable or a misuse of DllImport and "The name of the DLL file that contains the entry point" https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.dllimportattribute.value?view=net-5.0