@@ -142,6 +142,7 @@ bool mlir::linalg::isWindowIteratorType(Attribute attr) {
142142template struct mlir ::linalg::GenerateLoopNest<scf::ForOp>;
143143template struct mlir ::linalg::GenerateLoopNest<scf::ParallelOp>;
144144template struct mlir ::linalg::GenerateLoopNest<AffineForOp>;
145+ template struct mlir ::linalg::GenerateLoopNest<TiledLoopOp>;
145146
146147// / Given a list of subview ranges, extract individual values for lower, upper
147148// / bounds and steps and put them into the corresponding vectors.
@@ -186,10 +187,11 @@ IntegerAttr getSmallestBoundingIndex(Value size) {
186187// / Specialization to build an scf "for" nest.
187188template <>
188189void GenerateLoopNest<scf::ForOp>::doit(
189- ArrayRef<Range> loopRanges, ValueRange iterArgInitValues ,
190+ ArrayRef<Range> loopRanges, LinalgOp linalgOp ,
190191 ArrayRef<Attribute> iteratorTypes,
191192 function_ref<scf::ValueVector(ValueRange, ValueRange)> bodyBuilderFn,
192193 Optional<LinalgLoopDistributionOptions> distributionOptions) {
194+ auto iterArgInitValues = linalgOp.getOutputTensors ();
193195 // Create procInfo so it dominates loops, if appropriate.
194196 OpBuilder &builder = edsc::ScopedContext::getBuilderRef ();
195197 Location loc = edsc::ScopedContext::getLocation ();
@@ -216,10 +218,11 @@ void GenerateLoopNest<scf::ForOp>::doit(
216218// / Specialization to build affine "for" nest.
217219template <>
218220void GenerateLoopNest<AffineForOp>::doit(
219- ArrayRef<Range> loopRanges, ValueRange iterArgInitValues ,
221+ ArrayRef<Range> loopRanges, LinalgOp linalgOp ,
220222 ArrayRef<Attribute> iteratorTypes,
221223 function_ref<scf::ValueVector(ValueRange, ValueRange)> bodyBuilderFn,
222224 Optional<LinalgLoopDistributionOptions>) {
225+ auto iterArgInitValues = linalgOp.getOutputTensors ();
223226 assert (iterArgInitValues.empty () && " unexpected AffineForOp init values" );
224227 SmallVector<Value, 4 > lbs, ubs, steps;
225228 unpackRanges (loopRanges, lbs, ubs, steps);
@@ -240,6 +243,44 @@ void GenerateLoopNest<AffineForOp>::doit(
240243 bodyBuilderWithoutIterArgsFn);
241244}
242245
246+ // / Specialization to build an linalg.tiled_loop
247+ template <>
248+ void GenerateLoopNest<TiledLoopOp>::doit(
249+ ArrayRef<Range> loopRanges, LinalgOp linalgOp,
250+ ArrayRef<Attribute> iteratorTypes,
251+ function_ref<scf::ValueVector(ValueRange, ValueRange)> bodyBuilderFn,
252+ Optional<LinalgLoopDistributionOptions>) {
253+ OpBuilder &builder = edsc::ScopedContext::getBuilderRef ();
254+ Location loc = edsc::ScopedContext::getLocation ();
255+ SmallVector<ProcInfo, 2 > procInfo;
256+
257+ SmallVector<Value, 4 > lbs, ubs, steps;
258+ unpackRanges (loopRanges, lbs, ubs, steps);
259+
260+ auto wrappedBuilderFn = [&](OpBuilder &nestedBuilder, Location nestedLoc,
261+ ValueRange ivs, ValueRange inputs,
262+ ValueRange outputs) {
263+ ScopedContext context (nestedBuilder, nestedLoc);
264+ scf::ValueVector results = bodyBuilderFn (ivs, linalgOp.getOutputTensors ());
265+ nestedBuilder.create <linalg::YieldOp>(nestedLoc, results);
266+ };
267+
268+ auto tiledLoop = builder.create <TiledLoopOp>(
269+ loc, lbs, ubs, steps, linalgOp.getInputs (), linalgOp.getOutputs (),
270+ builder.getArrayAttr (iteratorTypes), wrappedBuilderFn);
271+
272+ // Replace inputs/outputs with the corresponding region args.
273+ auto isInsideTiledLoop = [&](OpOperand &operand) {
274+ return operand.getOwner ()->getBlock () == tiledLoop.getBody ();
275+ };
276+ for (auto it :
277+ llvm::zip (linalgOp.getInputs (), tiledLoop.getRegionInputArgs ()))
278+ std::get<0 >(it).replaceUsesWithIf (std::get<1 >(it), isInsideTiledLoop);
279+ for (auto it :
280+ llvm::zip (linalgOp.getOutputs (), tiledLoop.getRegionOutputArgs ()))
281+ std::get<0 >(it).replaceUsesWithIf (std::get<1 >(it), isInsideTiledLoop);
282+ }
283+
243284// / Update the `lb`, `ub` and `step` to get per processor `lb`, `ub` and `step`.
244285void updateBoundsForCyclicDistribution (OpBuilder &builder, Location loc,
245286 Value procId, Value nprocs, Value &lb,
@@ -373,10 +414,11 @@ generateParallelLoopNest(ValueRange lbs, ValueRange ubs, ValueRange steps,
373414// / Specialization for generating a mix of parallel and sequential scf loops.
374415template <>
375416void GenerateLoopNest<scf::ParallelOp>::doit(
376- ArrayRef<Range> loopRanges, ValueRange iterArgInitValues ,
417+ ArrayRef<Range> loopRanges, LinalgOp linalgOp ,
377418 ArrayRef<Attribute> iteratorTypes,
378419 function_ref<scf::ValueVector(ValueRange, ValueRange)> bodyBuilderFn,
379420 Optional<LinalgLoopDistributionOptions> distributionOptions) {
421+ auto iterArgInitValues = linalgOp.getOutputTensors ();
380422 assert (iterArgInitValues.empty () && " unexpected ParallelOp init values" );
381423 // This function may be passed more iterator types than ranges.
382424 assert (iteratorTypes.size () >= loopRanges.size () &&
0 commit comments