|
30 | 30 | #include "mlir/IR/OpDefinition.h" |
31 | 31 | #include "mlir/IR/TypeUtilities.h" |
32 | 32 | #include "mlir/IR/Value.h" |
| 33 | +#include "mlir/Support/LLVM.h" |
33 | 34 | #include "mlir/Transforms/DialectConversion.h" |
34 | 35 | #include "llvm/ADT/SmallVector.h" |
35 | 36 | #include "llvm/Support/Debug.h" |
| 37 | +#include "llvm/Support/LogicalResult.h" |
36 | 38 | #include "llvm/Support/MathExtras.h" |
37 | 39 | #include "llvm/Support/raw_ostream.h" |
38 | 40 | #include <cstdint> |
@@ -75,83 +77,137 @@ static FailureOr<Operation *> getCompressedMaskOp(OpBuilder &rewriter, |
75 | 77 | int numSrcElemsPerDest, |
76 | 78 | int numFrontPadElems = 0) { |
77 | 79 |
|
78 | | - assert(numFrontPadElems < numSrcElemsPerDest && "intraDataOffset must be less than scale"); |
| 80 | + assert(numFrontPadElems < numSrcElemsPerDest && |
| 81 | + "intraDataOffset must be less than scale"); |
79 | 82 |
|
80 | 83 | auto numElements = (numFrontPadElems + numSrcElems + numSrcElemsPerDest - 1) / |
81 | 84 | numSrcElemsPerDest; |
82 | 85 |
|
83 | 86 | Operation *maskOp = mask.getDefiningOp(); |
84 | 87 | SmallVector<vector::ExtractOp, 2> extractOps; |
85 | 88 | // 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)) { |
87 | 92 | if (auto extractOp = dyn_cast<vector::ExtractOp>(maskOp)) { |
88 | 93 | maskOp = extractOp.getVector().getDefiningOp(); |
89 | 94 | extractOps.push_back(extractOp); |
90 | 95 | } |
91 | 96 | } |
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)) |
95 | 100 | return failure(); |
96 | 101 |
|
97 | 102 | // Computing the "compressed" mask. All the emulation logic (i.e. computing |
98 | 103 | // new mask index) only happens on the last dimension of the vectors. |
99 | | - Operation *newMask = nullptr; |
100 | 104 | SmallVector<int64_t> shape( |
101 | 105 | cast<VectorType>(maskOp->getResultTypes()[0]).getShape()); |
102 | 106 | shape.back() = numElements; |
103 | 107 | 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 | | - } |
| 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>( |
| 129 | + [&](auto constantMaskOp) -> std::optional<Operation *> { |
| 130 | + ArrayRef<int64_t> maskDimSizes = |
| 131 | + constantMaskOp.getMaskDimSizes(); |
| 132 | + size_t numMaskOperands = maskDimSizes.size(); |
| 133 | + int64_t origIndex = maskDimSizes[numMaskOperands - 1]; |
| 134 | + int64_t startIndex = numFrontPadElems / numSrcElemsPerDest; |
| 135 | + int64_t maskIndex = llvm::divideCeil( |
| 136 | + numFrontPadElems + origIndex, numSrcElemsPerDest); |
| 137 | + |
| 138 | + // TODO: we only want the mask between [startIndex, maskIndex] |
| 139 | + // to be true, the rest are false. |
| 140 | + if (numFrontPadElems != 0 && maskDimSizes.size() > 1) |
| 141 | + return std::nullopt; |
| 142 | + |
| 143 | + SmallVector<int64_t> newMaskDimSizes(maskDimSizes.drop_back()); |
| 144 | + newMaskDimSizes.push_back(maskIndex); |
| 145 | + |
| 146 | + if (numFrontPadElems == 0) { |
| 147 | + return rewriter.create<vector::ConstantMaskOp>( |
| 148 | + loc, newMaskType, newMaskDimSizes); |
| 149 | + } else { |
| 150 | + SmallVector<bool> newMaskValues; |
| 151 | + for (int64_t i = 0; i < numElements; ++i) |
| 152 | + newMaskValues.push_back(i >= startIndex && i < maskIndex); |
| 153 | + auto denseAttr = |
| 154 | + DenseElementsAttr::get(newMaskType, newMaskValues); |
| 155 | + return rewriter.create<arith::ConstantOp>(loc, newMaskType, |
| 156 | + denseAttr); |
| 157 | + } |
| 158 | + }) |
| 159 | + .Case<arith::ConstantOp>([&](auto constantOp) |
| 160 | + -> std::optional<Operation *> { |
| 161 | + // TODO: Support multiple dimensions. |
| 162 | + if (shape.size() != 1) |
| 163 | + return std::nullopt; |
| 164 | + // Rearrange the original mask values to cover the whole potential |
| 165 | + // loading region. For example, in the case of using byte-size for |
| 166 | + // emulation, given the following mask: |
| 167 | + // |
| 168 | + // %mask = [false, true, false, true, false, false] |
| 169 | + // |
| 170 | + // With front offset of 1, the mask will be padded 0s in the front |
| 171 | + // and back so that: |
| 172 | + // 1. It is aligned with the effective loading bits |
| 173 | + // 2. Its length is multiple of `numSrcElemPerDest` (and the total |
| 174 | + // coverage size is mulitiple of bytes). The new mask will be like |
| 175 | + // this before compressing: |
| 176 | + // |
| 177 | + // %new_mask = [false, false, true, false, true, false, false, |
| 178 | + // false] |
| 179 | + auto denseAttr = |
| 180 | + dyn_cast<DenseIntElementsAttr>(constantOp.getValue()); |
| 181 | + if (!denseAttr) |
| 182 | + return std::nullopt; |
| 183 | + SmallVector<bool> maskValues(numFrontPadElems, false); |
| 184 | + maskValues.append(denseAttr.template value_begin<bool>(), |
| 185 | + denseAttr.template value_end<bool>()); |
| 186 | + maskValues.resize(numElements * numSrcElemsPerDest, false); |
| 187 | + |
| 188 | + // Compressing by combining every `numSrcElemsPerDest` elements: |
| 189 | + SmallVector<bool> compressedMaskValues; |
| 190 | + for (size_t i = 0; i < maskValues.size(); i += numSrcElemsPerDest) { |
| 191 | + bool combinedValue = false; |
| 192 | + for (int j = 0; j < numSrcElemsPerDest; ++j) { |
| 193 | + combinedValue |= maskValues[i + j]; |
| 194 | + } |
| 195 | + compressedMaskValues.push_back(combinedValue); |
| 196 | + } |
| 197 | + return rewriter.create<arith::ConstantOp>( |
| 198 | + loc, DenseElementsAttr::get(newMaskType, compressedMaskValues)); |
| 199 | + }); |
| 200 | + |
| 201 | + if (!newMask) |
| 202 | + return failure(); |
147 | 203 |
|
148 | 204 | while (!extractOps.empty()) { |
149 | 205 | newMask = rewriter.create<vector::ExtractOp>( |
150 | | - loc, newMask->getResults()[0], extractOps.back().getMixedPosition()); |
| 206 | + loc, (*newMask)->getResults()[0], extractOps.back().getMixedPosition()); |
151 | 207 | extractOps.pop_back(); |
152 | 208 | } |
153 | 209 |
|
154 | | - return newMask; |
| 210 | + return *newMask; |
155 | 211 | } |
156 | 212 |
|
157 | 213 | /// Extracts 1-D subvector from a 1-D vector. It is a wrapper function for |
|
0 commit comments