
Adds `@_init_resource_bindings()` function to module initialization that includes `handle.fromBinding` intrinsic calls for simple resource declarations. Arrays of resources or resources inside user defined types are not supported yet. While this unblocks our progress on [Compile a runnable shader from clang](https://github.com/llvm/wg-hlsl/issues/7) milestone, this is probably not the way we would like to handle resource binding initialization going forward. Ideally, it should be done via the resource class constructors in order to support dynamic resource binding or unbounded arrays if resources. Depends on PRs #110327 and #111203. Part 1 of #105076
166 lines
6.0 KiB
C++
166 lines
6.0 KiB
C++
//===----- CGHLSLRuntime.h - Interface to HLSL Runtimes -----*- C++ -*-===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This provides an abstract class for HLSL code generation. Concrete
|
|
// subclasses of this implement code generation for specific HLSL
|
|
// runtime libraries.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef LLVM_CLANG_LIB_CODEGEN_CGHLSLRUNTIME_H
|
|
#define LLVM_CLANG_LIB_CODEGEN_CGHLSLRUNTIME_H
|
|
|
|
#include "llvm/IR/IRBuilder.h"
|
|
#include "llvm/IR/Intrinsics.h"
|
|
#include "llvm/IR/IntrinsicsDirectX.h"
|
|
#include "llvm/IR/IntrinsicsSPIRV.h"
|
|
|
|
#include "clang/Basic/Builtins.h"
|
|
#include "clang/Basic/HLSLRuntime.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
#include "llvm/ADT/StringRef.h"
|
|
#include "llvm/Frontend/HLSL/HLSLResource.h"
|
|
|
|
#include <optional>
|
|
#include <vector>
|
|
|
|
// A function generator macro for picking the right intrinsic
|
|
// for the target backend
|
|
#define GENERATE_HLSL_INTRINSIC_FUNCTION(FunctionName, IntrinsicPostfix) \
|
|
llvm::Intrinsic::ID get##FunctionName##Intrinsic() { \
|
|
llvm::Triple::ArchType Arch = getArch(); \
|
|
switch (Arch) { \
|
|
case llvm::Triple::dxil: \
|
|
return llvm::Intrinsic::dx_##IntrinsicPostfix; \
|
|
case llvm::Triple::spirv: \
|
|
return llvm::Intrinsic::spv_##IntrinsicPostfix; \
|
|
default: \
|
|
llvm_unreachable("Intrinsic " #IntrinsicPostfix \
|
|
" not supported by target architecture"); \
|
|
} \
|
|
}
|
|
|
|
namespace llvm {
|
|
class GlobalVariable;
|
|
class Function;
|
|
class StructType;
|
|
} // namespace llvm
|
|
|
|
namespace clang {
|
|
class VarDecl;
|
|
class ParmVarDecl;
|
|
class HLSLBufferDecl;
|
|
class HLSLResourceBindingAttr;
|
|
class Type;
|
|
class DeclContext;
|
|
|
|
class FunctionDecl;
|
|
|
|
namespace CodeGen {
|
|
|
|
class CodeGenModule;
|
|
|
|
class CGHLSLRuntime {
|
|
public:
|
|
//===----------------------------------------------------------------------===//
|
|
// Start of reserved area for HLSL intrinsic getters.
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
GENERATE_HLSL_INTRINSIC_FUNCTION(All, all)
|
|
GENERATE_HLSL_INTRINSIC_FUNCTION(Any, any)
|
|
GENERATE_HLSL_INTRINSIC_FUNCTION(Cross, cross)
|
|
GENERATE_HLSL_INTRINSIC_FUNCTION(Degrees, degrees)
|
|
GENERATE_HLSL_INTRINSIC_FUNCTION(Frac, frac)
|
|
GENERATE_HLSL_INTRINSIC_FUNCTION(Length, length)
|
|
GENERATE_HLSL_INTRINSIC_FUNCTION(Lerp, lerp)
|
|
GENERATE_HLSL_INTRINSIC_FUNCTION(Normalize, normalize)
|
|
GENERATE_HLSL_INTRINSIC_FUNCTION(Rsqrt, rsqrt)
|
|
GENERATE_HLSL_INTRINSIC_FUNCTION(Saturate, saturate)
|
|
GENERATE_HLSL_INTRINSIC_FUNCTION(Sign, sign)
|
|
GENERATE_HLSL_INTRINSIC_FUNCTION(Step, step)
|
|
GENERATE_HLSL_INTRINSIC_FUNCTION(Radians, radians)
|
|
GENERATE_HLSL_INTRINSIC_FUNCTION(ThreadId, thread_id)
|
|
GENERATE_HLSL_INTRINSIC_FUNCTION(FDot, fdot)
|
|
GENERATE_HLSL_INTRINSIC_FUNCTION(SDot, sdot)
|
|
GENERATE_HLSL_INTRINSIC_FUNCTION(UDot, udot)
|
|
GENERATE_HLSL_INTRINSIC_FUNCTION(WaveIsFirstLane, wave_is_first_lane)
|
|
GENERATE_HLSL_INTRINSIC_FUNCTION(WaveReadLaneAt, wave_readlane)
|
|
|
|
GENERATE_HLSL_INTRINSIC_FUNCTION(CreateHandleFromBinding, handle_fromBinding)
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// End of reserved area for HLSL intrinsic getters.
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
struct BufferResBinding {
|
|
// The ID like 2 in register(b2, space1).
|
|
std::optional<unsigned> Reg;
|
|
// The Space like 1 is register(b2, space1).
|
|
// Default value is 0.
|
|
unsigned Space;
|
|
BufferResBinding(HLSLResourceBindingAttr *Attr);
|
|
};
|
|
struct Buffer {
|
|
Buffer(const HLSLBufferDecl *D);
|
|
llvm::StringRef Name;
|
|
// IsCBuffer - Whether the buffer is a cbuffer (and not a tbuffer).
|
|
bool IsCBuffer;
|
|
BufferResBinding Binding;
|
|
// Global variable and offset for each constant.
|
|
std::vector<std::pair<llvm::GlobalVariable *, unsigned>> Constants;
|
|
llvm::StructType *LayoutStruct = nullptr;
|
|
};
|
|
|
|
protected:
|
|
CodeGenModule &CGM;
|
|
|
|
llvm::Value *emitInputSemantic(llvm::IRBuilder<> &B, const ParmVarDecl &D,
|
|
llvm::Type *Ty);
|
|
|
|
public:
|
|
CGHLSLRuntime(CodeGenModule &CGM) : CGM(CGM) {}
|
|
virtual ~CGHLSLRuntime() {}
|
|
|
|
llvm::Type *convertHLSLSpecificType(const Type *T);
|
|
|
|
void annotateHLSLResource(const VarDecl *D, llvm::GlobalVariable *GV);
|
|
void generateGlobalCtorDtorCalls();
|
|
|
|
void addBuffer(const HLSLBufferDecl *D);
|
|
void finishCodeGen();
|
|
|
|
void setHLSLEntryAttributes(const FunctionDecl *FD, llvm::Function *Fn);
|
|
|
|
void emitEntryFunction(const FunctionDecl *FD, llvm::Function *Fn);
|
|
void setHLSLFunctionAttributes(const FunctionDecl *FD, llvm::Function *Fn);
|
|
void handleGlobalVarDefinition(const VarDecl *VD, llvm::GlobalVariable *Var);
|
|
|
|
bool needsResourceBindingInitFn();
|
|
llvm::Function *createResourceBindingInitFn();
|
|
|
|
private:
|
|
void addBufferResourceAnnotation(llvm::GlobalVariable *GV,
|
|
llvm::hlsl::ResourceClass RC,
|
|
llvm::hlsl::ResourceKind RK, bool IsROV,
|
|
llvm::hlsl::ElementType ET,
|
|
BufferResBinding &Binding);
|
|
void addConstant(VarDecl *D, Buffer &CB);
|
|
void addBufferDecls(const DeclContext *DC, Buffer &CB);
|
|
llvm::Triple::ArchType getArch();
|
|
llvm::SmallVector<Buffer> Buffers;
|
|
|
|
llvm::SmallVector<std::pair<const VarDecl *, llvm::GlobalVariable *>>
|
|
ResourcesToBind;
|
|
};
|
|
|
|
} // namespace CodeGen
|
|
} // namespace clang
|
|
|
|
#endif
|