LLVM 22.0.0git
DXILCBufferAccess.cpp
Go to the documentation of this file.
1//===- DXILCBufferAccess.cpp - Translate CBuffer Loads --------------------===//
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#include "DXILCBufferAccess.h"
10#include "DirectX.h"
14#include "llvm/IR/IRBuilder.h"
16#include "llvm/IR/IntrinsicsDirectX.h"
19#include "llvm/Pass.h"
22
23#define DEBUG_TYPE "dxil-cbuffer-access"
24using namespace llvm;
25
27 GlobalVariable *HandleGV, size_t Offset) {
28 for (Use &U : make_early_inc_range(Global->uses())) {
29 auto UseInst = dyn_cast<Instruction>(U.getUser());
30 // TODO: Constants? Metadata?
31 assert(UseInst && "Non-instruction use of cbuffer");
32
33 IRBuilder<> Builder(UseInst);
34 LoadInst *Handle = Builder.CreateLoad(HandleGV->getValueType(), HandleGV,
35 HandleGV->getName());
36 Value *Ptr = Builder.CreateIntrinsic(
37 Global->getType(), Intrinsic::dx_resource_getpointer,
38 ArrayRef<Value *>{Handle,
39 ConstantInt::get(Builder.getInt32Ty(), Offset)});
40 U.set(Ptr);
41 }
42
43 Global->removeFromParent();
44}
45
47 std::optional<hlsl::CBufferMetadata> CBufMD = hlsl::CBufferMetadata::get(
48 M, [](Type *Ty) { return isa<llvm::dxil::PaddingExtType>(Ty); });
49 if (!CBufMD)
50 return false;
51
52 SmallVector<Constant *> CBufferGlobals;
53 for (const hlsl::CBufferMapping &Mapping : *CBufMD)
54 for (const hlsl::CBufferMember &Member : Mapping.Members)
55 CBufferGlobals.push_back(Member.GV);
57
58 for (const hlsl::CBufferMapping &Mapping : *CBufMD)
59 for (const hlsl::CBufferMember &Member : Mapping.Members)
60 replaceUsersOfGlobal(Member.GV, Mapping.Handle, Member.Offset);
61
62 CBufMD->eraseFromModule();
63 return true;
64}
65
74
75namespace {
76class DXILCBufferAccessLegacy : public ModulePass {
77public:
78 bool runOnModule(Module &M) override { return replaceCBufferAccesses(M); }
79 StringRef getPassName() const override { return "DXIL CBuffer Access"; }
80 DXILCBufferAccessLegacy() : ModulePass(ID) {}
81
82 static char ID; // Pass identification.
83};
84char DXILCBufferAccessLegacy::ID = 0;
85} // end anonymous namespace
86
87INITIALIZE_PASS(DXILCBufferAccessLegacy, DEBUG_TYPE, "DXIL CBuffer Access",
88 false, false)
89
91 return new DXILCBufferAccessLegacy();
92}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool replaceCBufferAccesses(Module &M)
static void replaceUsersOfGlobal(GlobalVariable *Global, GlobalVariable *HandleGV, size_t Offset)
#define DEBUG_TYPE
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Type * getValueType() const
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2788
An instruction for reading from memory.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition Pass.h:255
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM Value Representation.
Definition Value.h:75
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
static std::optional< CBufferMetadata > get(Module &M, llvm::function_ref< bool(Type *)> IsPadding)
Definition CBuffer.cpp:39
Changed
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:532
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:632
ModulePass * createDXILCBufferAccessLegacyPass()
Pass to translate loads in the cbuffer address space to intrinsics.
LLVM_ABI bool convertUsersOfConstantsToInstructions(ArrayRef< Constant * > Consts, Function *RestrictToFunc=nullptr, bool RemoveDeadConstants=true, bool IncludeSelf=false)
Replace constant expressions users of the given constants with instructions.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
@ Global
Append to llvm.global_dtors.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39