Skip to content

Commit c652617

Browse files
committed
[mlir][Vector] Add a rewrite pattern for gather over a strided memref
This patch adds a rewrite pattern for `vector.gather` over a strided memref like the following: ```mlir %subview = memref.subview %arg0[0, 0] [100, 1] [1, 1] : memref<100x3xf32> to memref<100xf32, strided<[3]>> %gather = vector.gather %subview[%c0] [%idxs], %cst_0, %cst : memref<100xf32, strided<[3]>>, vector<4xindex>, vector<4xi1>, vector<4xf32> into vector<4xf32> ``` ```mlir %collapse_shape = memref.collapse_shape %arg0 [[0, 1]] : memref<100x3xf32> into memref<300xf32> %1 = arith.muli %arg3, %cst : vector<4xindex> %gather = vector.gather %collapse_shape[%c0] [%1], %cst_1, %cst_0 : memref<300xf32>, vector<4xindex>, vector<4xi1>, vector<4xf32> into vector<4xf32> ``` Fixes iree-org/iree#15364.
1 parent 03f05a4 commit c652617

File tree

2 files changed

+132
-2
lines changed

2 files changed

+132
-2
lines changed

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

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,82 @@ struct FlattenGather : OpRewritePattern<vector::GatherOp> {
9696
}
9797
};
9898

99+
/// Rewrites a vector.gather of a strided MemRef as a gather of a non-strided
100+
/// MemRef with updated indices that model the strided access.
101+
///
102+
/// ```mlir
103+
/// %subview = memref.subview %M (...) to memref<100xf32, strided<[3]>>
104+
/// %gather = vector.gather %subview (...) : memref<100xf32, strided<[3]>>
105+
/// ```
106+
/// ==>
107+
/// ```mlir
108+
/// %collapse_shape = memref.collapse_shape %M (...) into memref<300xf32>
109+
/// %1 = arith.muli %idxs, %c3 : vector<4xindex>
110+
/// %gather = vector.gather %collapse_shape (...) : memref<300xf32> (...)
111+
/// ```
112+
///
113+
/// ATM this is effectively limited to reading a 1D Vector from a 2D MemRef,
114+
/// but should be fairly straightforward to extend beyond that.
115+
struct RemoveStrideFromGatherSource : OpRewritePattern<vector::GatherOp> {
116+
using OpRewritePattern::OpRewritePattern;
117+
118+
LogicalResult matchAndRewrite(vector::GatherOp op,
119+
PatternRewriter &rewriter) const override {
120+
Value base = op.getBase();
121+
if (!base.getDefiningOp())
122+
return failure();
123+
124+
// TODO: Strided accesses might be coming from other ops as well
125+
auto subview = dyn_cast<memref::SubViewOp>(base.getDefiningOp());
126+
if (!subview)
127+
return failure();
128+
129+
// TODO: Allows ranks > 2.
130+
if (subview.getSource().getType().getRank() != 2)
131+
return failure();
132+
133+
// Get strides
134+
auto layout = subview.getResult().getType().getLayout();
135+
auto stridedLayoutAttr = llvm::dyn_cast<StridedLayoutAttr>(layout);
136+
137+
// TODO: Allow the access to be strided in multiple dimensions.
138+
if (stridedLayoutAttr.getStrides().size() != 1)
139+
return failure();
140+
141+
int64_t srcTrailingDim = subview.getSource().getType().getShape().back();
142+
143+
// Assume that the stride matches the trailing dimension of the source
144+
// memref.
145+
// TODO: Relax this assumption.
146+
if (stridedLayoutAttr.getStrides()[0] != srcTrailingDim)
147+
return failure();
148+
149+
// 1. Collapse the input memref so that it's "flat".
150+
SmallVector<ReassociationIndices> reassoc = {{0, 1}};
151+
Value collapsed = rewriter.create<memref::CollapseShapeOp>(
152+
op.getLoc(), subview.getSource(), reassoc);
153+
154+
// 2. Generate new gather indices that will model the
155+
// strided access.
156+
auto stride = rewriter.getIndexAttr(srcTrailingDim);
157+
auto vType = op.getIndexVec().getType();
158+
Value mulCst = rewriter.create<arith::ConstantOp>(
159+
op.getLoc(), vType, DenseElementsAttr::get(vType, stride));
160+
161+
Value newIdxs =
162+
rewriter.create<arith::MulIOp>(op.getLoc(), op.getIndexVec(), mulCst);
163+
164+
// 3. Create an updated gather op with the collapsed input memref and the
165+
// updated indices.
166+
Value newGather = rewriter.create<vector::GatherOp>(
167+
op.getLoc(), op.getResult().getType(), collapsed, op.getIndices(),
168+
newIdxs, op.getMask(), op.getPassThru());
169+
rewriter.replaceOp(op, newGather);
170+
171+
return success();
172+
}
173+
};
174+
99175
/// Turns 1-d `vector.gather` into a scalarized sequence of `vector.loads` or
100176
/// `tensor.extract`s. To avoid out-of-bounds memory accesses, these
101177
/// loads/extracts are made conditional using `scf.if` ops.
@@ -168,6 +244,6 @@ struct Gather1DToConditionalLoads : OpRewritePattern<vector::GatherOp> {
168244

169245
void mlir::vector::populateVectorGatherLoweringPatterns(
170246
RewritePatternSet &patterns, PatternBenefit benefit) {
171-
patterns.add<FlattenGather, Gather1DToConditionalLoads>(patterns.getContext(),
172-
benefit);
247+
patterns.add<FlattenGather, RemoveStrideFromGatherSource,
248+
Gather1DToConditionalLoads>(patterns.getContext(), benefit);
173249
}

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,57 @@ func.func @gather_tensor_1d_none_set(%base: tensor<?xf32>, %v: vector<2xindex>,
151151
%0 = vector.gather %base[%c0][%v], %mask, %pass_thru : tensor<?xf32>, vector<2xindex>, vector<2xi1>, vector<2xf32> into vector<2xf32>
152152
return %0 : vector<2xf32>
153153
}
154+
155+
// Check that vector.gather of a strided memref is replaced with a
156+
// vector.gather with indices encoding the original strides. Note that with the
157+
// other patterns
158+
#map = affine_map<()[s0] -> (s0 * 4096)>
159+
#map1 = affine_map<()[s0] -> (s0 * -4096 + 518400, 4096)>
160+
func.func @strided_gather(%M_in : memref<100x3xf32>, %M_out: memref<518400xf32>, %idxs : vector<4xindex>, %x : index, %y : index) {
161+
%c0 = arith.constant 0 : index
162+
%x_1 = affine.apply #map()[%x]
163+
// Strided MemRef
164+
%subview = memref.subview %M_in[0, 0] [100, 1] [1, 1] : memref<100x3xf32> to memref<100xf32, strided<[3]>>
165+
%cst_0 = arith.constant dense<true> : vector<4xi1>
166+
%cst = arith.constant dense<0.000000e+00> : vector<4xf32>
167+
// Gather of a strided MemRef
168+
%7 = vector.gather %subview[%c0] [%idxs], %cst_0, %cst : memref<100xf32, strided<[3]>>, vector<4xindex>, vector<4xi1>, vector<4xf32> into vector<4xf32>
169+
%subview_1 = memref.subview %M_out[%x_1] [%y] [1] : memref<518400xf32> to memref<?xf32, strided<[1], offset: ?>>
170+
vector.store %7, %subview_1[%c0] : memref<?xf32, strided<[1], offset: ?>>, vector<4xf32>
171+
return
172+
}
173+
// CHECK-LABEL: func.func @strided_gather(
174+
// CHECK-SAME: %[[M_in:.*]]: memref<100x3xf32>,
175+
// CHECK-SAME: %[[M_out:.*]]: memref<518400xf32>,
176+
// CHECK-SAME: %[[IDXS:.*]]: vector<4xindex>,
177+
// CHECK-SAME: %[[VAL_4:.*]]: index,
178+
// CHECK-SAME: %[[VAL_5:.*]]: index) {
179+
// CHECK: %[[CST_3:.*]] = arith.constant dense<3> : vector<4xindex>
180+
// CHECK: %[[MASK:.*]] = arith.constant dense<true> : vector<4xi1>
181+
182+
// CHECK: %[[COLLAPSED:.*]] = memref.collapse_shape %[[M_in]] {{\[\[}}0, 1]] : memref<100x3xf32> into memref<300xf32>
183+
// CHECK: %[[NEW_IDXS:.*]] = arith.muli %[[IDXS]], %[[CST_3]] : vector<4xindex>
184+
185+
// CHECK: %[[MASK_0:.*]] = vector.extract %[[MASK]][0] : i1 from vector<4xi1>
186+
// CHECK: %[[IDX_0:.*]] = vector.extract %[[NEW_IDXS]][0] : index from vector<4xindex>
187+
// CHECK: scf.if %[[MASK_0]] -> (vector<4xf32>)
188+
// CHECK: %[[M_0:.*]] = vector.load %[[COLLAPSED]]{{\[}}%[[IDX_0]]] : memref<300xf32>, vector<1xf32>
189+
// CHECK: %[[V_0:.*]] = vector.extract %[[M_0]][0] : f32 from vector<1xf32>
190+
191+
// CHECK: %[[MASK_1:.*]] = vector.extract %[[MASK]][1] : i1 from vector<4xi1>
192+
// CHECK: %[[IDX_1:.*]] = vector.extract %[[NEW_IDXS]][1] : index from vector<4xindex>
193+
// CHECK: scf.if %[[MASK_1]] -> (vector<4xf32>)
194+
// CHECK: %[[M_1:.*]] = vector.load %[[COLLAPSED]]{{\[}}%[[IDX_1]]] : memref<300xf32>, vector<1xf32>
195+
// CHECK: %[[V_1:.*]] = vector.extract %[[M_1]][0] : f32 from vector<1xf32>
196+
197+
// CHECK: %[[MASK_2:.*]] = vector.extract %[[MASK]][2] : i1 from vector<4xi1>
198+
// CHECK: %[[IDX_2:.*]] = vector.extract %[[NEW_IDXS]][2] : index from vector<4xindex>
199+
// CHECK: scf.if %[[MASK_2]] -> (vector<4xf32>)
200+
// CHECK: %[[M_2:.*]] = vector.load %[[COLLAPSED]][%[[IDX_2]]] : memref<300xf32>, vector<1xf32>
201+
// CHECK: %[[V_2:.*]] = vector.extract %[[M_2]][0] : f32 from vector<1xf32>
202+
203+
// CHECK: %[[MASK_3:.*]] = vector.extract %[[MASK]][3] : i1 from vector<4xi1>
204+
// CHECK: %[[IDX_3:.*]] = vector.extract %[[NEW_IDXS]][3] : index from vector<4xindex>
205+
// CHECK: scf.if %[[MASK_3]] -> (vector<4xf32>)
206+
// CHECK: %[[M_3:.*]] = vector.load %[[COLLAPSED]]{{\[}}%[[IDX_3]]] : memref<300xf32>, vector<1xf32>
207+
// CHECK: %[[V_3:.*]] = vector.extract %[[M_3]][0] : f32 from vector<1xf32>

0 commit comments

Comments
 (0)