Skip to content

Commit 574752d

Browse files
committed
[MLIR] Fix VectorEmulateNarrowType constant op mask bug
This commit adds support for handling mask constants generated by the `arith.constant` op in the `VectorEmulateNarrowType` pattern. Previously, this pattern would not match due to the lack of mask constant handling in `getCompressedMaskOp`. The changes include: 1. Updating `getCompressedMaskOp` to recognize and handle `arith.constant` ops as mask value sources. 2. Handling cases where the mask is not aligned with the emulated load width. The compressed mask is adjusted to account for the offset. Limitations: - The arith.constant op can only have 1-dimensional constant values. Resolves: #115742 Signed-off-by: Alan Li <[email protected]>
1 parent 618f231 commit 574752d

File tree

3 files changed

+196
-60
lines changed

3 files changed

+196
-60
lines changed

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

Lines changed: 108 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -75,83 +75,133 @@ static FailureOr<Operation *> getCompressedMaskOp(OpBuilder &rewriter,
7575
int numSrcElemsPerDest,
7676
int numFrontPadElems = 0) {
7777

78-
assert(numFrontPadElems < numSrcElemsPerDest && "intraDataOffset must be less than scale");
78+
assert(numFrontPadElems < numSrcElemsPerDest &&
79+
"numFrontPadElems must be less than numSrcElemsPerDest");
7980

80-
auto numElements = (numFrontPadElems + numSrcElems + numSrcElemsPerDest - 1) /
81-
numSrcElemsPerDest;
81+
auto numDestElems =
82+
(numFrontPadElems + numSrcElems + numSrcElemsPerDest - 1) /
83+
numSrcElemsPerDest;
8284

8385
Operation *maskOp = mask.getDefiningOp();
8486
SmallVector<vector::ExtractOp, 2> extractOps;
87+
// TODO: add support to `vector.splat`.
8588
// Finding the mask creation operation.
86-
while (maskOp && !isa<vector::CreateMaskOp, vector::ConstantMaskOp>(maskOp)) {
89+
while (maskOp &&
90+
!isa<arith::ConstantOp, vector::CreateMaskOp, vector::ConstantMaskOp>(
91+
maskOp)) {
8792
if (auto extractOp = dyn_cast<vector::ExtractOp>(maskOp)) {
8893
maskOp = extractOp.getVector().getDefiningOp();
8994
extractOps.push_back(extractOp);
9095
}
9196
}
92-
auto createMaskOp = dyn_cast_or_null<vector::CreateMaskOp>(maskOp);
93-
auto constantMaskOp = dyn_cast_or_null<vector::ConstantMaskOp>(maskOp);
94-
if (!createMaskOp && !constantMaskOp)
97+
98+
if (!isa<arith::ConstantOp, vector::CreateMaskOp, vector::ConstantMaskOp>(
99+
maskOp))
95100
return failure();
96101

97102
// Computing the "compressed" mask. All the emulation logic (i.e. computing
98103
// new mask index) only happens on the last dimension of the vectors.
99-
Operation *newMask = nullptr;
100-
SmallVector<int64_t> shape(
104+
SmallVector<int64_t> maskShape(
101105
cast<VectorType>(maskOp->getResultTypes()[0]).getShape());
102-
shape.back() = numElements;
103-
auto newMaskType = VectorType::get(shape, rewriter.getI1Type());
104-
if (createMaskOp) {
105-
OperandRange maskOperands = createMaskOp.getOperands();
106-
size_t numMaskOperands = maskOperands.size();
107-
AffineExpr s0;
108-
bindSymbols(rewriter.getContext(), s0);
109-
s0 = s0 + numSrcElemsPerDest - 1;
110-
s0 = s0.floorDiv(numSrcElemsPerDest);
111-
OpFoldResult origIndex =
112-
getAsOpFoldResult(maskOperands[numMaskOperands - 1]);
113-
OpFoldResult maskIndex =
114-
affine::makeComposedFoldedAffineApply(rewriter, loc, s0, origIndex);
115-
SmallVector<Value> newMaskOperands(maskOperands.drop_back());
116-
newMaskOperands.push_back(
117-
getValueOrCreateConstantIndexOp(rewriter, loc, maskIndex));
118-
newMask = rewriter.create<vector::CreateMaskOp>(loc, newMaskType,
119-
newMaskOperands);
120-
} else if (constantMaskOp) {
121-
ArrayRef<int64_t> maskDimSizes = constantMaskOp.getMaskDimSizes();
122-
size_t numMaskOperands = maskDimSizes.size();
123-
int64_t origIndex = maskDimSizes[numMaskOperands - 1];
124-
int64_t startIndex = numFrontPadElems / numSrcElemsPerDest;
125-
int64_t maskIndex =
126-
llvm::divideCeil(numFrontPadElems + origIndex, numSrcElemsPerDest);
127-
128-
// TODO: we only want the mask between [startIndex, maskIndex] to be true,
129-
// the rest are false.
130-
if (numFrontPadElems != 0 && maskDimSizes.size() > 1)
131-
return failure();
132-
133-
SmallVector<int64_t> newMaskDimSizes(maskDimSizes.drop_back());
134-
newMaskDimSizes.push_back(maskIndex);
135-
136-
if (numFrontPadElems == 0) {
137-
newMask = rewriter.create<vector::ConstantMaskOp>(loc, newMaskType,
138-
newMaskDimSizes);
139-
} else {
140-
SmallVector<bool> newMaskValues;
141-
for (int64_t i = 0; i < numElements; ++i)
142-
newMaskValues.push_back(i >= startIndex && i < maskIndex);
143-
auto denseAttr = DenseElementsAttr::get(newMaskType, newMaskValues);
144-
newMask = rewriter.create<arith::ConstantOp>(loc, newMaskType, denseAttr);
145-
}
146-
}
106+
maskShape.back() = numDestElems;
107+
auto newMaskType = VectorType::get(maskShape, rewriter.getI1Type());
108+
std::optional<Operation *> newMask =
109+
TypeSwitch<Operation *, std::optional<Operation *>>(maskOp)
110+
.Case<vector::CreateMaskOp>(
111+
[&](auto createMaskOp) -> std::optional<Operation *> {
112+
OperandRange maskOperands = createMaskOp.getOperands();
113+
size_t numMaskOperands = maskOperands.size();
114+
AffineExpr s0;
115+
bindSymbols(rewriter.getContext(), s0);
116+
s0 = s0 + numSrcElemsPerDest - 1;
117+
s0 = s0.floorDiv(numSrcElemsPerDest);
118+
OpFoldResult origIndex =
119+
getAsOpFoldResult(maskOperands[numMaskOperands - 1]);
120+
OpFoldResult maskIndex = affine::makeComposedFoldedAffineApply(
121+
rewriter, loc, s0, origIndex);
122+
SmallVector<Value> newMaskOperands(maskOperands.drop_back());
123+
newMaskOperands.push_back(
124+
getValueOrCreateConstantIndexOp(rewriter, loc, maskIndex));
125+
return rewriter.create<vector::CreateMaskOp>(loc, newMaskType,
126+
newMaskOperands);
127+
})
128+
.Case<vector::ConstantMaskOp>([&](auto constantMaskOp)
129+
-> std::optional<Operation *> {
130+
ArrayRef<int64_t> maskDimSizes = constantMaskOp.getMaskDimSizes();
131+
size_t numMaskOperands = maskDimSizes.size();
132+
int64_t origIndex = maskDimSizes[numMaskOperands - 1];
133+
int64_t startIndex = numFrontPadElems / numSrcElemsPerDest;
134+
int64_t maskIndex = llvm::divideCeil(numFrontPadElems + origIndex,
135+
numSrcElemsPerDest);
136+
137+
// TODO: we only want the mask between [startIndex, maskIndex]
138+
// to be true, the rest are false.
139+
if (numFrontPadElems != 0 && maskDimSizes.size() > 1)
140+
return std::nullopt;
141+
142+
SmallVector<int64_t> newMaskDimSizes(maskDimSizes.drop_back());
143+
newMaskDimSizes.push_back(maskIndex);
144+
145+
if (numFrontPadElems == 0)
146+
return rewriter.create<vector::ConstantMaskOp>(loc, newMaskType,
147+
newMaskDimSizes);
148+
149+
SmallVector<bool> newMaskValues;
150+
for (int64_t i = 0; i < numDestElems; ++i)
151+
newMaskValues.push_back(i >= startIndex && i < maskIndex);
152+
auto denseAttr = DenseElementsAttr::get(newMaskType, newMaskValues);
153+
return rewriter.create<arith::ConstantOp>(loc, newMaskType,
154+
denseAttr);
155+
})
156+
.Case<arith::ConstantOp>([&](auto constantOp)
157+
-> std::optional<Operation *> {
158+
// TODO: Support multiple dimensions.
159+
if (maskShape.size() != 1)
160+
return std::nullopt;
161+
// Rearrange the original mask values to cover the whole potential
162+
// loading region. For example, in the case of using byte-size for
163+
// emulation, given the following mask:
164+
//
165+
// %mask = [0, 1, 0, 1, 0, 0]
166+
//
167+
// With front offset of 1, the mask will be padded 0s in the front
168+
// and back so that:
169+
// 1. It is aligned with the effective loading bits
170+
// 2. Its length is multiple of `numSrcElemPerDest` (and the total
171+
// coverage size is mulitiple of bytes). The new mask will be like
172+
// this before compressing:
173+
//
174+
// %new_mask = [0, 0, 1, 0, 1, 0, 0, 0]
175+
auto denseAttr = cast<DenseIntElementsAttr>(constantOp.getValue());
176+
SmallVector<bool> paddedMaskValues(numFrontPadElems, false);
177+
paddedMaskValues.append(denseAttr.template value_begin<bool>(),
178+
denseAttr.template value_end<bool>());
179+
paddedMaskValues.resize(numDestElems * numSrcElemsPerDest, false);
180+
181+
// Compressing by combining every `numSrcElemsPerDest` elements:
182+
SmallVector<bool> compressedMaskValues;
183+
for (size_t i = 0; i < paddedMaskValues.size();
184+
i += numSrcElemsPerDest) {
185+
bool combinedValue = false;
186+
for (int j = 0; j < numSrcElemsPerDest; ++j) {
187+
combinedValue |= paddedMaskValues[i + j];
188+
}
189+
compressedMaskValues.push_back(combinedValue);
190+
}
191+
return rewriter.create<arith::ConstantOp>(
192+
loc, DenseElementsAttr::get(newMaskType, compressedMaskValues));
193+
});
194+
195+
if (!newMask)
196+
return failure();
147197

148198
while (!extractOps.empty()) {
149199
newMask = rewriter.create<vector::ExtractOp>(
150-
loc, newMask->getResults()[0], extractOps.back().getMixedPosition());
200+
loc, (*newMask)->getResults()[0], extractOps.back().getMixedPosition());
151201
extractOps.pop_back();
152202
}
153203

154-
return newMask;
204+
return *newMask;
155205
}
156206

157207
/// Extracts 1-D subvector from a 1-D vector. It is a wrapper function for
@@ -185,12 +235,10 @@ static Value staticallyExtractSubvector(OpBuilder &rewriter, Location loc,
185235
/// `vector.insert_strided_slice`.
186236
static Value staticallyInsertSubvector(OpBuilder &rewriter, Location loc,
187237
Value src, Value dest, int64_t offset) {
188-
auto srcType = cast<VectorType>(src.getType());
189-
auto destType = cast<VectorType>(dest.getType());
238+
[[maybe_unused]] auto srcType = cast<VectorType>(src.getType());
239+
[[maybe_unused]] auto destType = cast<VectorType>(dest.getType());
190240
assert(srcType.getRank() == 1 && destType.getRank() == 1 &&
191241
"expected source and dest to be vector type");
192-
(void)srcType;
193-
(void)destType;
194242
auto offsets = rewriter.getI64ArrayAttr({offset});
195243
auto strides = rewriter.getI64ArrayAttr({1});
196244
return rewriter.create<vector::InsertStridedSliceOp>(loc, dest.getType(), src,

mlir/test/Dialect/Vector/vector-emulate-narrow-type-unaligned.mlir

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,41 @@ func.func @vector_maskedload_i2_dynamic_indexing_mixed(%passthru: vector<3xi2>,
249249
// CHECK: %[[IN8:.+]] = vector.insert %[[EX8]], %[[IN7]] [1] : i2 into vector<3xi2>
250250
// CHECK: %[[EX9:.+]] = vector.extract %[[SELECT]][%[[INCIDX2]]] : i2 from vector<8xi2>
251251
// CHECK: %[[IN9:.+]] = vector.insert %[[EX9]], %[[IN8]] [2] : i2 into vector<3xi2>
252+
253+
// -----
254+
255+
func.func @vector_maskedload_i4_constant_mask_unaligned(%passthru: vector<5xi2>) -> vector<5xi2> {
256+
%0 = memref.alloc() : memref<3x5xi2>
257+
%mask = arith.constant dense<[false, true, true, true, false]> : vector<5xi1>
258+
%c0 = arith.constant 0 : index
259+
%c1 = arith.constant 1 : index
260+
%1 = vector.maskedload %0[%c1, %c0], %mask, %passthru :
261+
memref<3x5xi2>, vector<5xi1>, vector<5xi2> into vector<5xi2>
262+
return %1 : vector<5xi2>
263+
}
264+
265+
// CHECK: func @vector_maskedload_i4_constant_mask_unaligned(
266+
// CHECK-SAME: %[[PTH:.+]]: vector<5xi2>) -> vector<5xi2>
267+
// CHECK: %[[ALLOC:.+]] = memref.alloc() : memref<4xi8>
268+
// CHECK: %[[MASK:.+]] = arith.constant dense<[false, true, true, true, false]> : vector<5xi1>
269+
270+
// CHECK: %[[COMPRESSED_MASK:.+]] = arith.constant dense<true> : vector<2xi1>
271+
// CHECK: %[[EMPTY:.+]] = arith.constant dense<0> : vector<8xi2>
272+
// CHECK: %[[PTH_PADDED:.+]] = vector.insert_strided_slice %[[PTH]], %[[EMPTY]]
273+
// CHECK-SAME: {offsets = [1], strides = [1]} : vector<5xi2> into vector<8xi2>
274+
275+
// Emulated masked load from alloc:
276+
// CHECK: %[[PTH_PADDED_UPCAST:.+]] = vector.bitcast %[[PTH_PADDED]] : vector<8xi2> to vector<2xi8>
277+
// CHECK: %[[C1:.+]] = arith.constant 1 : index
278+
// CHECK: %[[MASKLOAD:.+]] = vector.maskedload %[[ALLOC]][%[[C1]]], %[[COMPRESSED_MASK]], %[[PTH_PADDED_UPCAST]]
279+
// CHECK: %[[MASKLOAD_DOWNCAST:.+]] = vector.bitcast %[[MASKLOAD]] : vector<2xi8> to vector<8xi2>
280+
281+
// Select from emulated loaded vector and passthru vector:
282+
// TODO: fold this part if possible.
283+
// CHECK: %[[EMPTY_MASK:.+]] = arith.constant dense<false> : vector<8xi1>
284+
// CHECK: %[[MASK_PADDED:.+]] = vector.insert_strided_slice %[[MASK]], %[[EMPTY_MASK]]
285+
// CHECK-SAME: {offsets = [1], strides = [1]} : vector<5xi1> into vector<8xi1>
286+
// CHECK: %[[SELECT:.+]] = arith.select %[[MASK_PADDED]], %[[MASKLOAD_DOWNCAST]], %[[PTH_PADDED]] : vector<8xi1>, vector<8xi2>
287+
// CHECK: %[[RESULT:.+]] = vector.extract_strided_slice %[[SELECT]]
288+
// CHECK-SAME: {offsets = [1], sizes = [5], strides = [1]} : vector<8xi2> to vector<5xi2>
289+
// CHECK: return %[[RESULT]] : vector<5xi2>

mlir/test/Dialect/Vector/vector-emulate-narrow-type.mlir

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,30 @@ func.func @vector_maskedload_i4_constant_mask(%arg1: index, %arg2: index, %passt
275275

276276
// -----
277277

278+
func.func @vector_maskedload_i4_arith_constant(%passthru: vector<8xi4>) -> vector<8xi4> {
279+
%0 = memref.alloc() : memref<3x8xi4>
280+
%cst = arith.constant dense<0> : vector<8xi4>
281+
%mask = arith.constant dense<[false, true, true, true, true, false, false, false]> : vector<8xi1>
282+
%c0 = arith.constant 0 : index
283+
%1 = vector.maskedload %0[%c0, %c0], %mask, %passthru :
284+
memref<3x8xi4>, vector<8xi1>, vector<8xi4> into vector<8xi4>
285+
return %1 : vector<8xi4>
286+
}
287+
288+
// CHECK: func @vector_maskedload_i4_arith_constant(
289+
// CHECK-SAME: %[[PASSTHRU:[a-zA-Z0-9]+]]
290+
// CHECK: %[[ALLOC:.+]] = memref.alloc() : memref<12xi8>
291+
// CHECK: %[[MASK:.+]] = arith.constant dense<[false, true, true, true, true, false, false, false]> : vector<8xi1>
292+
293+
// Emit a new, compressed mask for emulated maskedload:
294+
// CHECK: %[[COMPRESSED_MASK:.+]] = arith.constant dense<[true, true, true, false]> : vector<4xi1>
295+
// CHECK: %[[PTHU_UPCAST:.+]] = vector.bitcast %[[PASSTHRU]] : vector<8xi4> to vector<4xi8>
296+
// CHECK: %[[C0:.+]] = arith.constant 0 : index
297+
// CHECK: %[[LOAD:.+]] = vector.maskedload %[[ALLOC]][%[[C0]]], %[[COMPRESSED_MASK]], %[[PTHU_UPCAST]]
298+
// CHECK: %[[LOAD_DOWNCAST:.+]] = vector.bitcast %[[LOAD]] : vector<4xi8> to vector<8xi4>
299+
// CHECK: %[[SELECT:.+]] = arith.select %[[MASK]], %[[LOAD_DOWNCAST]], %[[PASSTHRU]] : vector<8xi1>, vector<8xi4>
300+
// CHECK: return %[[SELECT]] : vector<8xi4>
301+
278302
///----------------------------------------------------------------------------------------
279303
/// vector.extract -> vector.masked_load
280304
///----------------------------------------------------------------------------------------
@@ -624,3 +648,29 @@ func.func @vector_maskedstore_i4_constant_mask(
624648
// CHECK32: %[[SELECT:.+]] = arith.select %[[ORIG_MASK]], %[[VAL_TO_STORE]], %[[BITCAST]] : vector<8xi1>, vector<8xi4>
625649
// CHECK32: %[[NEW_VAL:.+]] = vector.bitcast %[[SELECT]] : vector<8xi4> to vector<1xi32>
626650
// CHECK32: vector.maskedstore %[[ALLOC]]{{\[}}%[[LIDX]]], %[[NEW_MASK]], %[[NEW_VAL]] : memref<3xi32>, vector<1xi1>, vector<1xi32>
651+
652+
// -----
653+
654+
func.func @vector_maskedstore_i4_arith_constant(%val_to_store: vector<8xi4>) {
655+
%0 = memref.alloc() : memref<5x8xi4>
656+
%cst = arith.constant dense<0> : vector<8xi4>
657+
%mask = arith.constant dense<[false, true, true, true, true, true, false, false]> : vector<8xi1>
658+
%c0 = arith.constant 0 : index
659+
%c3 = arith.constant 3 : index
660+
vector.maskedstore %0[%c3, %c0], %mask, %val_to_store :
661+
memref<5x8xi4>, vector<8xi1>, vector<8xi4>
662+
return
663+
}
664+
665+
// CHECK-LABEL: func @vector_maskedstore_i4_arith_constant
666+
// CHECK-SAME: %[[VAL_TO_STORE:[a-zA-Z0-9]+]]:
667+
// CHECK: %[[ALLOC:.+]] = memref.alloc() : memref<20xi8>
668+
// CHECK: %[[MASK:.+]] = arith.constant dense<[false, true, true, true, true, true, false, false]> : vector<8xi1>
669+
// CHECK: %[[C12:.+]] = arith.constant 12 : index
670+
// CHECK: %[[COMPRESSED_MASK:.+]] = arith.constant dense<[true, true, true, false]> : vector<4xi1>
671+
// CHECK: %[[EMPTY:.+]] = arith.constant dense<0> : vector<4xi8>
672+
// CHECK: %[[MASKEDLOAD:.+]] = vector.maskedload %[[ALLOC]][%[[C12]]], %[[COMPRESSED_MASK]], %[[EMPTY]]
673+
// CHECK: %[[LOAD_UPCAST:.+]] = vector.bitcast %[[MASKEDLOAD]]
674+
// CHECK: %[[SELECT:.+]] = arith.select %[[MASK]], %[[VAL_TO_STORE]], %[[LOAD_UPCAST]]
675+
// CHECK: %[[SELECT_DOWNCAST:.+]] = vector.bitcast %[[SELECT]]
676+
// CHECK: vector.maskedstore %[[ALLOC]][%[[C12]]], %[[COMPRESSED_MASK]], %[[SELECT_DOWNCAST]]

0 commit comments

Comments
 (0)