Skip to content

Commit 4bef66b

Browse files
wsmosesivanradanov
andauthored
Cleanup Mem2Reg and GPU (#204)
* Upgrade mem2reg * Fix test * Correct barrier elimination * Fix affine raising * Fix affine raise * Fix affine raising * Lowering update * Simplify affine and whilelicm * Fix affine raise * Fix infinite loop in memset/cpy to load/store * Optionally disable loop unroll * Fix cmpi bug * Fix allocation location * Add memcpy behavior * Properly handle continue * Do not duplicate already cached values crossing a barrier * Mem2reg and licm with affine * Fix unused scf inductive var * Fix parallel affine raise * Remove print * Partially speed up mem2reg * Reduce max unroll size to 32 * Add rank reduction * Disable RankReduction, fix for InductiveVarRemoval * Work around affineexpr bug * Improve ordering of loop distribute * Improve if reg2mem * Make cacheload * Fix reg2mem for (2) * Skip recursive load/store * Now ignoring barriers * No unnecessary internal store forwarding * Attempt fix * Add inner serialize * Fix set operands * Fix infinite loop * Remove unnecessary yield * Fix inner serialize * Do not use cacheload when not mincut * Fix recompute threshold * Strengthen alias analysis * No infinite loop * Add single execution query * Fix bug * Fix compile bug * First pass fix tests * Bump LLVM * Cleanup polygeist opt tests * Fix tests * Fix format Co-authored-by: Ivan Radanov Ivanov <[email protected]>
1 parent 745d684 commit 4bef66b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+3645
-1797
lines changed

include/polygeist/Ops.h

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,122 @@
1818
#define GET_OP_CLASSES
1919
#include "polygeist/PolygeistOps.h.inc"
2020

21+
#include "mlir/Dialect/Affine/IR/AffineOps.h"
22+
#include "mlir/Dialect/SCF/SCF.h"
23+
#include "mlir/IR/Matchers.h"
24+
#include "mlir/IR/PatternMatch.h"
25+
#include "llvm/Support/CommandLine.h"
26+
27+
bool getEffectsBefore(
28+
mlir::Operation *op,
29+
llvm::SmallVectorImpl<mlir::MemoryEffects::EffectInstance> &effects,
30+
bool stopAtBarrier);
31+
32+
bool getEffectsAfter(
33+
mlir::Operation *op,
34+
llvm::SmallVectorImpl<mlir::MemoryEffects::EffectInstance> &effects,
35+
bool stopAtBarrier);
36+
37+
bool isReadOnly(mlir::Operation *);
38+
bool isReadNone(mlir::Operation *);
39+
40+
bool mayReadFrom(mlir::Operation *, mlir::Value);
41+
bool mayWriteTo(mlir::Operation *, mlir::Value, bool ignoreBarrier = false);
42+
43+
bool mayAlias(mlir::MemoryEffects::EffectInstance a,
44+
mlir::MemoryEffects::EffectInstance b);
45+
46+
bool mayAlias(mlir::MemoryEffects::EffectInstance a, mlir::Value b);
47+
48+
extern llvm::cl::opt<bool> BarrierOpt;
49+
50+
template <bool NotTopLevel = false>
51+
class BarrierElim final
52+
: public mlir::OpRewritePattern<mlir::polygeist::BarrierOp> {
53+
public:
54+
using mlir::OpRewritePattern<mlir::polygeist::BarrierOp>::OpRewritePattern;
55+
mlir::LogicalResult
56+
matchAndRewrite(mlir::polygeist::BarrierOp barrier,
57+
mlir::PatternRewriter &rewriter) const override {
58+
using namespace mlir;
59+
using namespace polygeist;
60+
if (!BarrierOpt)
61+
return failure();
62+
// Remove if it only sync's constant indices.
63+
if (llvm::all_of(barrier.getOperands(), [](mlir::Value v) {
64+
IntegerAttr constValue;
65+
return matchPattern(v, m_Constant(&constValue));
66+
})) {
67+
rewriter.eraseOp(barrier);
68+
return success();
69+
}
70+
71+
Operation *op = barrier;
72+
if (NotTopLevel && isa<mlir::scf::ParallelOp, mlir::AffineParallelOp>(
73+
barrier->getParentOp()))
74+
return failure();
75+
76+
{
77+
SmallVector<MemoryEffects::EffectInstance> beforeEffects;
78+
getEffectsBefore(op, beforeEffects, /*stopAtBarrier*/ true);
79+
80+
SmallVector<MemoryEffects::EffectInstance> afterEffects;
81+
getEffectsAfter(op, afterEffects, /*stopAtBarrier*/ false);
82+
83+
bool conflict = false;
84+
for (auto before : beforeEffects)
85+
for (auto after : afterEffects) {
86+
if (mayAlias(before, after)) {
87+
// Read, read is okay
88+
if (isa<MemoryEffects::Read>(before.getEffect()) &&
89+
isa<MemoryEffects::Read>(after.getEffect())) {
90+
continue;
91+
}
92+
93+
// Write, write is not okay because may be different offsets and the
94+
// later must subsume other conflicts are invalid.
95+
conflict = true;
96+
break;
97+
}
98+
}
99+
100+
if (!conflict) {
101+
rewriter.eraseOp(barrier);
102+
return success();
103+
}
104+
}
105+
106+
{
107+
SmallVector<MemoryEffects::EffectInstance> beforeEffects;
108+
getEffectsBefore(op, beforeEffects, /*stopAtBarrier*/ false);
109+
110+
SmallVector<MemoryEffects::EffectInstance> afterEffects;
111+
getEffectsAfter(op, afterEffects, /*stopAtBarrier*/ true);
112+
113+
bool conflict = false;
114+
for (auto before : beforeEffects)
115+
for (auto after : afterEffects) {
116+
if (mayAlias(before, after)) {
117+
// Read, read is okay
118+
if (isa<MemoryEffects::Read>(before.getEffect()) &&
119+
isa<MemoryEffects::Read>(after.getEffect())) {
120+
continue;
121+
}
122+
// Write, write is not okay because may be different offsets and the
123+
// later must subsume other conflicts are invalid.
124+
conflict = true;
125+
break;
126+
}
127+
}
128+
129+
if (!conflict) {
130+
rewriter.eraseOp(barrier);
131+
return success();
132+
}
133+
}
134+
135+
return failure();
136+
}
137+
};
138+
21139
#endif // BFV_BFVOPS_H

include/polygeist/Passes/Passes.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
#include "mlir/Pass/Pass.h"
66
#include <memory>
77
namespace mlir {
8+
class PatternRewriter;
9+
class DominanceInfo;
810
namespace polygeist {
911
std::unique_ptr<Pass> createParallelLICMPass();
1012
std::unique_ptr<Pass> createMem2RegPass();
1113
std::unique_ptr<Pass> createLoopRestructurePass();
14+
std::unique_ptr<Pass> createInnerSerializationPass();
1215
std::unique_ptr<Pass> replaceAffineCFGPass();
1316
std::unique_ptr<Pass> createOpenMPOptPass();
1417
std::unique_ptr<Pass> createCanonicalizeForPass();
@@ -26,8 +29,8 @@ std::unique_ptr<Pass> createConvertPolygeistToLLVMPass();
2629
} // namespace mlir
2730

2831
void fully2ComposeAffineMapAndOperands(
29-
mlir::OpBuilder &, mlir::AffineMap *map,
30-
llvm::SmallVectorImpl<mlir::Value> *operands);
32+
mlir::PatternRewriter &rewriter, mlir::AffineMap *map,
33+
llvm::SmallVectorImpl<mlir::Value> *operands, mlir::DominanceInfo &DI);
3134
bool isValidIndex(mlir::Value val);
3235

3336
namespace mlir {

include/polygeist/Passes/Passes.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ def SCFCPUify : Pass<"cpuify"> {
3535
];
3636
}
3737

38+
def InnerSerialization : Pass<"inner-serialize"> {
39+
let summary = "remove scf.barrier";
40+
let constructor = "mlir::polygeist::createInnerSerializationPass()";
41+
let dependentDialects =
42+
["memref::MemRefDialect", "func::FuncDialect", "LLVM::LLVMDialect"];
43+
}
44+
3845
def SCFBarrierRemovalContinuation : InterfacePass<"barrier-removal-continuation", "FunctionOpInterface"> {
3946
let summary = "Remove scf.barrier using continuations";
4047
let constructor = "mlir::polygeist::createBarrierRemovalContinuation()";

0 commit comments

Comments
 (0)