Skip to content

Commit

Permalink
[BACKEND] Fold transpose(splat_const) (#5259)
Browse files Browse the repository at this point in the history
Add folding for a transpose of a splat constant.

---------

Co-authored-by: peterbell10 <[email protected]>
  • Loading branch information
ThomasRaoux and peterbell10 authored Nov 26, 2024
1 parent 22e212b commit deee78f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/Dialect/Triton/IR/Ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ OpFoldResult TransOp::fold(FoldAdaptor adaptor) {
return getResult();
}

// Eliminate splat constant transpose ops.
if (auto attr =
llvm::dyn_cast_if_present<SplatElementsAttr>(adaptor.getSrc()))
return attr.reshape(getType());

return {};
}

Expand Down
11 changes: 11 additions & 0 deletions test/Triton/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,14 @@ tt.func @fold_broadcast_constant_pattern(%cst : f32) -> tensor<8x2xf32> {
// CHECK-NEXT: tt.return %[[cst]] : tensor<8x2xf32>
tt.return %bst_out : tensor<8x2xf32>
}

// -----

// CHECK-LABEL: @fold_transpose_constant
tt.func @fold_transpose_constant() -> tensor<128x16xf32> {
// CHECK: %[[cst:.*]] = arith.constant dense<1.000000e+00> : tensor<128x16xf32>
%cst = arith.constant dense<1.0> : tensor<16x128xf32>
%r = tt.trans %cst {order = array<i32: 1, 0>} : tensor<16x128xf32> -> tensor<128x16xf32>
// CHECK-NEXT: tt.return %[[cst]] : tensor<128x16xf32>
tt.return %r : tensor<128x16xf32>
}

0 comments on commit deee78f

Please sign in to comment.