40#include "llvm/IR/IntrinsicsSPIRV.h"
49#define DEBUG_TYPE "spirv-lower-dynamic-local-memory"
63 return ArrTy && ArrTy->getNumElements() == 0;
77 F.getParent()->getFunctionList().insert(
F.getIterator(), NewF);
82 for (
auto [OldArg, NewArg] :
zip(
F.args(), NewF->
args())) {
83 OldArg.replaceAllUsesWith(&NewArg);
84 NewArg.takeName(&OldArg);
89 F.replaceAllUsesWith(NewF);
99 while (!Worklist.
empty()) {
104 if (!Visited.
insert(Current).second)
110 for (
auto &U : Current->
uses()) {
112 if (CB->isCallee(&U)) {
113 auto *Caller = CB->getCaller();
117 CB->setMemoryEffects(CB->getMemoryEffects() | NewEffects);
137 M.getModuleFlag(
"spirv-dynamic-local-memory"));
143 if (isDynamicLocalMemory(GV) && !GV.
use_empty())
151 Kernels.push_back(&
F);
162 Slot->setAlignment(M.getDataLayout().getPointerABIAlignment(LocalAS));
169 ConstantInt::get(MarkerTy, 1),
"__spirv_dynamic_local_memory_present",
182 "' cannot be referenced by a constant");
185 UsesOfGV[
F].push_back(&U);
188 for (
auto &[
F,
Uses] : UsesOfGV) {
189 IRBuilder<> Builder(
F->getEntryBlock().getFirstNonPHIOrAlloca());
190 auto *LoadedSlot = Builder.CreateLoad(LocalPtrTy, Slot,
"dyn_local_mem");
206 Function *NewF = appendKernelParam(*
F, LocalPtrTy);
212 Type *ThreadIdTy = M.getDataLayout().getIntPtrType(M.getContext(), LocalAS);
213 Value *IsFirst = Builder.getTrue();
214 for (
unsigned Dim = 0; Dim != 3; ++Dim) {
217 Builder.CreateIntrinsic(Intrinsic::spv_thread_id_in_group,
218 {ThreadIdTy}, {Builder.getInt32(Dim)});
219 IsFirst = Builder.CreateAnd(
221 Builder.CreateICmpEQ(ThreadId, ConstantInt::get(ThreadIdTy, 0)));
227 Builder.SetInsertPoint(ThenTerm);
231 Builder.SetInsertPoint(InsertBefore);
232 Builder.CreateIntrinsic(Intrinsic::spv_group_memory_barrier_with_group_sync,
242class SPIRVLowerDynamicLocalMemoryLegacy :
public ModulePass {
249 return "SPIRV lower dynamic local memory";
252 bool runOnModule(
Module &M)
override {
253 return lowerDynamicLocalMemory(M, TM);
268char SPIRVLowerDynamicLocalMemoryLegacy::ID = 0;
271 "spirv-lower-dynamic-local-memory",
272 "Lower SPIR-V dynamic local memory to kernel arguments",
false,
277 return new SPIRVLowerDynamicLocalMemoryLegacy(TM);
This file defines the DenseMap class.
Module.h This file contains the declarations for the Module class.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Remove Loads Into Fake Uses
This file defines the SmallVector class.
LLVM_ABI const_iterator getFirstNonPHIOrAlloca() const
Returns an iterator to the first instruction in this block that is not a PHINode, a static alloca,...
static LLVM_ABI FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
void splice(Function::iterator ToIt, Function *FromF)
Transfer all blocks from FromF to this function at ToIt.
const BasicBlock & getEntryBlock() const
iterator_range< arg_iterator > args()
MemoryEffects getMemoryEffects() const
void setMemoryEffects(MemoryEffects ME)
Argument * getArg(unsigned i) const
void copyAttributesFrom(const Function *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a Function) from the ...
LLVM_ABI void copyMetadata(const GlobalObject *Src, unsigned Offset)
Copy metadata from Src, adjusting offsets by Offset.
LLVM_ABI void setComdat(Comdat *C)
LLVM_ABI bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
unsigned getAddressSpace() const
@ InternalLinkage
Rename collisions when linking (static functions).
@ ExternalLinkage
Externally visible function.
Type * getValueType() const
LLVM_ABI void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
static MemoryEffectsBase otherMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
A Module instance is used to store all the information related to an LLVM module.
static LLVM_ABI PointerType * get(LLVMContext &C, unsigned AddressSpace)
This constructs an opaque pointer to an object in a numbered address space.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
const SPIRVSubtarget * getSubtargetImpl() const
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void push_back(const T &Elt)
LLVM_NO_SANITIZE_CFI_UNRELATED_CAST iterator begin()
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.
The instances of the Type class are immutable: once they are created, they are never changed.
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
A Use represents the edge between a Value definition and its users.
LLVM Value Representation.
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
iterator_range< use_iterator > uses()
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
@ SPIR_KERNEL
Used for SPIR kernel functions.
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract_or_null(Y &&MD)
Extract a Value from Metadata, allowing null.
This is an optimization pass for GlobalISel generic memory operations.
detail::zippy< detail::zip_shortest, T, U, Args... > zip(T &&t, U &&u, Args &&...args)
zip iterator for two or more iteratable types.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
MemoryEffectsBase< IRMemLocation > MemoryEffects
Summary of how a function affects memory in the program.
constexpr unsigned storageClassToAddressSpace(SPIRV::StorageClass::StorageClass SC)
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.
ModulePass * createSPIRVLowerDynamicLocalMemoryPass(const SPIRVTargetMachine &TM)
@ Ref
The access may reference the value stored in memory.
@ ModRef
The access may reference and may modify the value stored in memory.
LLVM_ABI Instruction * SplitBlockAndInsertIfThen(Value *Cond, BasicBlock::iterator SplitBefore, bool Unreachable, MDNode *BranchWeights=nullptr, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, BasicBlock *ThenBlock=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.