Skip to content

Commit

Permalink
[BACKEND] Fold transpose(splat_const)
Browse files Browse the repository at this point in the history
Add folding for a transpose of a splat constant.
  • Loading branch information
ThomasRaoux committed Nov 26, 2024
1 parent 85256a6 commit 5afcecd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/Dialect/Triton/IR/Ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ OpFoldResult TransOp::fold(FoldAdaptor adaptor) {
return getResult();
}

// Eliminate splat constant transpose ops.
if (auto attr =
llvm::dyn_cast_if_present<DenseElementsAttr>(adaptor.getSrc()))
if (attr.isSplat())
return attr.reshape(getResult().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 5afcecd

Please sign in to comment.