Skip to content

Commit 8913b11

Browse files
committed
add a macro for switch statement, remove type classes
1 parent 47518b4 commit 8913b11

File tree

6 files changed

+57
-44
lines changed

6 files changed

+57
-44
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "ABIInfo.h"
1414
#include "CGCUDARuntime.h"
1515
#include "CGCXXABI.h"
16+
#include "CGHLSLUtils.h"
1617
#include "CGObjCRuntime.h"
1718
#include "CGOpenCLRuntime.h"
1819
#include "CGRecordLayout.h"
@@ -51,7 +52,6 @@
5152
#include "llvm/IR/IntrinsicsR600.h"
5253
#include "llvm/IR/IntrinsicsRISCV.h"
5354
#include "llvm/IR/IntrinsicsS390.h"
54-
#include "llvm/IR/IntrinsicsSPIRV.h"
5555
#include "llvm/IR/IntrinsicsVE.h"
5656
#include "llvm/IR/IntrinsicsWebAssembly.h"
5757
#include "llvm/IR/IntrinsicsX86.h"
@@ -18177,17 +18177,6 @@ Intrinsic::ID getDotProductIntrinsic(QualType QT, int elementCount) {
1817718177
return Intrinsic::dx_udot;
1817818178
}
1817918179

18180-
Intrinsic::ID getAllIntrinsic(const llvm::Triple::ArchType Arch) {
18181-
switch (Arch) {
18182-
case llvm::Triple::dxil:
18183-
return Intrinsic::dx_all;
18184-
case llvm::Triple::spirv:
18185-
return Intrinsic::spv_all;
18186-
default:
18187-
llvm_unreachable("Input semantic not supported by target");
18188-
}
18189-
}
18190-
1819118180
Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
1819218181
const CallExpr *E) {
1819318182
if (!getLangOpts().HLSL)
@@ -18198,7 +18187,8 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
1819818187
Value *Op0 = EmitScalarExpr(E->getArg(0));
1819918188
return Builder.CreateIntrinsic(
1820018189
/*ReturnType=*/llvm::Type::getInt1Ty(getLLVMContext()),
18201-
getAllIntrinsic(CGM.getTarget().getTriple().getArch()),
18190+
CGHLSLUtils::get_hlsl_all_intrinsic(
18191+
CGM.getTarget().getTriple().getArch()),
1820218192
ArrayRef<Value *>{Op0}, nullptr, "hlsl.all");
1820318193
}
1820418194
case Builtin::BI__builtin_hlsl_elementwise_any: {

clang/lib/CodeGen/CGHLSLRuntime.cpp

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414

1515
#include "CGHLSLRuntime.h"
1616
#include "CGDebugInfo.h"
17+
#include "CGHLSLUtils.h"
1718
#include "CodeGenModule.h"
1819
#include "clang/AST/Decl.h"
1920
#include "clang/Basic/TargetOptions.h"
20-
#include "llvm/IR/IntrinsicsDirectX.h"
21-
#include "llvm/IR/IntrinsicsSPIRV.h"
2221
#include "llvm/IR/Metadata.h"
2322
#include "llvm/IR/Module.h"
2423
#include "llvm/Support/FormatVariadic.h"
@@ -343,18 +342,9 @@ llvm::Value *CGHLSLRuntime::emitInputSemantic(IRBuilder<> &B,
343342
return B.CreateCall(FunctionCallee(DxGroupIndex));
344343
}
345344
if (D.hasAttr<HLSLSV_DispatchThreadIDAttr>()) {
346-
llvm::Function *ThreadIDIntrinsic;
347-
switch (CGM.getTarget().getTriple().getArch()) {
348-
case llvm::Triple::dxil:
349-
ThreadIDIntrinsic = CGM.getIntrinsic(Intrinsic::dx_thread_id);
350-
break;
351-
case llvm::Triple::spirv:
352-
ThreadIDIntrinsic = CGM.getIntrinsic(Intrinsic::spv_thread_id);
353-
break;
354-
default:
355-
llvm_unreachable("Input semantic not supported by target");
356-
break;
357-
}
345+
llvm::Function *ThreadIDIntrinsic =
346+
CGM.getIntrinsic(CGHLSLUtils::get_hlsl_thread_id_intrinsic(
347+
CGM.getTarget().getTriple().getArch()));
358348
return buildVectorInput(B, ThreadIDIntrinsic, Ty);
359349
}
360350
assert(false && "Unhandled parameter attribute");

clang/lib/CodeGen/CGHLSLUtils.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
//===----- CGHLSLUtils.h - Utility functions for HLSL CodeGen ---*- C++ -*-===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// This File Provides utility function for HLSL code generation.
11+
// It is used to abstract away implementation details of backends.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef LLVM_CLANG_LIB_CODEGEN_CGHLSLUTILS_H
16+
#define LLVM_CLANG_LIB_CODEGEN_CGHLSLUTILS_H
17+
18+
#include "llvm/IR/Intrinsics.h"
19+
#include "llvm/IR/IntrinsicsDirectX.h"
20+
#include "llvm/IR/IntrinsicsSPIRV.h"
21+
22+
// Define the function generator macro
23+
#define GENERATE_HLSL_INTRINSIC_FUNCTION(name) \
24+
static llvm::Intrinsic::ID get_hlsl_##name##_intrinsic( \
25+
const llvm::Triple::ArchType Arch) { \
26+
switch (Arch) { \
27+
case llvm::Triple::dxil: \
28+
return llvm::Intrinsic::dx_##name; \
29+
case llvm::Triple::spirv: \
30+
return llvm::Intrinsic::spv_##name; \
31+
default: \
32+
llvm_unreachable("Input semantic not supported by target"); \
33+
} \
34+
}
35+
36+
class CGHLSLUtils {
37+
public:
38+
GENERATE_HLSL_INTRINSIC_FUNCTION(all)
39+
GENERATE_HLSL_INTRINSIC_FUNCTION(thread_id)
40+
private:
41+
CGHLSLUtils() = delete;
42+
};
43+
44+
#endif

llvm/include/llvm/IR/Intrinsics.td

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -684,18 +684,6 @@ class DefaultAttrsIntrinsic<list<LLVMType> ret_types,
684684
intr_properties, name,
685685
sd_properties, /*disable_default_attributes*/ 0> {}
686686

687-
// HLSL Intrinsic Classes
688-
class HLSLThreadId
689-
: Intrinsic</*ret_types*/ [llvm_i32_ty],
690-
/*param_types*/[llvm_i32_ty],
691-
/*intr_properties*/ [IntrNoMem, IntrWillReturn],
692-
/*name*/ ""> {}
693-
class HLSLAll : Intrinsic</*ret_types*/ [llvm_i1_ty],
694-
/*param_types*/[llvm_any_ty],
695-
/*intr_properties*/ [IntrNoMem, IntrWillReturn],
696-
/*name*/ ""> {}
697-
698-
699687
/// ClangBuiltin - If this intrinsic exactly corresponds to a Clang builtin, this
700688
/// specifies the name of the builtin. This provides automatic CBE and CFE
701689
/// support.

llvm/include/llvm/IR/IntrinsicsDirectX.td

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
let TargetPrefix = "dx" in {
14-
def int_dx_thread_id : HLSLThreadId;
14+
15+
def int_dx_thread_id : Intrinsic<[llvm_i32_ty], [llvm_i32_ty], [IntrNoMem, IntrWillReturn]>;
1516
def int_dx_group_id : Intrinsic<[llvm_i32_ty], [llvm_i32_ty], [IntrNoMem, IntrWillReturn]>;
1617
def int_dx_thread_id_in_group : Intrinsic<[llvm_i32_ty], [llvm_i32_ty], [IntrNoMem, IntrWillReturn]>;
1718
def int_dx_flattened_thread_id_in_group : Intrinsic<[llvm_i32_ty], [], [IntrNoMem, IntrWillReturn]>;
1819

1920
def int_dx_create_handle : ClangBuiltin<"__builtin_hlsl_create_handle">,
2021
Intrinsic<[ llvm_ptr_ty ], [llvm_i8_ty], [IntrWillReturn]>;
2122

22-
def int_dx_all : HLSLAll;
23-
def int_dx_any : DefaultAttrsIntrinsic<[llvm_i1_ty], [llvm_any_ty]>;
23+
def int_dx_all : DefaultAttrsIntrinsic<[llvm_i1_ty], [llvm_any_ty]>;
24+
def int_dx_any : DefaultAttrsIntrinsic<[llvm_i1_ty], [llvm_any_ty]>;
2425
def int_dx_clamp : DefaultAttrsIntrinsic<[llvm_any_ty], [LLVMMatchType<0>, LLVMMatchType<0>, LLVMMatchType<0>]>;
2526
def int_dx_uclamp : DefaultAttrsIntrinsic<[llvm_anyint_ty], [LLVMMatchType<0>, LLVMMatchType<0>, LLVMMatchType<0>]>;
2627

llvm/include/llvm/IR/IntrinsicsSPIRV.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ let TargetPrefix = "spv" in {
5353
ImmArg<ArgIndex<0>>]>;
5454

5555
// The following intrinsic(s) are mirrored from IntrinsicsDirectX.td for HLSL support.
56-
def int_spv_thread_id : HLSLThreadId;
57-
def int_spv_all : HLSLAll;
56+
def int_spv_thread_id : Intrinsic<[llvm_i32_ty], [llvm_i32_ty], [IntrNoMem, IntrWillReturn]>;
5857
def int_spv_create_handle : ClangBuiltin<"__builtin_hlsl_create_handle">,
5958
Intrinsic<[ llvm_ptr_ty ], [llvm_i8_ty], [IntrWillReturn]>;
59+
def int_spv_all : DefaultAttrsIntrinsic<[llvm_i1_ty], [llvm_any_ty]>;
6060
}

0 commit comments

Comments
 (0)