[PATCH] D122215: [WebAssembly] Initial support for reference type externref in clang

Moritz Sichert via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 13 05:07:16 PST 2022


MoritzS added a comment.

Thanks for the patch! I just tried it out and I think this enables many interesting use cases for WebAssembly when using C/C++.

Currently the lowering to wasm does not work when using externrefs when compiling without optimizations. Is that intended behavior?

I have this example C++ program (similar to the `wasm-externref.c` test):

  void extern_func(__externref_t ref);
  void my_func(__externref_t ref) {
      extern_func(ref);
  }

If I compile this without optimizations (exact command: `clang++ --target=wasm32 -mreference-types -c ./test_externref.cpp`), I get the following error:

  fatal error: error in backend: Cannot select: 0x621000066bd0: externref,ch = load<(dereferenceable load (s0) from %ir.2)> 0x621000066b58, TargetFrameIndex:i32<0>, undef:i32
    0x621000066a68: i32 = TargetFrameIndex<0>
    0x621000066ae0: i32 = undef

The reason for that can be seen when we look at the generated IR for this program:

  ; Function Attrs: mustprogress noinline optnone
  define hidden void @_Z7my_func11externref_t(ptr addrspace(10) %0) #0 {
    %2 = alloca ptr addrspace(10), align 1
    store ptr addrspace(10) %0, ptr %2, align 1
    %3 = load ptr addrspace(10), ptr %2, align 1
    call void @_Z11extern_func11externref_t(ptr addrspace(10) %3)
    ret void
  }
  
  declare void @_Z11extern_func11externref_t(ptr addrspace(10)) #1

Apparently when no optimizations are enabled, clang will write all function arguments to an alloca'd variable. This obviously won't work if the value is an externref. The test case in `wasm-externref.c` also has the alloca and store with an externref value as expected output, which I think should never be generated.

With -O3 this doesn't happen as the alloca'd variables are optimized out. But I still think it should also work with -O0.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122215/new/

https://reviews.llvm.org/D122215



More information about the llvm-commits mailing list