-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[WebAssembly] support getVT from externref
and funcref
#97080
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-backend-webassembly @llvm/pr-subscribers-llvm-ir Author: Congcong Cai (HerrCai0907) ChangesFixes: #69894 Full diff: https://github.com/llvm/llvm-project/pull/97080.diff 4 Files Affected:
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;
}
|
@llvm/pr-subscribers-clang Author: Congcong Cai (HerrCai0907) ChangesFixes: #69894 Full diff: https://github.com/llvm/llvm-project/pull/97080.diff 4 Files Affected:
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;
}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if you compile a file that really vectorizes reference types? The attached test doesn't seem to actually end up getting vectorized after all. Can you add a test that get vectorized?
Is it possible to vectorize external reference types? I don't think there are valid test case for it. |
Fixes: #69894
#71069 try to treat
externref
as invalid element type but It should be a normal opaque pointer containing some unknown data.getVT
should be inverse operation ofgetTypeForEVT
which convertMVT::externref
toexternref
.