Skip to content

Commit 11c4174

Browse files
authored
[Fix] crd2idx uses wrong dynamic basis leaf (#1052)
1 parent 73db4b5 commit 11c4174

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

lib/Dialect/Fly/Utils/IntTupleUtils.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -782,8 +782,27 @@ IntTupleValueAdaptor intTupleBasis2Tuple(const IntTupleBuilder<IntTupleValueAdap
782782
IntTupleValueAdaptor basis) {
783783
assert(basis.isLeafBasis());
784784
IntTupleAttr attr = builder.getAttr(basis);
785-
IntTupleAttr newAttr = intTupleBasis2Tuple(builder.getAttrBuilder(), attr);
786-
return IntTupleValueAdaptor{basis.getValue(), newAttr};
785+
BasisAttr basisAttr = attr.getLeafAsBasis();
786+
ArrayRef<int32_t> modes = basisAttr.getModes();
787+
assert(!modes.empty() && "modes must not be empty");
788+
789+
// The scalar value belongs to the leaf that sits under `modes`; wrapping it in the
790+
// enclosing tuples keeps the adaptor invariant, so later `at()` reads the leaf back
791+
// from a MakeIntTupleOp operand instead of re-interpreting the scalar's defining op.
792+
IntAttr zero = IntAttr::getStatic(attr.getContext(), 0);
793+
IntTupleValueAdaptor result =
794+
basisAttr.getValue().isStatic()
795+
? builder.materializeConstantLeaf(basisAttr.getValue())
796+
: IntTupleValueAdaptor{basis.getValue(), IntTupleAttr::get(basisAttr.getValue())};
797+
for (auto it = modes.rbegin(); it != modes.rend(); ++it) {
798+
IntTupleBuilder<IntTupleValueAdaptor>::ElemCollector elements;
799+
for (int32_t i = 0; i < *it; ++i) {
800+
elements.push_back(builder.materializeConstantLeaf(zero));
801+
}
802+
elements.push_back(result);
803+
result = builder.makeTuple(elements);
804+
}
805+
return result;
787806
}
788807

789808
static IntTupleAttr intTupleMakeBasisTupleLikeImpl(MLIRContext *ctx, IntTupleAttr profile,

tests/mlir/Transforms/layout_lowering.mlir

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,25 @@ func.func @test_crd2idx_dynamic(%c0: i32, %c1: i32) -> i32 {
262262
return %scalar : i32
263263
}
264264

265+
// Dynamic crd2idx onto scaled basis strides: the 48 scale of 48E1 must survive lowering,
266+
// i.e. mode 1 of the result is c0*1 + c1*48, not c0 + c1.
267+
// CHECK-LABEL: @test_crd2idx_dynamic_scaled_basis
268+
// CHECK-SAME: (%[[C0:.*]]: i32, %[[C1:.*]]: i32)
269+
func.func @test_crd2idx_dynamic_scaled_basis(%c0: i32, %c1: i32) -> i32 {
270+
%s = fly.make_int_tuple() : () -> !fly.int_tuple<(48, 21)>
271+
%d = fly.make_int_tuple() : () -> !fly.int_tuple<(1E1, 48E1)>
272+
%layout = fly.make_layout(%s, %d) : (!fly.int_tuple<(48, 21)>, !fly.int_tuple<(1E1, 48E1)>) -> !fly.layout<(48, 21) : (1E1, 48E1)>
273+
%coord = fly.make_int_tuple(%c0, %c1) : (i32, i32) -> !fly.int_tuple<(?, ?)>
274+
%idx = fly.crd2idx(%coord, %layout) : (!fly.int_tuple<(?, ?)>, !fly.layout<(48, 21) : (1E1, 48E1)>) -> !fly.int_tuple<(0, ?)>
275+
// CHECK: %[[C48:.*]] = arith.constant 48 : i32
276+
// CHECK: %[[MUL:.*]] = arith.muli %[[C1]], %[[C48]] : i32
277+
// CHECK: %[[ADD:.*]] = arith.addi %[[C0]], %[[MUL]] : i32
278+
// CHECK: return %[[ADD]]
279+
%elem = fly.select(%idx) {indices = array<i32: 1>} : (!fly.int_tuple<(0, ?)>) -> !fly.int_tuple<?>
280+
%scalar = fly.get_scalar(%elem) : (!fly.int_tuple<?>) -> i32
281+
return %scalar : i32
282+
}
283+
265284
// -----
266285

267286
// === IntTuple Binary Ops Lowering ===

0 commit comments

Comments
 (0)