MLIR 24.0.0git
StackToShared.cpp
Go to the documentation of this file.
1//===- StackToShared.cpp -------------------------------------------===//
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// This file implements transforms to swap stack allocations on the target
10// device with device shared memory where applicable.
11//
12//===----------------------------------------------------------------------===//
13
15
21#include "mlir/Pass/Pass.h"
22#include "llvm/ADT/STLExtras.h"
23
24namespace mlir {
25namespace omp {
26#define GEN_PASS_DEF_STACKTOSHAREDPASS
27#include "mlir/Dialect/OpenMP/Transforms/Passes.h.inc"
28} // namespace omp
29} // namespace mlir
30
31using namespace mlir;
32
33/// Tell whether to replace an operation representing a stack allocation with a
34/// device shared memory allocation/deallocation pair based on the location of
35/// the allocation and its uses.
38 llvm::any_of(op.getResults(), [&](Value result) {
39 return omp::allocaUsesRequireSharedMem(result);
40 });
41}
42
43/// Based on the location of the definition of the given value representing the
44/// result of a device shared memory allocation, find the corresponding points
45/// where its deallocation should be placed and introduce `omp.free_shared_mem`
46/// ops at those points.
48 TypeAttr elemType,
49 Value arraySize,
50 IntegerAttr alignment,
51 Value allocVal) {
52 Block *allocaBlock = allocVal.getParentBlock();
53 DominanceInfo domInfo;
54 for (Block &block : allocVal.getParentRegion()->getBlocks()) {
55 Operation *terminator = block.getTerminator();
56 if (!terminator->hasSuccessors() &&
57 domInfo.dominates(allocaBlock, &block)) {
58 builder.setInsertionPoint(terminator);
59 omp::FreeSharedMemOp::create(builder, allocVal.getLoc(), elemType,
60 arraySize, alignment, allocVal);
61 }
62 }
63}
64
65namespace {
66class StackToSharedPass
67 : public omp::impl::StackToSharedPassBase<StackToSharedPass> {
68public:
69 StackToSharedPass() = default;
70
71 void runOnOperation() override {
72 MLIRContext *context = &getContext();
73 OpBuilder builder(context);
74
75 LLVM::LLVMFuncOp funcOp = getOperation();
76 auto offloadIface = funcOp->getParentOfType<omp::OffloadModuleInterface>();
77 if (!offloadIface || !offloadIface.getIsTargetDevice())
78 return;
79
80 llvm::SmallVector<Operation *> toBeDeleted;
81 funcOp->walk([&](LLVM::AllocaOp allocaOp) {
83 return;
84 // Replace llvm.alloca with omp.alloc_shared_mem.
85 Type resultType = allocaOp.getResult().getType();
86
87 // TODO: The handling of non-default address spaces might need to be
88 // improved. This currently only handles the case where an alloca to
89 // non-default address space is only used by a single addrspacecast to
90 // default address space.
91 bool nonDefaultAddrSpace = false;
92 if (auto llvmPtrType = dyn_cast<LLVM::LLVMPointerType>(resultType))
93 nonDefaultAddrSpace = llvmPtrType.getAddressSpace() != 0;
94
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");
103 auto asCastOp =
104 cast<LLVM::AddrSpaceCastOp>(*allocaOp->getUsers().begin());
105 asCastOp.replaceAllUsesWith(sharedAllocOp.getOperation());
106 // Delete later because we can't delete the cast op before the top-level
107 // iteration visits it. Also, the alloca can't be deleted before because
108 // it's used by it.
109 toBeDeleted.push_back(asCastOp);
110 toBeDeleted.push_back(allocaOp);
111 } else {
112 allocaOp.replaceAllUsesWith(sharedAllocOp.getOperation());
113 allocaOp.erase();
114 }
115
116 // Create a new omp.free_shared_mem for the allocated buffer prior to
117 // exiting the region.
119 builder, sharedAllocOp.getMemElemTypeAttr(),
120 sharedAllocOp.getMemArraySize(), sharedAllocOp.getMemAlignmentAttr(),
121 sharedAllocOp.getResult());
122 });
123 for (Operation *op : toBeDeleted)
124 op->erase();
125 }
126};
127} // namespace
b getContext())
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.
Definition Block.h:34
A class for computing basic dominance information.
Definition Dominance.h:143
bool dominates(Operation *a, Operation *b) const
Return true if operation A dominates operation B, i.e.
Definition Dominance.h:161
This class helps build Operations.
Definition Builders.h:210
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
Definition Builders.h:401
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
bool hasSuccessors()
Definition Operation.h:759
result_range getResults()
Definition Operation.h:442
BlockListType & getBlocks()
Definition Region.h:45
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Block * getParentBlock()
Return the Block in which this Value is defined.
Definition Value.cpp:46
Location getLoc() const
Return the location of this value.
Definition Value.cpp:24
Region * getParentRegion()
Return the Region in which this Value is defined.
Definition Value.cpp:39
bool opInSharedDeviceContext(Operation &op)
Check whether the given operation is located in a context where an allocation to be used by multiple ...
Definition Utils.cpp:115
Include the generated interface declarations.