| 1 |
//===- OMPMapInfoFinalization.cpp |
| 2 |
//---------------------------------------------------===// |
| 3 |
// |
| 4 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 |
// See https://llvm.org/LICENSE.txt for license information. |
| 6 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 |
// |
| 8 |
//===----------------------------------------------------------------------===// |
| 9 |
|
| 10 |
//===----------------------------------------------------------------------===// |
| 11 |
/// \file |
| 12 |
/// An OpenMP dialect related pass for FIR/HLFIR which performs some |
| 13 |
/// pre-processing of MapInfoOp's after the module has been lowered to |
| 14 |
/// finalize them. |
| 15 |
/// |
| 16 |
/// For example, it expands MapInfoOp's containing descriptor related |
| 17 |
/// types (fir::BoxType's) into multiple MapInfoOp's containing the parent |
| 18 |
/// descriptor and pointer member components for individual mapping, |
| 19 |
/// treating the descriptor type as a record type for later lowering in the |
| 20 |
/// OpenMP dialect. |
| 21 |
/// |
| 22 |
/// The pass also adds MapInfoOp's that are members of a parent object but are |
| 23 |
/// not directly used in the body of a target region to its BlockArgument list |
| 24 |
/// to maintain consistency across all MapInfoOp's tied to a region directly or |
| 25 |
/// indirectly via a parent object. |
| 26 |
//===----------------------------------------------------------------------===// |
| 27 |
|
| 28 |
#include "flang/Optimizer/Builder/FIRBuilder.h" |
| 29 |
#include "flang/Optimizer/Dialect/FIRType.h" |
| 30 |
#include "flang/Optimizer/Dialect/Support/KindMapping.h" |
| 31 |
#include "flang/Optimizer/Transforms/Passes.h" |
| 32 |
#include "mlir/Dialect/Func/IR/FuncOps.h" |
| 33 |
#include "mlir/Dialect/OpenMP/OpenMPDialect.h" |
| 34 |
#include "mlir/IR/BuiltinDialect.h" |
| 35 |
#include "mlir/IR/BuiltinOps.h" |
| 36 |
#include "mlir/IR/Operation.h" |
| 37 |
#include "mlir/IR/SymbolTable.h" |
| 38 |
#include "mlir/Pass/Pass.h" |
| 39 |
#include "mlir/Support/LLVM.h" |
| 40 |
#include "llvm/ADT/SmallPtrSet.h" |
| 41 |
#include "llvm/Frontend/OpenMP/OMPConstants.h" |
| 42 |
#include <iterator> |
| 43 |
|
| 44 |
namespace fir { |
| 45 |
#define GEN_PASS_DEF_OMPMAPINFOFINALIZATIONPASS |
| 46 |
#include "flang/Optimizer/Transforms/Passes.h.inc" |
| 47 |
} // namespace fir |
| 48 |
|
| 49 |
namespace { |
| 50 |
class OMPMapInfoFinalizationPass |
| 51 |
: public fir::impl::OMPMapInfoFinalizationPassBase< |
| 52 |
OMPMapInfoFinalizationPass> { |
| 53 |
|
| 54 |
void genDescriptorMemberMaps(mlir::omp::MapInfoOp op, |
| 55 |
fir::FirOpBuilder &builder, |
| 56 |
mlir::Operation *target) { |
| 57 |
mlir::Location loc = op.getLoc(); |
| 58 |
mlir::Value descriptor = op.getVarPtr(); |
| 59 |
|
| 60 |
// If we enter this function, but the mapped type itself is not the |
| 61 |
// descriptor, then it's likely the address of the descriptor so we |
| 62 |
// must retrieve the descriptor SSA. |
| 63 |
if (!fir::isTypeWithDescriptor(op.getVarType())) { |
| 64 |
if (auto addrOp = mlir::dyn_cast_if_present<fir::BoxAddrOp>( |
| 65 |
op.getVarPtr().getDefiningOp())) { |
| 66 |
descriptor = addrOp.getVal(); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
// The fir::BoxOffsetOp only works with !fir.ref<!fir.box<...>> types, as |
| 71 |
// allowing it to access non-reference box operations can cause some |
| 72 |
// problematic SSA IR. However, in the case of assumed shape's the type |
| 73 |
// is not a !fir.ref, in these cases to retrieve the appropriate |
| 74 |
// !fir.ref<!fir.box<...>> to access the data we need to map we must |
| 75 |
// perform an alloca and then store to it and retrieve the data from the new |
| 76 |
// alloca. |
| 77 |
if (mlir::isa<fir::BaseBoxType>(descriptor.getType())) { |
| 78 |
mlir::OpBuilder::InsertPoint insPt = builder.saveInsertionPoint(); |
| 79 |
mlir::Block *allocaBlock = builder.getAllocaBlock(); |
| 80 |
assert(allocaBlock && "No alloca block found for this top level op"); |
| 81 |
builder.setInsertionPointToStart(allocaBlock); |
| 82 |
auto alloca = builder.create<fir::AllocaOp>(loc, descriptor.getType()); |
| 83 |
builder.restoreInsertionPoint(insPt); |
| 84 |
builder.create<fir::StoreOp>(loc, descriptor, alloca); |
| 85 |
descriptor = alloca; |
| 86 |
} |
| 87 |
|
| 88 |
mlir::Value baseAddrAddr = builder.create<fir::BoxOffsetOp>( |
| 89 |
loc, descriptor, fir::BoxFieldAttr::base_addr); |
| 90 |
|
| 91 |
// Member of the descriptor pointing at the allocated data |
| 92 |
mlir::Value baseAddr = builder.create<mlir::omp::MapInfoOp>( |
| 93 |
loc, baseAddrAddr.getType(), descriptor, |
| 94 |
mlir::TypeAttr::get(llvm::cast<mlir::omp::PointerLikeType>( |
| 95 |
fir::unwrapRefType(baseAddrAddr.getType())) |
| 96 |
.getElementType()), |
| 97 |
baseAddrAddr, /*members=*/mlir::SmallVector<mlir::Value>{}, |
| 98 |
/*member_index=*/mlir::DenseIntElementsAttr{}, op.getBounds(), |
| 99 |
builder.getIntegerAttr(builder.getIntegerType(64, false), |
| 100 |
op.getMapType().value()), |
| 101 |
builder.getAttr<mlir::omp::VariableCaptureKindAttr>( |
| 102 |
mlir::omp::VariableCaptureKind::ByRef), |
| 103 |
/*name=*/builder.getStringAttr(""), |
| 104 |
/*partial_map=*/builder.getBoolAttr(false)); |
| 105 |
|
| 106 |
// TODO: map the addendum segment of the descriptor, similarly to the |
| 107 |
// above base address/data pointer member. |
| 108 |
|
| 109 |
if (auto mapClauseOwner = |
| 110 |
llvm::dyn_cast<mlir::omp::MapClauseOwningOpInterface>(target)) { |
| 111 |
llvm::SmallVector<mlir::Value> newMapOps; |
| 112 |
mlir::OperandRange mapOperandsArr = mapClauseOwner.getMapOperands(); |
| 113 |
|
| 114 |
for (size_t i = 0; i < mapOperandsArr.size(); ++i) { |
| 115 |
if (mapOperandsArr[i] == op) { |
| 116 |
// Push new implicit maps generated for the descriptor. |
| 117 |
newMapOps.push_back(baseAddr); |
| 118 |
|
| 119 |
// for TargetOp's which have IsolatedFromAbove we must align the |
| 120 |
// new additional map operand with an appropriate BlockArgument, |
| 121 |
// as the printing and later processing currently requires a 1:1 |
| 122 |
// mapping of BlockArgs to MapInfoOp's at the same placement in |
| 123 |
// each array (BlockArgs and MapOperands). |
| 124 |
if (auto targetOp = llvm::dyn_cast<mlir::omp::TargetOp>(target)) |
| 125 |
targetOp.getRegion().insertArgument(i, baseAddr.getType(), loc); |
| 126 |
} |
| 127 |
newMapOps.push_back(mapOperandsArr[i]); |
| 128 |
} |
| 129 |
mapClauseOwner.getMapOperandsMutable().assign(newMapOps); |
| 130 |
} |
| 131 |
|
| 132 |
mlir::Value newDescParentMapOp = builder.create<mlir::omp::MapInfoOp>( |
| 133 |
op->getLoc(), op.getResult().getType(), descriptor, |
| 134 |
mlir::TypeAttr::get(fir::unwrapRefType(descriptor.getType())), |
| 135 |
/*varPtrPtr=*/mlir::Value{}, |
| 136 |
/*members=*/mlir::SmallVector<mlir::Value>{baseAddr}, |
| 137 |
/*members_index=*/ |
| 138 |
mlir::DenseIntElementsAttr::get( |
| 139 |
mlir::VectorType::get( |
| 140 |
llvm::ArrayRef<int64_t>({1, 1}), |
| 141 |
mlir::IntegerType::get(builder.getContext(), 32)), |
| 142 |
llvm::ArrayRef<int32_t>({0})), |
| 143 |
/*bounds=*/mlir::SmallVector<mlir::Value>{}, |
| 144 |
builder.getIntegerAttr(builder.getIntegerType(64, false), |
| 145 |
op.getMapType().value()), |
| 146 |
op.getMapCaptureTypeAttr(), op.getNameAttr(), op.getPartialMapAttr()); |
| 147 |
op.replaceAllUsesWith(newDescParentMapOp); |
| 148 |
op->erase(); |
| 149 |
} |
| 150 |
|
| 151 |
// We add all mapped record members not directly used in the target region |
| 152 |
// to the block arguments in front of their parent and we place them into |
| 153 |
// the map operands list for consistency. |
| 154 |
// |
| 155 |
// These indirect uses (via accesses to their parent) will still be |
| 156 |
// mapped individually in most cases, and a parent mapping doesn't |
| 157 |
// guarantee the parent will be mapped in its totality, partial |
| 158 |
// mapping is common. |
| 159 |
// |
| 160 |
// For example: |
| 161 |
// map(tofrom: x%y) |
| 162 |
// |
| 163 |
// Will generate a mapping for "x" (the parent) and "y" (the member). |
| 164 |
// The parent "x" will not be mapped, but the member "y" will. |
| 165 |
// However, we must have the parent as a BlockArg and MapOperand |
| 166 |
// in these cases, to maintain the correct uses within the region and |
| 167 |
// to help tracking that the member is part of a larger object. |
| 168 |
// |
| 169 |
// In the case of: |
| 170 |
// map(tofrom: x%y, x%z) |
| 171 |
// |
| 172 |
// The parent member becomes more critical, as we perform a partial |
| 173 |
// structure mapping where we link the mapping of the members y |
| 174 |
// and z together via the parent x. We do this at a kernel argument |
| 175 |
// level in LLVM IR and not just MLIR, which is important to maintain |
| 176 |
// similarity to Clang and for the runtime to do the correct thing. |
| 177 |
// However, we still do not map the structure in its totality but |
| 178 |
// rather we generate an un-sized "binding" map entry for it. |
| 179 |
// |
| 180 |
// In the case of: |
| 181 |
// map(tofrom: x, x%y, x%z) |
| 182 |
// |
| 183 |
// We do actually map the entirety of "x", so the explicit mapping of |
| 184 |
// x%y, x%z becomes unnecessary. It is redundant to write this from a |
| 185 |
// Fortran OpenMP perspective (although it is legal), as even if the |
| 186 |
// members were allocatables or pointers, we are mandated by the |
| 187 |
// specification to map these (and any recursive components) in their |
| 188 |
// entirety, which is different to the C++ equivalent, which requires |
| 189 |
// explicit mapping of these segments. |
| 190 |
void addImplicitMembersToTarget(mlir::omp::MapInfoOp op, |
| 191 |
fir::FirOpBuilder &builder, |
| 192 |
mlir::Operation *target) { |
| 193 |
auto mapClauseOwner = |
| 194 |
llvm::dyn_cast<mlir::omp::MapClauseOwningOpInterface>(target); |
| 195 |
if (!mapClauseOwner) |
| 196 |
return; |
| 197 |
|
| 198 |
llvm::SmallVector<mlir::Value> newMapOps; |
| 199 |
mlir::OperandRange mapOperandsArr = mapClauseOwner.getMapOperands(); |
| 200 |
auto targetOp = llvm::dyn_cast<mlir::omp::TargetOp>(target); |
| 201 |
|
| 202 |
for (size_t i = 0; i < mapOperandsArr.size(); ++i) { |
| 203 |
if (mapOperandsArr[i] == op) { |
| 204 |
for (auto [j, mapMember] : llvm::enumerate(op.getMembers())) { |
| 205 |
newMapOps.push_back(mapMember); |
| 206 |
// for TargetOp's which have IsolatedFromAbove we must align the |
| 207 |
// new additional map operand with an appropriate BlockArgument, |
| 208 |
// as the printing and later processing currently requires a 1:1 |
| 209 |
// mapping of BlockArgs to MapInfoOp's at the same placement in |
| 210 |
// each array (BlockArgs and MapOperands). |
| 211 |
if (targetOp) { |
| 212 |
targetOp.getRegion().insertArgument(i + j, mapMember.getType(), |
| 213 |
targetOp->getLoc()); |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
newMapOps.push_back(mapOperandsArr[i]); |
| 218 |
} |
| 219 |
mapClauseOwner.getMapOperandsMutable().assign(newMapOps); |
| 220 |
} |
| 221 |
|
| 222 |
// This pass executes on omp::MapInfoOp's containing descriptor based types |
| 223 |
// (allocatables, pointers, assumed shape etc.) and expanding them into |
| 224 |
// multiple omp::MapInfoOp's for each pointer member contained within the |
| 225 |
// descriptor. |
| 226 |
// |
| 227 |
// From the perspective of the MLIR pass manager this runs on the top level |
| 228 |
// operation (usually function) containing the MapInfoOp because this pass |
| 229 |
// will mutate siblings of MapInfoOp. |
| 230 |
void runOnOperation() override { |
| 231 |
mlir::ModuleOp module = |
| 232 |
mlir::dyn_cast_or_null<mlir::ModuleOp>(getOperation()); |
| 233 |
if (!module) |
| 234 |
module = getOperation()->getParentOfType<mlir::ModuleOp>(); |
| 235 |
fir::KindMapping kindMap = fir::getKindMapping(module); |
| 236 |
fir::FirOpBuilder builder{module, std::move(kindMap)}; |
| 237 |
|
| 238 |
getOperation()->walk([&](mlir::omp::MapInfoOp op) { |
| 239 |
// TODO: Currently only supports a single user for the MapInfoOp, this |
| 240 |
// is fine for the moment as the Fortran Frontend will generate a |
| 241 |
// new MapInfoOp per Target operation for the moment. However, when/if |
| 242 |
// we optimise/cleanup the IR, it likely isn't too difficult to |
| 243 |
// extend this function, it would require some modification to create a |
| 244 |
// single new MapInfoOp per new MapInfoOp generated and share it across |
| 245 |
// all users appropriately, making sure to only add a single member link |
| 246 |
// per new generation for the original originating descriptor MapInfoOp. |
| 247 |
assert(llvm::hasSingleElement(op->getUsers()) && |
| 248 |
"OMPMapInfoFinalization currently only supports single users " |
| 249 |
"of a MapInfoOp"); |
| 250 |
|
| 251 |
if (!op.getMembers().empty()) { |
| 252 |
addImplicitMembersToTarget(op, builder, *op->getUsers().begin()); |
| 253 |
} else if (fir::isTypeWithDescriptor(op.getVarType()) || |
| 254 |
mlir::isa_and_present<fir::BoxAddrOp>( |
| 255 |
op.getVarPtr().getDefiningOp())) { |
| 256 |
builder.setInsertionPoint(op); |
| 257 |
genDescriptorMemberMaps(op, builder, *op->getUsers().begin()); |
| 258 |
} |
| 259 |
}); |
| 260 |
} |
| 261 |
}; |
| 262 |
|
| 263 |
} // namespace |