|
| 1 | +//===- MapsForPrivatizedSymbols.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 creates MapInfoOp |
| 13 | +/// instances for certain privatized symbols. |
| 14 | +/// For example, if an allocatable variable is used in a private clause attached |
| 15 | +/// to a omp.target op, then the allocatable variable's descriptor will be |
| 16 | +/// needed on the device (e.g. GPU). This descriptor needs to be separately |
| 17 | +/// mapped onto the device. This pass creates the necessary omp.map.info ops for |
| 18 | +/// this. |
| 19 | +//===----------------------------------------------------------------------===// |
| 20 | +// TODO: |
| 21 | +// 1. Before adding omp.map.info, check if we already have an omp.map.info for |
| 22 | +// the variable in question. |
| 23 | +// 2. Generalize this for more than just omp.target ops. |
| 24 | +//===----------------------------------------------------------------------===// |
| 25 | + |
| 26 | +#include "flang/Optimizer/Builder/FIRBuilder.h" |
| 27 | +#include "flang/Optimizer/Dialect/FIRType.h" |
| 28 | +#include "flang/Optimizer/Dialect/Support/KindMapping.h" |
| 29 | +#include "flang/Optimizer/HLFIR/HLFIROps.h" |
| 30 | +#include "flang/Optimizer/OpenMP/Passes.h" |
| 31 | +#include "mlir/Dialect/Func/IR/FuncOps.h" |
| 32 | +#include "mlir/Dialect/OpenMP/OpenMPDialect.h" |
| 33 | +#include "mlir/IR/BuiltinAttributes.h" |
| 34 | +#include "mlir/IR/SymbolTable.h" |
| 35 | +#include "mlir/Pass/Pass.h" |
| 36 | +#include "llvm/Frontend/OpenMP/OMPConstants.h" |
| 37 | +#include "llvm/Support/Debug.h" |
| 38 | +#include <type_traits> |
| 39 | + |
| 40 | +#define DEBUG_TYPE "omp-maps-for-privatized-symbols" |
| 41 | + |
| 42 | +namespace flangomp { |
| 43 | +#define GEN_PASS_DEF_MAPSFORPRIVATIZEDSYMBOLSPASS |
| 44 | +#include "flang/Optimizer/OpenMP/Passes.h.inc" |
| 45 | +} // namespace flangomp |
| 46 | +using namespace mlir; |
| 47 | +namespace { |
| 48 | +class MapsForPrivatizedSymbolsPass |
| 49 | + : public flangomp::impl::MapsForPrivatizedSymbolsPassBase< |
| 50 | + MapsForPrivatizedSymbolsPass> { |
| 51 | + |
| 52 | + bool privatizerNeedsMap(omp::PrivateClauseOp &privatizer) { |
| 53 | + Region &allocRegion = privatizer.getAllocRegion(); |
| 54 | + Value blockArg0 = allocRegion.getArgument(0); |
| 55 | + if (blockArg0.use_empty()) |
| 56 | + return false; |
| 57 | + return true; |
| 58 | + } |
| 59 | + omp::MapInfoOp createMapInfo(Location loc, Value var, |
| 60 | + fir::FirOpBuilder &builder) { |
| 61 | + uint64_t mapTypeTo = static_cast< |
| 62 | + std::underlying_type_t<llvm::omp::OpenMPOffloadMappingFlags>>( |
| 63 | + llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_TO); |
| 64 | + Operation *definingOp = var.getDefiningOp(); |
| 65 | + auto declOp = llvm::dyn_cast_or_null<hlfir::DeclareOp>(definingOp); |
| 66 | + assert(declOp && |
| 67 | + "Expected defining Op of privatized var to be hlfir.declare"); |
| 68 | + |
| 69 | + // We want the first result of the hlfir.declare op because our goal |
| 70 | + // is to map the descriptor (fir.box or fir.boxchar) and the first |
| 71 | + // result for hlfir.declare is the descriptor if a the symbol being |
| 72 | + // decalred needs a descriptor. |
| 73 | + Value varPtr = declOp.getBase(); |
| 74 | + |
| 75 | + // If we do not have a reference to descritor, but the descriptor itself |
| 76 | + // then we need to store that on the stack so that we can map the |
| 77 | + // address of the descriptor. |
| 78 | + if (mlir::isa<fir::BaseBoxType>(varPtr.getType()) || |
| 79 | + mlir::isa<fir::BoxCharType>(varPtr.getType())) { |
| 80 | + OpBuilder::InsertPoint savedInsPoint = builder.saveInsertionPoint(); |
| 81 | + mlir::Block *allocaBlock = builder.getAllocaBlock(); |
| 82 | + assert(allocaBlock && "No allocablock found for a funcOp"); |
| 83 | + builder.setInsertionPointToStart(allocaBlock); |
| 84 | + auto alloca = builder.create<fir::AllocaOp>(loc, varPtr.getType()); |
| 85 | + builder.restoreInsertionPoint(savedInsPoint); |
| 86 | + builder.create<fir::StoreOp>(loc, varPtr, alloca); |
| 87 | + varPtr = alloca; |
| 88 | + } |
| 89 | + return builder.create<omp::MapInfoOp>( |
| 90 | + loc, varPtr.getType(), varPtr, |
| 91 | + TypeAttr::get(llvm::cast<omp::PointerLikeType>(varPtr.getType()) |
| 92 | + .getElementType()), |
| 93 | + /*varPtrPtr=*/Value{}, |
| 94 | + /*members=*/SmallVector<Value>{}, |
| 95 | + /*member_index=*/DenseIntElementsAttr{}, |
| 96 | + /*bounds=*/ValueRange{}, |
| 97 | + builder.getIntegerAttr(builder.getIntegerType(64, /*isSigned=*/false), |
| 98 | + mapTypeTo), |
| 99 | + builder.getAttr<omp::VariableCaptureKindAttr>( |
| 100 | + omp::VariableCaptureKind::ByRef), |
| 101 | + StringAttr(), builder.getBoolAttr(false)); |
| 102 | + } |
| 103 | + void addMapInfoOp(omp::TargetOp targetOp, omp::MapInfoOp mapInfoOp) { |
| 104 | + auto argIface = llvm::cast<omp::BlockArgOpenMPOpInterface>(*targetOp); |
| 105 | + unsigned insertIndex = |
| 106 | + argIface.getMapBlockArgsStart() + argIface.numMapBlockArgs(); |
| 107 | + targetOp.getMapVarsMutable().append(ValueRange{mapInfoOp}); |
| 108 | + targetOp.getRegion().insertArgument(insertIndex, mapInfoOp.getType(), |
| 109 | + mapInfoOp.getLoc()); |
| 110 | + } |
| 111 | + void addMapInfoOps(omp::TargetOp targetOp, |
| 112 | + llvm::SmallVectorImpl<omp::MapInfoOp> &mapInfoOps) { |
| 113 | + for (auto mapInfoOp : mapInfoOps) |
| 114 | + addMapInfoOp(targetOp, mapInfoOp); |
| 115 | + } |
| 116 | + void runOnOperation() override { |
| 117 | + ModuleOp module = getOperation()->getParentOfType<ModuleOp>(); |
| 118 | + fir::KindMapping kindMap = fir::getKindMapping(module); |
| 119 | + fir::FirOpBuilder builder{module, std::move(kindMap)}; |
| 120 | + llvm::DenseMap<Operation *, llvm::SmallVector<omp::MapInfoOp, 4>> |
| 121 | + mapInfoOpsForTarget; |
| 122 | + |
| 123 | + getOperation()->walk([&](omp::TargetOp targetOp) { |
| 124 | + if (targetOp.getPrivateVars().empty()) |
| 125 | + return; |
| 126 | + OperandRange privVars = targetOp.getPrivateVars(); |
| 127 | + std::optional<ArrayAttr> privSyms = targetOp.getPrivateSyms(); |
| 128 | + SmallVector<omp::MapInfoOp, 4> mapInfoOps; |
| 129 | + for (auto [privVar, privSym] : llvm::zip_equal(privVars, *privSyms)) { |
| 130 | + |
| 131 | + SymbolRefAttr privatizerName = llvm::cast<SymbolRefAttr>(privSym); |
| 132 | + omp::PrivateClauseOp privatizer = |
| 133 | + SymbolTable::lookupNearestSymbolFrom<omp::PrivateClauseOp>( |
| 134 | + targetOp, privatizerName); |
| 135 | + if (!privatizerNeedsMap(privatizer)) { |
| 136 | + continue; |
| 137 | + } |
| 138 | + builder.setInsertionPoint(targetOp); |
| 139 | + Location loc = targetOp.getLoc(); |
| 140 | + omp::MapInfoOp mapInfoOp = createMapInfo(loc, privVar, builder); |
| 141 | + mapInfoOps.push_back(mapInfoOp); |
| 142 | + LLVM_DEBUG(llvm::dbgs() << "MapsForPrivatizedSymbolsPass created ->\n"); |
| 143 | + LLVM_DEBUG(mapInfoOp.dump()); |
| 144 | + } |
| 145 | + if (!mapInfoOps.empty()) { |
| 146 | + mapInfoOpsForTarget.insert({targetOp.getOperation(), mapInfoOps}); |
| 147 | + } |
| 148 | + }); |
| 149 | + if (!mapInfoOpsForTarget.empty()) { |
| 150 | + for (auto &[targetOp, mapInfoOps] : mapInfoOpsForTarget) { |
| 151 | + addMapInfoOps(static_cast<omp::TargetOp>(targetOp), mapInfoOps); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | +}; |
| 156 | +} // namespace |
0 commit comments