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
80 GENERATE_HLSL_INTRINSIC_FUNCTION(ThreadId, thread_id)
81
82 //===----------------------------------------------------------------------===//
83 // End of reserved area for HLSL intrinsic getters.
84 //===----------------------------------------------------------------------===//
85
87 // The ID like 2 in register(b2, space1).
88 std::optional<unsigned> Reg;
89 // The Space like 1 is register(b2, space1).
90 // Default value is 0.
91 unsigned Space;
92 BufferResBinding(HLSLResourceBindingAttr *Attr);
93 };
94 struct Buffer {
95 Buffer(const HLSLBufferDecl *D);
96 llvm::StringRef Name;
97 // IsCBuffer - Whether the buffer is a cbuffer (and not a tbuffer).
100 // Global variable and offset for each constant.
101 std::vector<std::pair<llvm::GlobalVariable *, unsigned>> Constants;
102 llvm::StructType *LayoutStruct = nullptr;
103 };
104
105protected:
107
108 llvm::Value *emitInputSemantic(llvm::IRBuilder<> &B, const ParmVarDecl &D,
109 llvm::Type *Ty);
110
111public:
113 virtual ~CGHLSLRuntime() {}
114
115 void annotateHLSLResource(const VarDecl *D, llvm::GlobalVariable *GV);
117
118 void addBuffer(const HLSLBufferDecl *D);
119 void finishCodeGen();
120
121 void setHLSLEntryAttributes(const FunctionDecl *FD, llvm::Function *Fn);
122
123 void emitEntryFunction(const FunctionDecl *FD, llvm::Function *Fn);
124 void setHLSLFunctionAttributes(llvm::Function *, const FunctionDecl *);
125
126private:
127 void addBufferResourceAnnotation(llvm::GlobalVariable *GV,
128 llvm::hlsl::ResourceClass RC,
129 llvm::hlsl::ResourceKind RK, bool IsROV,
130 llvm::hlsl::ElementType ET,
131 BufferResBinding &Binding);
132 void addConstant(VarDecl *D, Buffer &CB);
133 void addBufferDecls(const DeclContext *DC, Buffer &CB);
134 llvm::Triple::ArchType getArch();
136};
137
138} // namespace CodeGen
139} // namespace clang
140
141#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:42
void setHLSLEntryAttributes(const FunctionDecl *FD, llvm::Function *Fn)
void emitEntryFunction(const FunctionDecl *FD, llvm::Function *Fn)
llvm::Value * emitInputSemantic(llvm::IRBuilder<> &B, const ParmVarDecl &D, llvm::Type *Ty)
CGHLSLRuntime(CodeGenModule &CGM)
void annotateHLSLResource(const VarDecl *D, llvm::GlobalVariable *GV)
void addBuffer(const HLSLBufferDecl *D)
void setHLSLFunctionAttributes(llvm::Function *, const FunctionDecl *)
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:1425
Represents a function declaration or definition.
Definition: Decl.h:1932
HLSLBufferDecl - Represent a cbuffer or tbuffer declaration.
Definition: Decl.h:4920
Represents a parameter to a function.
Definition: Decl.h:1722
Represents a variable declaration or definition.
Definition: Decl.h:879
The JSON file list parser is used to communicate input to InstallAPI.
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30
int __ovld __cnfn all(char)
Returns 1 if the most significant bit in all components of x is set; otherwise returns 0.
int __ovld __cnfn any(char)
Returns 1 if the most significant bit in any component of x is set; otherwise returns 0.
std::vector< std::pair< llvm::GlobalVariable *, unsigned > > Constants