xref: /llvm-project/mlir/lib/Dialect/Async/Transforms/AsyncParallelFor.cpp (revision 09dfc5713d7e2342bea4c8447d1ed76c85eb8225)
1 //===- AsyncParallelFor.cpp - Implementation of Async Parallel For --------===//
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 file implements scf.parallel to scf.for + async.execute conversion pass.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/Dialect/Async/Passes.h"
14 
15 #include "PassDetail.h"
16 #include "mlir/Dialect/Arith/IR/Arith.h"
17 #include "mlir/Dialect/Async/IR/Async.h"
18 #include "mlir/Dialect/Async/Transforms.h"
19 #include "mlir/Dialect/Func/IR/FuncOps.h"
20 #include "mlir/Dialect/SCF/IR/SCF.h"
21 #include "mlir/IR/IRMapping.h"
22 #include "mlir/IR/ImplicitLocOpBuilder.h"
23 #include "mlir/IR/Matchers.h"
24 #include "mlir/IR/PatternMatch.h"
25 #include "mlir/Support/LLVM.h"
26 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
27 #include "mlir/Transforms/RegionUtils.h"
28 #include <utility>
29 
30 namespace mlir {
31 #define GEN_PASS_DEF_ASYNCPARALLELFOR
32 #include "mlir/Dialect/Async/Passes.h.inc"
33 } // namespace mlir
34 
35 using namespace mlir;
36 using namespace mlir::async;
37 
38 #define DEBUG_TYPE "async-parallel-for"
39 
40 namespace {
41 
42 // Rewrite scf.parallel operation into multiple concurrent async.execute
43 // operations over non overlapping subranges of the original loop.
44 //
45 // Example:
46 //
47 //   scf.parallel (%i, %j) = (%lbi, %lbj) to (%ubi, %ubj) step (%si, %sj) {
48 //     "do_some_compute"(%i, %j): () -> ()
49 //   }
50 //
51 // Converted to:
52 //
53 //   // Parallel compute function that executes the parallel body region for
54 //   // a subset of the parallel iteration space defined by the one-dimensional
55 //   // compute block index.
56 //   func parallel_compute_function(%block_index : index, %block_size : index,
57 //                                  <parallel operation properties>, ...) {
58 //     // Compute multi-dimensional loop bounds for %block_index.
59 //     %block_lbi, %block_lbj = ...
60 //     %block_ubi, %block_ubj = ...
61 //
62 //     // Clone parallel operation body into the scf.for loop nest.
63 //     scf.for %i = %blockLbi to %blockUbi {
64 //       scf.for %j = block_lbj to %block_ubj {
65 //         "do_some_compute"(%i, %j): () -> ()
66 //       }
67 //     }
68 //   }
69 //
70 // And a dispatch function depending on the `asyncDispatch` option.
71 //
72 // When async dispatch is on: (pseudocode)
73 //
74 //   %block_size = ... compute parallel compute block size
75 //   %block_count = ... compute the number of compute blocks
76 //
77 //   func @async_dispatch(%block_start : index, %block_end : index, ...) {
78 //     // Keep splitting block range until we reached a range of size 1.
79 //     while (%block_end - %block_start > 1) {
80 //       %mid_index = block_start + (block_end - block_start) / 2;
81 //       async.execute { call @async_dispatch(%mid_index, %block_end); }
82 //       %block_end = %mid_index
83 //     }
84 //
85 //     // Call parallel compute function for a single block.
86 //     call @parallel_compute_fn(%block_start, %block_size, ...);
87 //   }
88 //
89 //   // Launch async dispatch for [0, block_count) range.
90 //   call @async_dispatch(%c0, %block_count);
91 //
92 // When async dispatch is off:
93 //
94 //   %block_size = ... compute parallel compute block size
95 //   %block_count = ... compute the number of compute blocks
96 //
97 //   scf.for %block_index = %c0 to %block_count {
98 //      call @parallel_compute_fn(%block_index, %block_size, ...)
99 //   }
100 //
101 struct AsyncParallelForPass
102     : public impl::AsyncParallelForBase<AsyncParallelForPass> {
103   AsyncParallelForPass() = default;
104 
105   AsyncParallelForPass(bool asyncDispatch, int32_t numWorkerThreads,
106                        int32_t minTaskSize) {
107     this->asyncDispatch = asyncDispatch;
108     this->numWorkerThreads = numWorkerThreads;
109     this->minTaskSize = minTaskSize;
110   }
111 
112   void runOnOperation() override;
113 };
114 
115 struct AsyncParallelForRewrite : public OpRewritePattern<scf::ParallelOp> {
116 public:
117   AsyncParallelForRewrite(
118       MLIRContext *ctx, bool asyncDispatch, int32_t numWorkerThreads,
119       AsyncMinTaskSizeComputationFunction computeMinTaskSize)
120       : OpRewritePattern(ctx), asyncDispatch(asyncDispatch),
121         numWorkerThreads(numWorkerThreads),
122         computeMinTaskSize(std::move(computeMinTaskSize)) {}
123 
124   LogicalResult matchAndRewrite(scf::ParallelOp op,
125                                 PatternRewriter &rewriter) const override;
126 
127 private:
128   bool asyncDispatch;
129   int32_t numWorkerThreads;
130   AsyncMinTaskSizeComputationFunction computeMinTaskSize;
131 };
132 
133 struct ParallelComputeFunctionType {
134   FunctionType type;
135   SmallVector<Value> captures;
136 };
137 
138 // Helper struct to parse parallel compute function argument list.
139 struct ParallelComputeFunctionArgs {
140   BlockArgument blockIndex();
141   BlockArgument blockSize();
142   ArrayRef<BlockArgument> tripCounts();
143   ArrayRef<BlockArgument> lowerBounds();
144   ArrayRef<BlockArgument> steps();
145   ArrayRef<BlockArgument> captures();
146 
147   unsigned numLoops;
148   ArrayRef<BlockArgument> args;
149 };
150 
151 struct ParallelComputeFunctionBounds {
152   SmallVector<IntegerAttr> tripCounts;
153   SmallVector<IntegerAttr> lowerBounds;
154   SmallVector<IntegerAttr> upperBounds;
155   SmallVector<IntegerAttr> steps;
156 };
157 
158 struct ParallelComputeFunction {
159   unsigned numLoops;
160   func::FuncOp func;
161   llvm::SmallVector<Value> captures;
162 };
163 
164 } // namespace
165 
166 BlockArgument ParallelComputeFunctionArgs::blockIndex() { return args[0]; }
167 BlockArgument ParallelComputeFunctionArgs::blockSize() { return args[1]; }
168 
169 ArrayRef<BlockArgument> ParallelComputeFunctionArgs::tripCounts() {
170   return args.drop_front(2).take_front(numLoops);
171 }
172 
173 ArrayRef<BlockArgument> ParallelComputeFunctionArgs::lowerBounds() {
174   return args.drop_front(2 + 1 * numLoops).take_front(numLoops);
175 }
176 
177 ArrayRef<BlockArgument> ParallelComputeFunctionArgs::steps() {
178   return args.drop_front(2 + 3 * numLoops).take_front(numLoops);
179 }
180 
181 ArrayRef<BlockArgument> ParallelComputeFunctionArgs::captures() {
182   return args.drop_front(2 + 4 * numLoops);
183 }
184 
185 template <typename ValueRange>
186 static SmallVector<IntegerAttr> integerConstants(ValueRange values) {
187   SmallVector<IntegerAttr> attrs(values.size());
188   for (unsigned i = 0; i < values.size(); ++i)
189     matchPattern(values[i], m_Constant(&attrs[i]));
190   return attrs;
191 }
192 
193 // Converts one-dimensional iteration index in the [0, tripCount) interval
194 // into multidimensional iteration coordinate.
195 static SmallVector<Value> delinearize(ImplicitLocOpBuilder &b, Value index,
196                                       ArrayRef<Value> tripCounts) {
197   SmallVector<Value> coords(tripCounts.size());
198   assert(!tripCounts.empty() && "tripCounts must be not empty");
199 
200   for (ssize_t i = tripCounts.size() - 1; i >= 0; --i) {
201     coords[i] = b.create<arith::RemSIOp>(index, tripCounts[i]);
202     index = b.create<arith::DivSIOp>(index, tripCounts[i]);
203   }
204 
205   return coords;
206 }
207 
208 // Returns a function type and implicit captures for a parallel compute
209 // function. We'll need a list of implicit captures to setup block and value
210 // mapping when we'll clone the body of the parallel operation.
211 static ParallelComputeFunctionType
212 getParallelComputeFunctionType(scf::ParallelOp op, PatternRewriter &rewriter) {
213   // Values implicitly captured by the parallel operation.
214   llvm::SetVector<Value> captures;
215   getUsedValuesDefinedAbove(op.getRegion(), op.getRegion(), captures);
216 
217   SmallVector<Type> inputs;
218   inputs.reserve(2 + 4 * op.getNumLoops() + captures.size());
219 
220   Type indexTy = rewriter.getIndexType();
221 
222   // One-dimensional iteration space defined by the block index and size.
223   inputs.push_back(indexTy); // blockIndex
224   inputs.push_back(indexTy); // blockSize
225 
226   // Multi-dimensional parallel iteration space defined by the loop trip counts.
227   for (unsigned i = 0; i < op.getNumLoops(); ++i)
228     inputs.push_back(indexTy); // loop tripCount
229 
230   // Parallel operation lower bound, upper bound and step. Lower bound, upper
231   // bound and step passed as contiguous arguments:
232   //   call @compute(%lb0, %lb1, ..., %ub0, %ub1, ..., %step0, %step1, ...)
233   for (unsigned i = 0; i < op.getNumLoops(); ++i) {
234     inputs.push_back(indexTy); // lower bound
235     inputs.push_back(indexTy); // upper bound
236     inputs.push_back(indexTy); // step
237   }
238 
239   // Types of the implicit captures.
240   for (Value capture : captures)
241     inputs.push_back(capture.getType());
242 
243   // Convert captures to vector for later convenience.
244   SmallVector<Value> capturesVector(captures.begin(), captures.end());
245   return {rewriter.getFunctionType(inputs, TypeRange()), capturesVector};
246 }
247 
248 // Create a parallel compute fuction from the parallel operation.
249 static ParallelComputeFunction createParallelComputeFunction(
250     scf::ParallelOp op, const ParallelComputeFunctionBounds &bounds,
251     unsigned numBlockAlignedInnerLoops, PatternRewriter &rewriter) {
252   OpBuilder::InsertionGuard guard(rewriter);
253   ImplicitLocOpBuilder b(op.getLoc(), rewriter);
254 
255   ModuleOp module = op->getParentOfType<ModuleOp>();
256 
257   ParallelComputeFunctionType computeFuncType =
258       getParallelComputeFunctionType(op, rewriter);
259 
260   FunctionType type = computeFuncType.type;
261   func::FuncOp func = func::FuncOp::create(
262       op.getLoc(),
263       numBlockAlignedInnerLoops > 0 ? "parallel_compute_fn_with_aligned_loops"
264                                     : "parallel_compute_fn",
265       type);
266   func.setPrivate();
267 
268   // Insert function into the module symbol table and assign it unique name.
269   SymbolTable symbolTable(module);
270   symbolTable.insert(func);
271   rewriter.getListener()->notifyOperationInserted(func, /*previous=*/{});
272 
273   // Create function entry block.
274   Block *block =
275       b.createBlock(&func.getBody(), func.begin(), type.getInputs(),
276                     SmallVector<Location>(type.getNumInputs(), op.getLoc()));
277   b.setInsertionPointToEnd(block);
278 
279   ParallelComputeFunctionArgs args = {op.getNumLoops(), func.getArguments()};
280 
281   // Block iteration position defined by the block index and size.
282   BlockArgument blockIndex = args.blockIndex();
283   BlockArgument blockSize = args.blockSize();
284 
285   // Constants used below.
286   Value c0 = b.create<arith::ConstantIndexOp>(0);
287   Value c1 = b.create<arith::ConstantIndexOp>(1);
288 
289   // Materialize known constants as constant operation in the function body.
290   auto values = [&](ArrayRef<BlockArgument> args, ArrayRef<IntegerAttr> attrs) {
291     return llvm::to_vector(
292         llvm::map_range(llvm::zip(args, attrs), [&](auto tuple) -> Value {
293           if (IntegerAttr attr = std::get<1>(tuple))
294             return b.create<arith::ConstantOp>(attr);
295           return std::get<0>(tuple);
296         }));
297   };
298 
299   // Multi-dimensional parallel iteration space defined by the loop trip counts.
300   auto tripCounts = values(args.tripCounts(), bounds.tripCounts);
301 
302   // Parallel operation lower bound and step.
303   auto lowerBounds = values(args.lowerBounds(), bounds.lowerBounds);
304   auto steps = values(args.steps(), bounds.steps);
305 
306   // Remaining arguments are implicit captures of the parallel operation.
307   ArrayRef<BlockArgument> captures = args.captures();
308 
309   // Compute a product of trip counts to get the size of the flattened
310   // one-dimensional iteration space.
311   Value tripCount = tripCounts[0];
312   for (unsigned i = 1; i < tripCounts.size(); ++i)
313     tripCount = b.create<arith::MulIOp>(tripCount, tripCounts[i]);
314 
315   // Find one-dimensional iteration bounds: [blockFirstIndex, blockLastIndex]:
316   //   blockFirstIndex = blockIndex * blockSize
317   Value blockFirstIndex = b.create<arith::MulIOp>(blockIndex, blockSize);
318 
319   // The last one-dimensional index in the block defined by the `blockIndex`:
320   //   blockLastIndex = min(blockFirstIndex + blockSize, tripCount) - 1
321   Value blockEnd0 = b.create<arith::AddIOp>(blockFirstIndex, blockSize);
322   Value blockEnd1 = b.create<arith::MinSIOp>(blockEnd0, tripCount);
323   Value blockLastIndex = b.create<arith::SubIOp>(blockEnd1, c1);
324 
325   // Convert one-dimensional indices to multi-dimensional coordinates.
326   auto blockFirstCoord = delinearize(b, blockFirstIndex, tripCounts);
327   auto blockLastCoord = delinearize(b, blockLastIndex, tripCounts);
328 
329   // Compute loops upper bounds derived from the block last coordinates:
330   //   blockEndCoord[i] = blockLastCoord[i] + 1
331   //
332   // Block first and last coordinates can be the same along the outer compute
333   // dimension when inner compute dimension contains multiple blocks.
334   SmallVector<Value> blockEndCoord(op.getNumLoops());
335   for (size_t i = 0; i < blockLastCoord.size(); ++i)
336     blockEndCoord[i] = b.create<arith::AddIOp>(blockLastCoord[i], c1);
337 
338   // Construct a loop nest out of scf.for operations that will iterate over
339   // all coordinates in [blockFirstCoord, blockLastCoord] range.
340   using LoopBodyBuilder =
341       std::function<void(OpBuilder &, Location, Value, ValueRange)>;
342   using LoopNestBuilder = std::function<LoopBodyBuilder(size_t loopIdx)>;
343 
344   // Parallel region induction variables computed from the multi-dimensional
345   // iteration coordinate using parallel operation bounds and step:
346   //
347   //   computeBlockInductionVars[loopIdx] =
348   //       lowerBound[loopIdx] + blockCoord[loopIdx] * step[loopIdx]
349   SmallVector<Value> computeBlockInductionVars(op.getNumLoops());
350 
351   // We need to know if we are in the first or last iteration of the
352   // multi-dimensional loop for each loop in the nest, so we can decide what
353   // loop bounds should we use for the nested loops: bounds defined by compute
354   // block interval, or bounds defined by the parallel operation.
355   //
356   // Example: 2d parallel operation
357   //                   i   j
358   //   loop sizes:   [50, 50]
359   //   first coord:  [25, 25]
360   //   last coord:   [30, 30]
361   //
362   // If `i` is equal to 25 then iteration over `j` should start at 25, when `i`
363   // is between 25 and 30 it should start at 0. The upper bound for `j` should
364   // be 50, except when `i` is equal to 30, then it should also be 30.
365   //
366   // Value at ith position specifies if all loops in [0, i) range of the loop
367   // nest are in the first/last iteration.
368   SmallVector<Value> isBlockFirstCoord(op.getNumLoops());
369   SmallVector<Value> isBlockLastCoord(op.getNumLoops());
370 
371   // Builds inner loop nest inside async.execute operation that does all the
372   // work concurrently.
373   LoopNestBuilder workLoopBuilder = [&](size_t loopIdx) -> LoopBodyBuilder {
374     return [&, loopIdx](OpBuilder &nestedBuilder, Location loc, Value iv,
375                         ValueRange args) {
376       ImplicitLocOpBuilder b(loc, nestedBuilder);
377 
378       // Compute induction variable for `loopIdx`.
379       computeBlockInductionVars[loopIdx] = b.create<arith::AddIOp>(
380           lowerBounds[loopIdx], b.create<arith::MulIOp>(iv, steps[loopIdx]));
381 
382       // Check if we are inside first or last iteration of the loop.
383       isBlockFirstCoord[loopIdx] = b.create<arith::CmpIOp>(
384           arith::CmpIPredicate::eq, iv, blockFirstCoord[loopIdx]);
385       isBlockLastCoord[loopIdx] = b.create<arith::CmpIOp>(
386           arith::CmpIPredicate::eq, iv, blockLastCoord[loopIdx]);
387 
388       // Check if the previous loop is in its first or last iteration.
389       if (loopIdx > 0) {
390         isBlockFirstCoord[loopIdx] = b.create<arith::AndIOp>(
391             isBlockFirstCoord[loopIdx], isBlockFirstCoord[loopIdx - 1]);
392         isBlockLastCoord[loopIdx] = b.create<arith::AndIOp>(
393             isBlockLastCoord[loopIdx], isBlockLastCoord[loopIdx - 1]);
394       }
395 
396       // Keep building loop nest.
397       if (loopIdx < op.getNumLoops() - 1) {
398         if (loopIdx + 1 >= op.getNumLoops() - numBlockAlignedInnerLoops) {
399           // For block aligned loops we always iterate starting from 0 up to
400           // the loop trip counts.
401           b.create<scf::ForOp>(c0, tripCounts[loopIdx + 1], c1, ValueRange(),
402                                workLoopBuilder(loopIdx + 1));
403 
404         } else {
405           // Select nested loop lower/upper bounds depending on our position in
406           // the multi-dimensional iteration space.
407           auto lb = b.create<arith::SelectOp>(isBlockFirstCoord[loopIdx],
408                                               blockFirstCoord[loopIdx + 1], c0);
409 
410           auto ub = b.create<arith::SelectOp>(isBlockLastCoord[loopIdx],
411                                               blockEndCoord[loopIdx + 1],
412                                               tripCounts[loopIdx + 1]);
413 
414           b.create<scf::ForOp>(lb, ub, c1, ValueRange(),
415                                workLoopBuilder(loopIdx + 1));
416         }
417 
418         b.create<scf::YieldOp>(loc);
419         return;
420       }
421 
422       // Copy the body of the parallel op into the inner-most loop.
423       IRMapping mapping;
424       mapping.map(op.getInductionVars(), computeBlockInductionVars);
425       mapping.map(computeFuncType.captures, captures);
426 
427       for (auto &bodyOp : op.getRegion().front().without_terminator())
428         b.clone(bodyOp, mapping);
429       b.create<scf::YieldOp>(loc);
430     };
431   };
432 
433   b.create<scf::ForOp>(blockFirstCoord[0], blockEndCoord[0], c1, ValueRange(),
434                        workLoopBuilder(0));
435   b.create<func::ReturnOp>(ValueRange());
436 
437   return {op.getNumLoops(), func, std::move(computeFuncType.captures)};
438 }
439 
440 // Creates recursive async dispatch function for the given parallel compute
441 // function. Dispatch function keeps splitting block range into halves until it
442 // reaches a single block, and then excecutes it inline.
443 //
444 // Function pseudocode (mix of C++ and MLIR):
445 //
446 //   func @async_dispatch(%block_start : index, %block_end : index, ...) {
447 //
448 //     // Keep splitting block range until we reached a range of size 1.
449 //     while (%block_end - %block_start > 1) {
450 //       %mid_index = block_start + (block_end - block_start) / 2;
451 //       async.execute { call @async_dispatch(%mid_index, %block_end); }
452 //       %block_end = %mid_index
453 //     }
454 //
455 //     // Call parallel compute function for a single block.
456 //     call @parallel_compute_fn(%block_start, %block_size, ...);
457 //   }
458 //
459 static func::FuncOp
460 createAsyncDispatchFunction(ParallelComputeFunction &computeFunc,
461                             PatternRewriter &rewriter) {
462   OpBuilder::InsertionGuard guard(rewriter);
463   Location loc = computeFunc.func.getLoc();
464   ImplicitLocOpBuilder b(loc, rewriter);
465 
466   ModuleOp module = computeFunc.func->getParentOfType<ModuleOp>();
467 
468   ArrayRef<Type> computeFuncInputTypes =
469       computeFunc.func.getFunctionType().getInputs();
470 
471   // Compared to the parallel compute function async dispatch function takes
472   // additional !async.group argument. Also instead of a single `blockIndex` it
473   // takes `blockStart` and `blockEnd` arguments to define the range of
474   // dispatched blocks.
475   SmallVector<Type> inputTypes;
476   inputTypes.push_back(async::GroupType::get(rewriter.getContext()));
477   inputTypes.push_back(rewriter.getIndexType()); // add blockStart argument
478   inputTypes.append(computeFuncInputTypes.begin(), computeFuncInputTypes.end());
479 
480   FunctionType type = rewriter.getFunctionType(inputTypes, TypeRange());
481   func::FuncOp func = func::FuncOp::create(loc, "async_dispatch_fn", type);
482   func.setPrivate();
483 
484   // Insert function into the module symbol table and assign it unique name.
485   SymbolTable symbolTable(module);
486   symbolTable.insert(func);
487   rewriter.getListener()->notifyOperationInserted(func, /*previous=*/{});
488 
489   // Create function entry block.
490   Block *block = b.createBlock(&func.getBody(), func.begin(), type.getInputs(),
491                                SmallVector<Location>(type.getNumInputs(), loc));
492   b.setInsertionPointToEnd(block);
493 
494   Type indexTy = b.getIndexType();
495   Value c1 = b.create<arith::ConstantIndexOp>(1);
496   Value c2 = b.create<arith::ConstantIndexOp>(2);
497 
498   // Get the async group that will track async dispatch completion.
499   Value group = block->getArgument(0);
500 
501   // Get the block iteration range: [blockStart, blockEnd)
502   Value blockStart = block->getArgument(1);
503   Value blockEnd = block->getArgument(2);
504 
505   // Create a work splitting while loop for the [blockStart, blockEnd) range.
506   SmallVector<Type> types = {indexTy, indexTy};
507   SmallVector<Value> operands = {blockStart, blockEnd};
508   SmallVector<Location> locations = {loc, loc};
509 
510   // Create a recursive dispatch loop.
511   scf::WhileOp whileOp = b.create<scf::WhileOp>(types, operands);
512   Block *before = b.createBlock(&whileOp.getBefore(), {}, types, locations);
513   Block *after = b.createBlock(&whileOp.getAfter(), {}, types, locations);
514 
515   // Setup dispatch loop condition block: decide if we need to go into the
516   // `after` block and launch one more async dispatch.
517   {
518     b.setInsertionPointToEnd(before);
519     Value start = before->getArgument(0);
520     Value end = before->getArgument(1);
521     Value distance = b.create<arith::SubIOp>(end, start);
522     Value dispatch =
523         b.create<arith::CmpIOp>(arith::CmpIPredicate::sgt, distance, c1);
524     b.create<scf::ConditionOp>(dispatch, before->getArguments());
525   }
526 
527   // Setup the async dispatch loop body: recursively call dispatch function
528   // for the seconds half of the original range and go to the next iteration.
529   {
530     b.setInsertionPointToEnd(after);
531     Value start = after->getArgument(0);
532     Value end = after->getArgument(1);
533     Value distance = b.create<arith::SubIOp>(end, start);
534     Value halfDistance = b.create<arith::DivSIOp>(distance, c2);
535     Value midIndex = b.create<arith::AddIOp>(start, halfDistance);
536 
537     // Call parallel compute function inside the async.execute region.
538     auto executeBodyBuilder = [&](OpBuilder &executeBuilder,
539                                   Location executeLoc, ValueRange executeArgs) {
540       // Update the original `blockStart` and `blockEnd` with new range.
541       SmallVector<Value> operands{block->getArguments().begin(),
542                                   block->getArguments().end()};
543       operands[1] = midIndex;
544       operands[2] = end;
545 
546       executeBuilder.create<func::CallOp>(executeLoc, func.getSymName(),
547                                           func.getResultTypes(), operands);
548       executeBuilder.create<async::YieldOp>(executeLoc, ValueRange());
549     };
550 
551     // Create async.execute operation to dispatch half of the block range.
552     auto execute = b.create<ExecuteOp>(TypeRange(), ValueRange(), ValueRange(),
553                                        executeBodyBuilder);
554     b.create<AddToGroupOp>(indexTy, execute.getToken(), group);
555     b.create<scf::YieldOp>(ValueRange({start, midIndex}));
556   }
557 
558   // After dispatching async operations to process the tail of the block range
559   // call the parallel compute function for the first block of the range.
560   b.setInsertionPointAfter(whileOp);
561 
562   // Drop async dispatch specific arguments: async group, block start and end.
563   auto forwardedInputs = block->getArguments().drop_front(3);
564   SmallVector<Value> computeFuncOperands = {blockStart};
565   computeFuncOperands.append(forwardedInputs.begin(), forwardedInputs.end());
566 
567   b.create<func::CallOp>(computeFunc.func.getSymName(),
568                          computeFunc.func.getResultTypes(),
569                          computeFuncOperands);
570   b.create<func::ReturnOp>(ValueRange());
571 
572   return func;
573 }
574 
575 // Launch async dispatch of the parallel compute function.
576 static void doAsyncDispatch(ImplicitLocOpBuilder &b, PatternRewriter &rewriter,
577                             ParallelComputeFunction &parallelComputeFunction,
578                             scf::ParallelOp op, Value blockSize,
579                             Value blockCount,
580                             const SmallVector<Value> &tripCounts) {
581   MLIRContext *ctx = op->getContext();
582 
583   // Add one more level of indirection to dispatch parallel compute functions
584   // using async operations and recursive work splitting.
585   func::FuncOp asyncDispatchFunction =
586       createAsyncDispatchFunction(parallelComputeFunction, rewriter);
587 
588   Value c0 = b.create<arith::ConstantIndexOp>(0);
589   Value c1 = b.create<arith::ConstantIndexOp>(1);
590 
591   // Appends operands shared by async dispatch and parallel compute functions to
592   // the given operands vector.
593   auto appendBlockComputeOperands = [&](SmallVector<Value> &operands) {
594     operands.append(tripCounts);
595     operands.append(op.getLowerBound().begin(), op.getLowerBound().end());
596     operands.append(op.getUpperBound().begin(), op.getUpperBound().end());
597     operands.append(op.getStep().begin(), op.getStep().end());
598     operands.append(parallelComputeFunction.captures);
599   };
600 
601   // Check if the block size is one, in this case we can skip the async dispatch
602   // completely. If this will be known statically, then canonicalization will
603   // erase async group operations.
604   Value isSingleBlock =
605       b.create<arith::CmpIOp>(arith::CmpIPredicate::eq, blockCount, c1);
606 
607   auto syncDispatch = [&](OpBuilder &nestedBuilder, Location loc) {
608     ImplicitLocOpBuilder b(loc, nestedBuilder);
609 
610     // Call parallel compute function for the single block.
611     SmallVector<Value> operands = {c0, blockSize};
612     appendBlockComputeOperands(operands);
613 
614     b.create<func::CallOp>(parallelComputeFunction.func.getSymName(),
615                            parallelComputeFunction.func.getResultTypes(),
616                            operands);
617     b.create<scf::YieldOp>();
618   };
619 
620   auto asyncDispatch = [&](OpBuilder &nestedBuilder, Location loc) {
621     ImplicitLocOpBuilder b(loc, nestedBuilder);
622 
623     // Create an async.group to wait on all async tokens from the concurrent
624     // execution of multiple parallel compute function. First block will be
625     // executed synchronously in the caller thread.
626     Value groupSize = b.create<arith::SubIOp>(blockCount, c1);
627     Value group = b.create<CreateGroupOp>(GroupType::get(ctx), groupSize);
628 
629     // Launch async dispatch function for [0, blockCount) range.
630     SmallVector<Value> operands = {group, c0, blockCount, blockSize};
631     appendBlockComputeOperands(operands);
632 
633     b.create<func::CallOp>(asyncDispatchFunction.getSymName(),
634                            asyncDispatchFunction.getResultTypes(), operands);
635 
636     // Wait for the completion of all parallel compute operations.
637     b.create<AwaitAllOp>(group);
638 
639     b.create<scf::YieldOp>();
640   };
641 
642   // Dispatch either single block compute function, or launch async dispatch.
643   b.create<scf::IfOp>(isSingleBlock, syncDispatch, asyncDispatch);
644 }
645 
646 // Dispatch parallel compute functions by submitting all async compute tasks
647 // from a simple for loop in the caller thread.
648 static void
649 doSequentialDispatch(ImplicitLocOpBuilder &b, PatternRewriter &rewriter,
650                      ParallelComputeFunction &parallelComputeFunction,
651                      scf::ParallelOp op, Value blockSize, Value blockCount,
652                      const SmallVector<Value> &tripCounts) {
653   MLIRContext *ctx = op->getContext();
654 
655   func::FuncOp compute = parallelComputeFunction.func;
656 
657   Value c0 = b.create<arith::ConstantIndexOp>(0);
658   Value c1 = b.create<arith::ConstantIndexOp>(1);
659 
660   // Create an async.group to wait on all async tokens from the concurrent
661   // execution of multiple parallel compute function. First block will be
662   // executed synchronously in the caller thread.
663   Value groupSize = b.create<arith::SubIOp>(blockCount, c1);
664   Value group = b.create<CreateGroupOp>(GroupType::get(ctx), groupSize);
665 
666   // Call parallel compute function for all blocks.
667   using LoopBodyBuilder =
668       std::function<void(OpBuilder &, Location, Value, ValueRange)>;
669 
670   // Returns parallel compute function operands to process the given block.
671   auto computeFuncOperands = [&](Value blockIndex) -> SmallVector<Value> {
672     SmallVector<Value> computeFuncOperands = {blockIndex, blockSize};
673     computeFuncOperands.append(tripCounts);
674     computeFuncOperands.append(op.getLowerBound().begin(),
675                                op.getLowerBound().end());
676     computeFuncOperands.append(op.getUpperBound().begin(),
677                                op.getUpperBound().end());
678     computeFuncOperands.append(op.getStep().begin(), op.getStep().end());
679     computeFuncOperands.append(parallelComputeFunction.captures);
680     return computeFuncOperands;
681   };
682 
683   // Induction variable is the index of the block: [0, blockCount).
684   LoopBodyBuilder loopBuilder = [&](OpBuilder &loopBuilder, Location loc,
685                                     Value iv, ValueRange args) {
686     ImplicitLocOpBuilder b(loc, loopBuilder);
687 
688     // Call parallel compute function inside the async.execute region.
689     auto executeBodyBuilder = [&](OpBuilder &executeBuilder,
690                                   Location executeLoc, ValueRange executeArgs) {
691       executeBuilder.create<func::CallOp>(executeLoc, compute.getSymName(),
692                                           compute.getResultTypes(),
693                                           computeFuncOperands(iv));
694       executeBuilder.create<async::YieldOp>(executeLoc, ValueRange());
695     };
696 
697     // Create async.execute operation to launch parallel computate function.
698     auto execute = b.create<ExecuteOp>(TypeRange(), ValueRange(), ValueRange(),
699                                        executeBodyBuilder);
700     b.create<AddToGroupOp>(rewriter.getIndexType(), execute.getToken(), group);
701     b.create<scf::YieldOp>();
702   };
703 
704   // Iterate over all compute blocks and launch parallel compute operations.
705   b.create<scf::ForOp>(c1, blockCount, c1, ValueRange(), loopBuilder);
706 
707   // Call parallel compute function for the first block in the caller thread.
708   b.create<func::CallOp>(compute.getSymName(), compute.getResultTypes(),
709                          computeFuncOperands(c0));
710 
711   // Wait for the completion of all async compute operations.
712   b.create<AwaitAllOp>(group);
713 }
714 
715 LogicalResult
716 AsyncParallelForRewrite::matchAndRewrite(scf::ParallelOp op,
717                                          PatternRewriter &rewriter) const {
718   // We do not currently support rewrite for parallel op with reductions.
719   if (op.getNumReductions() != 0)
720     return failure();
721 
722   ImplicitLocOpBuilder b(op.getLoc(), rewriter);
723 
724   // Computing minTaskSize emits IR and can be implemented as executing a cost
725   // model on the body of the scf.parallel. Thus it needs to be computed before
726   // the body of the scf.parallel has been manipulated.
727   Value minTaskSize = computeMinTaskSize(b, op);
728 
729   // Make sure that all constants will be inside the parallel operation body to
730   // reduce the number of parallel compute function arguments.
731   cloneConstantsIntoTheRegion(op.getRegion(), rewriter);
732 
733   // Compute trip count for each loop induction variable:
734   //   tripCount = ceil_div(upperBound - lowerBound, step);
735   SmallVector<Value> tripCounts(op.getNumLoops());
736   for (size_t i = 0; i < op.getNumLoops(); ++i) {
737     auto lb = op.getLowerBound()[i];
738     auto ub = op.getUpperBound()[i];
739     auto step = op.getStep()[i];
740     auto range = b.createOrFold<arith::SubIOp>(ub, lb);
741     tripCounts[i] = b.createOrFold<arith::CeilDivSIOp>(range, step);
742   }
743 
744   // Compute a product of trip counts to get the 1-dimensional iteration space
745   // for the scf.parallel operation.
746   Value tripCount = tripCounts[0];
747   for (size_t i = 1; i < tripCounts.size(); ++i)
748     tripCount = b.create<arith::MulIOp>(tripCount, tripCounts[i]);
749 
750   // Short circuit no-op parallel loops (zero iterations) that can arise from
751   // the memrefs with dynamic dimension(s) equal to zero.
752   Value c0 = b.create<arith::ConstantIndexOp>(0);
753   Value isZeroIterations =
754       b.create<arith::CmpIOp>(arith::CmpIPredicate::eq, tripCount, c0);
755 
756   // Do absolutely nothing if the trip count is zero.
757   auto noOp = [&](OpBuilder &nestedBuilder, Location loc) {
758     nestedBuilder.create<scf::YieldOp>(loc);
759   };
760 
761   // Compute the parallel block size and dispatch concurrent tasks computing
762   // results for each block.
763   auto dispatch = [&](OpBuilder &nestedBuilder, Location loc) {
764     ImplicitLocOpBuilder b(loc, nestedBuilder);
765 
766     // Collect statically known constants defining the loop nest in the parallel
767     // compute function. LLVM can't always push constants across the non-trivial
768     // async dispatch call graph, by providing these values explicitly we can
769     // choose to build more efficient loop nest, and rely on a better constant
770     // folding, loop unrolling and vectorization.
771     ParallelComputeFunctionBounds staticBounds = {
772         integerConstants(tripCounts),
773         integerConstants(op.getLowerBound()),
774         integerConstants(op.getUpperBound()),
775         integerConstants(op.getStep()),
776     };
777 
778     // Find how many inner iteration dimensions are statically known, and their
779     // product is smaller than the `512`. We align the parallel compute block
780     // size by the product of statically known dimensions, so that we can
781     // guarantee that the inner loops executes from 0 to the loop trip counts
782     // and we can elide dynamic loop boundaries, and give LLVM an opportunity to
783     // unroll the loops. The constant `512` is arbitrary, it should depend on
784     // how many iterations LLVM will typically decide to unroll.
785     static constexpr int64_t maxUnrollableIterations = 512;
786 
787     // The number of inner loops with statically known number of iterations less
788     // than the `maxUnrollableIterations` value.
789     int numUnrollableLoops = 0;
790 
791     auto getInt = [](IntegerAttr attr) { return attr ? attr.getInt() : 0; };
792 
793     SmallVector<int64_t> numIterations(op.getNumLoops());
794     numIterations.back() = getInt(staticBounds.tripCounts.back());
795 
796     for (int i = op.getNumLoops() - 2; i >= 0; --i) {
797       int64_t tripCount = getInt(staticBounds.tripCounts[i]);
798       int64_t innerIterations = numIterations[i + 1];
799       numIterations[i] = tripCount * innerIterations;
800 
801       // Update the number of inner loops that we can potentially unroll.
802       if (innerIterations > 0 && innerIterations <= maxUnrollableIterations)
803         numUnrollableLoops++;
804     }
805 
806     Value numWorkerThreadsVal;
807     if (numWorkerThreads >= 0)
808       numWorkerThreadsVal = b.create<arith::ConstantIndexOp>(numWorkerThreads);
809     else
810       numWorkerThreadsVal = b.create<async::RuntimeNumWorkerThreadsOp>();
811 
812     // With large number of threads the value of creating many compute blocks
813     // is reduced because the problem typically becomes memory bound. For this
814     // reason we scale the number of workers using an equivalent to the
815     // following logic:
816     //   float overshardingFactor = numWorkerThreads <= 4    ? 8.0
817     //                              : numWorkerThreads <= 8  ? 4.0
818     //                              : numWorkerThreads <= 16 ? 2.0
819     //                              : numWorkerThreads <= 32 ? 1.0
820     //                              : numWorkerThreads <= 64 ? 0.8
821     //                                                       : 0.6;
822 
823     // Pairs of non-inclusive lower end of the bracket and factor that the
824     // number of workers needs to be scaled with if it falls in that bucket.
825     const SmallVector<std::pair<int, float>> overshardingBrackets = {
826         {4, 4.0f}, {8, 2.0f}, {16, 1.0f}, {32, 0.8f}, {64, 0.6f}};
827     const float initialOvershardingFactor = 8.0f;
828 
829     Value scalingFactor = b.create<arith::ConstantFloatOp>(
830         llvm::APFloat(initialOvershardingFactor), b.getF32Type());
831     for (const std::pair<int, float> &p : overshardingBrackets) {
832       Value bracketBegin = b.create<arith::ConstantIndexOp>(p.first);
833       Value inBracket = b.create<arith::CmpIOp>(
834           arith::CmpIPredicate::sgt, numWorkerThreadsVal, bracketBegin);
835       Value bracketScalingFactor = b.create<arith::ConstantFloatOp>(
836           llvm::APFloat(p.second), b.getF32Type());
837       scalingFactor = b.create<arith::SelectOp>(inBracket, bracketScalingFactor,
838                                                 scalingFactor);
839     }
840     Value numWorkersIndex =
841         b.create<arith::IndexCastOp>(b.getI32Type(), numWorkerThreadsVal);
842     Value numWorkersFloat =
843         b.create<arith::SIToFPOp>(b.getF32Type(), numWorkersIndex);
844     Value scaledNumWorkers =
845         b.create<arith::MulFOp>(scalingFactor, numWorkersFloat);
846     Value scaledNumInt =
847         b.create<arith::FPToSIOp>(b.getI32Type(), scaledNumWorkers);
848     Value scaledWorkers =
849         b.create<arith::IndexCastOp>(b.getIndexType(), scaledNumInt);
850 
851     Value maxComputeBlocks = b.create<arith::MaxSIOp>(
852         b.create<arith::ConstantIndexOp>(1), scaledWorkers);
853 
854     // Compute parallel block size from the parallel problem size:
855     //   blockSize = min(tripCount,
856     //                   max(ceil_div(tripCount, maxComputeBlocks),
857     //                       minTaskSize))
858     Value bs0 = b.create<arith::CeilDivSIOp>(tripCount, maxComputeBlocks);
859     Value bs1 = b.create<arith::MaxSIOp>(bs0, minTaskSize);
860     Value blockSize = b.create<arith::MinSIOp>(tripCount, bs1);
861 
862     // Dispatch parallel compute function using async recursive work splitting,
863     // or by submitting compute task sequentially from a caller thread.
864     auto doDispatch = asyncDispatch ? doAsyncDispatch : doSequentialDispatch;
865 
866     // Create a parallel compute function that takes a block id and computes
867     // the parallel operation body for a subset of iteration space.
868 
869     // Compute the number of parallel compute blocks.
870     Value blockCount = b.create<arith::CeilDivSIOp>(tripCount, blockSize);
871 
872     // Dispatch parallel compute function without hints to unroll inner loops.
873     auto dispatchDefault = [&](OpBuilder &nestedBuilder, Location loc) {
874       ParallelComputeFunction compute =
875           createParallelComputeFunction(op, staticBounds, 0, rewriter);
876 
877       ImplicitLocOpBuilder b(loc, nestedBuilder);
878       doDispatch(b, rewriter, compute, op, blockSize, blockCount, tripCounts);
879       b.create<scf::YieldOp>();
880     };
881 
882     // Dispatch parallel compute function with hints for unrolling inner loops.
883     auto dispatchBlockAligned = [&](OpBuilder &nestedBuilder, Location loc) {
884       ParallelComputeFunction compute = createParallelComputeFunction(
885           op, staticBounds, numUnrollableLoops, rewriter);
886 
887       ImplicitLocOpBuilder b(loc, nestedBuilder);
888       // Align the block size to be a multiple of the statically known
889       // number of iterations in the inner loops.
890       Value numIters = b.create<arith::ConstantIndexOp>(
891           numIterations[op.getNumLoops() - numUnrollableLoops]);
892       Value alignedBlockSize = b.create<arith::MulIOp>(
893           b.create<arith::CeilDivSIOp>(blockSize, numIters), numIters);
894       doDispatch(b, rewriter, compute, op, alignedBlockSize, blockCount,
895                  tripCounts);
896       b.create<scf::YieldOp>();
897     };
898 
899     // Dispatch to block aligned compute function only if the computed block
900     // size is larger than the number of iterations in the unrollable inner
901     // loops, because otherwise it can reduce the available parallelism.
902     if (numUnrollableLoops > 0) {
903       Value numIters = b.create<arith::ConstantIndexOp>(
904           numIterations[op.getNumLoops() - numUnrollableLoops]);
905       Value useBlockAlignedComputeFn = b.create<arith::CmpIOp>(
906           arith::CmpIPredicate::sge, blockSize, numIters);
907 
908       b.create<scf::IfOp>(useBlockAlignedComputeFn, dispatchBlockAligned,
909                           dispatchDefault);
910       b.create<scf::YieldOp>();
911     } else {
912       dispatchDefault(b, loc);
913     }
914   };
915 
916   // Replace the `scf.parallel` operation with the parallel compute function.
917   b.create<scf::IfOp>(isZeroIterations, noOp, dispatch);
918 
919   // Parallel operation was replaced with a block iteration loop.
920   rewriter.eraseOp(op);
921 
922   return success();
923 }
924 
925 void AsyncParallelForPass::runOnOperation() {
926   MLIRContext *ctx = &getContext();
927 
928   RewritePatternSet patterns(ctx);
929   populateAsyncParallelForPatterns(
930       patterns, asyncDispatch, numWorkerThreads,
931       [&](ImplicitLocOpBuilder builder, scf::ParallelOp op) {
932         return builder.create<arith::ConstantIndexOp>(minTaskSize);
933       });
934   if (failed(applyPatternsGreedily(getOperation(), std::move(patterns))))
935     signalPassFailure();
936 }
937 
938 std::unique_ptr<Pass> mlir::createAsyncParallelForPass() {
939   return std::make_unique<AsyncParallelForPass>();
940 }
941 
942 std::unique_ptr<Pass> mlir::createAsyncParallelForPass(bool asyncDispatch,
943                                                        int32_t numWorkerThreads,
944                                                        int32_t minTaskSize) {
945   return std::make_unique<AsyncParallelForPass>(asyncDispatch, numWorkerThreads,
946                                                 minTaskSize);
947 }
948 
949 void mlir::async::populateAsyncParallelForPatterns(
950     RewritePatternSet &patterns, bool asyncDispatch, int32_t numWorkerThreads,
951     const AsyncMinTaskSizeComputationFunction &computeMinTaskSize) {
952   MLIRContext *ctx = patterns.getContext();
953   patterns.add<AsyncParallelForRewrite>(ctx, asyncDispatch, numWorkerThreads,
954                                         computeMinTaskSize);
955 }
956