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
24#include "clang/AST/Attr.h"
25#include "clang/AST/Decl.h"
28
29#include "llvm/ADT/SmallVector.h"
30#include "llvm/ADT/StringRef.h"
31#include "llvm/Frontend/HLSL/HLSLResource.h"
32
33#include <optional>
34#include <vector>
35
36// A function generator macro for picking the right intrinsic
37// for the target backend
38#define GENERATE_HLSL_INTRINSIC_FUNCTION(FunctionName, IntrinsicPostfix) \
39 llvm::Intrinsic::ID get##FunctionName##Intrinsic() { \
40 llvm::Triple::ArchType Arch = getArch(); \
41 switch (Arch) { \
42 case llvm::Triple::dxil: \
43 return llvm::Intrinsic::dx_##IntrinsicPostfix; \
44 case llvm::Triple::spirv: \
45 return llvm::Intrinsic::spv_##IntrinsicPostfix; \
46 default: \
47 llvm_unreachable("Intrinsic " #IntrinsicPostfix \
48 " not supported by target architecture"); \
49 } \
50 }
51
52using ResourceClass = llvm::dxil::ResourceClass;
53
54namespace llvm {
55class GlobalVariable;
56class Function;
57class StructType;
58class Metadata;
59} // namespace llvm
60
61namespace clang {
62class NamedDecl;
63class VarDecl;
64class ParmVarDecl;
65class InitListExpr;
66class HLSLBufferDecl;
68class HLSLVkBindingAttr;
69class HLSLResourceBindingAttr;
70class Type;
71class RecordType;
72class DeclContext;
73class HLSLPackOffsetAttr;
75
76class FunctionDecl;
77
78namespace CodeGen {
79
80class CodeGenModule;
81class CodeGenFunction;
82class LValue;
83
85public:
86 //===----------------------------------------------------------------------===//
87 // Start of reserved area for HLSL intrinsic getters.
88 //===----------------------------------------------------------------------===//
89
95 GENERATE_HLSL_INTRINSIC_FUNCTION(FlattenedThreadIdInGroup,
96 flattened_thread_id_in_group)
101 GENERATE_HLSL_INTRINSIC_FUNCTION(Saturate, saturate)
105 GENERATE_HLSL_INTRINSIC_FUNCTION(ThreadId, thread_id)
106 GENERATE_HLSL_INTRINSIC_FUNCTION(GroupThreadId, thread_id_in_group)
107 GENERATE_HLSL_INTRINSIC_FUNCTION(GroupId, group_id)
111 GENERATE_HLSL_INTRINSIC_FUNCTION(Dot4AddI8Packed, dot4add_i8packed)
112 GENERATE_HLSL_INTRINSIC_FUNCTION(Dot4AddU8Packed, dot4add_u8packed)
113 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveActiveAllTrue, wave_all)
114 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveActiveAnyTrue, wave_any)
115 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveActiveCountBits, wave_active_countbits)
116 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveIsFirstLane, wave_is_first_lane)
117 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveGetLaneCount, wave_get_lane_count)
118 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveReadLaneAt, wave_readlane)
119 GENERATE_HLSL_INTRINSIC_FUNCTION(FirstBitUHigh, firstbituhigh)
120 GENERATE_HLSL_INTRINSIC_FUNCTION(FirstBitSHigh, firstbitshigh)
121 GENERATE_HLSL_INTRINSIC_FUNCTION(FirstBitLow, firstbitlow)
125
126 GENERATE_HLSL_INTRINSIC_FUNCTION(CreateResourceGetPointer,
127 resource_getpointer)
128 GENERATE_HLSL_INTRINSIC_FUNCTION(CreateHandleFromBinding,
129 resource_handlefrombinding)
130 GENERATE_HLSL_INTRINSIC_FUNCTION(CreateHandleFromImplicitBinding,
131 resource_handlefromimplicitbinding)
132 GENERATE_HLSL_INTRINSIC_FUNCTION(BufferUpdateCounter, resource_updatecounter)
133 GENERATE_HLSL_INTRINSIC_FUNCTION(GroupMemoryBarrierWithGroupSync,
134 group_memory_barrier_with_group_sync)
135
136 //===----------------------------------------------------------------------===//
137 // End of reserved area for HLSL intrinsic getters.
138 //===----------------------------------------------------------------------===//
139
140protected:
141 CodeGenModule &CGM;
142
143 void collectInputSemantic(llvm::IRBuilder<> &B, const DeclaratorDecl *D,
144 llvm::Type *Type,
145 SmallVectorImpl<llvm::Value *> &Inputs);
146
151
152 llvm::Value *emitSystemSemanticLoad(llvm::IRBuilder<> &B, llvm::Type *Type,
154 SemanticInfo &ActiveSemantic);
155
156 llvm::Value *handleScalarSemanticLoad(llvm::IRBuilder<> &B, llvm::Type *Type,
158 SemanticInfo &ActiveSemantic);
159
160 llvm::Value *handleSemanticLoad(llvm::IRBuilder<> &B, llvm::Type *Type,
162 SemanticInfo &ActiveSemantic);
163
164public:
165 CGHLSLRuntime(CodeGenModule &CGM) : CGM(CGM) {}
166 virtual ~CGHLSLRuntime() {}
167
168 llvm::Type *
170 SmallVector<int32_t> *Packoffsets = nullptr);
171
173
174 void addBuffer(const HLSLBufferDecl *D);
176 void finishCodeGen();
177
178 void setHLSLEntryAttributes(const FunctionDecl *FD, llvm::Function *Fn);
179
180 void emitEntryFunction(const FunctionDecl *FD, llvm::Function *Fn);
181 void setHLSLFunctionAttributes(const FunctionDecl *FD, llvm::Function *Fn);
182 void handleGlobalVarDefinition(const VarDecl *VD, llvm::GlobalVariable *Var);
183
184 llvm::Instruction *getConvergenceToken(llvm::BasicBlock &BB);
185
186 llvm::TargetExtType *
187 getHLSLBufferLayoutType(const RecordType *LayoutStructTy);
188 void addHLSLBufferLayoutType(const RecordType *LayoutStructTy,
189 llvm::TargetExtType *LayoutTy);
191
192 std::optional<LValue>
194 CodeGenFunction &CGF);
195
196private:
197 void emitBufferGlobalsAndMetadata(const HLSLBufferDecl *BufDecl,
198 llvm::GlobalVariable *BufGV);
199 void initializeBufferFromBinding(const HLSLBufferDecl *BufDecl,
200 llvm::GlobalVariable *GV,
201 HLSLVkBindingAttr *VkBinding);
202 void initializeBufferFromBinding(const HLSLBufferDecl *BufDecl,
203 llvm::GlobalVariable *GV,
204 HLSLResourceBindingAttr *RBA);
205 llvm::Triple::ArchType getArch();
206
207 llvm::DenseMap<const clang::RecordType *, llvm::TargetExtType *> LayoutTypes;
208};
209
210} // namespace CodeGen
211} // namespace clang
212
213#endif
Defines enum values for all the target-independent builtin functions.
#define GENERATE_HLSL_INTRINSIC_FUNCTION(FunctionName, IntrinsicPostfix)
llvm::dxil::ResourceClass ResourceClass
Defines helper utilities for supporting the HLSL runtime environment.
__DEVICE__ bool isinf(float __x)
Test for infinity value (+ve or -ve) .
__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)
llvm::Value * handleScalarSemanticLoad(llvm::IRBuilder<> &B, llvm::Type *Type, const clang::DeclaratorDecl *Decl, SemanticInfo &ActiveSemantic)
llvm::Value * emitSystemSemanticLoad(llvm::IRBuilder<> &B, llvm::Type *Type, const clang::DeclaratorDecl *Decl, SemanticInfo &ActiveSemantic)
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 void collectInputSemantic(llvm::IRBuilder<> &B, const DeclaratorDecl *D, llvm::Type *Type, SmallVectorImpl< llvm::Value * > &Inputs)
llvm::Type * convertHLSLSpecificType(const Type *T, SmallVector< int32_t > *Packoffsets=nullptr)
llvm::Value * handleSemanticLoad(llvm::IRBuilder<> &B, llvm::Type *Type, const clang::DeclaratorDecl *Decl, SemanticInfo &ActiveSemantic)
CGHLSLRuntime(CodeGenModule &CGM)
std::optional< LValue > emitResourceArraySubscriptExpr(const ArraySubscriptExpr *E, CodeGenFunction &CGF)
void addRootSignature(const HLSLRootSignatureDecl *D)
GENERATE_HLSL_INTRINSIC_FUNCTION(FlattenedThreadIdInGroup, flattened_thread_id_in_group) GENERATE_HLSL_INTRINSIC_FUNCTION(CreateResourceGetPointer
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.
LValue - This represents an lvalue references.
Definition CGValue.h:182
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition DeclBase.h:1449
Represents a ValueDecl that came out of a declarator.
Definition Decl.h:779
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
This represents a decl that may have a name.
Definition Decl.h:273
Represents a parameter to a function.
Definition Decl.h:1789
The base class of the type hierarchy.
Definition TypeBase.h:1833
Represents a variable declaration or definition.
Definition Decl.h:925
@ Type
The l-value was considered opaque, so the alignment was determined from a type.
Definition CGValue.h:154
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
Definition CGValue.h:145
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.