1//===- TestReifyValueBounds.cpp - Test value bounds reification -----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "TestOpsInterfaces.h"
10#include "mlir/Dialect/Affine/IR/AffineOps.h"
11#include "mlir/Dialect/Affine/IR/ValueBoundsOpInterfaceImpl.h"
12#include "mlir/Dialect/Affine/Transforms/Transforms.h"
13#include "mlir/Dialect/Arith/IR/Arith.h"
14#include "mlir/Dialect/Arith/Transforms/Transforms.h"
15#include "mlir/Dialect/Func/IR/FuncOps.h"
16#include "mlir/Dialect/MemRef/IR/MemRefDialect.h"
17#include "mlir/Dialect/Tensor/IR/Tensor.h"
18#include "mlir/Dialect/Vector/IR/ScalableValueBoundsConstraintSet.h"
19#include "mlir/IR/PatternMatch.h"
20#include "mlir/Interfaces/FunctionInterfaces.h"
21#include "mlir/Interfaces/ValueBoundsOpInterface.h"
22#include "mlir/Pass/Pass.h"
23
24#define PASS_NAME "test-affine-reify-value-bounds"
25
26using namespace mlir;
27using namespace mlir::affine;
28
29namespace {
30
31/// This pass applies the permutation on the first maximal perfect nest.
32struct TestReifyValueBounds
33 : public PassWrapper<TestReifyValueBounds,
34 InterfacePass<FunctionOpInterface>> {
35 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestReifyValueBounds)
36
37 StringRef getArgument() const final { return PASS_NAME; }
38 StringRef getDescription() const final {
39 return "Tests ValueBoundsOpInterface with affine dialect reification";
40 }
41 TestReifyValueBounds() = default;
42 TestReifyValueBounds(const TestReifyValueBounds &pass) : PassWrapper(pass){};
43
44 void getDependentDialects(DialectRegistry &registry) const override {
45 registry.insert<affine::AffineDialect, tensor::TensorDialect,
46 memref::MemRefDialect>();
47 }
48
49 void runOnOperation() override;
50
51private:
52 Option<bool> reifyToFuncArgs{
53 *this, "reify-to-func-args",
54 llvm::cl::desc("Reify in terms of function args"), llvm::cl::init(Val: false)};
55
56 Option<bool> useArithOps{*this, "use-arith-ops",
57 llvm::cl::desc("Reify with arith dialect ops"),
58 llvm::cl::init(Val: false)};
59};
60
61} // namespace
62
63static ValueBoundsConstraintSet::ComparisonOperator
64invertComparisonOperator(ValueBoundsConstraintSet::ComparisonOperator cmp) {
65 if (cmp == ValueBoundsConstraintSet::ComparisonOperator::LT)
66 return ValueBoundsConstraintSet::ComparisonOperator::GE;
67 if (cmp == ValueBoundsConstraintSet::ComparisonOperator::LE)
68 return ValueBoundsConstraintSet::ComparisonOperator::GT;
69 if (cmp == ValueBoundsConstraintSet::ComparisonOperator::GT)
70 return ValueBoundsConstraintSet::ComparisonOperator::LE;
71 if (cmp == ValueBoundsConstraintSet::ComparisonOperator::GE)
72 return ValueBoundsConstraintSet::ComparisonOperator::LT;
73 llvm_unreachable("unsupported comparison operator");
74}
75
76/// Look for "test.reify_bound" ops in the input and replace their results with
77/// the reified values.
78static LogicalResult testReifyValueBounds(FunctionOpInterface funcOp,
79 bool reifyToFuncArgs,
80 bool useArithOps) {
81 IRRewriter rewriter(funcOp.getContext());
82 WalkResult result = funcOp.walk(callback: [&](test::ReifyBoundOp op) {
83 auto boundType = op.getBoundType();
84 Value value = op.getVar();
85 std::optional<int64_t> dim = op.getDim();
86 auto shapedType = dyn_cast<ShapedType>(Val: value.getType());
87 if (!shapedType && dim.has_value()) {
88 op->emitOpError("dim specified for non-shaped type");
89 return WalkResult::interrupt();
90 }
91 if (shapedType && !dim.has_value()) {
92 op->emitOpError("dim not specified for shaped type");
93 return WalkResult::interrupt();
94 }
95 if (shapedType && shapedType.hasRank() && dim.has_value()) {
96 if (dim.value() < 0) {
97 op->emitOpError("dim must be non-negative");
98 return WalkResult::interrupt();
99 }
100
101 if (dim.value() >= shapedType.getRank()) {
102 op->emitOpError("invalid dim for shaped type rank");
103 return WalkResult::interrupt();
104 }
105 }
106
107 bool constant = op.getConstant();
108 bool scalable = op.getScalable();
109 ValueBoundsOptions options;
110 options.allowIntegerType = op.getAllowIntegerType();
111
112 // Prepare stop condition. By default, reify in terms of the op's
113 // operands. No stop condition is used when a constant was requested.
114 std::function<bool(Value, std::optional<int64_t>,
115 ValueBoundsConstraintSet & cstr)>
116 stopCondition = [&](Value v, std::optional<int64_t> d,
117 ValueBoundsConstraintSet &cstr) {
118 // Reify in terms of SSA values that are different from `value`.
119 return v != value;
120 };
121 if (reifyToFuncArgs) {
122 // Reify in terms of function block arguments.
123 stopCondition = [](Value v, std::optional<int64_t> d,
124 ValueBoundsConstraintSet &cstr) {
125 auto bbArg = dyn_cast<BlockArgument>(Val&: v);
126 if (!bbArg)
127 return false;
128 return isa<FunctionOpInterface>(Val: bbArg.getParentBlock()->getParentOp());
129 };
130 }
131
132 // Reify value bound
133 rewriter.setInsertionPointAfter(op);
134 FailureOr<OpFoldResult> reified = failure();
135 if (constant) {
136 auto reifiedConst = ValueBoundsConstraintSet::computeConstantBound(
137 type: boundType, var: {value, dim}, /*stopCondition=*/nullptr, options);
138 if (succeeded(reifiedConst))
139 reified = FailureOr<OpFoldResult>(rewriter.getIndexAttr(value: *reifiedConst));
140 } else if (scalable) {
141 auto loc = op->getLoc();
142 options.closedUB = true;
143 auto reifiedScalable =
144 vector::ScalableValueBoundsConstraintSet::computeScalableBound(
145 value, dim, vscaleMin: *op.getVscaleMin(), vscaleMax: *op.getVscaleMax(), boundType: boundType,
146 options);
147 if (succeeded(reifiedScalable)) {
148 SmallVector<std::pair<Value, std::optional<int64_t>>, 1> vscaleOperand;
149 if (reifiedScalable->map.getNumInputs() == 1) {
150 // The only possible input to the bound is vscale.
151 vscaleOperand.push_back(Elt: std::make_pair(
152 vector::VectorScaleOp::create(rewriter, loc), std::nullopt));
153 }
154 reified = affine::materializeComputedBound(
155 b&: rewriter, loc: loc, boundMap: reifiedScalable->map, mapOperands: vscaleOperand);
156 }
157 } else {
158 if (useArithOps) {
159 reified =
160 arith::reifyValueBound(b&: rewriter, loc: op->getLoc(), type: boundType,
161 var: op.getVariable(), stopCondition, options);
162 } else {
163 reified = reifyValueBound(rewriter, op->getLoc(), boundType,
164 op.getVariable(), stopCondition, options);
165 }
166 }
167 if (failed(Result: reified)) {
168 op->emitOpError("could not reify bound");
169 return WalkResult::interrupt();
170 }
171
172 // Replace the op with the reified bound.
173 if (auto val = llvm::dyn_cast_if_present<Value>(Val&: *reified)) {
174 rewriter.replaceOp(op, val);
175 return WalkResult::skip();
176 }
177 auto attr = cast<IntegerAttr>(Val: cast<Attribute>(Val&: *reified));
178 Value constOp = arith::ConstantOp::create(
179 rewriter, op->getLoc(),
180 rewriter.getIntegerAttr(op.getResult().getType(), attr.getInt()));
181 rewriter.replaceOp(op, constOp);
182 return WalkResult::skip();
183 });
184 return failure(IsFailure: result.wasInterrupted());
185}
186
187/// Look for "test.compare" ops and emit errors/remarks.
188static LogicalResult testEquality(FunctionOpInterface funcOp) {
189 IRRewriter rewriter(funcOp.getContext());
190 WalkResult result = funcOp.walk(callback: [&](test::CompareOp op) {
191 auto cmpType = op.getComparisonOperator();
192 if (op.getCompose()) {
193 if (cmpType != ValueBoundsConstraintSet::EQ) {
194 op->emitOpError(
195 "comparison operator must be EQ when 'composed' is specified");
196 return WalkResult::interrupt();
197 }
198 FailureOr<int64_t> delta = affine::fullyComposeAndComputeConstantDelta(
199 value1: op->getOperand(0), value2: op->getOperand(1));
200 if (failed(Result: delta)) {
201 op->emitError("could not determine equality");
202 } else if (*delta == 0) {
203 op->emitRemark("equal");
204 } else {
205 op->emitRemark("different");
206 }
207 return WalkResult::advance();
208 }
209
210 auto compare = [&](ValueBoundsConstraintSet::ComparisonOperator cmp) {
211 return ValueBoundsConstraintSet::compare(op.getLhs(), cmp, op.getRhs());
212 };
213 if (compare(cmpType)) {
214 op->emitRemark("true");
215 } else if (cmpType != ValueBoundsConstraintSet::EQ &&
216 compare(invertComparisonOperator(cmpType))) {
217 op->emitRemark("false");
218 } else if (cmpType == ValueBoundsConstraintSet::EQ &&
219 (compare(ValueBoundsConstraintSet::ComparisonOperator::LT) ||
220 compare(ValueBoundsConstraintSet::ComparisonOperator::GT))) {
221 op->emitRemark("false");
222 } else {
223 op->emitError("unknown");
224 }
225 return WalkResult::advance();
226 });
227 return failure(IsFailure: result.wasInterrupted());
228}
229
230void TestReifyValueBounds::runOnOperation() {
231 if (failed(
232 Result: testReifyValueBounds(funcOp: getOperation(), reifyToFuncArgs, useArithOps)))
233 signalPassFailure();
234 if (failed(Result: testEquality(funcOp: getOperation())))
235 signalPassFailure();
236}
237
238namespace mlir {
239void registerTestAffineReifyValueBoundsPass() {
240 PassRegistration<TestReifyValueBounds>();
241}
242} // namespace mlir
243