Skip to content

Commit c59810e

Browse files
committed
Rebase
Created using spr 1.3.5
2 parents e27d3f9 + adae370 commit c59810e

File tree

22 files changed

+476
-393
lines changed

22 files changed

+476
-393
lines changed

clang/include/clang/AST/Type.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2724,6 +2724,7 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
27242724
bool isHLSLAttributedResourceType() const;
27252725
bool isHLSLInlineSpirvType() const;
27262726
bool isHLSLResourceRecord() const;
2727+
bool isHLSLResourceRecordArray() const;
27272728
bool isHLSLIntangibleType()
27282729
const; // Any HLSL intangible type (builtin, array, class)
27292730

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13234,9 +13234,9 @@ def err_wasm_builtin_arg_must_match_table_element_type : Error <
1323413234
"%ordinal0 argument must match the element type of the WebAssembly table in the %ordinal1 argument">;
1323513235
def err_wasm_builtin_arg_must_be_integer_type : Error <
1323613236
"%ordinal0 argument must be an integer">;
13237-
def err_wasm_builtin_test_fp_sig_cannot_include_reference_type
13238-
: Error<"not supported for "
13239-
"function pointers with a reference type %select{return "
13237+
def err_wasm_builtin_test_fp_sig_cannot_include_struct_or_union
13238+
: Error<"not supported with the multivalue ABI for "
13239+
"function pointers with a struct/union as %select{return "
1324013240
"value|parameter}0">;
1324113241

1324213242
// OpenACC diagnostics.

clang/include/clang/Sema/SemaWasm.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ class SemaWasm : public SemaBase {
3737
bool BuiltinWasmTableGrow(CallExpr *TheCall);
3838
bool BuiltinWasmTableFill(CallExpr *TheCall);
3939
bool BuiltinWasmTableCopy(CallExpr *TheCall);
40-
bool BuiltinWasmTestFunctionPointerSignature(CallExpr *TheCall);
40+
bool BuiltinWasmTestFunctionPointerSignature(const TargetInfo &TI,
41+
CallExpr *TheCall);
4142

4243
WebAssemblyImportNameAttr *
4344
mergeImportNameAttr(Decl *D, const WebAssemblyImportNameAttr &AL);

clang/lib/AST/Type.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5246,6 +5246,15 @@ bool Type::isHLSLResourceRecord() const {
52465246
return HLSLAttributedResourceType::findHandleTypeOnResource(this) != nullptr;
52475247
}
52485248

5249+
bool Type::isHLSLResourceRecordArray() const {
5250+
const Type *Ty = getUnqualifiedDesugaredType();
5251+
if (!Ty->isArrayType())
5252+
return false;
5253+
while (isa<ConstantArrayType>(Ty))
5254+
Ty = Ty->getArrayElementTypeNoTypeQual();
5255+
return Ty->isHLSLResourceRecord();
5256+
}
5257+
52495258
bool Type::isHLSLIntangibleType() const {
52505259
const Type *Ty = getUnqualifiedDesugaredType();
52515260

clang/lib/CodeGen/CGHLSLRuntime.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,6 @@ llvm::Triple::ArchType CGHLSLRuntime::getArch() {
103103
return CGM.getTarget().getTriple().getArch();
104104
}
105105

106-
// Returns true if the type is an HLSL resource class or an array of them
107-
static bool isResourceRecordTypeOrArrayOf(const clang::Type *Ty) {
108-
while (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
109-
Ty = CAT->getArrayElementTypeNoTypeQual();
110-
return Ty->isHLSLResourceRecord();
111-
}
112-
113106
// Emits constant global variables for buffer constants declarations
114107
// and creates metadata linking the constant globals with the buffer global.
115108
void CGHLSLRuntime::emitBufferGlobalsAndMetadata(const HLSLBufferDecl *BufDecl,
@@ -146,7 +139,7 @@ void CGHLSLRuntime::emitBufferGlobalsAndMetadata(const HLSLBufferDecl *BufDecl,
146139
if (VDTy.getAddressSpace() != LangAS::hlsl_constant) {
147140
if (VD->getStorageClass() == SC_Static ||
148141
VDTy.getAddressSpace() == LangAS::hlsl_groupshared ||
149-
isResourceRecordTypeOrArrayOf(VDTy.getTypePtr())) {
142+
VDTy->isHLSLResourceRecord() || VDTy->isHLSLResourceRecordArray()) {
150143
// Emit static and groupshared variables and resource classes inside
151144
// cbuffer as regular globals
152145
CGM.EmitGlobal(VD);

clang/lib/CodeGen/TargetBuiltins/WebAssembly.cpp

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -246,35 +246,26 @@ Value *CodeGenFunction::EmitWebAssemblyBuiltinExpr(unsigned BuiltinID,
246246
llvm::FunctionType *LLVMFuncTy =
247247
cast<llvm::FunctionType>(ConvertType(QualType(FuncTy, 0)));
248248

249+
bool VarArg = LLVMFuncTy->isVarArg();
249250
unsigned NParams = LLVMFuncTy->getNumParams();
250251
std::vector<Value *> Args;
251-
Args.reserve(NParams + 3);
252+
Args.reserve(NParams + 3 + VarArg);
252253
// The only real argument is the FuncRef
253254
Args.push_back(FuncRef);
254255

255256
// Add the type information
256-
auto addType = [this, &Args](llvm::Type *T) {
257-
if (T->isVoidTy()) {
258-
// Do nothing
259-
} else if (T->isFloatingPointTy()) {
260-
Args.push_back(ConstantFP::get(T, 0));
261-
} else if (T->isIntegerTy()) {
262-
Args.push_back(ConstantInt::get(T, 0));
263-
} else if (T->isPointerTy()) {
264-
Args.push_back(ConstantPointerNull::get(llvm::PointerType::get(
265-
getLLVMContext(), T->getPointerAddressSpace())));
266-
} else {
267-
// TODO: Handle reference types. For now, we reject them in Sema.
268-
llvm_unreachable("Unhandled type");
269-
}
270-
};
271-
272-
addType(LLVMFuncTy->getReturnType());
257+
llvm::Type *RetType = LLVMFuncTy->getReturnType();
258+
if (!RetType->isVoidTy()) {
259+
Args.push_back(PoisonValue::get(RetType));
260+
}
273261
// The token type indicates the boundary between return types and param
274262
// types.
275263
Args.push_back(PoisonValue::get(llvm::Type::getTokenTy(getLLVMContext())));
276264
for (unsigned i = 0; i < NParams; i++) {
277-
addType(LLVMFuncTy->getParamType(i));
265+
Args.push_back(PoisonValue::get(LLVMFuncTy->getParamType(i)));
266+
}
267+
if (VarArg) {
268+
Args.push_back(PoisonValue::get(Builder.getPtrTy()));
278269
}
279270
Function *Callee = CGM.getIntrinsic(Intrinsic::wasm_ref_test_func);
280271
return Builder.CreateCall(Callee, Args);

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -337,16 +337,9 @@ static bool isZeroSizedArray(const ConstantArrayType *CAT) {
337337
return CAT != nullptr;
338338
}
339339

340-
// Returns true if the record type is an HLSL resource class or an array of
341-
// resource classes
342-
static bool isResourceRecordTypeOrArrayOf(const Type *Ty) {
343-
while (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
344-
Ty = CAT->getArrayElementTypeNoTypeQual();
345-
return HLSLAttributedResourceType::findHandleTypeOnResource(Ty) != nullptr;
346-
}
347-
348340
static bool isResourceRecordTypeOrArrayOf(VarDecl *VD) {
349-
return isResourceRecordTypeOrArrayOf(VD->getType().getTypePtr());
341+
const Type *Ty = VD->getType().getTypePtr();
342+
return Ty->isHLSLResourceRecord() || Ty->isHLSLResourceRecordArray();
350343
}
351344

352345
// Returns true if the type is a leaf element type that is not valid to be
@@ -355,7 +348,7 @@ static bool isResourceRecordTypeOrArrayOf(VarDecl *VD) {
355348
// type or if it is a record type that needs to be inspected further.
356349
static bool isInvalidConstantBufferLeafElementType(const Type *Ty) {
357350
Ty = Ty->getUnqualifiedDesugaredType();
358-
if (isResourceRecordTypeOrArrayOf(Ty))
351+
if (Ty->isHLSLResourceRecord() || Ty->isHLSLResourceRecordArray())
359352
return true;
360353
if (Ty->isRecordType())
361354
return Ty->getAsCXXRecordDecl()->isEmpty();
@@ -3597,7 +3590,7 @@ void SemaHLSL::deduceAddressSpace(VarDecl *Decl) {
35973590
return;
35983591

35993592
// Resource handles.
3600-
if (isResourceRecordTypeOrArrayOf(Type->getUnqualifiedDesugaredType()))
3593+
if (Type->isHLSLResourceRecord() || Type->isHLSLResourceRecordArray())
36013594
return;
36023595

36033596
// Only static globals belong to the Private address space.
@@ -3637,10 +3630,7 @@ void SemaHLSL::ActOnVariableDeclarator(VarDecl *VD) {
36373630
if (VD->getType()->isHLSLIntangibleType())
36383631
collectResourceBindingsOnVarDecl(VD);
36393632

3640-
const Type *VarType = VD->getType().getTypePtr();
3641-
while (VarType->isArrayType())
3642-
VarType = VarType->getArrayElementTypeNoTypeQual();
3643-
if (VarType->isHLSLResourceRecord() ||
3633+
if (isResourceRecordTypeOrArrayOf(VD) ||
36443634
VD->hasAttr<HLSLVkConstantIdAttr>()) {
36453635
// Make the variable for resources static. The global externally visible
36463636
// storage is accessed through the handle, which is a member. The variable

clang/lib/Sema/SemaWasm.cpp

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "clang/Basic/AddressSpaces.h"
1818
#include "clang/Basic/DiagnosticSema.h"
1919
#include "clang/Basic/TargetBuiltins.h"
20+
#include "clang/Basic/TargetInfo.h"
2021
#include "clang/Sema/Attr.h"
2122
#include "clang/Sema/Sema.h"
2223

@@ -227,7 +228,8 @@ bool SemaWasm::BuiltinWasmTableCopy(CallExpr *TheCall) {
227228
return false;
228229
}
229230

230-
bool SemaWasm::BuiltinWasmTestFunctionPointerSignature(CallExpr *TheCall) {
231+
bool SemaWasm::BuiltinWasmTestFunctionPointerSignature(const TargetInfo &TI,
232+
CallExpr *TheCall) {
231233
if (SemaRef.checkArgCount(TheCall, 1))
232234
return true;
233235

@@ -250,27 +252,31 @@ bool SemaWasm::BuiltinWasmTestFunctionPointerSignature(CallExpr *TheCall) {
250252
<< ArgType << FuncPtrArg->getSourceRange();
251253
}
252254

253-
// Check that the function pointer doesn't use reference types
254-
if (FuncTy->getReturnType().isWebAssemblyReferenceType()) {
255-
return Diag(
256-
FuncPtrArg->getBeginLoc(),
257-
diag::err_wasm_builtin_test_fp_sig_cannot_include_reference_type)
258-
<< 0 << FuncTy->getReturnType() << FuncPtrArg->getSourceRange();
259-
}
260-
auto NParams = FuncTy->getNumParams();
261-
for (unsigned I = 0; I < NParams; I++) {
262-
if (FuncTy->getParamType(I).isWebAssemblyReferenceType()) {
255+
if (TI.getABI() == "experimental-mv") {
256+
auto isStructOrUnion = [](QualType T) {
257+
return T->isUnionType() || T->isStructureType();
258+
};
259+
if (isStructOrUnion(FuncTy->getReturnType())) {
263260
return Diag(
264261
FuncPtrArg->getBeginLoc(),
265262
diag::
266-
err_wasm_builtin_test_fp_sig_cannot_include_reference_type)
267-
<< 1 << FuncPtrArg->getSourceRange();
263+
err_wasm_builtin_test_fp_sig_cannot_include_struct_or_union)
264+
<< 0 << FuncTy->getReturnType() << FuncPtrArg->getSourceRange();
265+
}
266+
auto NParams = FuncTy->getNumParams();
267+
for (unsigned I = 0; I < NParams; I++) {
268+
if (isStructOrUnion(FuncTy->getParamType(I))) {
269+
return Diag(
270+
FuncPtrArg->getBeginLoc(),
271+
diag::
272+
err_wasm_builtin_test_fp_sig_cannot_include_struct_or_union)
273+
<< 1 << FuncPtrArg->getSourceRange();
274+
}
268275
}
269276
}
270277

271278
// Set return type to int (the result of the test)
272279
TheCall->setType(getASTContext().IntTy);
273-
274280
return false;
275281
}
276282

@@ -297,7 +303,7 @@ bool SemaWasm::CheckWebAssemblyBuiltinFunctionCall(const TargetInfo &TI,
297303
case WebAssembly::BI__builtin_wasm_table_copy:
298304
return BuiltinWasmTableCopy(TheCall);
299305
case WebAssembly::BI__builtin_wasm_test_function_pointer_signature:
300-
return BuiltinWasmTestFunctionPointerSignature(TheCall);
306+
return BuiltinWasmTestFunctionPointerSignature(TI, TheCall);
301307
}
302308

303309
return false;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// RUN: %clang_cc1 -triple wasm32-unknown-unknown -target-feature +gc -O3 -emit-llvm -DSINGLE_VALUE -o - %s | FileCheck %s -check-prefixes WEBASSEMBLY,WEBASSEMBLY-SV
2+
// RUN: %clang_cc1 -triple wasm64-unknown-unknown -target-feature +gc -O3 -emit-llvm -DSINGLE_VALUE -o - %s | FileCheck %s -check-prefixes WEBASSEMBLY,WEBASSEMBLY-SV
3+
// RUN: %clang_cc1 -triple wasm64-unknown-unknown -target-feature +gc -target-abi experimental-mv -O3 -emit-llvm -o - %s 2>&1 | FileCheck %s -check-prefixes WEBASSEMBLY
4+
// RUN: not %clang_cc1 -triple wasm64-unknown-unknown -O3 -emit-llvm -o - %s 2>&1 | FileCheck %s -check-prefixes MISSING-GC
5+
6+
void use(int);
7+
8+
typedef void (*Fvoid)(void);
9+
void test_function_pointer_signature_void(Fvoid func) {
10+
// MISSING-GC: error: '__builtin_wasm_test_function_pointer_signature' needs target feature gc
11+
// WEBASSEMBLY: %0 = tail call i32 (ptr, ...) @llvm.wasm.ref.test.func(ptr %func, token poison)
12+
use(__builtin_wasm_test_function_pointer_signature(func));
13+
}
14+
15+
typedef float (*Ffloats)(float, double, int);
16+
void test_function_pointer_signature_floats(Ffloats func) {
17+
// WEBASSEMBLY: %0 = tail call i32 (ptr, ...) @llvm.wasm.ref.test.func(ptr %func, float poison, token poison, float poison, double poison, i32 poison)
18+
use(__builtin_wasm_test_function_pointer_signature(func));
19+
}
20+
21+
typedef void (*Fpointers)(Fvoid, Ffloats, void*, int*, int***, char[5]);
22+
void test_function_pointer_signature_pointers(Fpointers func) {
23+
// WEBASSEMBLY: %0 = tail call i32 (ptr, ...) @llvm.wasm.ref.test.func(ptr %func, token poison, ptr poison, ptr poison, ptr poison, ptr poison, ptr poison, ptr poison)
24+
use(__builtin_wasm_test_function_pointer_signature(func));
25+
}
26+
27+
typedef void (*FVarArgs)(int, ...);
28+
void test_function_pointer_signature_varargs(FVarArgs func) {
29+
// WEBASSEMBLY: %0 = tail call i32 (ptr, ...) @llvm.wasm.ref.test.func(ptr %func, token poison, i32 poison, ptr poison)
30+
use(__builtin_wasm_test_function_pointer_signature(func));
31+
}
32+
33+
typedef __externref_t (*FExternRef)(__externref_t, __externref_t);
34+
void test_function_pointer_externref(FExternRef func) {
35+
// WEBASSEMBLY: %0 = tail call i32 (ptr, ...) @llvm.wasm.ref.test.func(ptr %func, ptr addrspace(10) poison, token poison, ptr addrspace(10) poison, ptr addrspace(10) poison)
36+
use(__builtin_wasm_test_function_pointer_signature(func));
37+
}
38+
39+
typedef __funcref Fpointers (*FFuncRef)(__funcref Fvoid, __funcref Ffloats);
40+
void test_function_pointer_funcref(FFuncRef func) {
41+
// WEBASSEMBLY: %0 = tail call i32 (ptr, ...) @llvm.wasm.ref.test.func(ptr %func, ptr addrspace(20) poison, token poison, ptr addrspace(20) poison, ptr addrspace(20) poison)
42+
use(__builtin_wasm_test_function_pointer_signature(func));
43+
}
44+
45+
#ifdef SINGLE_VALUE
46+
// Some tests that we get struct ABIs correct. There is no special code in
47+
// __builtin_wasm_test_function_pointer_signature for this, it gets handled by
48+
// the normal type lowering code.
49+
// Single element structs are unboxed, multi element structs are passed on
50+
// stack.
51+
typedef struct {double x;} (*Fstructs1)(struct {double x;}, struct {float x;}, struct {double x; float y;});
52+
void test_function_pointer_structs1(Fstructs1 func) {
53+
// WEBASSEMBLY-SV: %0 = tail call i32 (ptr, ...) @llvm.wasm.ref.test.func(ptr %func, double poison, token poison, double poison, float poison, ptr poison)
54+
use(__builtin_wasm_test_function_pointer_signature(func));
55+
}
56+
57+
// Two element return struct ==> return ptr on stack
58+
typedef struct {double x; double y;} (*Fstructs2)(void);
59+
void test_function_pointer_structs2(Fstructs2 func) {
60+
// WEBASSEMBLY-SV: %0 = tail call i32 (ptr, ...) @llvm.wasm.ref.test.func(ptr %func, token poison, ptr poison)
61+
use(__builtin_wasm_test_function_pointer_signature(func));
62+
}
63+
64+
// Return union ==> return ptr on stack, one element union => unboxed
65+
typedef union {double x; float y;} (*FUnions)(union {double x; float y;}, union {double x;});
66+
void test_function_pointer_unions(FUnions func) {
67+
// WEBASSEMBLY-SV: %0 = tail call i32 (ptr, ...) @llvm.wasm.ref.test.func(ptr %func, token poison, ptr poison, ptr poison, double poison)
68+
use(__builtin_wasm_test_function_pointer_signature(func));
69+
}
70+
#endif

clang/test/CodeGen/builtins-wasm.c

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -751,24 +751,3 @@ void *tp (void) {
751751
return __builtin_thread_pointer ();
752752
// WEBASSEMBLY: call {{.*}} @llvm.thread.pointer.p0()
753753
}
754-
755-
typedef void (*Fvoid)(void);
756-
typedef float (*Ffloats)(float, double, int);
757-
typedef void (*Fpointers)(Fvoid, Ffloats, void*, int*, int***, char[5]);
758-
759-
void use(int);
760-
761-
void test_function_pointer_signature_void(Fvoid func) {
762-
// WEBASSEMBLY: %0 = tail call i32 (ptr, ...) @llvm.wasm.ref.test.func(ptr %func, token poison)
763-
use(__builtin_wasm_test_function_pointer_signature(func));
764-
}
765-
766-
void test_function_pointer_signature_floats(Ffloats func) {
767-
// WEBASSEMBLY: tail call i32 (ptr, ...) @llvm.wasm.ref.test.func(ptr %func, float 0.000000e+00, token poison, float 0.000000e+00, double 0.000000e+00, i32 0)
768-
use(__builtin_wasm_test_function_pointer_signature(func));
769-
}
770-
771-
void test_function_pointer_signature_pointers(Fpointers func) {
772-
// WEBASSEMBLY: %0 = tail call i32 (ptr, ...) @llvm.wasm.ref.test.func(ptr %func, token poison, ptr null, ptr null, ptr null, ptr null, ptr null, ptr null)
773-
use(__builtin_wasm_test_function_pointer_signature(func));
774-
}

0 commit comments

Comments
 (0)