@@ -30,6 +30,9 @@ namespace looputils {
3030struct InductionVariableInfo {
3131 // / the operation allocating memory for iteration variable,
3232 mlir::Operation *iterVarMemDef;
33+ // / the operation(s) updating the iteration variable with the current
34+ // / iteration number.
35+ llvm::SetVector<mlir::Operation *> indVarUpdateOps;
3336};
3437
3538using LoopNestToIndVarMap =
@@ -102,6 +105,47 @@ mlir::Operation *findLoopIterationVarMemDecl(fir::DoLoopOp doLoop) {
102105 return result.getDefiningOp ();
103106}
104107
108+ // / Collects the op(s) responsible for updating a loop's iteration variable with
109+ // / the current iteration number. For example, for the input IR:
110+ // / ```
111+ // / %i = fir.alloca i32 {bindc_name = "i"}
112+ // / %i_decl:2 = hlfir.declare %i ...
113+ // / ...
114+ // / fir.do_loop %i_iv = %lb to %ub step %step unordered {
115+ // / %1 = fir.convert %i_iv : (index) -> i32
116+ // / fir.store %1 to %i_decl#1 : !fir.ref<i32>
117+ // / ...
118+ // / }
119+ // / ```
120+ // / this function would return the first 2 ops in the `fir.do_loop`'s region.
121+ llvm::SetVector<mlir::Operation *>
122+ extractIndVarUpdateOps (fir::DoLoopOp doLoop) {
123+ mlir::Value indVar = doLoop.getInductionVar ();
124+ llvm::SetVector<mlir::Operation *> indVarUpdateOps;
125+
126+ llvm::SmallVector<mlir::Value> toProcess;
127+ toProcess.push_back (indVar);
128+
129+ llvm::DenseSet<mlir::Value> done;
130+
131+ while (!toProcess.empty ()) {
132+ mlir::Value val = toProcess.back ();
133+ toProcess.pop_back ();
134+
135+ if (!done.insert (val).second )
136+ continue ;
137+
138+ for (mlir::Operation *user : val.getUsers ()) {
139+ indVarUpdateOps.insert (user);
140+
141+ for (mlir::Value result : user->getResults ())
142+ toProcess.push_back (result);
143+ }
144+ }
145+
146+ return std::move (indVarUpdateOps);
147+ }
148+
105149// / Loop \p innerLoop is considered perfectly-nested inside \p outerLoop iff
106150// / there are no operations in \p outerloop's body other than:
107151// /
@@ -198,7 +242,9 @@ mlir::LogicalResult collectLoopNest(fir::DoLoopOp currentLoop,
198242 while (true ) {
199243 loopNest.insert (
200244 {currentLoop,
201- InductionVariableInfo{findLoopIterationVarMemDecl (currentLoop)}});
245+ InductionVariableInfo{
246+ findLoopIterationVarMemDecl (currentLoop),
247+ std::move (looputils::extractIndVarUpdateOps (currentLoop))}});
202248
203249 llvm::SmallVector<fir::DoLoopOp> unorderedLoops;
204250
@@ -225,6 +271,96 @@ mlir::LogicalResult collectLoopNest(fir::DoLoopOp currentLoop,
225271
226272 return mlir::success ();
227273}
274+
275+ // / Prepares the `fir.do_loop` nest to be easily mapped to OpenMP. In
276+ // / particular, this function would take this input IR:
277+ // / ```
278+ // / fir.do_loop %i_iv = %i_lb to %i_ub step %i_step unordered {
279+ // / fir.store %i_iv to %i#1 : !fir.ref<i32>
280+ // / %j_lb = arith.constant 1 : i32
281+ // / %j_ub = arith.constant 10 : i32
282+ // / %j_step = arith.constant 1 : index
283+ // /
284+ // / fir.do_loop %j_iv = %j_lb to %j_ub step %j_step unordered {
285+ // / fir.store %j_iv to %j#1 : !fir.ref<i32>
286+ // / ...
287+ // / }
288+ // / }
289+ // / ```
290+ // /
291+ // / into the following form (using generic op form since the result is
292+ // / technically an invalid `fir.do_loop` op:
293+ // /
294+ // / ```
295+ // / "fir.do_loop"(%i_lb, %i_ub, %i_step) <{unordered}> ({
296+ // / ^bb0(%i_iv: index):
297+ // / %j_lb = "arith.constant"() <{value = 1 : i32}> : () -> i32
298+ // / %j_ub = "arith.constant"() <{value = 10 : i32}> : () -> i32
299+ // / %j_step = "arith.constant"() <{value = 1 : index}> : () -> index
300+ // /
301+ // / "fir.do_loop"(%j_lb, %j_ub, %j_step) <{unordered}> ({
302+ // / ^bb0(%new_i_iv: index, %new_j_iv: index):
303+ // / "fir.store"(%new_i_iv, %i#1) : (i32, !fir.ref<i32>) -> ()
304+ // / "fir.store"(%new_j_iv, %j#1) : (i32, !fir.ref<i32>) -> ()
305+ // / ...
306+ // / })
307+ // / ```
308+ // /
309+ // / What happened to the loop nest is the following:
310+ // /
311+ // / * the innermost loop's entry block was updated from having one operand to
312+ // / having `n` operands where `n` is the number of loops in the nest,
313+ // /
314+ // / * the outer loop(s)' ops that update the IVs were sank inside the innermost
315+ // / loop (see the `"fir.store"(%new_i_iv, %i#1)` op above),
316+ // /
317+ // / * the innermost loop's entry block's arguments were mapped in order from the
318+ // / outermost to the innermost IV.
319+ // /
320+ // / With this IR change, we can directly inline the innermost loop's region into
321+ // / the newly generated `omp.loop_nest` op.
322+ // /
323+ // / Note that this function has a pre-condition that \p loopNest consists of
324+ // / perfectly nested loops; i.e. there are no in-between ops between 2 nested
325+ // / loops except for the ops to setup the inner loop's LB, UB, and step. These
326+ // / ops are handled/cloned by `genLoopNestClauseOps(..)`.
327+ void sinkLoopIVArgs (mlir::ConversionPatternRewriter &rewriter,
328+ looputils::LoopNestToIndVarMap &loopNest) {
329+ if (loopNest.size () <= 1 )
330+ return ;
331+
332+ fir::DoLoopOp innermostLoop = loopNest.back ().first ;
333+ mlir::Operation &innermostFirstOp = innermostLoop.getRegion ().front ().front ();
334+
335+ llvm::SmallVector<mlir::Type> argTypes;
336+ llvm::SmallVector<mlir::Location> argLocs;
337+
338+ for (auto &[doLoop, indVarInfo] : llvm::drop_end (loopNest)) {
339+ // Sink the IV update ops to the innermost loop. We need to do for all loops
340+ // except for the innermost one, hence the `drop_end` usage above.
341+ for (mlir::Operation *op : indVarInfo.indVarUpdateOps )
342+ op->moveBefore (&innermostFirstOp);
343+
344+ argTypes.push_back (doLoop.getInductionVar ().getType ());
345+ argLocs.push_back (doLoop.getInductionVar ().getLoc ());
346+ }
347+
348+ mlir::Region &innermmostRegion = innermostLoop.getRegion ();
349+ // Extend the innermost entry block with arguments to represent the outer IVs.
350+ innermmostRegion.addArguments (argTypes, argLocs);
351+
352+ unsigned idx = 1 ;
353+ // In reverse, remap the IVs of the loop nest from the old values to the new
354+ // ones. We do that in reverse since the first argument before this loop is
355+ // the old IV for the innermost loop. Therefore, we want to replace it first
356+ // before the old value (1st argument in the block) is remapped to be the IV
357+ // of the outermost loop in the nest.
358+ for (auto &[doLoop, _] : llvm::reverse (loopNest)) {
359+ doLoop.getInductionVar ().replaceAllUsesWith (
360+ innermmostRegion.getArgument (innermmostRegion.getNumArguments () - idx));
361+ ++idx;
362+ }
363+ }
228364} // namespace looputils
229365
230366class DoConcurrentConversion : public mlir ::OpConversionPattern<fir::DoLoopOp> {
@@ -247,6 +383,7 @@ class DoConcurrentConversion : public mlir::OpConversionPattern<fir::DoLoopOp> {
247383 " Some `do concurent` loops are not perfectly-nested. "
248384 " These will be serialized." );
249385
386+ looputils::sinkLoopIVArgs (rewriter, loopNest);
250387 mlir::IRMapping mapper;
251388 genParallelOp (doLoop.getLoc (), rewriter, loopNest, mapper);
252389 mlir::omp::LoopNestOperands loopNestClauseOps;
0 commit comments