LLVM 24.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 "DirectX.h"
13#include "llvm/IR/IRBuilder.h"
15#include "llvm/IR/IntrinsicsDirectX.h"
18#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->eraseFromParent();
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;
54 for (const hlsl::CBufferMapping &Mapping : *CBufMD) {
55 CBufferHandles.insert(Mapping.Handle);
56 for (const hlsl::CBufferMember &Member : Mapping.Members)
57 CBufferGlobals.push_back(Member.GV);
58 }
60
61 for (const hlsl::CBufferMapping &Mapping : *CBufMD)
62 for (const hlsl::CBufferMember &Member : Mapping.Members)
63 replaceUsersOfGlobal(Member.GV, Mapping.Handle, Member.Offset);
64
65 // Remove cbuffer handle globals from @llvm.compiler.used list.
66 llvm::removeFromUsedLists(M, [&](Constant *C) -> bool {
67 auto *GV = dyn_cast<GlobalVariable>(C);
68 return GV && CBufferHandles.contains(GV);
69 });
70 for (GlobalVariable *HandleGV : CBufferHandles)
71 HandleGV->removeDeadConstantUsers();
72
73 CBufMD->eraseFromModule();
74 return true;
75}
76
86
87namespace {
88class DXILCBufferAccessLegacy : public ModulePass {
89public:
90 bool runOnModule(Module &M) override { return replaceCBufferAccesses(M); }
91 StringRef getPassName() const override { return "DXIL CBuffer Access"; }
92 DXILCBufferAccessLegacy() : ModulePass(ID) {}
93
94 static char ID; // Pass identification.
95};
96char DXILCBufferAccessLegacy::ID = 0;
97} // end anonymous namespace
98
99INITIALIZE_PASS(DXILCBufferAccessLegacy, DEBUG_TYPE, "DXIL CBuffer Access",
100 false, false)
101
103 return new DXILCBufferAccessLegacy();
104}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
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
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
This is an important base class in LLVM.
Definition Constant.h:44
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:2947
An instruction for reading from memory.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition Pass.h:253
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
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
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
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:315
static LLVM_ABI std::optional< CBufferMetadata > get(Module &M, llvm::function_ref< bool(Type *)> IsPadding)
Definition CBuffer.cpp:38
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
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:649
ModulePass * createDXILCBufferAccessLegacyPass()
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
LLVM_ABI void removeFromUsedLists(Module &M, function_ref< bool(Constant *)> ShouldRemove)
Removes global values from the llvm.used and llvm.compiler.used arrays.
@ Global
Append to llvm.global_dtors.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39