forked from NVIDIA/cuda-quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoopNormalizePatterns.inc
More file actions
187 lines (173 loc) · 8.06 KB
/
Copy pathLoopNormalizePatterns.inc
File metadata and controls
187 lines (173 loc) · 8.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/****************************************************************-*- C++ -*-****
* Copyright (c) 2022 - 2025 NVIDIA Corporation & Affiliates. *
* All rights reserved. *
* *
* This source code and the accompanying materials are made available under *
* the terms of the Apache License 2.0 which accompanies this distribution. *
******************************************************************************/
// These loop normalization patterns are used by the cc-loop-normalize pass
// and cc-loop-unroll pass
// This file must be included after a `using namespace mlir;` as it uses bare
// identifiers from that namespace.
// Return true if \p loop is not monotonic or it is an invariant loop.
// Normalization is to be done on any loop that is monotonic and not invariant
// (which includes loops that are already in counted form).
static bool isNotMonotonicOrInvariant(cudaq::cc::LoopOp loop,
bool allowClosedInterval,
bool allowEarlyExit) {
cudaq::opt::LoopComponents c;
return !cudaq::opt::isaMonotonicLoop(loop, allowEarlyExit, &c) ||
(cudaq::opt::isaInvariantLoop(c, allowClosedInterval) &&
!c.isLinearExpr());
}
namespace {
class LoopPat : public OpRewritePattern<cudaq::cc::LoopOp> {
public:
explicit LoopPat(MLIRContext *ctx, bool aci, bool ab)
: OpRewritePattern(ctx), allowClosedInterval(aci), allowEarlyExit(ab) {}
LogicalResult matchAndRewrite(cudaq::cc::LoopOp loop,
PatternRewriter &rewriter) const override {
if (loop->hasAttr(cudaq::opt::NormalizedLoopAttr) ||
loop->hasAttr(cudaq::opt::DeadLoopAttr))
return failure();
if (isNotMonotonicOrInvariant(loop, allowClosedInterval, allowEarlyExit))
return failure();
// loop is monotonic but not invariant.
LLVM_DEBUG(llvm::dbgs() << "loop before normalization: " << loop << '\n');
auto componentsOpt = cudaq::opt::getLoopComponents(loop);
assert(componentsOpt && "loop must have components");
auto c = *componentsOpt;
if (c.hasAlwaysTrueCondition()) {
loop->emitWarning("Loop condition is always true. This loop is not "
"supported in a kernel.");
return failure();
}
if (c.hasAlwaysFalseCondition()) {
rewriter.startOpModification(loop);
rewriter.replaceOpWithNewOp<arith::ConstantIntOp>(c.compareOp, 0, 1);
loop->setAttr(cudaq::opt::DeadLoopAttr, rewriter.getUnitAttr());
rewriter.finalizeOpModification(loop);
return success();
}
auto loc = loop.getLoc();
// 1) Set initial value to 0.
auto ty = c.initialValue.getType();
rewriter.startOpModification(loop);
auto createConstantOp = [&](std::int64_t val) -> Value {
return arith::ConstantIntOp::create(rewriter, loc, ty, val);
};
auto zero = createConstantOp(0);
loop->setOperand(c.induction, zero);
// 2) Compute the number of iterations as an invariant. `iterations = max(0,
// (upper - lower + step) / step)`.
Value upper = c.compareValue;
auto one = createConstantOp(1);
Value step = c.stepValue;
Value lower = c.initialValue;
if (!c.stepIsAnAddOp())
step = arith::SubIOp::create(rewriter, loc, zero, step);
if (c.isLinearExpr()) {
// Induction is part of a linear expression. Deal with the terms of the
// equation. `m` scales the step. `b` is an addend to the lower bound.
if (c.addendValue) {
if (c.negatedAddend) {
// `m * i - b`, u += `b`.
upper = arith::AddIOp::create(rewriter, loc, upper, c.addendValue);
} else {
// `m * i + b`, u -= `b`.
upper = arith::SubIOp::create(rewriter, loc, upper, c.addendValue);
}
}
if (c.minusOneMult) {
// `b - m * i` (b eliminated), multiply lower and step by `-1` (`m`
// follows).
auto negOne = createConstantOp(-1);
lower = arith::MulIOp::create(rewriter, loc, lower, negOne);
step = arith::MulIOp::create(rewriter, loc, step, negOne);
}
if (c.scaleValue) {
if (c.reciprocalScale) {
// `1/m * i + b` (b eliminated), multiply upper by `m`.
upper = arith::MulIOp::create(rewriter, loc, upper, c.scaleValue);
} else {
// `m * i + b` (b eliminated), multiple lower and step by `m`.
lower = arith::MulIOp::create(rewriter, loc, lower, c.scaleValue);
step = arith::MulIOp::create(rewriter, loc, step, c.scaleValue);
}
}
}
if (!c.isClosedIntervalForm()) {
// Note: treating the step as a signed value to process countdown loops as
// well as countup loops.
Value negStepCond = arith::CmpIOp::create(
rewriter, loc, arith::CmpIPredicate::slt, step, zero);
auto negOne = createConstantOp(-1);
Value adj =
arith::SelectOp::create(rewriter, loc, ty, negStepCond, negOne, one);
upper = arith::SubIOp::create(rewriter, loc, upper, adj);
}
Value diff = arith::SubIOp::create(rewriter, loc, upper, lower);
Value disp = arith::AddIOp::create(rewriter, loc, diff, step);
auto cmpOp = cast<arith::CmpIOp>(c.compareOp);
Value newUpper = arith::DivSIOp::create(rewriter, loc, disp, step);
if (cudaq::opt::isSignedPredicate(cmpOp.getPredicate())) {
Value noLoopCond = arith::CmpIOp::create(
rewriter, loc, arith::CmpIPredicate::sgt, newUpper, zero);
newUpper = arith::SelectOp::create(rewriter, loc, ty, noLoopCond,
newUpper, zero);
}
// 3) Rewrite the comparison (!=) and step operations (+1).
Value v1 = c.getCompareInduction();
rewriter.setInsertionPoint(cmpOp);
Value newCmp = arith::CmpIOp::create(
rewriter, cmpOp.getLoc(), arith::CmpIPredicate::ne, v1, newUpper);
cmpOp->replaceAllUsesWith(ValueRange{newCmp});
auto v2 = c.stepOp->getOperand(
c.stepIsAnAddOp() && c.shouldCommuteStepOp() ? 1 : 0);
rewriter.setInsertionPoint(c.stepOp);
auto newStep = arith::AddIOp::create(rewriter, c.stepOp->getLoc(), v2, one);
c.stepOp->replaceAllUsesWith(ValueRange{newStep.getResult()});
// 4) Compute original induction value as a loop variant and replace the
// uses. `lower + step * i`. Careful to not replace the new induction.
if (!loop.getBodyRegion().empty()) {
Block *entry = &loop.getBodyRegion().front();
rewriter.setInsertionPointToStart(entry);
Value induct = entry->getArgument(c.induction);
auto mul = arith::MulIOp::create(rewriter, loc, induct, c.stepValue);
auto newInd = [&]() -> Value {
if (c.stepIsAnAddOp())
return arith::AddIOp::create(rewriter, loc, c.initialValue, mul);
return arith::SubIOp::create(rewriter, loc, c.initialValue, mul);
}();
induct.replaceUsesWithIf(newInd, [&](OpOperand &opnd) {
auto *op = opnd.getOwner();
return op != newStep.getOperation() && op != mul &&
!isa<cudaq::cc::ContinueOp>(op);
});
}
// 5) Back-convert the loop's induction-position result for external uses
if (c.induction < loop.getNumResults()) {
Value loopResult = loop.getResult(c.induction);
if (!loopResult.use_empty()) {
rewriter.setInsertionPointAfter(loop);
auto mulRes =
arith::MulIOp::create(rewriter, loc, loopResult, c.stepValue);
Value recovered;
if (c.stepIsAnAddOp())
recovered =
arith::AddIOp::create(rewriter, loc, c.initialValue, mulRes);
else
recovered =
arith::SubIOp::create(rewriter, loc, c.initialValue, mulRes);
loopResult.replaceAllUsesExcept(recovered, mulRes.getOperation());
}
}
loop->setAttr(cudaq::opt::NormalizedLoopAttr, rewriter.getUnitAttr());
rewriter.finalizeOpModification(loop);
LLVM_DEBUG(llvm::dbgs() << "loop after normalization: " << loop << '\n');
return success();
}
bool allowClosedInterval;
bool allowEarlyExit;
};
} // namespace