| 1 | //===- ACCEmitRemarksLoop.cpp - Emit OpenACC loop mapping remarks --------===// |
| 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 | // This pass emits optimization remarks describing how loops inside OpenACC |
| 10 | // compute regions are mapped to parallelism levels and GPU dimensions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "mlir/Dialect/Func/IR/FuncOps.h" |
| 15 | #include "mlir/Dialect/OpenACC/Analysis/OpenACCSupport.h" |
| 16 | #include "mlir/Dialect/OpenACC/Extensions/AllExtensions.h" |
| 17 | #include "mlir/Dialect/OpenACC/OpenACC.h" |
| 18 | #include "mlir/Dialect/OpenACC/OpenACCParMapping.h" |
| 19 | #include "mlir/Dialect/OpenACC/OpenACCUtilsLoop.h" |
| 20 | #include "mlir/Dialect/OpenACC/Transforms/Passes.h" |
| 21 | #include "mlir/Dialect/SCF/IR/SCF.h" |
| 22 | #include "mlir/Interfaces/FunctionInterfaces.h" |
| 23 | #include "mlir/Interfaces/LoopLikeInterface.h" |
| 24 | #include "llvm/ADT/StringExtras.h" |
| 25 | #include "llvm/Support/Debug.h" |
| 26 | |
| 27 | namespace mlir { |
| 28 | namespace acc { |
| 29 | #define |
| 30 | #include "mlir/Dialect/OpenACC/Transforms/Passes.h.inc" |
| 31 | } // namespace acc |
| 32 | } // namespace mlir |
| 33 | |
| 34 | #define DEBUG_TYPE "acc-emit-remarks-loop" |
| 35 | |
| 36 | using namespace mlir; |
| 37 | |
| 38 | namespace { |
| 39 | static bool |
| 40 | computeRegionInSpecializedAccRoutine(acc::ComputeRegionOp computeRegion) { |
| 41 | if (auto func = computeRegion->getParentOfType<FunctionOpInterface>()) |
| 42 | return acc::isSpecializedAccRoutine(op: func); |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | static bool (acc::ComputeRegionOp computeRegion) { |
| 47 | StringRef origin = computeRegion.getOrigin(); |
| 48 | if (origin == acc::KernelsOp::getOperationName() || |
| 49 | origin == acc::ParallelOp::getOperationName() || |
| 50 | origin == acc::SerialOp::getOperationName()) |
| 51 | return true; |
| 52 | |
| 53 | return computeRegionInSpecializedAccRoutine(computeRegion); |
| 54 | } |
| 55 | |
| 56 | static std::string getACCParLevelName(acc::GPUParallelDimAttr parDim, |
| 57 | const acc::ACCToGPUMappingPolicy &policy, |
| 58 | acc::ComputeRegionOp computeRegion) { |
| 59 | std::string accName; |
| 60 | if (policy.isSeq(attr: parDim)) |
| 61 | accName = "sequential" ; |
| 62 | else if (policy.isVector(attr: parDim)) |
| 63 | accName = "vector" ; |
| 64 | else if (policy.isWorker(attr: parDim)) |
| 65 | accName = "worker" ; |
| 66 | else if (policy.isGang(attr: parDim)) |
| 67 | accName = "gang" ; |
| 68 | |
| 69 | // Don't specify the constant launch args for sequential loops or loops in |
| 70 | // specialized acc routines as the launch args are not determined here. |
| 71 | if (!policy.isSeq(attr: parDim) && |
| 72 | !computeRegionInSpecializedAccRoutine(computeRegion)) { |
| 73 | if (std::optional<uint64_t> constant = |
| 74 | computeRegion.getKnownConstantLaunchArg(parDim)) |
| 75 | accName += "(" + std::to_string(val: *constant) + ")" ; |
| 76 | } |
| 77 | return accName; |
| 78 | } |
| 79 | |
| 80 | static std::string getGPUParDimName(acc::GPUParallelDimAttr parDim, |
| 81 | llvm::StringRef separator) { |
| 82 | auto formatDim = [&](llvm::StringRef prefix, char axis) { |
| 83 | return (prefix + separator).str() + axis; |
| 84 | }; |
| 85 | |
| 86 | if (parDim.isThreadX()) |
| 87 | return formatDim("threadidx" , 'x'); |
| 88 | if (parDim.isThreadY()) |
| 89 | return formatDim("threadidx" , 'y'); |
| 90 | if (parDim.isThreadZ()) |
| 91 | return formatDim("threadidx" , 'z'); |
| 92 | if (parDim.isBlockX()) |
| 93 | return formatDim("blockidx" , 'x'); |
| 94 | if (parDim.isBlockY()) |
| 95 | return formatDim("blockidx" , 'y'); |
| 96 | if (parDim.isBlockZ()) |
| 97 | return formatDim("blockidx" , 'z'); |
| 98 | return {}; |
| 99 | } |
| 100 | |
| 101 | static void (acc::ComputeRegionOp computeRegion, |
| 102 | LoopLikeOpInterface loopOp, |
| 103 | acc::OpenACCSupport &accSupport, |
| 104 | const acc::ACCToGPUMappingPolicy &policy, |
| 105 | llvm::StringRef gpuDimSeparator) { |
| 106 | acc::GPUParallelDimsAttr parDimsAttr = |
| 107 | loopOp->getDiscardableAttrOfType<acc::GPUParallelDimsAttr>( |
| 108 | name: acc::GPUParallelDimsAttr::name); |
| 109 | |
| 110 | SmallVector<acc::GPUParallelDimAttr, 1> seqParDims; |
| 111 | ArrayRef<acc::GPUParallelDimAttr> parDims; |
| 112 | if (parDimsAttr) { |
| 113 | parDims = parDimsAttr.getArray(); |
| 114 | } else if (isa<scf::ForOp>(Val: loopOp.getOperation())) { |
| 115 | seqParDims.push_back(Elt: acc::GPUParallelDimAttr::seqDim(loopOp->getContext())); |
| 116 | parDims = seqParDims; |
| 117 | } else { |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | accSupport.emitRemark( |
| 122 | op: loopOp, |
| 123 | messageFn: [&]() { |
| 124 | SmallVector<std::string> accMsgs; |
| 125 | SmallVector<std::string> gpuMsgs; |
| 126 | |
| 127 | for (acc::GPUParallelDimAttr parDim : parDims) { |
| 128 | accMsgs.push_back(Elt: getACCParLevelName(parDim, policy, computeRegion)); |
| 129 | if (std::string gpuName = getGPUParDimName(parDim, separator: gpuDimSeparator); |
| 130 | !gpuName.empty()) |
| 131 | gpuMsgs.push_back(Elt: std::move(gpuName)); |
| 132 | } |
| 133 | |
| 134 | std::string msg = "!$acc loop " + llvm::join(R&: accMsgs, Separator: ", " ); |
| 135 | |
| 136 | if (uint64_t collapseCount = acc::getCollapseCount(op: loopOp); |
| 137 | collapseCount > 1) |
| 138 | msg += " collapse(" + std::to_string(val: collapseCount) + ")" ; |
| 139 | |
| 140 | if (!gpuMsgs.empty()) |
| 141 | msg += " ! " + llvm::join(R&: gpuMsgs, Separator: " " ); |
| 142 | return msg; |
| 143 | }, |
| 144 | DEBUG_TYPE); |
| 145 | } |
| 146 | |
| 147 | static void |
| 148 | (Operation *op, |
| 149 | acc::OpenACCSupport &accSupport) { |
| 150 | accSupport.emitRemark(op, |
| 151 | message: "Loop is not parallelized: complex control " |
| 152 | "flow prevents parallelization" , |
| 153 | DEBUG_TYPE); |
| 154 | } |
| 155 | |
| 156 | class |
| 157 | : public acc::impl::ACCEmitRemarksLoopBase<ACCEmitRemarksLoop> { |
| 158 | public: |
| 159 | using ACCEmitRemarksLoopBase<ACCEmitRemarksLoop>::ACCEmitRemarksLoopBase; |
| 160 | |
| 161 | void (DialectRegistry ®istry) const override { |
| 162 | ACCEmitRemarksLoopBase<ACCEmitRemarksLoop>::getDependentDialects(registry); |
| 163 | acc::registerAllExtensions(registry); |
| 164 | } |
| 165 | |
| 166 | void () override { |
| 167 | func::FuncOp func = getOperation(); |
| 168 | acc::OpenACCSupport &accSupport = getAnalysis<acc::OpenACCSupport>(); |
| 169 | acc::DefaultACCToGPUMappingPolicy policy; |
| 170 | if (gpuDimSeparator.empty()) |
| 171 | gpuDimSeparator = "." ; |
| 172 | |
| 173 | func.walk(callback: [&](acc::ComputeRegionOp computeRegion) { |
| 174 | if (!shouldEmitLoopRemarks(computeRegion)) |
| 175 | return; |
| 176 | |
| 177 | computeRegion.getRegion().walk(callback: [&](Operation *op) { |
| 178 | if (auto loopOp = dyn_cast<LoopLikeOpInterface>(Val: op)) |
| 179 | emitLoopMappingRemark(computeRegion, loopOp, accSupport, policy, |
| 180 | gpuDimSeparator); |
| 181 | else if (op->hasAttr(name: acc::getUnstructuredIndependentLoopAttrName())) |
| 182 | emitUnstructuredIndependentLoopRemark(op, accSupport); |
| 183 | }); |
| 184 | }); |
| 185 | } |
| 186 | }; |
| 187 | } // namespace |
| 188 | |