Skip to content

Commit a950dc6

Browse files
committed
add a templated macro for when we have builtins
and make GENERATE_HLSL_INTRINSIC_FUNCTION take a function name
1 parent 2d83924 commit a950dc6

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18177,8 +18177,9 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
1817718177
Value *Op0 = EmitScalarExpr(E->getArg(0));
1817818178
return Builder.CreateIntrinsic(
1817918179
/*ReturnType=*/llvm::Type::getInt1Ty(getLLVMContext()),
18180-
CGM.getHLSLRuntime().get_hlsl_all_intrinsic(), ArrayRef<Value *>{Op0},
18181-
nullptr, "hlsl.all");
18180+
CGM.getHLSLRuntime()
18181+
.getIntrinsic<Builtin::BI__builtin_hlsl_elementwise_all>(),
18182+
ArrayRef<Value *>{Op0}, nullptr, "hlsl.all");
1818218183
}
1818318184
case Builtin::BI__builtin_hlsl_elementwise_any: {
1818418185
Value *Op0 = EmitScalarExpr(E->getArg(0));

clang/lib/CodeGen/CGHLSLRuntime.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ llvm::Value *CGHLSLRuntime::emitInputSemantic(IRBuilder<> &B,
346346
}
347347
if (D.hasAttr<HLSLSV_DispatchThreadIDAttr>()) {
348348
llvm::Function *ThreadIDIntrinsic =
349-
CGM.getIntrinsic(get_hlsl_thread_id_intrinsic());
349+
CGM.getIntrinsic(getThreadIdIntrinsic());
350350
return buildVectorInput(B, ThreadIDIntrinsic, Ty);
351351
}
352352
assert(false && "Unhandled parameter attribute");

clang/lib/CodeGen/CGHLSLRuntime.h

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "llvm/IR/IntrinsicsDirectX.h"
2121
#include "llvm/IR/IntrinsicsSPIRV.h"
2222

23+
#include "clang/Basic/Builtins.h"
2324
#include "clang/Basic/HLSLRuntime.h"
2425

2526
#include "llvm/ADT/SmallVector.h"
@@ -29,19 +30,29 @@
2930
#include <optional>
3031
#include <vector>
3132

32-
// Define the function generator macro
33-
#define GENERATE_HLSL_INTRINSIC_FUNCTION(name) \
34-
llvm::Intrinsic::ID get_hlsl_##name##_intrinsic() { \
35-
llvm::Triple::ArchType Arch = getArch(); \
36-
switch (Arch) { \
37-
case llvm::Triple::dxil: \
38-
return llvm::Intrinsic::dx_##name; \
39-
case llvm::Triple::spirv: \
40-
return llvm::Intrinsic::spv_##name; \
41-
default: \
42-
llvm_unreachable("Intrinsic " #name \
43-
" not supported by target architecture"); \
44-
} \
33+
#define GENERATE_HLSL_INTRINSIC_BASE(IntrinsicPostfix) \
34+
llvm::Triple::ArchType Arch = getArch(); \
35+
switch (Arch) { \
36+
case llvm::Triple::dxil: \
37+
return llvm::Intrinsic::dx_##IntrinsicPostfix; \
38+
case llvm::Triple::spirv: \
39+
return llvm::Intrinsic::spv_##IntrinsicPostfix; \
40+
default: \
41+
llvm_unreachable("Intrinsic " #IntrinsicPostfix \
42+
" not supported by target architecture"); \
43+
}
44+
45+
// A function generator macro for when there is no builtins or
46+
// when builtins are mapped to a set of intrinsics for different types.
47+
#define GENERATE_HLSL_INTRINSIC_FUNCTION(FunctionName, IntrinsicPostfix) \
48+
llvm::Intrinsic::ID get##FunctionName##Intrinsic() { \
49+
GENERATE_HLSL_INTRINSIC_BASE(IntrinsicPostfix) \
50+
}
51+
52+
// A template function generator macro for when we have builtins.
53+
#define GENERATE_HLSL_INTRINSIC_TEMPLATE(BuiltinName, IntrinsicPostfix) \
54+
template <> llvm::Intrinsic::ID getIntrinsic<Builtin::BI__##BuiltinName>() { \
55+
GENERATE_HLSL_INTRINSIC_BASE(IntrinsicPostfix) \
4556
}
4657

4758
namespace llvm {
@@ -69,9 +80,9 @@ class CGHLSLRuntime {
6980
//===----------------------------------------------------------------------===//
7081
// Start of reserved area for HLSL intrinsic getters.
7182
//===----------------------------------------------------------------------===//
72-
73-
GENERATE_HLSL_INTRINSIC_FUNCTION(all)
74-
GENERATE_HLSL_INTRINSIC_FUNCTION(thread_id)
83+
template <unsigned BI> llvm::Intrinsic::ID getIntrinsic();
84+
GENERATE_HLSL_INTRINSIC_TEMPLATE(builtin_hlsl_elementwise_all, all)
85+
GENERATE_HLSL_INTRINSIC_FUNCTION(ThreadId, thread_id)
7586

7687
//===----------------------------------------------------------------------===//
7788
// End of reserved area for HLSL intrinsic getters.

0 commit comments

Comments
 (0)