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"
14#include "llvm/IR/IntrinsicsDirectX.h"
16#include "llvm/Pass.h"
19
20#define DEBUG_TYPE "dxil-cbuffer-access"
21using namespace llvm;
22
24 GlobalVariable *HandleGV, size_t Offset) {
25 for (Use &U : make_early_inc_range(Global->uses())) {
26 auto UseInst = dyn_cast<Instruction>(U.getUser());
27 // TODO: Constants? Metadata?
28 assert(UseInst && "Non-instruction use of cbuffer");
29
30 IRBuilder<> Builder(UseInst);
31 LoadInst *Handle = Builder.CreateLoad(HandleGV->getValueType(), HandleGV,
32 HandleGV->getName());
33 Value *Ptr = Builder.CreateIntrinsic(
34 Global->getType(), Intrinsic::dx_resource_getpointer,
35 ArrayRef<Value *>{Handle,
36 ConstantInt::get(Builder.getInt32Ty(), Offset)});
37 U.set(Ptr);
38 }
39
40 Global->eraseFromParent();
41}
42
44 std::optional<hlsl::CBufferMetadata> CBufMD = hlsl::CBufferMetadata::get(
45 M, [](Type *Ty) { return isa<llvm::dxil::PaddingExtType>(Ty); });
46 if (!CBufMD)
47 return false;
48
49 SmallVector<Constant *> CBufferGlobals;
51 for (const hlsl::CBufferMapping &Mapping : *CBufMD) {
52 CBufferHandles.insert(Mapping.Handle);
53 for (const hlsl::CBufferMember &Member : Mapping.Members)
54 CBufferGlobals.push_back(Member.GV);
55 }
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 // Remove cbuffer handle globals from @llvm.compiler.used list.
63 llvm::removeFromUsedLists(M, [&](Constant *C) -> bool {
64 auto *GV = dyn_cast<GlobalVariable>(C);
65 return GV && CBufferHandles.contains(GV);
66 });
67 for (GlobalVariable *HandleGV : CBufferHandles)
68 HandleGV->removeDeadConstantUsers();
69
70 CBufMD->eraseFromModule();
71 return true;
72}
73
83
84namespace {
85class DXILCBufferAccessLegacy : public ModulePass {
86public:
87 bool runOnModule(Module &M) override { return replaceCBufferAccesses(M); }
88 StringRef getPassName() const override { return "DXIL CBuffer Access"; }
89 DXILCBufferAccessLegacy() : ModulePass(ID) {}
90
91 static char ID; // Pass identification.
92};
93char DXILCBufferAccessLegacy::ID = 0;
94} // end anonymous namespace
95
96INITIALIZE_PASS(DXILCBufferAccessLegacy, DEBUG_TYPE, "DXIL CBuffer Access",
97 false, false)
98
100 return new DXILCBufferAccessLegacy();
101}
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:2949
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:314
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