Path: blob/main/contrib/llvm-project/clang/lib/CodeGen/CGHLSLRuntime.h
35233 views
//===----- CGHLSLRuntime.h - Interface to HLSL Runtimes -----*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This provides an abstract class for HLSL code generation. Concrete9// subclasses of this implement code generation for specific HLSL10// runtime libraries.11//12//===----------------------------------------------------------------------===//1314#ifndef LLVM_CLANG_LIB_CODEGEN_CGHLSLRUNTIME_H15#define LLVM_CLANG_LIB_CODEGEN_CGHLSLRUNTIME_H1617#include "llvm/IR/IRBuilder.h"18#include "llvm/IR/Intrinsics.h"19#include "llvm/IR/IntrinsicsDirectX.h"20#include "llvm/IR/IntrinsicsSPIRV.h"2122#include "clang/Basic/Builtins.h"23#include "clang/Basic/HLSLRuntime.h"2425#include "llvm/ADT/SmallVector.h"26#include "llvm/ADT/StringRef.h"27#include "llvm/Frontend/HLSL/HLSLResource.h"2829#include <optional>30#include <vector>3132// A function generator macro for picking the right intrinsic33// for the target backend34#define GENERATE_HLSL_INTRINSIC_FUNCTION(FunctionName, IntrinsicPostfix) \35llvm::Intrinsic::ID get##FunctionName##Intrinsic() { \36llvm::Triple::ArchType Arch = getArch(); \37switch (Arch) { \38case llvm::Triple::dxil: \39return llvm::Intrinsic::dx_##IntrinsicPostfix; \40case llvm::Triple::spirv: \41return llvm::Intrinsic::spv_##IntrinsicPostfix; \42default: \43llvm_unreachable("Intrinsic " #IntrinsicPostfix \44" not supported by target architecture"); \45} \46}4748namespace llvm {49class GlobalVariable;50class Function;51class StructType;52} // namespace llvm5354namespace clang {55class VarDecl;56class ParmVarDecl;57class HLSLBufferDecl;58class HLSLResourceBindingAttr;59class Type;60class DeclContext;6162class FunctionDecl;6364namespace CodeGen {6566class CodeGenModule;6768class CGHLSLRuntime {69public:70//===----------------------------------------------------------------------===//71// Start of reserved area for HLSL intrinsic getters.72//===----------------------------------------------------------------------===//7374GENERATE_HLSL_INTRINSIC_FUNCTION(All, all)75GENERATE_HLSL_INTRINSIC_FUNCTION(Any, any)76GENERATE_HLSL_INTRINSIC_FUNCTION(Lerp, lerp)77GENERATE_HLSL_INTRINSIC_FUNCTION(Rsqrt, rsqrt)78GENERATE_HLSL_INTRINSIC_FUNCTION(ThreadId, thread_id)7980//===----------------------------------------------------------------------===//81// End of reserved area for HLSL intrinsic getters.82//===----------------------------------------------------------------------===//8384struct BufferResBinding {85// The ID like 2 in register(b2, space1).86std::optional<unsigned> Reg;87// The Space like 1 is register(b2, space1).88// Default value is 0.89unsigned Space;90BufferResBinding(HLSLResourceBindingAttr *Attr);91};92struct Buffer {93Buffer(const HLSLBufferDecl *D);94llvm::StringRef Name;95// IsCBuffer - Whether the buffer is a cbuffer (and not a tbuffer).96bool IsCBuffer;97BufferResBinding Binding;98// Global variable and offset for each constant.99std::vector<std::pair<llvm::GlobalVariable *, unsigned>> Constants;100llvm::StructType *LayoutStruct = nullptr;101};102103protected:104CodeGenModule &CGM;105106llvm::Value *emitInputSemantic(llvm::IRBuilder<> &B, const ParmVarDecl &D,107llvm::Type *Ty);108109public:110CGHLSLRuntime(CodeGenModule &CGM) : CGM(CGM) {}111virtual ~CGHLSLRuntime() {}112113void annotateHLSLResource(const VarDecl *D, llvm::GlobalVariable *GV);114void generateGlobalCtorDtorCalls();115116void addBuffer(const HLSLBufferDecl *D);117void finishCodeGen();118119void setHLSLEntryAttributes(const FunctionDecl *FD, llvm::Function *Fn);120121void emitEntryFunction(const FunctionDecl *FD, llvm::Function *Fn);122void setHLSLFunctionAttributes(llvm::Function *, const FunctionDecl *);123124private:125void addBufferResourceAnnotation(llvm::GlobalVariable *GV,126llvm::hlsl::ResourceClass RC,127llvm::hlsl::ResourceKind RK, bool IsROV,128llvm::hlsl::ElementType ET,129BufferResBinding &Binding);130void addConstant(VarDecl *D, Buffer &CB);131void addBufferDecls(const DeclContext *DC, Buffer &CB);132llvm::Triple::ArchType getArch();133llvm::SmallVector<Buffer> Buffers;134};135136} // namespace CodeGen137} // namespace clang138139#endif140141142