23#include "llvm/ADT/StringExtras.h"
24#include "llvm/Support/Debug.h"
28#define GEN_PASS_DEF_ACCEMITREMARKSLOOP
29#include "mlir/Dialect/OpenACC/Transforms/Passes.h.inc"
33#define DEBUG_TYPE "acc-emit-remarks-loop"
39static bool shouldEmitLoopRemarks(acc::ComputeRegionOp computeRegion) {
40 StringRef origin = computeRegion.getOrigin();
41 if (origin == acc::KernelsOp::getOperationName() ||
42 origin == acc::ParallelOp::getOperationName() ||
43 origin == acc::SerialOp::getOperationName())
46 if (
auto func = computeRegion->getParentOfType<FunctionOpInterface>())
51static std::string getACCParLevelName(acc::GPUParallelDimAttr parDim,
53 acc::ComputeRegionOp computeRegion) {
55 if (policy.
isSeq(parDim))
56 accName =
"sequential";
61 else if (policy.
isGang(parDim))
64 if (!policy.
isSeq(parDim)) {
65 if (std::optional<uint64_t> constant =
66 computeRegion.getKnownConstantLaunchArg(parDim))
67 accName +=
"(" + std::to_string(*constant) +
")";
72static std::string getGPUParDimName(acc::GPUParallelDimAttr parDim,
73 llvm::StringRef separator) {
74 auto formatDim = [&](llvm::StringRef prefix,
char axis) {
75 return (prefix + separator).str() + axis;
78 if (parDim.isThreadX())
79 return formatDim(
"threadidx",
'x');
80 if (parDim.isThreadY())
81 return formatDim(
"threadidx",
'y');
82 if (parDim.isThreadZ())
83 return formatDim(
"threadidx",
'z');
84 if (parDim.isBlockX())
85 return formatDim(
"blockidx",
'x');
86 if (parDim.isBlockY())
87 return formatDim(
"blockidx",
'y');
88 if (parDim.isBlockZ())
89 return formatDim(
"blockidx",
'z');
93static void emitLoopMappingRemark(acc::ComputeRegionOp computeRegion,
94 LoopLikeOpInterface loopOp,
97 llvm::StringRef gpuDimSeparator) {
98 acc::GPUParallelDimsAttr parDimsAttr =
99 loopOp->getAttrOfType<acc::GPUParallelDimsAttr>(
100 acc::GPUParallelDimsAttr::name);
105 parDims = parDimsAttr.getArray();
106 }
else if (isa<scf::ForOp>(loopOp.getOperation())) {
107 seqParDims.push_back(acc::GPUParallelDimAttr::seqDim(loopOp->getContext()));
108 parDims = seqParDims;
119 for (acc::GPUParallelDimAttr parDim : parDims) {
120 accMsgs.push_back(getACCParLevelName(parDim, policy, computeRegion));
121 if (std::string gpuName = getGPUParDimName(parDim, gpuDimSeparator);
123 gpuMsgs.push_back(std::move(gpuName));
126 std::string msg =
"!$acc loop " + llvm::join(accMsgs,
", ");
130 msg +=
" collapse(" + std::to_string(collapseCount) +
")";
132 if (!gpuMsgs.empty())
133 msg +=
" ! " + llvm::join(gpuMsgs,
" ");
139class ACCEmitRemarksLoop
142 using ACCEmitRemarksLoopBase<ACCEmitRemarksLoop>::ACCEmitRemarksLoopBase;
144 void runOnOperation()
override {
145 func::FuncOp
func = getOperation();
148 if (gpuDimSeparator.empty())
149 gpuDimSeparator =
".";
151 func.walk([&](acc::ComputeRegionOp computeRegion) {
152 if (!shouldEmitLoopRemarks(computeRegion))
155 computeRegion.getRegion().walk([&](LoopLikeOpInterface loopOp) {
156 emitLoopMappingRemark(computeRegion, loopOp, accSupport, policy,
virtual bool isWorker(ParDimAttrT attr) const =0
Check if the attribute represents worker parallelism.
virtual bool isSeq(ParDimAttrT attr) const =0
Check if the attribute represents sequential execution.
virtual bool isVector(ParDimAttrT attr) const =0
Check if the attribute represents vector parallelism.
virtual bool isGang(ParDimAttrT attr) const =0
Check if the attribute represents gang parallelism (any gang dimension).
Default policy that provides the standard GPU mapping: gang(dim:1) -> BlockX (gridDim....
remark::detail::InFlightRemark emitRemark(Operation *op, std::function< std::string()> messageFn, llvm::StringRef category="openacc")
Emit an OpenACC remark with lazy message generation.
uint64_t getCollapseCount(Operation *op)
Number of original loops collapsed into op, or 1 when op carries no collapse_count attribute.
bool isSpecializedAccRoutine(mlir::Operation *op)
Used to check whether this is a specialized accelerator version of acc routine function.
ACCParMappingPolicy< mlir::acc::GPUParallelDimAttr > ACCToGPUMappingPolicy
Type alias for the GPU-specific mapping policy.
Include the generated interface declarations.