1//===----------------------------------------------------------------------===//
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 "mlir/Dialect/Affine/IR/AffineOps.h"
10#include "mlir/Interfaces/Utils/MemorySlotUtils.h"
11
12using namespace mlir;
13using namespace mlir::affine;
14
15//===----------------------------------------------------------------------===//
16// AffineForOp
17//===----------------------------------------------------------------------===//
18
19bool AffineForOp::isRegionPromotable(const MemorySlot &slot, Region *region,
20 bool hasValueStores) {
21 return true;
22}
23
24void AffineForOp::setupPromotion(
25 const MemorySlot &slot, Value reachingDef, bool hasValueStores,
26 llvm::SmallMapVector<Region *, Value, 2> &regionsToProcess) {
27 Region &bodyRegion = getBodyRegion();
28 if (!hasValueStores) {
29 regionsToProcess.insert(KV: {&bodyRegion, reachingDef});
30 return;
31 }
32
33 getInitsMutable().append(values: reachingDef);
34 bodyRegion.addArgument(type: slot.valueType, loc: slot.ptr.getLoc());
35 regionsToProcess.insert(KV: {&bodyRegion, bodyRegion.getArguments().back()});
36}
37
38Value AffineForOp::finalizePromotion(
39 const MemorySlot &slot, Value reachingDef, bool hasValueStores,
40 const llvm::DenseMap<Block *, Value> &reachingAtBlockEnd,
41 OpBuilder &builder) {
42 if (!hasValueStores)
43 return reachingDef;
44
45 // Update the yield terminator to return the newly defined reaching
46 // definition.
47 memoryslot::updateTerminator(block: getBody(), defaultReachingDef: reachingDef, reachingAtBlockEnd);
48
49 SmallVector<Type> resultTypes(getResultTypes());
50 resultTypes.push_back(Elt: slot.valueType);
51
52 IRRewriter rewriter(builder);
53 Operation *newOp =
54 memoryslot::replaceWithNewResults(rewriter, op: getOperation(), resultTypes);
55 return newOp->getResults().back();
56}
57