Skip to content

Commit 250d251

Browse files
authored
[mlir][vector] Propagate alignment in LowerVectorGather. (#155683)
Alignment is properly propagated when patterns `UnrollGather`, `RemoveStrideFromGatherSource`, or `Gather1DToConditionalLoads` are applied.
1 parent e58de06 commit 250d251

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

mlir/lib/Dialect/Vector/Transforms/LowerVectorGather.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ struct UnrollGather : OpRewritePattern<vector::GatherOp> {
7070
vector::ExtractOp::create(rewriter, loc, passThruVec, thisIdx);
7171
return vector::GatherOp::create(rewriter, loc, subTy, op.getBase(),
7272
op.getOffsets(), indexSubVec, maskSubVec,
73-
passThruSubVec);
73+
passThruSubVec, op.getAlignmentAttr());
7474
};
7575

7676
return unrollVectorOp(op, rewriter, unrollGatherFn);
@@ -152,7 +152,8 @@ struct RemoveStrideFromGatherSource : OpRewritePattern<vector::GatherOp> {
152152
// updated indices.
153153
Value newGather = vector::GatherOp::create(
154154
rewriter, op.getLoc(), op.getResult().getType(), collapsed,
155-
op.getOffsets(), newIdxs, op.getMask(), op.getPassThru());
155+
op.getOffsets(), newIdxs, op.getMask(), op.getPassThru(),
156+
op.getAlignmentAttr());
156157
rewriter.replaceOp(op, newGather);
157158

158159
return success();
@@ -200,6 +201,8 @@ struct Gather1DToConditionalLoads : OpRewritePattern<vector::GatherOp> {
200201
Value lastBaseOffset = baseOffsets.back();
201202

202203
Value result = op.getPassThru();
204+
BoolAttr nontemporalAttr = nullptr;
205+
IntegerAttr alignmentAttr = op.getAlignmentAttr();
203206

204207
// Emit a conditional access for each vector element.
205208
for (int64_t i = 0, e = resultTy.getNumElements(); i < e; ++i) {
@@ -216,7 +219,8 @@ struct Gather1DToConditionalLoads : OpRewritePattern<vector::GatherOp> {
216219
// `vector.load` does not support scalar result; emit a vector load
217220
// and extract the single result instead.
218221
Value load =
219-
vector::LoadOp::create(b, loc, elemVecTy, base, baseOffsets);
222+
vector::LoadOp::create(b, loc, elemVecTy, base, baseOffsets,
223+
nontemporalAttr, alignmentAttr);
220224
int64_t zeroIdx[1] = {0};
221225
extracted = vector::ExtractOp::create(b, loc, load, zeroIdx);
222226
} else {

mlir/test/Dialect/Vector/vector-gather-lowering.mlir

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,18 @@ func.func @scalable_gather_memref_2d(%base: memref<?x?xf32>, %v: vector<2x[3]xin
100100
return %0 : vector<2x[3]xf32>
101101
}
102102

103+
// CHECK-LABEL: @scalable_gather_memref_2d_with_alignment
104+
// CHECK: vector.gather
105+
// CHECK-SAME: {alignment = 8 : i64}
106+
// CHECK: vector.gather
107+
// CHECK-SAME: {alignment = 8 : i64}
108+
func.func @scalable_gather_memref_2d_with_alignment(%base: memref<?x?xf32>, %v: vector<2x[3]xindex>, %mask: vector<2x[3]xi1>, %pass_thru: vector<2x[3]xf32>) -> vector<2x[3]xf32> {
109+
%c0 = arith.constant 0 : index
110+
%c1 = arith.constant 1 : index
111+
%0 = vector.gather %base[%c0, %c1][%v], %mask, %pass_thru {alignment = 8} : memref<?x?xf32>, vector<2x[3]xindex>, vector<2x[3]xi1>, vector<2x[3]xf32> into vector<2x[3]xf32>
112+
return %0 : vector<2x[3]xf32>
113+
}
114+
103115
// CHECK-LABEL: @scalable_gather_cant_unroll
104116
// CHECK-NOT: extract
105117
// CHECK: vector.gather
@@ -234,7 +246,7 @@ func.func @strided_gather(%base : memref<100x3xf32>,
234246
%mask = arith.constant dense<true> : vector<4xi1>
235247
%pass_thru = arith.constant dense<0.000000e+00> : vector<4xf32>
236248
// Gather of a strided MemRef
237-
%res = vector.gather %subview[%c0] [%idxs], %mask, %pass_thru : memref<100xf32, strided<[3]>>, vector<4xindex>, vector<4xi1>, vector<4xf32> into vector<4xf32>
249+
%res = vector.gather %subview[%c0] [%idxs], %mask, %pass_thru {alignment = 8} : memref<100xf32, strided<[3]>>, vector<4xindex>, vector<4xi1>, vector<4xf32> into vector<4xf32>
238250
return %res : vector<4xf32>
239251
}
240252
// CHECK-LABEL: func.func @strided_gather(
@@ -250,22 +262,22 @@ func.func @strided_gather(%base : memref<100x3xf32>,
250262

251263
// CHECK: %[[IDX_0:.*]] = vector.extract %[[NEW_IDXS]][0] : index from vector<4xindex>
252264
// CHECK: scf.if %[[TRUE]] -> (vector<4xf32>)
253-
// CHECK: %[[M_0:.*]] = vector.load %[[COLLAPSED]][%[[IDX_0]]] : memref<300xf32>, vector<1xf32>
265+
// CHECK: %[[M_0:.*]] = vector.load %[[COLLAPSED]][%[[IDX_0]]] {alignment = 8 : i64} : memref<300xf32>, vector<1xf32>
254266
// CHECK: %[[V_0:.*]] = vector.extract %[[M_0]][0] : f32 from vector<1xf32>
255267

256268
// CHECK: %[[IDX_1:.*]] = vector.extract %[[NEW_IDXS]][1] : index from vector<4xindex>
257269
// CHECK: scf.if %[[TRUE]] -> (vector<4xf32>)
258-
// CHECK: %[[M_1:.*]] = vector.load %[[COLLAPSED]][%[[IDX_1]]] : memref<300xf32>, vector<1xf32>
270+
// CHECK: %[[M_1:.*]] = vector.load %[[COLLAPSED]][%[[IDX_1]]] {alignment = 8 : i64} : memref<300xf32>, vector<1xf32>
259271
// CHECK: %[[V_1:.*]] = vector.extract %[[M_1]][0] : f32 from vector<1xf32>
260272

261273
// CHECK: %[[IDX_2:.*]] = vector.extract %[[NEW_IDXS]][2] : index from vector<4xindex>
262274
// CHECK: scf.if %[[TRUE]] -> (vector<4xf32>)
263-
// CHECK: %[[M_2:.*]] = vector.load %[[COLLAPSED]][%[[IDX_2]]] : memref<300xf32>, vector<1xf32>
275+
// CHECK: %[[M_2:.*]] = vector.load %[[COLLAPSED]][%[[IDX_2]]] {alignment = 8 : i64} : memref<300xf32>, vector<1xf32>
264276
// CHECK: %[[V_2:.*]] = vector.extract %[[M_2]][0] : f32 from vector<1xf32>
265277

266278
// CHECK: %[[IDX_3:.*]] = vector.extract %[[NEW_IDXS]][3] : index from vector<4xindex>
267279
// CHECK: scf.if %[[TRUE]] -> (vector<4xf32>)
268-
// CHECK: %[[M_3:.*]] = vector.load %[[COLLAPSED]][%[[IDX_3]]] : memref<300xf32>, vector<1xf32>
280+
// CHECK: %[[M_3:.*]] = vector.load %[[COLLAPSED]][%[[IDX_3]]] {alignment = 8 : i64} : memref<300xf32>, vector<1xf32>
269281
// CHECK: %[[V_3:.*]] = vector.extract %[[M_3]][0] : f32 from vector<1xf32>
270282

271283
// CHECK-LABEL: @scalable_gather_1d

0 commit comments

Comments
 (0)