[clang] [llvm] [WebAssembly] support getVT from `externref` and `funcref` (PR #97080)

via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 28 09:26:24 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Congcong Cai (HerrCai0907)

<details>
<summary>Changes</summary>

Fixes: #<!-- -->69894 

---
Full diff: https://github.com/llvm/llvm-project/pull/97080.diff


4 Files Affected:

- (added) clang/test/CodeGen/WebAssembly/wasm-externref-novec.c (+18) 
- (modified) llvm/include/llvm/IR/Type.h (+2) 
- (modified) llvm/lib/CodeGen/ValueTypes.cpp (+9) 
- (modified) llvm/lib/IR/Type.cpp (+2-2) 


``````````diff
diff --git a/clang/test/CodeGen/WebAssembly/wasm-externref-novec.c b/clang/test/CodeGen/WebAssembly/wasm-externref-novec.c
new file mode 100644
index 0000000000000..d91072f379623
--- /dev/null
+++ b/clang/test/CodeGen/WebAssembly/wasm-externref-novec.c
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 -O2 -triple wasm32-unknown-unknown-wasm -target-feature +reference-types -emit-llvm -o - %s | FileCheck %s
+
+// From issue 69894. Reftypes need to be marked as not valid as vector elements.
+
+__externref_t foo(void);
+// CHECK: declare ptr addrspace(10) @foo()
+
+void bar(__externref_t);
+// CHECK: declare void @bar(ptr addrspace(10))
+
+void test(int flag, __externref_t ref1, __externref_t ref2) {
+  if (flag) {
+    ref1 = foo();
+    ref2 = foo();
+  }
+  bar(ref1);
+  bar(ref2);
+}
diff --git a/llvm/include/llvm/IR/Type.h b/llvm/include/llvm/IR/Type.h
index 1f0133c08e7d6..cacb0539b3ddf 100644
--- a/llvm/include/llvm/IR/Type.h
+++ b/llvm/include/llvm/IR/Type.h
@@ -479,6 +479,8 @@ class Type {
   }
   static Type *getFloatingPointTy(LLVMContext &C, const fltSemantics &S);
 
+  static constexpr unsigned WasmExternrefAddressSpace = 10;
+  static constexpr unsigned WasmFuncrefAddressSpace = 20;
   //===--------------------------------------------------------------------===//
   // Convenience methods for getting pointer types.
   //
diff --git a/llvm/lib/CodeGen/ValueTypes.cpp b/llvm/lib/CodeGen/ValueTypes.cpp
index b0f736a49c20e..5aa7c406206d9 100644
--- a/llvm/lib/CodeGen/ValueTypes.cpp
+++ b/llvm/lib/CodeGen/ValueTypes.cpp
@@ -263,6 +263,15 @@ MVT MVT::getVT(Type *Ty, bool HandleUnknown){
       getVT(VTy->getElementType(), /*HandleUnknown=*/ false),
             VTy->getElementCount());
   }
+  case Type::PointerTyID: {
+    if (Ty->getPointerAddressSpace() == Type::WasmExternrefAddressSpace)
+      return MVT(MVT::externref);
+    if (Ty->getPointerAddressSpace() == Type::WasmFuncrefAddressSpace)
+      return MVT(MVT::funcref);
+    if (HandleUnknown)
+      return MVT(MVT::Other);
+    llvm_unreachable("Unknown pointer type!");
+  }
   }
 }
 
diff --git a/llvm/lib/IR/Type.cpp b/llvm/lib/IR/Type.cpp
index 5c61ad9f000b0..2279b5eb873b7 100644
--- a/llvm/lib/IR/Type.cpp
+++ b/llvm/lib/IR/Type.cpp
@@ -261,13 +261,13 @@ IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) {
 
 Type *Type::getWasm_ExternrefTy(LLVMContext &C) {
   // opaque pointer in addrspace(10)
-  static PointerType *Ty = PointerType::get(C, 10);
+  static PointerType *Ty = PointerType::get(C, WasmExternrefAddressSpace);
   return Ty;
 }
 
 Type *Type::getWasm_FuncrefTy(LLVMContext &C) {
   // opaque pointer in addrspace(20)
-  static PointerType *Ty = PointerType::get(C, 20);
+  static PointerType *Ty = PointerType::get(C, WasmFuncrefAddressSpace);
   return Ty;
 }
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/97080


More information about the cfe-commits mailing list