Skip to content

Commit c474f8f

Browse files
authored
[clang][SPIRV] Add builtin for OpGenericCastToPtrExplicit and its SPIR-V friendly binding (#137805)
The patch introduce __builtin_spirv_generic_cast_to_ptr_explicit which is lowered to the llvm.spv.generic.cast.to.ptr.explicit intrinsic. The SPIR-V builtins are now split into 3 differents file: BuiltinsSPIRVCore.td, BuiltinsSPIRVVK.td for Vulkan specific builtins, BuiltinsSPIRVCL.td for OpenCL specific builtins and BuiltinsSPIRVCommon.td for common ones. The patch also introduces a new header defining its SPIR-V friendly equivalent (__spirv_GenericCastToPtrExplicit_ToGlobal, __spirv_GenericCastToPtrExplicit_ToLocal and __spirv_GenericCastToPtrExplicit_ToPrivate). The functions are declared as aliases to the new builtin allowing C-like languages to have a definition to rely on as well as gaining proper front-end diagnostics. The motivation for the header is to provide a stable binding for applications or library (such as SYCL) and allows non SPIR-V targets to provide an implementation (via libclc or similar to how it is done for gpuintrin.h).
1 parent 7efb79b commit c474f8f

24 files changed

+587
-59
lines changed

clang/include/clang/Basic/BuiltinsSPIRV.td

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//===--- BuiltinsSPIRVBase.td - SPIRV Builtin function database -*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
include "clang/Basic/BuiltinsBase.td"
10+
11+
class SPIRVBuiltin<string prototype, list<Attribute> Attr> : Builtin {
12+
let Spellings = ["__builtin_spirv_"#NAME];
13+
let Prototype = prototype;
14+
let Attributes = !listconcat([NoThrow], Attr);
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//===--- BuiltinsSPIRVCL.td - SPIRV Builtin function database ---*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
include "clang/Basic/BuiltinsSPIRVBase.td"
10+
11+
def generic_cast_to_ptr_explicit
12+
: SPIRVBuiltin<"void*(void*, int)", [NoThrow, Const, CustomTypeChecking]>;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===- BuiltinsSPIRVCommon.td - SPIRV Builtin function database -*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
include "clang/Basic/BuiltinsSPIRVBase.td"
10+
11+
def distance : SPIRVBuiltin<"void(...)", [NoThrow, Const]>;
12+
def length : SPIRVBuiltin<"void(...)", [NoThrow, Const]>;
13+
def smoothstep : SPIRVBuiltin<"void(...)", [NoThrow, Const, CustomTypeChecking]>;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===--- BuiltinsSPIRVVK.td - SPIRV Builtin function database ---*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
include "clang/Basic/BuiltinsSPIRVBase.td"
10+
11+
12+
def reflect : SPIRVBuiltin<"void(...)", [NoThrow, Const]>;
13+
def faceforward : SPIRVBuiltin<"void(...)", [NoThrow, Const, CustomTypeChecking]>;

clang/include/clang/Basic/CMakeLists.txt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,17 @@ clang_tablegen(BuiltinsRISCV.inc -gen-clang-builtins
109109
SOURCE BuiltinsRISCV.td
110110
TARGET ClangBuiltinsRISCV)
111111

112-
clang_tablegen(BuiltinsSPIRV.inc -gen-clang-builtins
113-
SOURCE BuiltinsSPIRV.td
114-
TARGET ClangBuiltinsSPIRV)
112+
clang_tablegen(BuiltinsSPIRVCommon.inc -gen-clang-builtins
113+
SOURCE BuiltinsSPIRVCommon.td
114+
TARGET ClangBuiltinsSPIRVCommon)
115+
116+
clang_tablegen(BuiltinsSPIRVVK.inc -gen-clang-builtins
117+
SOURCE BuiltinsSPIRVVK.td
118+
TARGET ClangBuiltinsSPIRVVK)
119+
120+
clang_tablegen(BuiltinsSPIRVCL.inc -gen-clang-builtins
121+
SOURCE BuiltinsSPIRVCL.td
122+
TARGET ClangBuiltinsSPIRVCL)
115123

116124
clang_tablegen(BuiltinsX86.inc -gen-clang-builtins
117125
SOURCE BuiltinsX86.td

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4679,7 +4679,7 @@ def err_attribute_preferred_name_arg_invalid : Error<
46794679
"argument %0 to 'preferred_name' attribute is not a typedef for "
46804680
"a specialization of %1">;
46814681
def err_attribute_builtin_alias : Error<
4682-
"%0 attribute can only be applied to a ARM, HLSL or RISC-V builtin">;
4682+
"%0 attribute can only be applied to a ARM, HLSL, SPIR-V or RISC-V builtin">;
46834683

46844684
// called-once attribute diagnostics.
46854685
def err_called_once_attribute_wrong_type : Error<
@@ -12878,6 +12878,16 @@ def err_bit_int_bad_size : Error<"%select{signed|unsigned}0 _BitInt must "
1287812878
def err_bit_int_max_size : Error<"%select{signed|unsigned}0 _BitInt of bit "
1287912879
"sizes greater than %1 not supported">;
1288012880

12881+
// SPIR-V builtins diagnostics
12882+
def err_spirv_invalid_target : Error<
12883+
"builtin requires %select{spirv|spirv32 or spirv64}0 target">;
12884+
def err_spirv_builtin_generic_cast_invalid_arg : Error<
12885+
"expecting a pointer argument to the generic address space">;
12886+
def err_spirv_enum_not_int : Error<
12887+
"%0{storage class} argument for SPIR-V builtin is not a 32-bits integer">;
12888+
def err_spirv_enum_not_valid : Error<
12889+
"invalid value for %select{storage class}0 argument">;
12890+
1288112891
// errors of expect.with.probability
1288212892
def err_probability_not_constant_float : Error<
1288312893
"probability argument to __builtin_expect_with_probability must be constant "

clang/include/clang/Basic/TargetBuiltins.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,17 @@ namespace clang {
157157
enum {
158158
LastTIBuiltin = clang::Builtin::FirstTSBuiltin - 1,
159159
#define GET_BUILTIN_ENUMERATORS
160-
#include "clang/Basic/BuiltinsSPIRV.inc"
160+
#include "clang/Basic/BuiltinsSPIRVCommon.inc"
161+
#undef GET_BUILTIN_ENUMERATORS
162+
FirstVKBuiltin,
163+
LastCoreBuiltin = FirstVKBuiltin - 1,
164+
#define GET_BUILTIN_ENUMERATORS
165+
#include "clang/Basic/BuiltinsSPIRVVK.inc"
166+
#undef GET_BUILTIN_ENUMERATORS
167+
FirstCLBuiltin,
168+
LastVKBuiltin = FirstCLBuiltin - 1,
169+
#define GET_BUILTIN_ENUMERATORS
170+
#include "clang/Basic/BuiltinsSPIRVCL.inc"
161171
#undef GET_BUILTIN_ENUMERATORS
162172
LastTSBuiltin
163173
};

clang/include/clang/Sema/SemaSPIRV.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class SemaSPIRV : public SemaBase {
2121
public:
2222
SemaSPIRV(Sema &S);
2323

24-
bool CheckSPIRVBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall);
24+
bool CheckSPIRVBuiltinFunctionCall(const TargetInfo &TI, unsigned BuiltinID,
25+
CallExpr *TheCall);
2526
};
2627
} // namespace clang
2728

clang/lib/AST/ASTContext.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10165,6 +10165,11 @@ bool ASTContext::canBuiltinBeRedeclared(const FunctionDecl *FD) const {
1016510165
if (LangOpts.HLSL && FD->getBuiltinID() != Builtin::NotBuiltin &&
1016610166
BuiltinInfo.hasCustomTypechecking(FD->getBuiltinID()))
1016710167
return true;
10168+
// Allow redecl custom type checking builtin for SPIR-V.
10169+
if (getTargetInfo().getTriple().isSPIROrSPIRV() &&
10170+
BuiltinInfo.isTSBuiltin(FD->getBuiltinID()) &&
10171+
BuiltinInfo.hasCustomTypechecking(FD->getBuiltinID()))
10172+
return true;
1016810173
return BuiltinInfo.canBeRedeclared(FD->getBuiltinID());
1016910174
}
1017010175

0 commit comments

Comments
 (0)