clang 20.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/IR/IRBuilder.h"
19#include "llvm/IR/Intrinsics.h"
20#include "llvm/IR/IntrinsicsDirectX.h"
21#include "llvm/IR/IntrinsicsSPIRV.h"
22
25
26#include "llvm/ADT/SmallVector.h"
27#include "llvm/ADT/StringRef.h"
28#include "llvm/Frontend/HLSL/HLSLResource.h"
29
30#include <optional>
31#include <vector>
32
33// A function generator macro for picking the right intrinsic
34// for the target backend
35#define GENERATE_HLSL_INTRINSIC_FUNCTION(FunctionName, IntrinsicPostfix) \
36 llvm::Intrinsic::ID get##FunctionName##Intrinsic() { \
37 llvm::Triple::ArchType Arch = getArch(); \
38 switch (Arch) { \
39 case llvm::Triple::dxil: \
40 return llvm::Intrinsic::dx_##IntrinsicPostfix; \
41 case llvm::Triple::spirv: \
42 return llvm::Intrinsic::spv_##IntrinsicPostfix; \
43 default: \
44 llvm_unreachable("Intrinsic " #IntrinsicPostfix \
45 " not supported by target architecture"); \
46 } \
47 }
48
49namespace llvm {
50class GlobalVariable;
51class Function;
52class StructType;
53} // namespace llvm
54
55namespace clang {
56class VarDecl;
57class ParmVarDecl;
58class HLSLBufferDecl;
59class HLSLResourceBindingAttr;
60class Type;
61class DeclContext;
62
63class FunctionDecl;
64
65namespace CodeGen {
66
67class CodeGenModule;
68
70public:
71 //===----------------------------------------------------------------------===//
72 // Start of reserved area for HLSL intrinsic getters.
73 //===----------------------------------------------------------------------===//
74
84 GENERATE_HLSL_INTRINSIC_FUNCTION(Saturate, saturate)
88 GENERATE_HLSL_INTRINSIC_FUNCTION(ThreadId, thread_id)
89 GENERATE_HLSL_INTRINSIC_FUNCTION(GroupThreadId, thread_id_in_group)
93 GENERATE_HLSL_INTRINSIC_FUNCTION(Dot4AddI8Packed, dot4add_i8packed)
94 GENERATE_HLSL_INTRINSIC_FUNCTION(Dot4AddU8Packed, dot4add_u8packed)
95 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveActiveAllTrue, wave_all)
96 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveActiveAnyTrue, wave_any)
97 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveActiveCountBits, wave_active_countbits)
98 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveIsFirstLane, wave_is_first_lane)
99 GENERATE_HLSL_INTRINSIC_FUNCTION(WaveReadLaneAt, wave_readlane)
100 GENERATE_HLSL_INTRINSIC_FUNCTION(FirstBitUHigh, firstbituhigh)
101 GENERATE_HLSL_INTRINSIC_FUNCTION(FirstBitSHigh, firstbitshigh)
105
106 GENERATE_HLSL_INTRINSIC_FUNCTION(CreateHandleFromBinding,
107 resource_handlefrombinding)
108 GENERATE_HLSL_INTRINSIC_FUNCTION(BufferUpdateCounter, resource_updatecounter)
109 GENERATE_HLSL_INTRINSIC_FUNCTION(GroupMemoryBarrierWithGroupSync,
110 group_memory_barrier_with_group_sync)
111
112 //===----------------------------------------------------------------------===//
113 // End of reserved area for HLSL intrinsic getters.
114 //===----------------------------------------------------------------------===//
115
117 // The ID like 2 in register(b2, space1).
118 std::optional<unsigned> Reg;
119 // The Space like 1 is register(b2, space1).
120 // Default value is 0.
121 unsigned Space;
122 BufferResBinding(HLSLResourceBindingAttr *Attr);
123 };
124 struct Buffer {
125 Buffer(const HLSLBufferDecl *D);
126 llvm::StringRef Name;
127 // IsCBuffer - Whether the buffer is a cbuffer (and not a tbuffer).
130 // Global variable and offset for each constant.
131 std::vector<std::pair<llvm::GlobalVariable *, unsigned>> Constants;
132 llvm::StructType *LayoutStruct = nullptr;
133 };
134
135protected:
137
138 llvm::Value *emitInputSemantic(llvm::IRBuilder<> &B, const ParmVarDecl &D,
139 llvm::Type *Ty);
140
141public:
143 virtual ~CGHLSLRuntime() {}
144
145 llvm::Type *convertHLSLSpecificType(const Type *T);
146
147 void annotateHLSLResource(const VarDecl *D, llvm::GlobalVariable *GV);
149
150 void addBuffer(const HLSLBufferDecl *D);
151 void finishCodeGen();
152
153 void setHLSLEntryAttributes(const FunctionDecl *FD, llvm::Function *Fn);
154
155 void emitEntryFunction(const FunctionDecl *FD, llvm::Function *Fn);
156 void setHLSLFunctionAttributes(const FunctionDecl *FD, llvm::Function *Fn);
157 void handleGlobalVarDefinition(const VarDecl *VD, llvm::GlobalVariable *Var);
158
160 llvm::Function *createResourceBindingInitFn();
161 llvm::Instruction *getConvergenceToken(llvm::BasicBlock &BB);
162
163private:
164 void addBufferResourceAnnotation(llvm::GlobalVariable *GV,
165 llvm::hlsl::ResourceClass RC,
166 llvm::hlsl::ResourceKind RK, bool IsROV,
167 llvm::hlsl::ElementType ET,
168 BufferResBinding &Binding);
169 void addConstant(VarDecl *D, Buffer &CB);
170 void addBufferDecls(const DeclContext *DC, Buffer &CB);
171 llvm::Triple::ArchType getArch();
173
175 ResourcesToBind;
176};
177
178} // namespace CodeGen
179} // namespace clang
180
181#endif
MatchType Type
Defines enum values for all the target-independent builtin functions.
#define GENERATE_HLSL_INTRINSIC_FUNCTION(FunctionName, IntrinsicPostfix)
Definition: CGHLSLRuntime.h:35
const Decl * D
Defines helper utilities for supporting the HLSL runtime environment.
__DEVICE__ double rsqrt(double __a)
Attr - This represents one attribute.
Definition: Attr.h:43
llvm::Instruction * getConvergenceToken(llvm::BasicBlock &BB)
void setHLSLEntryAttributes(const FunctionDecl *FD, llvm::Function *Fn)
void setHLSLFunctionAttributes(const FunctionDecl *FD, llvm::Function *Fn)
void emitEntryFunction(const FunctionDecl *FD, llvm::Function *Fn)
void handleGlobalVarDefinition(const VarDecl *VD, llvm::GlobalVariable *Var)
llvm::Type * convertHLSLSpecificType(const Type *T)
llvm::Value * emitInputSemantic(llvm::IRBuilder<> &B, const ParmVarDecl &D, llvm::Type *Ty)
llvm::Function * createResourceBindingInitFn()
CGHLSLRuntime(CodeGenModule &CGM)
void annotateHLSLResource(const VarDecl *D, llvm::GlobalVariable *GV)
void addBuffer(const HLSLBufferDecl *D)
This class organizes the cross-function state that is used while generating LLVM code.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1435
Represents a function declaration or definition.
Definition: Decl.h:1935
HLSLBufferDecl - Represent a cbuffer or tbuffer declaration.
Definition: Decl.h:4927
Represents a parameter to a function.
Definition: Decl.h:1725
The base class of the type hierarchy.
Definition: Type.h:1828
Represents a variable declaration or definition.
Definition: Decl.h:882
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 length(float)
Return the length of vector p, i.e., sqrt(p.x2 + p.y 2 + ...)
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.
std::vector< std::pair< llvm::GlobalVariable *, unsigned > > Constants