Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanradanov committed May 17, 2024
1 parent 9099670 commit 5aaf7a8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
1 change: 1 addition & 0 deletions include/polygeist/Passes/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def AffineOpt : Pass<"affine-opt"> {
let dependentDialects = [
"scf::SCFDialect",
"arith::ArithDialect",
"memref::MemRefDialect",
];
}

Expand Down
17 changes: 10 additions & 7 deletions lib/polygeist/Passes/AffineOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ static void inlineAll(func::CallOp callOp, ModuleOp m = nullptr) {
m = callOp->getParentOfType<ModuleOp>();
auto name = callOp.getCallee();
if (auto funcOp = dyn_cast<func::FuncOp>(m.lookupSymbol(name))) {
funcOp->walk(
[&](func::CallOp nestedCallOp) { inlineAll(nestedCallOp, m); });
if (funcOp->hasAttr(SCOP_STMT_ATTR_NAME))
funcOp->walk(
[&](func::CallOp nestedCallOp) { inlineAll(nestedCallOp, m); });
alwaysInlineCall(callOp);
} else {
llvm::errs() << "Unexpected call to non-FuncOp\n";
Expand Down Expand Up @@ -166,6 +167,7 @@ void AffineOptPass::runOnOperation() {
mlir::func::CallOp call = pair.second;
// Reg2Mem
polymer::separateAffineIfBlocks(f, b);
polymer::demoteLoopReduction(f, b);
polymer::demoteRegisterToMemory(f, b);
// Extract scop stmt
polymer::replaceUsesByStored(f, b);
Expand All @@ -179,16 +181,17 @@ void AffineOptPass::runOnOperation() {
signalPassFailure();
return;
}
if (mlir::func::FuncOp g = polymer::plutoTransform(f, b, "")) {
mlir::func::FuncOp g = nullptr;
if ((g = polymer::plutoTransform(f, b, ""))) {
g.setPublic();
g->setAttrs(f->getAttrs());

g.setName(f.getName());
f.erase();
inlineAll(call);
if (true) {
polymer::plutoParallelize(g, b);
}
}
inlineAll(call);
if (g && /*options.parallelize=*/true) {
polymer::plutoParallelize(g, b);
}
}
}
Expand Down
32 changes: 27 additions & 5 deletions tools/polymer/lib/Support/ScopStmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,35 @@ void ScopStmt::getAccessMapAndMemRef(mlir::Operation *op,
IRMapping argMap;
impl->getArgsValueMapping(argMap);

// TODO: assert op is in the callee.
affine::MemRefAccess access(op);
SmallVector<Value, 4> indices;
if (auto loadOp = dyn_cast<affine::AffineReadOpInterface>(op)) {
*memref = loadOp.getMemRef();
llvm::append_range(indices, loadOp.getMapOperands());
} else {
assert(isa<affine::AffineWriteOpInterface>(op) &&
"Affine read/write op expected");
auto storeOp = cast<affine::AffineWriteOpInterface>(op);
*memref = storeOp.getMemRef();
llvm::append_range(indices, storeOp.getMapOperands());
}

// Get affine map from AffineLoad/Store.
AffineMap map;
if (auto loadOp = dyn_cast<affine::AffineReadOpInterface>(op))
map = loadOp.getAffineMap();
else
map = cast<affine::AffineWriteOpInterface>(op).getAffineMap();

// Collect the access affine::AffineValueMap that binds to operands in the
// callee.
affine::AffineValueMap aMap;
access.getAccessMap(&aMap);

// SmallVector<Value, 8> operands2(indices.begin(), indices.end());
// affine::fullyComposeAffineMapAndOperands(&map, &operands2);
// map = simplifyAffineMap(map);
// affine::canonicalizeMapAndOperands(&map, &operands2);
aMap.reset(map, indices);

// TODO: assert op is in the callee.
affine::MemRefAccess access(op);

// Replace its operands by what the caller uses.
SmallVector<mlir::Value, 8> operands;
Expand Down
10 changes: 6 additions & 4 deletions tools/polymer/lib/Transforms/Reg2Mem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,9 @@ cloneAffineForWithoutIterArgs(mlir::affine::AffineForOp forOp, OpBuilder &b) {
return newForOp;
}

static void demoteLoopReduction(mlir::func::FuncOp f,
mlir::affine::AffineForOp forOp, OpBuilder &b) {
namespace polymer {
void demoteLoopReduction(mlir::func::FuncOp f, mlir::affine::AffineForOp forOp,
OpBuilder &b) {
SmallVector<mlir::Value, 4> initVals{forOp.getInits()};
mlir::Block *body = forOp.getBody();
mlir::affine::AffineYieldOp yieldOp = findYieldOp(forOp);
Expand All @@ -546,13 +547,14 @@ static void demoteLoopReduction(mlir::func::FuncOp f,
forOp.erase();
}

static void demoteLoopReduction(mlir::func::FuncOp f, OpBuilder &b) {
void demoteLoopReduction(mlir::func::FuncOp f, OpBuilder &b) {
SmallVector<mlir::affine::AffineForOp, 4> forOps;
findReductionLoops(f, forOps);

for (mlir::affine::AffineForOp forOp : forOps)
demoteLoopReduction(f, forOp, b);
}
} // namespace polymer

class DemoteLoopReductionPass
: public mlir::PassWrapper<DemoteLoopReductionPass,
Expand All @@ -562,7 +564,7 @@ class DemoteLoopReductionPass
mlir::func::FuncOp f = getOperation();
OpBuilder b(f.getContext());

demoteLoopReduction(f, b);
polymer::demoteLoopReduction(f, b);
}
};

Expand Down

0 comments on commit 5aaf7a8

Please sign in to comment.