20#include "llvm/ADT/STLExtras.h"
24#define GEN_PASS_DEF_STACKTOSHAREDPASS
25#include "mlir/Dialect/OpenMP/Transforms/Passes.h.inc"
37 return omp::allocaUsesRequireSharedMem(result);
48 IntegerAttr alignment,
53 Operation *terminator = block.getTerminator();
57 omp::FreeSharedMemOp::create(builder, allocVal.
getLoc(), elemType,
58 arraySize, alignment, allocVal);
64class StackToSharedPass
65 :
public omp::impl::StackToSharedPassBase<StackToSharedPass> {
67 StackToSharedPass() =
default;
69 void runOnOperation()
override {
71 OpBuilder builder(context);
73 LLVM::LLVMFuncOp funcOp = getOperation();
74 auto offloadIface = funcOp->getParentOfType<omp::OffloadModuleInterface>();
75 if (!offloadIface || !offloadIface.getIsTargetDevice())
78 llvm::SmallVector<Operation *> toBeDeleted;
79 funcOp->walk([&](LLVM::AllocaOp allocaOp) {
83 Type resultType = allocaOp.getResult().getType();
89 bool nonDefaultAddrSpace =
false;
90 if (
auto llvmPtrType = dyn_cast<LLVM::LLVMPointerType>(resultType))
91 nonDefaultAddrSpace = llvmPtrType.getAddressSpace() != 0;
93 builder.setInsertionPoint(allocaOp);
94 auto sharedAllocOp = omp::AllocSharedMemOp::create(
95 builder, allocaOp->getLoc(), LLVM::LLVMPointerType::get(context),
96 allocaOp.getElemTypeAttr(), allocaOp.getArraySize(),
97 allocaOp.getAlignmentAttr());
98 if (nonDefaultAddrSpace) {
99 assert(allocaOp->hasOneUse() &&
" unsupported non-default address "
100 "space alloca with multiple uses");
102 cast<LLVM::AddrSpaceCastOp>(*allocaOp->getUsers().begin());
103 asCastOp.replaceAllUsesWith(sharedAllocOp.getOperation());
107 toBeDeleted.push_back(asCastOp);
108 toBeDeleted.push_back(allocaOp);
110 allocaOp.replaceAllUsesWith(sharedAllocOp.getOperation());
117 builder, sharedAllocOp.getMemElemTypeAttr(),
118 sharedAllocOp.getMemArraySize(), sharedAllocOp.getMemAlignmentAttr(),
119 sharedAllocOp.getResult());
121 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.