Skip to content

[mlir][scf]Fix scf.forall inlining: add shared outputs #132197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions mlir/lib/Dialect/SCF/Transforms/ForallToFor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@ mlir::scf::forallToForLoop(RewriterBase &rewriter, scf::ForallOp forallOp,
SmallVector<Value> ivs = llvm::map_to_vector(
loopNest.loops, [](scf::ForOp loop) { return loop.getInductionVar(); });

SmallVector<Value> replacementVals = ivs;
for (Value shared : forallOp.getOutputs())
replacementVals.push_back(shared);
Block *innermostBlock = loopNest.loops.back().getBody();
rewriter.eraseOp(forallOp.getBody()->getTerminator());
rewriter.inlineBlockBefore(forallOp.getBody(), innermostBlock,
innermostBlock->getTerminator()->getIterator(),
ivs);
rewriter.eraseOp(forallOp);
replacementVals);
rewriter.replaceOp(forallOp, forallOp.getOutputs());

if (results) {
llvm::move(loopNest.loops, std::back_inserter(*results));
Expand Down
23 changes: 23 additions & 0 deletions mlir/test/Dialect/SCF/forall-to-for.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,26 @@ func.func @nested(%ub1: index, %ub2: index, %ub3: index, %ub4: index) {
}
return
}

// -----

func.func @parallel_insert_slice(%arg0: tensor<100xf32>) -> tensor<100xf32> {
%c100 = arith.constant 100 : index
%res = scf.forall (%i) in (%c100) shared_outs(%s = %arg0) -> (tensor<100xf32>) {
%t = "test.foo"() : () -> tensor<100xf32>
scf.forall.in_parallel {
tensor.parallel_insert_slice %t into %s[%i] [100] [1] : tensor<100xf32> into tensor<100xf32>
}
}
return %res : tensor<100xf32>
}
// CHECK-LABEL: func.func @parallel_insert_slice(
// CHECK-SAME: %[[VAL_0:[0-9]+|[a-zA-Z$._-][a-zA-Z0-9$._-]*]]: tensor<100xf32>) -> tensor<100xf32> {
// CHECK: %[[VAL_1:.*]] = arith.constant 100 : index
// CHECK: %[[VAL_2:.*]] = arith.constant 0 : index
// CHECK: %[[VAL_3:.*]] = arith.constant 1 : index
// CHECK: scf.for %[[VAL_4:.*]] = %[[VAL_2]] to %[[VAL_1]] step %[[VAL_3]] {
// CHECK: %[[VAL_5:.*]] = "test.foo"() : () -> tensor<100xf32>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did the parallel_insert_slice go? I think this pass is incorrect. It should have replaced the parallel_insert_slice with insert_slice.

// CHECK: }
// CHECK: return %[[VAL_0]] : tensor<100xf32>
Copy link
Member

@matthias-springer matthias-springer Mar 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The result of the scf.for should have been used here. It looks like the generated loop nest does not even have a result/iter_args. The issue that you are fixing here was probably an undocumented limitation of this pass, not necessarily a bug: shared_outs are generally not supported, which made the implementation a bit easier.

But it would be nice to support shared_outs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, instead of dropping the terminator of the scf.forall loop, you have to replace it with tensor.insert_slice and yield the result. Also, the loop nest that this pass is generating must have an iter_arg (and result); one per shared_out.

// CHECK: }