|
8 | 8 |
|
9 | 9 | #include "flang/Utils/OpenMP.h"
|
10 | 10 |
|
| 11 | +#include "flang/Lower/ConvertExprToHLFIR.h" |
| 12 | +#include "flang/Optimizer/Builder/DirectivesCommon.h" |
| 13 | +#include "flang/Optimizer/Builder/FIRBuilder.h" |
11 | 14 | #include "flang/Optimizer/Dialect/FIROps.h"
|
12 | 15 | #include "flang/Optimizer/Dialect/FIRType.h"
|
13 | 16 |
|
14 | 17 | #include "mlir/Dialect/OpenMP/OpenMPDialect.h"
|
| 18 | +#include "mlir/Transforms/RegionUtils.h" |
15 | 19 |
|
16 | 20 | namespace Fortran::utils::openmp {
|
17 | 21 | mlir::omp::MapInfoOp createMapInfoOp(mlir::OpBuilder &builder,
|
@@ -44,4 +48,113 @@ mlir::omp::MapInfoOp createMapInfoOp(mlir::OpBuilder &builder,
|
44 | 48 | builder.getStringAttr(name), builder.getBoolAttr(partialMap));
|
45 | 49 | return op;
|
46 | 50 | }
|
| 51 | + |
| 52 | +mlir::Value mapTemporaryValue(fir::FirOpBuilder &firOpBuilder, |
| 53 | + mlir::omp::TargetOp targetOp, mlir::Value val, llvm::StringRef name) { |
| 54 | + mlir::OpBuilder::InsertionGuard guard(firOpBuilder); |
| 55 | + mlir::Operation *valOp = val.getDefiningOp(); |
| 56 | + |
| 57 | + if (valOp) |
| 58 | + firOpBuilder.setInsertionPointAfter(valOp); |
| 59 | + else |
| 60 | + // This means val is a block argument |
| 61 | + firOpBuilder.setInsertionPoint(targetOp); |
| 62 | + |
| 63 | + auto copyVal = firOpBuilder.createTemporary(val.getLoc(), val.getType()); |
| 64 | + firOpBuilder.createStoreWithConvert(copyVal.getLoc(), val, copyVal); |
| 65 | + |
| 66 | + fir::factory::AddrAndBoundsInfo info = fir::factory::getDataOperandBaseAddr( |
| 67 | + firOpBuilder, val, /*isOptional=*/false, val.getLoc()); |
| 68 | + llvm::SmallVector<mlir::Value> bounds = |
| 69 | + fir::factory::genImplicitBoundsOps<mlir::omp::MapBoundsOp, |
| 70 | + mlir::omp::MapBoundsType>(firOpBuilder, info, |
| 71 | + hlfir::translateToExtendedValue( |
| 72 | + val.getLoc(), firOpBuilder, hlfir::Entity{val}) |
| 73 | + .first, |
| 74 | + /*dataExvIsAssumedSize=*/false, val.getLoc()); |
| 75 | + |
| 76 | + firOpBuilder.setInsertionPoint(targetOp); |
| 77 | + |
| 78 | + llvm::omp::OpenMPOffloadMappingFlags mapFlag = |
| 79 | + llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_IMPLICIT; |
| 80 | + mlir::omp::VariableCaptureKind captureKind = |
| 81 | + mlir::omp::VariableCaptureKind::ByRef; |
| 82 | + |
| 83 | + mlir::Type eleType = copyVal.getType(); |
| 84 | + if (auto refType = mlir::dyn_cast<fir::ReferenceType>(copyVal.getType())) { |
| 85 | + eleType = refType.getElementType(); |
| 86 | + } |
| 87 | + |
| 88 | + if (fir::isa_trivial(eleType) || fir::isa_char(eleType)) { |
| 89 | + captureKind = mlir::omp::VariableCaptureKind::ByCopy; |
| 90 | + } else if (!fir::isa_builtin_cptr_type(eleType)) { |
| 91 | + mapFlag |= llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_TO; |
| 92 | + } |
| 93 | + |
| 94 | + mlir::Value mapOp = createMapInfoOp(firOpBuilder, copyVal.getLoc(), copyVal, |
| 95 | + /*varPtrPtr=*/mlir::Value{}, name.str(), bounds, |
| 96 | + /*members=*/llvm::SmallVector<mlir::Value>{}, |
| 97 | + /*membersIndex=*/mlir::ArrayAttr{}, |
| 98 | + static_cast<std::underlying_type_t<llvm::omp::OpenMPOffloadMappingFlags>>( |
| 99 | + mapFlag), |
| 100 | + captureKind, copyVal.getType()); |
| 101 | + |
| 102 | + auto argIface = llvm::cast<mlir::omp::BlockArgOpenMPOpInterface>(*targetOp); |
| 103 | + mlir::Region ®ion = targetOp.getRegion(); |
| 104 | + |
| 105 | + // Get the index of the first non-map argument before modifying mapVars, |
| 106 | + // then append an element to mapVars and an associated entry block |
| 107 | + // argument at that index. |
| 108 | + unsigned insertIndex = |
| 109 | + argIface.getMapBlockArgsStart() + argIface.numMapBlockArgs(); |
| 110 | + targetOp.getMapVarsMutable().append(mapOp); |
| 111 | + mlir::Value clonedValArg = |
| 112 | + region.insertArgument(insertIndex, copyVal.getType(), copyVal.getLoc()); |
| 113 | + |
| 114 | + mlir::Block *entryBlock = ®ion.getBlocks().front(); |
| 115 | + firOpBuilder.setInsertionPointToStart(entryBlock); |
| 116 | + auto loadOp = |
| 117 | + firOpBuilder.create<fir::LoadOp>(clonedValArg.getLoc(), clonedValArg); |
| 118 | + return loadOp.getResult(); |
| 119 | +} |
| 120 | + |
| 121 | +void cloneOrMapRegionOutsiders( |
| 122 | + fir::FirOpBuilder &firOpBuilder, mlir::omp::TargetOp targetOp) { |
| 123 | + mlir::Region ®ion = targetOp.getRegion(); |
| 124 | + mlir::Block *entryBlock = ®ion.getBlocks().front(); |
| 125 | + |
| 126 | + llvm::SetVector<mlir::Value> valuesDefinedAbove; |
| 127 | + mlir::getUsedValuesDefinedAbove(region, valuesDefinedAbove); |
| 128 | + while (!valuesDefinedAbove.empty()) { |
| 129 | + for (mlir::Value val : valuesDefinedAbove) { |
| 130 | + mlir::Operation *valOp = val.getDefiningOp(); |
| 131 | + |
| 132 | + // NOTE: We skip BoxDimsOp's as the lesser of two evils is to map the |
| 133 | + // indices separately, as the alternative is to eventually map the Box, |
| 134 | + // which comes with a fairly large overhead comparatively. We could be |
| 135 | + // more robust about this and check using a BackwardsSlice to see if we |
| 136 | + // run the risk of mapping a box. |
| 137 | + if (valOp && mlir::isMemoryEffectFree(valOp) && |
| 138 | + !mlir::isa<fir::BoxDimsOp>(valOp)) { |
| 139 | + mlir::Operation *clonedOp = valOp->clone(); |
| 140 | + entryBlock->push_front(clonedOp); |
| 141 | + |
| 142 | + auto replace = [entryBlock](mlir::OpOperand &use) { |
| 143 | + return use.getOwner()->getBlock() == entryBlock; |
| 144 | + }; |
| 145 | + |
| 146 | + valOp->getResults().replaceUsesWithIf(clonedOp->getResults(), replace); |
| 147 | + valOp->replaceUsesWithIf(clonedOp, replace); |
| 148 | + } else { |
| 149 | + mlir::Value mappedTemp = mapTemporaryValue(firOpBuilder, targetOp, val, |
| 150 | + /*name=*/{}); |
| 151 | + val.replaceUsesWithIf(mappedTemp, [entryBlock](mlir::OpOperand &use) { |
| 152 | + return use.getOwner()->getBlock() == entryBlock; |
| 153 | + }); |
| 154 | + } |
| 155 | + } |
| 156 | + valuesDefinedAbove.clear(); |
| 157 | + mlir::getUsedValuesDefinedAbove(region, valuesDefinedAbove); |
| 158 | + } |
| 159 | +} |
47 | 160 | } // namespace Fortran::utils::openmp
|
0 commit comments