|
| 1 | +//===- ACCRecipeBufferization.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 | +// Bufferize OpenACC recipes that yield fir.box<T> to operate on |
| 10 | +// fir.ref<fir.box<T>> and update uses accordingly. |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +#include "flang/Optimizer/Dialect/FIROps.h" |
| 15 | +#include "flang/Optimizer/OpenACC/Passes.h" |
| 16 | +#include "mlir/Dialect/OpenACC/OpenACC.h" |
| 17 | +#include "mlir/IR/Block.h" |
| 18 | +#include "mlir/IR/Builders.h" |
| 19 | +#include "mlir/IR/BuiltinOps.h" |
| 20 | +#include "mlir/IR/SymbolTable.h" |
| 21 | +#include "mlir/IR/Value.h" |
| 22 | +#include "mlir/IR/Visitors.h" |
| 23 | +#include "llvm/ADT/TypeSwitch.h" |
| 24 | + |
| 25 | +namespace fir::acc { |
| 26 | +#define GEN_PASS_DEF_ACCRECIPEBUFFERIZATION |
| 27 | +#include "flang/Optimizer/OpenACC/Passes.h.inc" |
| 28 | +} // namespace fir::acc |
| 29 | + |
| 30 | +namespace { |
| 31 | + |
| 32 | +class BufferizeInterface { |
| 33 | +public: |
| 34 | + static std::optional<mlir::Type> mustBufferize(mlir::Type recipeType) { |
| 35 | + if (auto boxTy = llvm::dyn_cast<fir::BaseBoxType>(recipeType)) |
| 36 | + return fir::ReferenceType::get(boxTy); |
| 37 | + return std::nullopt; |
| 38 | + } |
| 39 | + |
| 40 | + static mlir::Operation *load(mlir::OpBuilder &builder, mlir::Location loc, |
| 41 | + mlir::Value value) { |
| 42 | + return builder.create<fir::LoadOp>(loc, value); |
| 43 | + } |
| 44 | + |
| 45 | + static mlir::Value placeInMemory(mlir::OpBuilder &builder, mlir::Location loc, |
| 46 | + mlir::Value value) { |
| 47 | + auto alloca = builder.create<fir::AllocaOp>(loc, value.getType()); |
| 48 | + builder.create<fir::StoreOp>(loc, value, alloca); |
| 49 | + return alloca; |
| 50 | + } |
| 51 | +}; |
| 52 | + |
| 53 | +static void bufferizeRegionArgsAndYields(mlir::Region ®ion, |
| 54 | + mlir::Location loc, mlir::Type oldType, |
| 55 | + mlir::Type newType) { |
| 56 | + if (region.empty()) |
| 57 | + return; |
| 58 | + |
| 59 | + mlir::OpBuilder builder(®ion); |
| 60 | + for (mlir::BlockArgument arg : region.getArguments()) { |
| 61 | + if (arg.getType() == oldType) { |
| 62 | + arg.setType(newType); |
| 63 | + if (!arg.use_empty()) { |
| 64 | + mlir::Operation *loadOp = BufferizeInterface::load(builder, loc, arg); |
| 65 | + arg.replaceAllUsesExcept(loadOp->getResult(0), loadOp); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + if (auto yield = |
| 70 | + llvm::dyn_cast<mlir::acc::YieldOp>(region.back().getTerminator())) { |
| 71 | + llvm::SmallVector<mlir::Value> newOperands; |
| 72 | + newOperands.reserve(yield.getNumOperands()); |
| 73 | + bool changed = false; |
| 74 | + for (mlir::Value oldYieldArg : yield.getOperands()) { |
| 75 | + if (oldYieldArg.getType() == oldType) { |
| 76 | + builder.setInsertionPoint(yield); |
| 77 | + mlir::Value alloca = |
| 78 | + BufferizeInterface::placeInMemory(builder, loc, oldYieldArg); |
| 79 | + newOperands.push_back(alloca); |
| 80 | + changed = true; |
| 81 | + } else { |
| 82 | + newOperands.push_back(oldYieldArg); |
| 83 | + } |
| 84 | + } |
| 85 | + if (changed) |
| 86 | + yield->setOperands(newOperands); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +static void updateRecipeUse(mlir::ArrayAttr recipes, mlir::ValueRange operands, |
| 91 | + llvm::StringRef recipeSymName, |
| 92 | + mlir::Operation *computeOp) { |
| 93 | + if (!recipes) |
| 94 | + return; |
| 95 | + for (auto [recipeSym, oldRes] : llvm::zip(recipes, operands)) { |
| 96 | + if (llvm::cast<mlir::SymbolRefAttr>(recipeSym).getLeafReference() != |
| 97 | + recipeSymName) |
| 98 | + continue; |
| 99 | + |
| 100 | + mlir::Operation *dataOp = oldRes.getDefiningOp(); |
| 101 | + assert(dataOp && "dataOp must be paired with computeOp"); |
| 102 | + mlir::Location loc = dataOp->getLoc(); |
| 103 | + mlir::OpBuilder builder(dataOp); |
| 104 | + llvm::TypeSwitch<mlir::Operation *, void>(dataOp) |
| 105 | + .Case<mlir::acc::PrivateOp, mlir::acc::FirstprivateOp, |
| 106 | + mlir::acc::ReductionOp>([&](auto privateOp) { |
| 107 | + builder.setInsertionPointAfterValue(privateOp.getVar()); |
| 108 | + mlir::Value alloca = BufferizeInterface::placeInMemory( |
| 109 | + builder, loc, privateOp.getVar()); |
| 110 | + privateOp.getVarMutable().assign(alloca); |
| 111 | + privateOp.getAccVar().setType(alloca.getType()); |
| 112 | + }); |
| 113 | + |
| 114 | + llvm::SmallVector<mlir::Operation *> users(oldRes.getUsers().begin(), |
| 115 | + oldRes.getUsers().end()); |
| 116 | + for (mlir::Operation *useOp : users) { |
| 117 | + if (useOp == computeOp) |
| 118 | + continue; |
| 119 | + builder.setInsertionPoint(useOp); |
| 120 | + mlir::Operation *load = BufferizeInterface::load(builder, loc, oldRes); |
| 121 | + useOp->replaceUsesOfWith(oldRes, load->getResult(0)); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +class ACCRecipeBufferization |
| 127 | + : public fir::acc::impl::ACCRecipeBufferizationBase< |
| 128 | + ACCRecipeBufferization> { |
| 129 | +public: |
| 130 | + void runOnOperation() override { |
| 131 | + mlir::ModuleOp module = getOperation(); |
| 132 | + |
| 133 | + llvm::SmallVector<llvm::StringRef> recipeNames; |
| 134 | + module.walk([&](mlir::Operation *recipe) { |
| 135 | + llvm::TypeSwitch<mlir::Operation *, void>(recipe) |
| 136 | + .Case<mlir::acc::PrivateRecipeOp, mlir::acc::FirstprivateRecipeOp, |
| 137 | + mlir::acc::ReductionRecipeOp>([&](auto recipe) { |
| 138 | + mlir::Type oldType = recipe.getType(); |
| 139 | + auto bufferizedType = |
| 140 | + BufferizeInterface::mustBufferize(recipe.getType()); |
| 141 | + if (!bufferizedType) |
| 142 | + return; |
| 143 | + recipe.setTypeAttr(mlir::TypeAttr::get(*bufferizedType)); |
| 144 | + mlir::Location loc = recipe.getLoc(); |
| 145 | + using RecipeOp = decltype(recipe); |
| 146 | + bufferizeRegionArgsAndYields(recipe.getInitRegion(), loc, oldType, |
| 147 | + *bufferizedType); |
| 148 | + if constexpr (std::is_same_v<RecipeOp, |
| 149 | + mlir::acc::FirstprivateRecipeOp>) |
| 150 | + bufferizeRegionArgsAndYields(recipe.getCopyRegion(), loc, oldType, |
| 151 | + *bufferizedType); |
| 152 | + if constexpr (std::is_same_v<RecipeOp, |
| 153 | + mlir::acc::ReductionRecipeOp>) |
| 154 | + bufferizeRegionArgsAndYields(recipe.getCombinerRegion(), loc, |
| 155 | + oldType, *bufferizedType); |
| 156 | + bufferizeRegionArgsAndYields(recipe.getDestroyRegion(), loc, |
| 157 | + oldType, *bufferizedType); |
| 158 | + recipeNames.push_back(recipe.getSymName()); |
| 159 | + }); |
| 160 | + }); |
| 161 | + if (recipeNames.empty()) |
| 162 | + return; |
| 163 | + |
| 164 | + module.walk([&](mlir::Operation *op) { |
| 165 | + llvm::TypeSwitch<mlir::Operation *, void>(op) |
| 166 | + .Case<mlir::acc::LoopOp, mlir::acc::ParallelOp, mlir::acc::SerialOp>( |
| 167 | + [&](auto computeOp) { |
| 168 | + for (llvm::StringRef recipeName : recipeNames) { |
| 169 | + if (computeOp.getPrivatizationRecipes()) |
| 170 | + updateRecipeUse(computeOp.getPrivatizationRecipesAttr(), |
| 171 | + computeOp.getPrivateOperands(), recipeName, |
| 172 | + op); |
| 173 | + if (computeOp.getFirstprivatizationRecipes()) |
| 174 | + updateRecipeUse( |
| 175 | + computeOp.getFirstprivatizationRecipesAttr(), |
| 176 | + computeOp.getFirstprivateOperands(), recipeName, op); |
| 177 | + if (computeOp.getReductionRecipes()) |
| 178 | + updateRecipeUse(computeOp.getReductionRecipesAttr(), |
| 179 | + computeOp.getReductionOperands(), |
| 180 | + recipeName, op); |
| 181 | + } |
| 182 | + }); |
| 183 | + }); |
| 184 | + } |
| 185 | +}; |
| 186 | + |
| 187 | +} // namespace |
| 188 | + |
| 189 | +std::unique_ptr<mlir::Pass> fir::acc::createACCRecipeBufferizationPass() { |
| 190 | + return std::make_unique<ACCRecipeBufferization>(); |
| 191 | +} |
0 commit comments