[clang] [Clang][WebAssembly] Fix WASM tables to allow `__funcref` function pointers (PR #178720)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 29 10:24:59 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Demetrius Kanios (QuantumSegfault)
<details>
<summary>Changes</summary>
Allows __funcref pointers to be used as the element type for WASM tables in Clang (static, global, zero-length arrays of a reference type). Modifies `QualType::isWebAssemblyFuncrefType` to accept function pointer types with the `__funcptr`, in addition to pointers in address space 20.
Related: #<!-- -->140933
---
Full diff: https://github.com/llvm/llvm-project/pull/178720.diff
2 Files Affected:
- (modified) clang/lib/AST/Type.cpp (+2-1)
- (modified) clang/test/CodeGen/WebAssembly/builtins-table.c (+17)
``````````diff
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 53082bcf78f6a..8cbe49a23c13d 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2947,7 +2947,8 @@ bool QualType::isWebAssemblyExternrefType() const {
bool QualType::isWebAssemblyFuncrefType() const {
return getTypePtr()->isFunctionPointerType() &&
- getAddressSpace() == LangAS::wasm_funcref;
+ (getAddressSpace() == LangAS::wasm_funcref ||
+ getTypePtr()->hasAttr(attr::WebAssemblyFuncref));
}
QualType::PrimitiveDefaultInitializeKind
diff --git a/clang/test/CodeGen/WebAssembly/builtins-table.c b/clang/test/CodeGen/WebAssembly/builtins-table.c
index 74bb2442fe552..4069da2c4c225 100644
--- a/clang/test/CodeGen/WebAssembly/builtins-table.c
+++ b/clang/test/CodeGen/WebAssembly/builtins-table.c
@@ -65,3 +65,20 @@ static __externref_t other_table[0];
void test_table_copy(int dst_idx, int src_idx, int nelem) {
__builtin_wasm_table_copy(table, other_table, dst_idx, src_idx, nelem);
}
+
+
+typedef void (*__funcref funcref_t)();
+static funcref_t funcref_table[0];
+
+// CHECK-LABEL: define {{[^@]+}}@test_funcref_table
+// CHECK-SAME: (ptr addrspace(20) noundef [[FUNCREF:%.*]], i32 noundef [[INDEX:%.*]]) #[[ATTR0]] {
+// CHECK-NEXT: entry:
+// CHECK-NEXT: call void @llvm.wasm.table.set.funcref(ptr addrspace(1) @funcref_table, i32 [[INDEX]], ptr addrspace(20) [[FUNCREF]])
+// CHECK-NEXT: [[TMP0:%.*]] = call ptr addrspace(20) @llvm.wasm.table.get.funcref(ptr addrspace(1) @funcref_table, i32 [[INDEX]])
+// CHECK-NEXT: ret ptr addrspace(20) [[TMP0]]
+//
+funcref_t test_funcref_table(funcref_t funcref, int index) {
+ __builtin_wasm_table_set(funcref_table, index, funcref);
+
+ return __builtin_wasm_table_get(funcref_table, index);
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/178720
More information about the cfe-commits
mailing list