22#include "llvm/ADT/STLExtras.h"
26#define GEN_PASS_DEF_STACKTOSHAREDPASS
27#include "mlir/Dialect/OpenMP/Transforms/Passes.h.inc"
39 return omp::allocaUsesRequireSharedMem(result);
50 IntegerAttr alignment,
55 Operation *terminator = block.getTerminator();
59 omp::FreeSharedMemOp::create(builder, allocVal.
getLoc(), elemType,
60 arraySize, alignment, allocVal);
66class StackToSharedPass
69 StackToSharedPass() =
default;
71 void runOnOperation()
override {
73 OpBuilder builder(context);
75 LLVM::LLVMFuncOp funcOp = getOperation();
76 auto offloadIface = funcOp->getParentOfType<omp::OffloadModuleInterface>();
77 if (!offloadIface || !offloadIface.getIsTargetDevice())
80 llvm::SmallVector<Operation *> toBeDeleted;
81 funcOp->walk([&](LLVM::AllocaOp allocaOp) {
85 Type resultType = allocaOp.getResult().getType();
91 bool nonDefaultAddrSpace =
false;
92 if (
auto llvmPtrType = dyn_cast<LLVM::LLVMPointerType>(resultType))
93 nonDefaultAddrSpace = llvmPtrType.getAddressSpace() != 0;
95 builder.setInsertionPoint(allocaOp);
96 auto sharedAllocOp = omp::AllocSharedMemOp::create(
97 builder, allocaOp->getLoc(), LLVM::LLVMPointerType::get(context),
98 allocaOp.getElemTypeAttr(), allocaOp.getArraySize(),
99 allocaOp.getAlignmentAttr());
100 if (nonDefaultAddrSpace) {
101 assert(allocaOp->hasOneUse() &&
" unsupported non-default address "
102 "space alloca with multiple uses");
104 cast<LLVM::AddrSpaceCastOp>(*allocaOp->getUsers().begin());
105 asCastOp.replaceAllUsesWith(sharedAllocOp.getOperation());
109 toBeDeleted.push_back(asCastOp);
110 toBeDeleted.push_back(allocaOp);
112 allocaOp.replaceAllUsesWith(sharedAllocOp.getOperation());
119 builder, sharedAllocOp.getMemElemTypeAttr(),
120 sharedAllocOp.getMemArraySize(), sharedAllocOp.getMemAlignmentAttr(),
121 sharedAllocOp.getResult());
123 for (Operation *op : toBeDeleted)
static void insertDeviceSharedMemDeallocation(OpBuilder &builder, TypeAttr elemType, Value arraySize, IntegerAttr alignment, Value allocVal)
Based on the location of the definition of the given value representing the result of a device shared...
static bool shouldReplaceAllocaWithDeviceSharedMem(Operation &op)
Tell whether to replace an operation representing a stack allocation with a device shared memory allo...
Block represents an ordered list of Operations.
A class for computing basic dominance information.
bool dominates(Operation *a, Operation *b) const
Return true if operation A dominates operation B, i.e.
This class helps build Operations.
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
Operation is the basic unit of execution within MLIR.
result_range getResults()
BlockListType & getBlocks()
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Block * getParentBlock()
Return the Block in which this Value is defined.
Location getLoc() const
Return the location of this value.
Region * getParentRegion()
Return the Region in which this Value is defined.
bool opInSharedDeviceContext(Operation &op)
Check whether the given operation is located in a context where an allocation to be used by multiple ...
Include the generated interface declarations.