18#define GEN_PASS_DEF_ELIMINATEVECTORMASKS
19#include "mlir/Dialect/Vector/Transforms/Passes.h.inc"
29LogicalResult resolveAllFalseCreateMaskOp(
IRRewriter &rewriter,
30 vector::CreateMaskOp createMaskOp) {
32 bool allFalse =
false;
33 for (
Value dimSize : createMaskOp.getOperands()) {
43 unknownDims.push_back(dimSize);
49 allFalse = llvm::any_of(unknownDims, [&](
Value dimSize) {
57 auto allFalseMask = vector::ConstantMaskOp::create(
58 rewriter, createMaskOp.getLoc(), createMaskOp.getVectorType(),
69resolveAllTrueCreateMaskOp(
IRRewriter &rewriter,
70 vector::CreateMaskOp createMaskOp,
71 std::optional<VscaleRange> vscaleRange) {
72 auto maskType = createMaskOp.getVectorType();
73 auto maskTypeDimScalableFlags = maskType.getScalableDims();
74 auto maskTypeDimSizes = maskType.getShape();
77 constexpr std::array<int64_t, 1> rankZeroShape{1};
78 constexpr std::array<bool, 1> rankZeroScalableDims{
false};
79 if (maskType.getRank() == 0) {
80 maskTypeDimSizes = rankZeroShape;
81 maskTypeDimScalableFlags = rankZeroScalableDims;
84 struct UnknownMaskDim {
96 for (
auto [i, dimSize] : llvm::enumerate(createMaskOp.getOperands())) {
99 if (maskTypeDimScalableFlags[i] || intSize < maskTypeDimSizes[i])
103 if (vscaleMultiplier < maskTypeDimSizes[i])
107 unknownDims.push_back(UnknownMaskDim{i, dimSize});
111 for (
auto [i, dimSize] : unknownDims) {
120 if (maskTypeDimScalableFlags[i])
122 FailureOr<int64_t> constantLowerBound =
125 if (
failed(constantLowerBound))
128 if (*constantLowerBound < maskTypeDimSizes[i])
135 FailureOr<ConstantOrScalableBound> dimLowerBound =
137 dimSize, {}, vscaleRange->vscaleMin, vscaleRange->vscaleMax,
139 if (
failed(dimLowerBound))
141 auto dimLowerBoundSize = dimLowerBound->getSize();
142 if (
failed(dimLowerBoundSize))
144 if (dimLowerBoundSize->scalable) {
147 if (dimLowerBoundSize->baseSize < maskTypeDimSizes[i])
152 if (maskTypeDimScalableFlags[i])
155 if (dimLowerBoundSize->baseSize < maskTypeDimSizes[i])
163 auto allTrue = vector::ConstantMaskOp::create(
174 std::optional<VscaleRange> vscaleRange) {
176 if (function.isExternal())
183 function.walk([&](vector::CreateMaskOp createMaskOp) {
184 worklist.push_back(createMaskOp);
188 for (
auto mask : worklist) {
189 if (succeeded(resolveAllTrueCreateMaskOp(rewriter, mask, vscaleRange)))
191 (
void)resolveAllFalseCreateMaskOp(rewriter, mask);
196struct EliminateVectorMasksPass
197 :
public impl::EliminateVectorMasksBase<EliminateVectorMasksPass> {
203 bool unset = !vscaleMin && !vscaleMax;
204 bool valid = vscaleMin && vscaleMax && vscaleMin <= vscaleMax;
207 return emitError(UnknownLoc::get(context))
208 <<
"invalid vscale range 'vscale-min="
209 <<
static_cast<unsigned>(vscaleMin)
210 <<
" vscale-max=" <<
static_cast<unsigned>(vscaleMax)
211 <<
"': expected both to be 0 (unknown), or both non-zero with "
212 "'vscale-min' <= 'vscale-max'";
215 void runOnOperation()
override {
216 std::optional<VscaleRange> vscaleRange;
217 if (vscaleMin && vscaleMax)
218 vscaleRange = VscaleRange{vscaleMin, vscaleMax};
LogicalResult initialize(unsigned origNumLoops, ArrayRef< ReassociationIndices > foldedIterationDims)
IntegerAttr getIndexAttr(int64_t value)
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.
A variable that can be added to the constraint set as a "column".
static bool compare(const Variable &lhs, ComparisonOperator cmp, const Variable &rhs)
Return "true" if "lhs cmp rhs" was proven to hold.
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.