[WASM] add CheckWasmTableElement helper (#181172)

Discard qualifiers from the types (e.g. addrspace, volatile, etc) before
checking that the underlying types are the same, and DRY the code to
emit a consistent Sema.Diag before returning failure.
This commit is contained in:
Jameson Nash 2026-03-02 17:46:52 -05:00 committed by GitHub
parent 6719ec1e95
commit 4e3e4f25bc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 47 additions and 16 deletions

View File

@ -40,6 +40,20 @@ static bool CheckWasmBuiltinArgIsTable(Sema &S, CallExpr *E, unsigned ArgIndex,
return false;
}
static bool CheckWasmTableElement(Sema &S, QualType &ElTy, CallExpr *E,
unsigned TableIndex, unsigned ArgIndex) {
Expr *NewElemArg = E->getArg(ArgIndex);
QualType QT = NewElemArg->getType();
// Compare the types after removing insignificant qualifiers
if (!S.getASTContext().hasSameType(ElTy.getTypePtr(), QT.getTypePtr())) {
return S.Diag(NewElemArg->getBeginLoc(),
diag::err_wasm_builtin_arg_must_match_table_element_type)
<< (ArgIndex + 1) << (TableIndex + 1)
<< NewElemArg->getSourceRange();
}
return false;
}
/// Checks the argument at the given index is an integer.
static bool CheckWasmBuiltinArgIsInteger(Sema &S, CallExpr *E,
unsigned ArgIndex) {
@ -115,7 +129,7 @@ bool SemaWasm::BuiltinWasmTableGet(CallExpr *TheCall) {
return false;
}
/// Check that the first argumnet is a WebAssembly table, the second is
/// Check that the first argument is a WebAssembly table, the second is
/// an index to use as index into the table and the third is the reference
/// type to set into the table.
bool SemaWasm::BuiltinWasmTableSet(CallExpr *TheCall) {
@ -129,7 +143,7 @@ bool SemaWasm::BuiltinWasmTableSet(CallExpr *TheCall) {
if (CheckWasmBuiltinArgIsInteger(SemaRef, TheCall, 1))
return true;
if (!getASTContext().hasSameType(ElTy, TheCall->getArg(2)->getType()))
if (CheckWasmTableElement(SemaRef, ElTy, TheCall, 0, 2))
return true;
return false;
@ -158,12 +172,8 @@ bool SemaWasm::BuiltinWasmTableGrow(CallExpr *TheCall) {
if (CheckWasmBuiltinArgIsTable(SemaRef, TheCall, 0, ElTy))
return true;
Expr *NewElemArg = TheCall->getArg(1);
if (!getASTContext().hasSameType(ElTy, NewElemArg->getType())) {
return Diag(NewElemArg->getBeginLoc(),
diag::err_wasm_builtin_arg_must_match_table_element_type)
<< 2 << 1 << NewElemArg->getSourceRange();
}
if (CheckWasmTableElement(SemaRef, ElTy, TheCall, 0, 1))
return true;
if (CheckWasmBuiltinArgIsInteger(SemaRef, TheCall, 2))
return true;
@ -185,12 +195,8 @@ bool SemaWasm::BuiltinWasmTableFill(CallExpr *TheCall) {
if (CheckWasmBuiltinArgIsInteger(SemaRef, TheCall, 1))
return true;
Expr *NewElemArg = TheCall->getArg(2);
if (!getASTContext().hasSameType(ElTy, NewElemArg->getType())) {
return Diag(NewElemArg->getBeginLoc(),
diag::err_wasm_builtin_arg_must_match_table_element_type)
<< 3 << 1 << NewElemArg->getSourceRange();
}
if (CheckWasmTableElement(SemaRef, ElTy, TheCall, 0, 2))
return true;
if (CheckWasmBuiltinArgIsInteger(SemaRef, TheCall, 3))
return true;
@ -214,7 +220,7 @@ bool SemaWasm::BuiltinWasmTableCopy(CallExpr *TheCall) {
return true;
Expr *TableYArg = TheCall->getArg(1);
if (!getASTContext().hasSameType(XElTy, YElTy)) {
if (!getASTContext().hasSameType(XElTy.getTypePtr(), YElTy.getTypePtr())) {
return Diag(TableYArg->getBeginLoc(),
diag::err_wasm_builtin_arg_must_match_table_element_type)
<< 2 << 1 << TableYArg->getSourceRange();

View File

@ -3,6 +3,7 @@
// REQUIRES: webassembly-registered-target
static __externref_t table[0];
static const __externref_t const_table[0];
// CHECK-LABEL: define {{[^@]+}}@test_builtin_wasm_table_get
// CHECK-SAME: (i32 noundef [[INDEX:%.*]]) #[[ATTR0:[0-9]+]] {
@ -14,16 +15,40 @@ __externref_t test_builtin_wasm_table_get(int index) {
return __builtin_wasm_table_get(table, index);
}
// CHECK-LABEL: define {{[^@]+}}@test_builtin_wasm_table_get_const
// CHECK-SAME: (i32 noundef [[INDEX:%.*]]) #[[ATTR0]] {
// CHECK-NEXT: entry:
// CHECK-NEXT: [[TMP0:%.*]] = call ptr addrspace(10) @llvm.wasm.table.get.externref(ptr addrspace(1) @table, i32 [[INDEX]])
// CHECK-NEXT: ret ptr addrspace(10) [[TMP0]]
//
__externref_t test_builtin_wasm_table_get_const(const int index) {
return __builtin_wasm_table_get(table, index);
}
// CHECK-LABEL: define {{[^@]+}}@test_builtin_wasm_table_set
// CHECK-SAME: (i32 noundef [[INDEX:%.*]], ptr addrspace(10) [[REF:%.*]]) #[[ATTR0]] {
// CHECK-NEXT: entry:
// CHECK-NEXT: call void @llvm.wasm.table.set.externref(ptr addrspace(1) @const_table, i32 [[INDEX]], ptr addrspace(10) [[REF]])
// CHECK-NEXT: call void @llvm.wasm.table.set.externref(ptr addrspace(1) @table, i32 [[INDEX]], ptr addrspace(10) [[REF]])
// CHECK-NEXT: ret void
//
void test_builtin_wasm_table_set(int index, __externref_t ref) {
void test_builtin_wasm_table_set(const int index, __externref_t ref) {
__builtin_wasm_table_set(const_table, index, ref);
return __builtin_wasm_table_set(table, index, ref);
}
// CHECK-LABEL: define {{[^@]+}}@test_builtin_wasm_table_set_const
// CHECK-SAME: (i32 noundef [[INDEX:%.*]], ptr addrspace(10) [[REF:%.*]]) #[[ATTR0]] {
// CHECK-NEXT: entry:
// CHECK-NEXT: call void @llvm.wasm.table.set.externref(ptr addrspace(1) @table, i32 [[INDEX]], ptr addrspace(10) [[REF]])
// CHECK-NEXT: call void @llvm.wasm.table.set.externref(ptr addrspace(1) @const_table, i32 [[INDEX]], ptr addrspace(10) [[REF]])
// CHECK-NEXT: ret void
//
void test_builtin_wasm_table_set_const(const int index, const __externref_t ref) {
__builtin_wasm_table_set(table, index, ref);
return __builtin_wasm_table_set(const_table, index, ref);
}
// CHECK-LABEL: define {{[^@]+}}@test_builtin_wasm_table_size
// CHECK-SAME: () #[[ATTR0]] {
// CHECK-NEXT: entry: