Skip to content

Commit

Permalink
[BACKEND] Fix condition to do a naive reshape with allow_reorder (#6012)
Browse files Browse the repository at this point in the history
Even though allow_reorder is set, a reshape is not always a no-op as we
may swap a replicated value for a non-replicated one.
  • Loading branch information
ThomasRaoux authored Feb 25, 2025
1 parent 0a8e3cc commit 013453f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/Dialect/TritonGPU/IR/Dialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,20 @@ SmallVector<unsigned> getShapePerCTATile(Attribute layout) {
}

bool isExpensiveView(Type srcType, Type dstType) {
auto tensorSrcType = cast<RankedTensorType>(srcType);
auto tensorDstType = cast<RankedTensorType>(dstType);
auto llSrc =
toLinearLayout(tensorSrcType.getShape(), tensorSrcType.getEncoding());
auto llDst =
toLinearLayout(tensorDstType.getShape(), tensorDstType.getEncoding());
// In case there are replicated value we need to make sure the new and old
// layout have matching masks.
for (auto [srcMask, dstMask] :
llvm::zip(llSrc.getFreeVariableMasks(), llDst.getFreeVariableMasks())) {
assert(srcMask.first == dstMask.first);
if (srcMask.second != dstMask.second)
return true;
}
return getTotalElemsPerThread(srcType) != getTotalElemsPerThread(dstType);
}

Expand Down
19 changes: 19 additions & 0 deletions test/TritonGPU/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ tt.func @test_canonicalize_convert_expensive_view(%arg0: tensor<256x16xf32, #blo

// -----

// test that the convert doesn't get combined with view if the resulting operations
// is an expensive view which would require moving data across threads.
// CHECK-LABEL: @test_canonicalize_convert_expensive_view
// CHECK-SAME: (%[[ARG:.+]]: tensor<2xf32
// CHECK: %[[C:.+]] = ttg.convert_layout %[[ARG]]
// CHECK: %[[V:.+]] = tt.reshape %[[C]] allow_reorder
// CHECK: tt.return %[[V]]
#blocked = #ttg.blocked<{sizePerThread = [1, 1], threadsPerWarp = [8, 4], warpsPerCTA = [4, 1], order = [1, 0]}>
#blocked1 = #ttg.blocked<{sizePerThread = [1], threadsPerWarp = [32], warpsPerCTA = [4], order = [0]}>
module attributes {"ttg.num-ctas" = 1 : i32, "ttg.num-warps" = 4 : i32, ttg.target = "cuda:80"} {
tt.func @test_canonicalize_convert_expensive_view2(%arg0: tensor<2xf32, #ttg.slice<{dim = 1, parent = #blocked}>>) -> tensor<2xf32, #blocked1> {
%c = ttg.convert_layout %arg0 : tensor<2xf32, #ttg.slice<{dim = 1, parent = #blocked}>> -> tensor<2xf32, #blocked1>
%r = tt.reshape %c allow_reorder : tensor<2xf32, #blocked1> -> tensor<2xf32, #blocked1>
tt.return %r : tensor<2xf32, #blocked1>
}
}

// -----

// test that the convert does get combined with the view even if the resulting operation
// is an efficient view.
// CHECK-LABEL: @test_canonicalize_convert_view
Expand Down

0 comments on commit 013453f

Please sign in to comment.