|
| 1 | +//===- AutomapToTargetData.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 | +#include "flang/Optimizer/Builder/FIRBuilder.h" |
| 10 | +#include "flang/Optimizer/Builder/HLFIRTools.h" |
| 11 | +#include "flang/Optimizer/Dialect/FIROps.h" |
| 12 | +#include "flang/Optimizer/Dialect/FIRType.h" |
| 13 | +#include "flang/Optimizer/Dialect/Support/KindMapping.h" |
| 14 | +#include "flang/Optimizer/HLFIR/HLFIROps.h" |
| 15 | +#include "flang/Support/OpenMP-utils.h" |
| 16 | + |
| 17 | +#include "mlir/Dialect/OpenMP/OpenMPDialect.h" |
| 18 | +#include "mlir/Dialect/OpenMP/OpenMPInterfaces.h" |
| 19 | +#include "mlir/IR/BuiltinAttributes.h" |
| 20 | +#include "mlir/IR/Operation.h" |
| 21 | +#include "mlir/Pass/Pass.h" |
| 22 | + |
| 23 | +#include "llvm/Frontend/OpenMP/OMPConstants.h" |
| 24 | + |
| 25 | +namespace flangomp { |
| 26 | +#define GEN_PASS_DEF_AUTOMAPTOTARGETDATAPASS |
| 27 | +#include "flang/Optimizer/OpenMP/Passes.h.inc" |
| 28 | +} // namespace flangomp |
| 29 | + |
| 30 | +using namespace mlir; |
| 31 | +using namespace Fortran::common::openmp; |
| 32 | + |
| 33 | +namespace { |
| 34 | +class AutomapToTargetDataPass |
| 35 | + : public flangomp::impl::AutomapToTargetDataPassBase< |
| 36 | + AutomapToTargetDataPass> { |
| 37 | + void findRelatedAllocmemFreemem(fir::AddrOfOp addressOfOp, |
| 38 | + llvm::DenseSet<fir::StoreOp> &allocmems, |
| 39 | + llvm::DenseSet<fir::LoadOp> &freemems) { |
| 40 | + assert(addressOfOp->hasOneUse() && "op must have single use"); |
| 41 | + |
| 42 | + auto declaredRef = |
| 43 | + cast<hlfir::DeclareOp>(*addressOfOp->getUsers().begin())->getResult(0); |
| 44 | + |
| 45 | + for (Operation *refUser : declaredRef.getUsers()) { |
| 46 | + if (auto storeOp = dyn_cast<fir::StoreOp>(refUser)) |
| 47 | + if (auto emboxOp = storeOp.getValue().getDefiningOp<fir::EmboxOp>()) |
| 48 | + if (auto allocmemOp = |
| 49 | + emboxOp.getOperand(0).getDefiningOp<fir::AllocMemOp>()) |
| 50 | + allocmems.insert(storeOp); |
| 51 | + |
| 52 | + if (auto loadOp = dyn_cast<fir::LoadOp>(refUser)) |
| 53 | + for (Operation *loadUser : loadOp.getResult().getUsers()) |
| 54 | + if (auto boxAddrOp = dyn_cast<fir::BoxAddrOp>(loadUser)) |
| 55 | + for (Operation *boxAddrUser : boxAddrOp.getResult().getUsers()) |
| 56 | + if (auto freememOp = dyn_cast<fir::FreeMemOp>(boxAddrUser)) |
| 57 | + freemems.insert(loadOp); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + void runOnOperation() override { |
| 62 | + ModuleOp module = getOperation()->getParentOfType<ModuleOp>(); |
| 63 | + if (!module) |
| 64 | + module = dyn_cast<ModuleOp>(getOperation()); |
| 65 | + if (!module) |
| 66 | + return; |
| 67 | + |
| 68 | + // Build FIR builder for helper utilities. |
| 69 | + fir::KindMapping kindMap = fir::getKindMapping(module); |
| 70 | + fir::FirOpBuilder builder{module, std::move(kindMap)}; |
| 71 | + |
| 72 | + // Collect global variables with AUTOMAP flag. |
| 73 | + llvm::DenseSet<fir::GlobalOp> automapGlobals; |
| 74 | + module.walk([&](fir::GlobalOp globalOp) { |
| 75 | + if (auto iface = |
| 76 | + dyn_cast<omp::DeclareTargetInterface>(globalOp.getOperation())) |
| 77 | + if (iface.isDeclareTarget() && iface.getDeclareTargetAutomap() && |
| 78 | + iface.getDeclareTargetDeviceType() != |
| 79 | + omp::DeclareTargetDeviceType::host) |
| 80 | + automapGlobals.insert(globalOp); |
| 81 | + }); |
| 82 | + |
| 83 | + auto addMapInfo = [&](auto globalOp, auto memOp) { |
| 84 | + builder.setInsertionPointAfter(memOp); |
| 85 | + SmallVector<Value> bounds; |
| 86 | + if (needsBoundsOps(memOp.getMemref())) |
| 87 | + genBoundsOps(builder, memOp.getMemref(), bounds); |
| 88 | + |
| 89 | + omp::TargetEnterExitUpdateDataOperands clauses; |
| 90 | + mlir::omp::MapInfoOp mapInfo = mlir::omp::MapInfoOp::create( |
| 91 | + builder, memOp.getLoc(), memOp.getMemref().getType(), |
| 92 | + memOp.getMemref(), |
| 93 | + TypeAttr::get(fir::unwrapRefType(memOp.getMemref().getType())), |
| 94 | + builder.getIntegerAttr( |
| 95 | + builder.getIntegerType(64, false), |
| 96 | + static_cast<unsigned>( |
| 97 | + isa<fir::StoreOp>(memOp) |
| 98 | + ? llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_TO |
| 99 | + : llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_DELETE)), |
| 100 | + builder.getAttr<omp::VariableCaptureKindAttr>( |
| 101 | + omp::VariableCaptureKind::ByCopy), |
| 102 | + /*var_ptr_ptr=*/mlir::Value{}, |
| 103 | + /*members=*/SmallVector<Value>{}, |
| 104 | + /*members_index=*/ArrayAttr{}, bounds, |
| 105 | + /*mapperId=*/mlir::FlatSymbolRefAttr(), globalOp.getSymNameAttr(), |
| 106 | + builder.getBoolAttr(false)); |
| 107 | + clauses.mapVars.push_back(mapInfo); |
| 108 | + isa<fir::StoreOp>(memOp) |
| 109 | + ? builder.create<omp::TargetEnterDataOp>(memOp.getLoc(), clauses) |
| 110 | + : builder.create<omp::TargetExitDataOp>(memOp.getLoc(), clauses); |
| 111 | + }; |
| 112 | + |
| 113 | + for (fir::GlobalOp globalOp : automapGlobals) { |
| 114 | + if (auto uses = globalOp.getSymbolUses(module.getOperation())) { |
| 115 | + llvm::DenseSet<fir::StoreOp> allocmemStores; |
| 116 | + llvm::DenseSet<fir::LoadOp> freememLoads; |
| 117 | + for (auto &x : *uses) |
| 118 | + if (auto addrOp = dyn_cast<fir::AddrOfOp>(x.getUser())) |
| 119 | + findRelatedAllocmemFreemem(addrOp, allocmemStores, freememLoads); |
| 120 | + |
| 121 | + for (auto storeOp : allocmemStores) |
| 122 | + addMapInfo(globalOp, storeOp); |
| 123 | + |
| 124 | + for (auto loadOp : freememLoads) |
| 125 | + addMapInfo(globalOp, loadOp); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +}; |
| 130 | +} // namespace |
0 commit comments