23#include <mlir/Dialect/SCF/Utils/Utils.h>
25#include <llvm/Support/Debug.h>
26#include <llvm/Support/SMTAPI.h>
33#define GEN_PASS_DEF_FUSEPRODUCTLOOPSPASS
43constexpr int INDEX_WIDTH = 64;
45static inline bool isConstOrStructParam(Value val) {
47 return llvm::isa<arith::ConstantIndexOp, polymorphic::ConstReadOp, felt::FeltConstantOp>(
52static llvm::SMTExprRef mkExpr(Value value, llvm::SMTSolver *solver) {
53 if (
auto constOp = value.getDefiningOp<arith::ConstantIndexOp>()) {
54 return solver->mkBitvector(llvm::APSInt::get(constOp.value()), INDEX_WIDTH);
57 return solver->mkSymbol(
58 std::string {polyReadOp.getConstName()}.c_str(), solver->getBitvectorSort(INDEX_WIDTH)
61 assert(
false &&
"unsupported: checking non-constant trip counts");
65static llvm::SMTExprRef tripCount(scf::ForOp op, llvm::SMTSolver *solver) {
66 const auto *one = solver->mkBitvector(llvm::APSInt::get(1), INDEX_WIDTH);
67 return solver->mkBVSDiv(
70 solver->mkBVSub(mkExpr(op.getUpperBound(), solver), mkExpr(op.getLowerBound(), solver))
72 mkExpr(op.getStep(), solver)
76static inline bool canLoopsBeFused(scf::ForOp a, scf::ForOp b) {
83 if (a->getParentRegion() != b->getParentRegion()) {
104 auto tripCountA = constantTripCount(a.getLowerBound(), a.getUpperBound(), a.getStep());
105 auto tripCountB = constantTripCount(b.getLowerBound(), b.getUpperBound(), b.getStep());
106 if (tripCountA.has_value() && tripCountB.has_value() && *tripCountA == *tripCountB) {
110 if (!isConstOrStructParam(a.getLowerBound()) || !isConstOrStructParam(a.getUpperBound()) ||
111 !isConstOrStructParam(a.getStep()) || !isConstOrStructParam(b.getLowerBound()) ||
112 !isConstOrStructParam(b.getUpperBound()) || !isConstOrStructParam(b.getStep())) {
116 llvm::SMTSolverRef solver = llvm::CreateZ3Solver();
117 solver->addConstraint( solver->mkNot(
118 solver->mkEqual(tripCount(a, solver.get()), tripCount(b, solver.get()))
121 return !*solver->check();
127static FailureOr<SmallVector<Operation *>>
128canPrepareForFusion(scf::ForOp witnessLoop, scf::ForOp constraintLoop) {
129 if (witnessLoop->getBlock() != constraintLoop->getBlock()) {
133 SmallVector<Operation *> opsToSink;
134 for (
auto *op = witnessLoop->getNextNode(); op != constraintLoop; op = op->getNextNode()) {
142 opsToSink.push_back(op);
149prepareForFusion(scf::ForOp witnessLoop, scf::ForOp constraintLoop, IRRewriter &rewriter) {
150 auto computeOpsToSink = canPrepareForFusion(witnessLoop, constraintLoop);
151 if (failed(computeOpsToSink)) {
155 Operation *insertionPoint = constraintLoop.getOperation();
156 for (Operation *op : *computeOpsToSink) {
157 rewriter.moveOpAfter(op, insertionPoint);
164static LogicalResult fuseMatchingLoopPairs(Region &body, MLIRContext *context) {
166 llvm::SmallVector<scf::ForOp> witnessLoops, constraintLoops;
167 body.walk<WalkOrder::PreOrder>([&witnessLoops, &constraintLoops](scf::ForOp forOp) {
169 return WalkResult::skip();
171 auto productSource = forOp->getAttrOfType<StringAttr>(
PRODUCT_SOURCE);
173 witnessLoops.push_back(forOp);
175 constraintLoops.push_back(forOp);
178 return WalkResult::skip();
184 witnessLoops, constraintLoops, canLoopsBeFused
188 if (failed(fusionCandidates)) {
193 IRRewriter rewriter {context};
194 for (
auto [w, c] : *fusionCandidates) {
195 if (failed(prepareForFusion(w, c, rewriter))) {
198 auto fusedLoop = fuseIndependentSiblingForLoops(w, c, rewriter);
199 fusedLoop->setAttr(
PRODUCT_SOURCE, rewriter.getAttr<StringAttr>(
"fused"));
201 if (failed(fuseMatchingLoopPairs(fusedLoop.getBodyRegion(), context))) {
209 using Base = FuseProductLoopsPassBase<PassImpl>;
212 void runOnOperation()
override {
213 ModuleOp
mod = getOperation();
214 mod.walk([
this](function::FuncDefOp funcDef) {
216 if (failed(fuseMatchingLoopPairs(funcDef.getFunctionBody(), &getContext()))) {
bool isStructProduct()
Return true iff the function is within a StructDefOp and named FUNC_NAME_PRODUCT.
llvm::FailureOr< llvm::SetVector< std::pair< ValueT, ValueT > > > getMatchingPairs(llvm::ArrayRef< ValueT > as, llvm::ArrayRef< ValueT > bs, FnT doesMatch, bool allowPartial=true)
constexpr char FUNC_NAME_COMPUTE[]
Symbol name for the witness generation (and resp.
ExpressionValue mod(const llvm::SMTSolverRef &solver, const ExpressionValue &lhs, const ExpressionValue &rhs)
constexpr char PRODUCT_SOURCE[]
Name of the attribute on aligned product program ops that specifies where they came from.
constexpr char FUNC_NAME_CONSTRAIN[]