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

Congcong Cai via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 28 17:06:02 PDT 2024


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

>From b49737505ad3ad84e977787daf141b98fe3e6607 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcong.cai at bmw.com>
Date: Sat, 29 Jun 2024 00:24:30 +0800
Subject: [PATCH] [WebAssembly] support getVT from wasm externref and funcref

---
 .../CodeGen/WebAssembly/wasm-externref-novec.c | 18 ++++++++++++++++++
 llvm/include/llvm/IR/Type.h                    |  2 ++
 llvm/lib/CodeGen/ValueTypes.cpp                |  9 +++++++++
 llvm/lib/IR/Type.cpp                           |  4 ++--
 4 files changed, 31 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/CodeGen/WebAssembly/wasm-externref-novec.c

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;
 }
 



More information about the cfe-commits mailing list