clang 22.0.0git
CGHLSLRuntime.h
Go to the documentation of this file.
1//===----- CGHLSLRuntime.h - Interface to HLSL Runtimes -----*- 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// This provides an abstract class for HLSL code generation. Concrete
10// subclasses of this implement code generation for specific HLSL
11// runtime libraries.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_LIB_CODEGEN_CGHLSLRUNTIME_H
16#define LLVM_CLANG_LIB_CODEGEN_CGHLSLRUNTIME_H
17
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/IR/IRBuilder.h"
20#include "llvm/IR/Intrinsics.h"
21#include "llvm/IR/IntrinsicsDirectX.h"
22#include "llvm/IR/IntrinsicsSPIRV.h"
23
26
27#include "llvm/ADT/SmallVector.h"
28#include "llvm/ADT/StringRef.h"
29#include "llvm/Frontend/HLSL/HLSLResource.h"
30
31#include <optional>
32#include <vector>
33
34// A function generator macro for picking the right intrinsic
35// for the target backend
36#define GENERATE_HLSL_INTRINSIC_FUNCTION(FunctionName, IntrinsicPostfix) \
37 llvm::Intrinsic::ID get##FunctionName##Intrinsic() { \
38 llvm::Triple::ArchType Arch = getArch(); \
39 switch (Arch) { \
40 case llvm::Triple::dxil: \
41 return llvm::Intrinsic::dx_##IntrinsicPostfix; \
42 case llvm::Triple::spirv: \
43 return llvm::Intrinsic::spv_##IntrinsicPostfix; \
44 default: \
45 llvm_unreachable("Intrinsic " #IntrinsicPostfix \
46 " not supported by target architecture"); \
47 } \
48 }
49
50using ResourceClass = llvm::dxil::ResourceClass;
51
52namespace llvm {
53class GlobalVariable;
54class Function;
55class StructType;
56class Metadata;
57} // namespace llvm
58
59namespace clang {
60class NamedDecl;
61class VarDecl;
62class ParmVarDecl;
63class InitListExpr;
64class HLSLBufferDecl;
65class HLSLVkBindingAttr;
66class HLSLResourceBindingAttr;
67class Type;
68class RecordType;
69class DeclContext;
70class HLSLPackOffsetAttr;
71class ArraySubscriptExpr;
72
73class FunctionDecl;
74
75namespace CodeGen {
76
77class CodeGenModule;
78class CodeGenFunction;
79class LValue;
80
82public:
83 //===----------------------------------------------------------------------===//
84 // Start of reserved area for HLSL intrinsic getters.
85 //===----------------------------------------------------------------------===//
86
92 GENERATE_HLSL_INTRINSIC_FUNCTION(FlattenedThreadIdInGroup,
93 flattened_thread_id_in_group)
97 GENERATE_HLSL_INTRINSIC_FUNCTION(Saturate, saturate)
101 GENERATE_HLSL_INTRINSIC_FUNCTION(ThreadId, thread_id)
102 GENERATE_HLSL_INTRINSIC_FUNCTION(GroupThreadId, thread_id_in_group)
103 GENERATE_HLSL_INTRINSIC_FUNCTION(GroupId, group_id)
107 GENERATE_HLSL_INTRINSIC_FUNCTION(Dot4AddI8Packed, dot4add_i8packed)
108 GENERATE_HLSL_INTRINSIC_FUNCTION(Dot4AddU8Packed, dot4add_u8packed)
109 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveActiveAllTrue, wave_all)
110 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveActiveAnyTrue, wave_any)
111 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveActiveCountBits, wave_active_countbits)
112 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveIsFirstLane, wave_is_first_lane)
113 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveGetLaneCount, wave_get_lane_count)
114 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveReadLaneAt, wave_readlane)
115 GENERATE_HLSL_INTRINSIC_FUNCTION(FirstBitUHigh, firstbituhigh)
116 GENERATE_HLSL_INTRINSIC_FUNCTION(FirstBitSHigh, firstbitshigh)
117 GENERATE_HLSL_INTRINSIC_FUNCTION(FirstBitLow, firstbitlow)
121
122 GENERATE_HLSL_INTRINSIC_FUNCTION(CreateResourceGetPointer,
123 resource_getpointer)
124 GENERATE_HLSL_INTRINSIC_FUNCTION(CreateHandleFromBinding,
125 resource_handlefrombinding)
126 GENERATE_HLSL_INTRINSIC_FUNCTION(CreateHandleFromImplicitBinding,
127 resource_handlefromimplicitbinding)
128 GENERATE_HLSL_INTRINSIC_FUNCTION(BufferUpdateCounter, resource_updatecounter)
129 GENERATE_HLSL_INTRINSIC_FUNCTION(GroupMemoryBarrierWithGroupSync,
130 group_memory_barrier_with_group_sync)
131
132 //===----------------------------------------------------------------------===//
133 // End of reserved area for HLSL intrinsic getters.
134 //===----------------------------------------------------------------------===//
135
136protected:
137 CodeGenModule &CGM;
138
139 llvm::Value *emitInputSemantic(llvm::IRBuilder<> &B, const ParmVarDecl &D,
140 llvm::Type *Ty);
141
142public:
143 CGHLSLRuntime(CodeGenModule &CGM) : CGM(CGM) {}
144 virtual ~CGHLSLRuntime() {}
145
146 llvm::Type *
148 SmallVector<int32_t> *Packoffsets = nullptr);
149
151
152 void addBuffer(const HLSLBufferDecl *D);
153 void finishCodeGen();
154
155 void setHLSLEntryAttributes(const FunctionDecl *FD, llvm::Function *Fn);
156
157 void emitEntryFunction(const FunctionDecl *FD, llvm::Function *Fn);
158 void setHLSLFunctionAttributes(const FunctionDecl *FD, llvm::Function *Fn);
159 void handleGlobalVarDefinition(const VarDecl *VD, llvm::GlobalVariable *Var);
160
161 llvm::Instruction *getConvergenceToken(llvm::BasicBlock &BB);
162
163 llvm::TargetExtType *
164 getHLSLBufferLayoutType(const RecordType *LayoutStructTy);
165 void addHLSLBufferLayoutType(const RecordType *LayoutStructTy,
166 llvm::TargetExtType *LayoutTy);
168
169 std::optional<LValue>
171 CodeGenFunction &CGF);
172
173private:
174 void emitBufferGlobalsAndMetadata(const HLSLBufferDecl *BufDecl,
175 llvm::GlobalVariable *BufGV);
176 void initializeBufferFromBinding(const HLSLBufferDecl *BufDecl,
177 llvm::GlobalVariable *GV,
178 HLSLVkBindingAttr *VkBinding);
179 void initializeBufferFromBinding(const HLSLBufferDecl *BufDecl,
180 llvm::GlobalVariable *GV,
181 HLSLResourceBindingAttr *RBA);
182 llvm::Triple::ArchType getArch();
183
184 llvm::DenseMap<const clang::RecordType *, llvm::TargetExtType *> LayoutTypes;
185};
186
187} // namespace CodeGen
188} // namespace clang
189
190#endif
MatchType Type
Defines enum values for all the target-independent builtin functions.
#define GENERATE_HLSL_INTRINSIC_FUNCTION(FunctionName, IntrinsicPostfix)
Definition: CGHLSLRuntime.h:36
llvm::dxil::ResourceClass ResourceClass
Definition: CGHLSLRuntime.h:50
const Decl * D
Expr * E
Defines helper utilities for supporting the HLSL runtime environment.
__DEVICE__ double rsqrt(double __a)
ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
Definition: Expr.h:2723
llvm::Instruction * getConvergenceToken(llvm::BasicBlock &BB)
void setHLSLEntryAttributes(const FunctionDecl *FD, llvm::Function *Fn)
void addHLSLBufferLayoutType(const RecordType *LayoutStructTy, llvm::TargetExtType *LayoutTy)
void emitEntryFunction(const FunctionDecl *FD, llvm::Function *Fn)
void handleGlobalVarDefinition(const VarDecl *VD, llvm::GlobalVariable *Var)
llvm::TargetExtType * getHLSLBufferLayoutType(const RecordType *LayoutStructTy)
resource_getpointer resource_handlefromimplicitbinding GENERATE_HLSL_INTRINSIC_FUNCTION(GroupMemoryBarrierWithGroupSync, group_memory_barrier_with_group_sync) protected llvm::Value * emitInputSemantic(llvm::IRBuilder<> &B, const ParmVarDecl &D, llvm::Type *Ty)
llvm::Type * convertHLSLSpecificType(const Type *T, SmallVector< int32_t > *Packoffsets=nullptr)
std::optional< LValue > emitResourceArraySubscriptExpr(const ArraySubscriptExpr *E, CodeGenFunction &CGF)
void addBuffer(const HLSLBufferDecl *D)
void setHLSLFunctionAttributes(const FunctionDecl *FD, llvm::Function *Fn)
void emitInitListOpaqueValues(CodeGenFunction &CGF, InitListExpr *E)
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
This class organizes the cross-function state that is used while generating LLVM code.
Represents a function declaration or definition.
Definition: Decl.h:1999
HLSLBufferDecl - Represent a cbuffer or tbuffer declaration.
Definition: Decl.h:5156
Describes an C or C++ initializer list.
Definition: Expr.h:5235
Represents a parameter to a function.
Definition: Decl.h:1789
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: TypeBase.h:6502
The base class of the type hierarchy.
Definition: TypeBase.h:1833
Represents a variable declaration or definition.
Definition: Decl.h:925
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30
float __ovld __cnfn step(float, float)
Returns 0.0 if x < edge, otherwise it returns 1.0.
float __ovld __cnfn sign(float)
Returns 1.0 if x > 0, -0.0 if x = -0.0, +0.0 if x = +0.0, or -1.0 if x < 0.
float __ovld __cnfn radians(float)
Converts degrees to radians, i.e.
float __ovld __cnfn degrees(float)
Converts radians to degrees, i.e.
int __ovld __cnfn all(char)
Returns 1 if the most significant bit in all components of x is set; otherwise returns 0.
float __ovld __cnfn normalize(float)
Returns a vector in the same direction as p but with a length of 1.
int __ovld __cnfn any(char)
Returns 1 if the most significant bit in any component of x is set; otherwise returns 0.
float4 __ovld __cnfn cross(float4, float4)
Returns the cross product of p0.xyz and p1.xyz.