18#define GEN_PASS_DEF_ELIMINATEVECTORMASKS
19#include "mlir/Dialect/Vector/Transforms/Passes.h.inc"
33resolveAllTrueCreateMaskOp(
IRRewriter &rewriter,
34 vector::CreateMaskOp createMaskOp,
35 std::optional<VscaleRange> vscaleRange) {
36 auto maskType = createMaskOp.getVectorType();
37 auto maskTypeDimScalableFlags = maskType.getScalableDims();
38 auto maskTypeDimSizes = maskType.getShape();
41 constexpr std::array<int64_t, 1> rankZeroShape{1};
42 constexpr std::array<bool, 1> rankZeroScalableDims{
false};
43 if (maskType.getRank() == 0) {
44 maskTypeDimSizes = rankZeroShape;
45 maskTypeDimScalableFlags = rankZeroScalableDims;
48 struct UnknownMaskDim {
60 for (
auto [i, dimSize] : llvm::enumerate(createMaskOp.getOperands())) {
63 if (maskTypeDimScalableFlags[i] || intSize < maskTypeDimSizes[i])
67 if (vscaleMultiplier < maskTypeDimSizes[i])
71 unknownDims.push_back(UnknownMaskDim{i, dimSize});
75 for (
auto [i, dimSize] : unknownDims) {
84 if (maskTypeDimScalableFlags[i])
86 FailureOr<int64_t> constantLowerBound =
89 if (failed(constantLowerBound))
92 if (*constantLowerBound < maskTypeDimSizes[i])
99 FailureOr<ConstantOrScalableBound> dimLowerBound =
101 dimSize, {}, vscaleRange->vscaleMin, vscaleRange->vscaleMax,
103 if (failed(dimLowerBound))
105 auto dimLowerBoundSize = dimLowerBound->getSize();
106 if (failed(dimLowerBoundSize))
108 if (dimLowerBoundSize->scalable) {
111 if (dimLowerBoundSize->baseSize < maskTypeDimSizes[i])
116 if (maskTypeDimScalableFlags[i])
119 if (dimLowerBoundSize->baseSize < maskTypeDimSizes[i])
127 auto allTrue = vector::ConstantMaskOp::create(
138 std::optional<VscaleRange> vscaleRange) {
140 if (function.isExternal())
148 function.walk([&](vector::CreateMaskOp createMaskOp) {
149 worklist.push_back(createMaskOp);
153 for (
auto mask : worklist)
154 (
void)resolveAllTrueCreateMaskOp(rewriter, mask, vscaleRange);
158struct EliminateVectorMasksPass
165 bool unset = !vscaleMin && !vscaleMax;
166 bool valid = vscaleMin && vscaleMax && vscaleMin <= vscaleMax;
169 return emitError(UnknownLoc::get(context))
170 <<
"invalid vscale range 'vscale-min="
171 <<
static_cast<unsigned>(vscaleMin)
172 <<
" vscale-max=" <<
static_cast<unsigned>(vscaleMax)
173 <<
"': expected both to be 0 (unknown), or both non-zero with "
174 "'vscale-min' <= 'vscale-max'";
177 void runOnOperation()
override {
178 std::optional<VscaleRange> vscaleRange;
179 if (vscaleMin && vscaleMax)
180 vscaleRange = VscaleRange{vscaleMin, vscaleMax};
LogicalResult initialize(unsigned origNumLoops, ArrayRef< ReassociationIndices > foldedIterationDims)
This class coordinates rewriting a piece of IR outside of a pattern rewrite, providing a way to keep ...
MLIRContext is the top-level object for a collection of MLIR operations.
RAII guard to reset the insertion point of the builder when destroyed.
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
virtual void replaceAllUsesWith(Value from, Value to)
Find uses of from and replace them with to.
static FailureOr< int64_t > computeConstantBound(presburger::BoundType type, const Variable &var, const StopConditionFn &stopCondition=nullptr, ValueBoundsOptions options={})
Compute a constant bound for the given variable.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
std::optional< int64_t > getConstantVscaleMultiplier(Value value)
If value is a constant multiple of vector.vscale (e.g.
void eliminateVectorMasks(IRRewriter &rewriter, FunctionOpInterface function, std::optional< VscaleRange > vscaleRange={})
Split a vector.transfer operation into an in-bounds (i.e., no out-of-bounds masking) fastpath and a s...
Include the generated interface declarations.
std::optional< int64_t > getConstantIntValue(OpFoldResult ofr)
If ofr is a constant integer or an IntegerAttr, return the integer.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
static FailureOr< ConstantOrScalableBound > computeScalableBound(Value value, std::optional< int64_t > dim, unsigned vscaleMin, unsigned vscaleMax, presburger::BoundType boundType, ValueBoundsOptions options={true}, const StopConditionFn &stopCondition=nullptr)
Computes a (possibly) scalable bound for a given value.