23#define GEN_PASS_DEF_GPUMAPPARALLELLOOPSPASS
24#include "mlir/Dialect/GPU/Transforms/Passes.h.inc"
38 for (
auto dimAttr : mapping) {
39 gpu::Processor processor = dimAttr.getProcessor();
40 if (processor != gpu::Processor::Sequential &&
41 specifiedMappings.count(processor))
42 return ploopOp.emitError(
43 "invalid mapping multiple loops to same processor");
44 specifiedMappings.insert(processor);
46 ArrayRef<Attribute> mappingAsAttrs(mapping.data(), mapping.size());
47 ploopOp->setDiscardableAttr(
49 ArrayAttr::get(ploopOp.getContext(), mappingAsAttrs));
55enum MappingLevel { MapGrid = 0, MapBlock = 1, Sequential = 2 };
56enum class MappingPolicy { OutermostFirst, InnermostFirst };
63static MappingLevel &
operator++(MappingLevel &mappingLevel) {
64 if (mappingLevel < Sequential) {
65 mappingLevel =
static_cast<MappingLevel
>(mappingLevel + 1);
73 std::string policyCanonical = policy.trim().lower();
75 std::optional<MappingPolicy> option =
77 .Case(
"innermost-first", MappingPolicy::InnermostFirst)
78 .Case(
"outermost-first", MappingPolicy::OutermostFirst)
79 .Default(std::nullopt);
92 return Processor::Sequential;
98 return Processor::BlockX;
100 return Processor::BlockY;
102 return Processor::BlockZ;
104 return Processor::Sequential;
110 return Processor::ThreadX;
112 return Processor::ThreadY;
114 return Processor::ThreadZ;
116 return Processor::Sequential;
120 return Processor::Sequential;
128 MappingPolicy mappingPolicy = MappingPolicy::OutermostFirst) {
131 ((mappingLevel == MapGrid) && parallelOp->getParentOfType<ParallelOp>()))
134 const int numLoops =
static_cast<int>(parallelOp.getNumLoops());
140 attrs.reserve(numLoops);
142 for (
int i = 0; i < numLoops; ++i) {
147 if (i < loopsToMap) {
148 hwMapping = (mappingPolicy == MappingPolicy::OutermostFirst)
150 : (loopsToMap - 1 - i);
153 attrs.push_back(
b.getAttr<ParallelLoopDimMappingAttr>(
155 b.getDimIdentityMap()));
161 for (
Operation &op : *parallelOp.getBody()) {
162 if (ParallelOp nested = dyn_cast<ParallelOp>(op))
168struct GpuMapParallelLoopsPass
169 :
public impl::GpuMapParallelLoopsPassBase<GpuMapParallelLoopsPass> {
172 void runOnOperation()
override {
174 FailureOr<MappingPolicy> policyOrFailure =
176 if (failed(policyOrFailure)) {
177 getOperation()->emitError() <<
"Invalid mapping policy specified.";
178 return signalPassFailure();
181 MappingPolicy policy = *policyOrFailure;
182 MappingLevel topLevel = MappingLevel::MapGrid;
184 for (Region ®ion : getOperation()->getRegions()) {
185 region.walk([&](ParallelOp parallelOp) {
This class is a general helper class for creating context-global objects like types,...
MLIRContext is the top-level object for a collection of MLIR operations.
Operation is the basic unit of execution within MLIR.
static Processor getHardwareIdForMapping(MappingLevel level, int dimension)
Computed the hardware id to use for a given mapping level.
static FailureOr< MappingPolicy > getMappingPolicyFromStr(StringRef policy)
static MappingLevel & operator++(MappingLevel &mappingLevel)
Bounded increment on MappingLevel.
LogicalResult setMappingAttr(scf::ParallelOp ploopOp, ArrayRef< ParallelLoopDimMappingAttr > mapping)
Sets the mapping attribute of a scf.parallel operation.
static constexpr int kNumHardwareIds
StringRef getMappingAttrName()
Name of the mapping attribute produced by loop mappers.
static void mapParallelOp(ParallelOp parallelOp, MappingLevel mappingLevel=MapGrid, MappingPolicy mappingPolicy=MappingPolicy::OutermostFirst)
Add mapping information to the given parallel loop.
Include the generated interface declarations.