23#include <mlir/Dialect/SCF/Utils/Utils.h>
25#include <llvm/Support/Debug.h>
26#include <llvm/Support/SMTAPI.h>
32#define GEN_PASS_DECL_FUSEPRODUCTLOOPSPASS
33#define GEN_PASS_DEF_FUSEPRODUCTLOOPSPASS
45 mlir::ModuleOp
mod = getOperation();
56static inline bool isConstOrStructParam(mlir::Value val) {
58 return val.getDefiningOp<mlir::arith::ConstantIndexOp>() ||
62llvm::SMTExprRef
mkExpr(mlir::Value value, llvm::SMTSolver *solver) {
63 if (
auto constOp = value.getDefiningOp<mlir::arith::ConstantIndexOp>()) {
64 return solver->mkBitvector(llvm::APSInt::get(constOp.value()),
INDEX_WIDTH);
67 return solver->mkSymbol(
68 std::string {polyReadOp.getConstName()}.c_str(), solver->getBitvectorSort(
INDEX_WIDTH)
71 assert(
false &&
"unsupported: checking non-constant trip counts");
75llvm::SMTExprRef
tripCount(mlir::scf::ForOp op, llvm::SMTSolver *solver) {
76 const auto *one = solver->mkBitvector(llvm::APSInt::get(1),
INDEX_WIDTH);
77 return solver->mkBVSDiv(
80 solver->mkBVSub(
mkExpr(op.getUpperBound(), solver),
mkExpr(op.getLowerBound(), solver))
82 mkExpr(op.getStep(), solver)
86static inline bool canLoopsBeFused(mlir::scf::ForOp a, mlir::scf::ForOp b) {
93 if (a->getParentRegion() != b->getParentRegion()) {
114 auto tripCountA = mlir::constantTripCount(a.getLowerBound(), a.getUpperBound(), a.getStep());
115 auto tripCountB = mlir::constantTripCount(b.getLowerBound(), b.getUpperBound(), b.getStep());
116 if (tripCountA.has_value() && tripCountB.has_value() && *tripCountA == *tripCountB) {
120 if (!isConstOrStructParam(a.getLowerBound()) || !isConstOrStructParam(a.getUpperBound()) ||
121 !isConstOrStructParam(a.getStep()) || !isConstOrStructParam(b.getLowerBound()) ||
122 !isConstOrStructParam(b.getUpperBound()) || !isConstOrStructParam(b.getStep())) {
126 llvm::SMTSolverRef solver = llvm::CreateZ3Solver();
127 solver->addConstraint( solver->mkNot(
131 return !*solver->check();
136 llvm::SmallVector<mlir::scf::ForOp> witnessLoops, constraintLoops;
137 body.walk<mlir::WalkOrder::PreOrder>([&witnessLoops, &constraintLoops](mlir::scf::ForOp forOp) {
139 return mlir::WalkResult::skip();
141 auto productSource = forOp->getAttrOfType<mlir::StringAttr>(
PRODUCT_SOURCE);
143 witnessLoops.push_back(forOp);
145 constraintLoops.push_back(forOp);
148 return mlir::WalkResult::skip();
154 witnessLoops, constraintLoops, canLoopsBeFused
158 if (mlir::failed(fusionCandidates)) {
159 return mlir::failure();
163 mlir::IRRewriter rewriter {context};
164 for (
auto [w, c] : *fusionCandidates) {
165 auto fusedLoop = mlir::fuseIndependentSiblingForLoops(w, c, rewriter);
166 fusedLoop->setAttr(
PRODUCT_SOURCE, rewriter.getAttr<mlir::StringAttr>(
"fused"));
169 return mlir::failure();
172 return mlir::success();
176 return std::make_unique<FuseProductLoopsPass>();
void runOnOperation() override
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.
llvm::SMTExprRef tripCount(mlir::scf::ForOp op, llvm::SMTSolver *solver)
constexpr char PRODUCT_SOURCE[]
Name of the attribute on aligned product program ops that specifies where they came from.
constexpr char FUNC_NAME_CONSTRAIN[]
mlir::LogicalResult fuseMatchingLoopPairs(mlir::Region &body, mlir::MLIRContext *context)
Identify pairs of scf.for loops that can be fused, fuse them, and then recurse to fuse nested loops.
llvm::SMTExprRef mkExpr(mlir::Value value, llvm::SMTSolver *solver)
ExpressionValue mod(llvm::SMTSolverRef solver, const ExpressionValue &lhs, const ExpressionValue &rhs)
constexpr int INDEX_WIDTH
std::unique_ptr< mlir::Pass > createFuseProductLoopsPass()