Skip to content

Commit 266a5a9

Browse files
authored
mlir/Presburger: optimize to avoid creating copies (llvm#97897)
Optimize the Presburger library to avoid unnecessarily creating copies. While at it, fix some other minor issues in the codebase.
1 parent 148d907 commit 266a5a9

File tree

9 files changed

+123
-111
lines changed

9 files changed

+123
-111
lines changed

mlir/include/mlir/Analysis/Presburger/QuasiPolynomial.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ namespace presburger {
3636
// g_{ij} : Q^n -> Q are affine functionals.
3737
class QuasiPolynomial : public PresburgerSpace {
3838
public:
39-
QuasiPolynomial(unsigned numVars, SmallVector<Fraction> coeffs = {},
40-
std::vector<std::vector<SmallVector<Fraction>>> aff = {});
39+
QuasiPolynomial(unsigned numVars, ArrayRef<Fraction> coeffs = {},
40+
ArrayRef<std::vector<SmallVector<Fraction>>> aff = {});
4141

42-
QuasiPolynomial(unsigned numVars, Fraction constant);
42+
QuasiPolynomial(unsigned numVars, const Fraction &constant);
4343

4444
// Find the number of inputs (numDomain) to the polynomial.
4545
// numSymbols is set to zero.
@@ -57,7 +57,7 @@ class QuasiPolynomial : public PresburgerSpace {
5757
QuasiPolynomial operator+(const QuasiPolynomial &x) const;
5858
QuasiPolynomial operator-(const QuasiPolynomial &x) const;
5959
QuasiPolynomial operator*(const QuasiPolynomial &x) const;
60-
QuasiPolynomial operator/(const Fraction x) const;
60+
QuasiPolynomial operator/(const Fraction &x) const;
6161

6262
// Removes terms which evaluate to zero from the expression
6363
// and folds affine functions which are constant into the
@@ -77,4 +77,4 @@ class QuasiPolynomial : public PresburgerSpace {
7777
} // namespace presburger
7878
} // namespace mlir
7979

80-
#endif // MLIR_ANALYSIS_PRESBURGER_QUASIPOLYNOMIAL_H
80+
#endif // MLIR_ANALYSIS_PRESBURGER_QUASIPOLYNOMIAL_H

mlir/lib/Analysis/Presburger/Barvinok.cpp

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ mlir::presburger::detail::computePolytopeGeneratingFunction(
361361
continue;
362362
// If this subset corresponds to a vertex that has not been considered,
363363
// store it.
364-
vertices.push_back(*vertex);
364+
vertices.emplace_back(*vertex);
365365

366366
// If a vertex is formed by the intersection of more than d facets, we
367367
// assume that any d-subset of these facets can be solved to obtain its
@@ -472,10 +472,10 @@ mlir::presburger::detail::computePolytopeGeneratingFunction(
472472
Point mlir::presburger::detail::getNonOrthogonalVector(
473473
ArrayRef<Point> vectors) {
474474
unsigned dim = vectors[0].size();
475-
assert(
476-
llvm::all_of(vectors,
477-
[&](const Point &vector) { return vector.size() == dim; }) &&
478-
"all vectors need to be the same size!");
475+
assert(llvm::all_of(
476+
vectors,
477+
[&dim](const Point &vector) { return vector.size() == dim; }) &&
478+
"all vectors need to be the same size!");
479479

480480
SmallVector<Fraction> newPoint = {Fraction(1, 1)};
481481
Fraction maxDisallowedValue = -Fraction(1, 0),
@@ -493,7 +493,7 @@ Point mlir::presburger::detail::getNonOrthogonalVector(
493493
// Find the biggest such value
494494
maxDisallowedValue = std::max(maxDisallowedValue, disallowedValue);
495495
}
496-
newPoint.push_back(maxDisallowedValue + 1);
496+
newPoint.emplace_back(maxDisallowedValue + 1);
497497
}
498498
return newPoint;
499499
}
@@ -519,19 +519,20 @@ QuasiPolynomial mlir::presburger::detail::getCoefficientInRationalFunction(
519519
unsigned numParam = num[0].getNumInputs();
520520
// We use the `isEqual` method of PresburgerSpace, which QuasiPolynomial
521521
// inherits from.
522-
assert(
523-
llvm::all_of(
524-
num, [&](const QuasiPolynomial &qp) { return num[0].isEqual(qp); }) &&
525-
"the quasipolynomials should all belong to the same space!");
522+
assert(llvm::all_of(num,
523+
[&num](const QuasiPolynomial &qp) {
524+
return num[0].isEqual(qp);
525+
}) &&
526+
"the quasipolynomials should all belong to the same space!");
526527

527528
std::vector<QuasiPolynomial> coefficients;
528529
coefficients.reserve(power + 1);
529530

530-
coefficients.push_back(num[0] / den[0]);
531+
coefficients.emplace_back(num[0] / den[0]);
531532
for (unsigned i = 1; i <= power; ++i) {
532533
// If the power is not there in the numerator, the coefficient is zero.
533-
coefficients.push_back(i < num.size() ? num[i]
534-
: QuasiPolynomial(numParam, 0));
534+
coefficients.emplace_back(i < num.size() ? num[i]
535+
: QuasiPolynomial(numParam, 0));
535536

536537
// After den.size(), the coefficients are zero, so we stop
537538
// subtracting at that point (if it is less than i).
@@ -573,15 +574,15 @@ substituteMuInTerm(unsigned numParams, const ParamPoint &v,
573574
SmallVector<Fraction> coefficients;
574575
coefficients.reserve(numDims);
575576
for (const Point &d : ds)
576-
coefficients.push_back(-dotProduct(mu, d));
577+
coefficients.emplace_back(-dotProduct(mu, d));
577578

578579
// Then, the affine function is a single floor expression, given by the
579580
// corresponding column of v.
580581
ParamPoint vTranspose = v.transpose();
581582
std::vector<std::vector<SmallVector<Fraction>>> affine;
582583
affine.reserve(numDims);
583584
for (unsigned j = 0; j < numDims; ++j)
584-
affine.push_back({SmallVector<Fraction>(vTranspose.getRow(j))});
585+
affine.push_back({SmallVector<Fraction>{vTranspose.getRow(j)}});
585586

586587
QuasiPolynomial num(numParams, coefficients, affine);
587588
num = num.simplify();
@@ -593,7 +594,7 @@ substituteMuInTerm(unsigned numParams, const ParamPoint &v,
593594
for (const Point &d : ds) {
594595
// This term in the denominator is
595596
// (1 - t^dens.back())
596-
dens.push_back(dotProduct(d, mu));
597+
dens.emplace_back(dotProduct(d, mu));
597598
}
598599

599600
return {num, dens};
@@ -641,7 +642,7 @@ std::vector<QuasiPolynomial> getBinomialCoefficients(const QuasiPolynomial &n,
641642
coefficients.emplace_back(numParams, 1);
642643
for (unsigned j = 1; j <= r; ++j)
643644
// We use the recursive formula for binomial coefficients here and below.
644-
coefficients.push_back(
645+
coefficients.emplace_back(
645646
(coefficients[j - 1] * (n - QuasiPolynomial(numParams, j - 1)) /
646647
Fraction(j, 1))
647648
.simplify());
@@ -656,7 +657,7 @@ std::vector<Fraction> getBinomialCoefficients(const Fraction &n,
656657
coefficients.reserve((int64_t)floor(r));
657658
coefficients.emplace_back(1);
658659
for (unsigned j = 1; j <= r; ++j)
659-
coefficients.push_back(coefficients[j - 1] * (n - (j - 1)) / (j));
660+
coefficients.emplace_back(coefficients[j - 1] * (n - (j - 1)) / (j));
660661
return coefficients;
661662
}
662663

@@ -764,8 +765,8 @@ mlir::presburger::detail::computeNumTerms(const GeneratingFunction &gf) {
764765
eachTermDenCoefficients.reserve(r);
765766
for (const Fraction &den : dens) {
766767
singleTermDenCoefficients = getBinomialCoefficients(den + 1, den + 1);
767-
eachTermDenCoefficients.push_back(
768-
ArrayRef<Fraction>(singleTermDenCoefficients).slice(1));
768+
eachTermDenCoefficients.emplace_back(
769+
ArrayRef<Fraction>(singleTermDenCoefficients).drop_front());
769770
}
770771

771772
// Now we find the coefficients in Q(s) itself

mlir/lib/Analysis/Presburger/IntegerRelation.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -511,10 +511,10 @@ void IntegerRelation::getLowerAndUpperBoundIndices(
511511
continue;
512512
if (atIneq(r, pos) >= 1) {
513513
// Lower bound.
514-
lbIndices->push_back(r);
514+
lbIndices->emplace_back(r);
515515
} else if (atIneq(r, pos) <= -1) {
516516
// Upper bound.
517-
ubIndices->push_back(r);
517+
ubIndices->emplace_back(r);
518518
}
519519
}
520520

@@ -528,7 +528,7 @@ void IntegerRelation::getLowerAndUpperBoundIndices(
528528
continue;
529529
if (containsConstraintDependentOnRange(r, /*isEq=*/true))
530530
continue;
531-
eqIndices->push_back(r);
531+
eqIndices->emplace_back(r);
532532
}
533533
}
534534

@@ -791,7 +791,7 @@ IntMatrix IntegerRelation::getBoundedDirections() const {
791791
// processes all the inequalities.
792792
for (unsigned i = 0, e = getNumInequalities(); i < e; ++i) {
793793
if (simplex.isBoundedAlongConstraint(i))
794-
boundedIneqs.push_back(i);
794+
boundedIneqs.emplace_back(i);
795795
}
796796

797797
// The direction vector is given by the coefficients and does not include the
@@ -1981,13 +1981,13 @@ void IntegerRelation::fourierMotzkinEliminate(unsigned pos, bool darkShadow,
19811981
for (unsigned r = 0, e = getNumInequalities(); r < e; r++) {
19821982
if (atIneq(r, pos) == 0) {
19831983
// Var does not appear in bound.
1984-
nbIndices.push_back(r);
1984+
nbIndices.emplace_back(r);
19851985
} else if (atIneq(r, pos) >= 1) {
19861986
// Lower bound.
1987-
lbIndices.push_back(r);
1987+
lbIndices.emplace_back(r);
19881988
} else {
19891989
// Upper bound.
1990-
ubIndices.push_back(r);
1990+
ubIndices.emplace_back(r);
19911991
}
19921992
}
19931993

@@ -2028,8 +2028,8 @@ void IntegerRelation::fourierMotzkinEliminate(unsigned pos, bool darkShadow,
20282028
continue;
20292029
assert(lbCoeff >= 1 && ubCoeff >= 1 && "bounds wrongly identified");
20302030
DynamicAPInt lcm = llvm::lcm(lbCoeff, ubCoeff);
2031-
ineq.push_back(atIneq(ubPos, l) * (lcm / ubCoeff) +
2032-
atIneq(lbPos, l) * (lcm / lbCoeff));
2031+
ineq.emplace_back(atIneq(ubPos, l) * (lcm / ubCoeff) +
2032+
atIneq(lbPos, l) * (lcm / lbCoeff));
20332033
assert(lcm > 0 && "lcm should be positive!");
20342034
if (lcm != 1)
20352035
allLCMsAreOne = false;
@@ -2057,7 +2057,7 @@ void IntegerRelation::fourierMotzkinEliminate(unsigned pos, bool darkShadow,
20572057
for (unsigned l = 0, e = getNumCols(); l < e; l++) {
20582058
if (l == pos)
20592059
continue;
2060-
ineq.push_back(atIneq(nbPos, l));
2060+
ineq.emplace_back(atIneq(nbPos, l));
20612061
}
20622062
newRel.addInequality(ineq);
20632063
}
@@ -2072,7 +2072,7 @@ void IntegerRelation::fourierMotzkinEliminate(unsigned pos, bool darkShadow,
20722072
for (unsigned l = 0, e = getNumCols(); l < e; l++) {
20732073
if (l == pos)
20742074
continue;
2075-
eq.push_back(atEq(r, l));
2075+
eq.emplace_back(atEq(r, l));
20762076
}
20772077
newRel.addEquality(eq);
20782078
}
@@ -2264,8 +2264,8 @@ IntegerRelation::unionBoundingBox(const IntegerRelation &otherCst) {
22642264
std::negate<DynamicAPInt>());
22652265
std::copy(maxUb.begin(), maxUb.end(), newUb.begin() + getNumDimVars());
22662266

2267-
boundingLbs.push_back(newLb);
2268-
boundingUbs.push_back(newUb);
2267+
boundingLbs.emplace_back(newLb);
2268+
boundingUbs.emplace_back(newUb);
22692269
}
22702270

22712271
// Clear all constraints and add the lower/upper bounds for the bounding box.
@@ -2309,7 +2309,7 @@ static void getIndependentConstraints(const IntegerRelation &cst, unsigned pos,
23092309
break;
23102310
}
23112311
if (c == pos + num)
2312-
nbIneqIndices.push_back(r);
2312+
nbIneqIndices.emplace_back(r);
23132313
}
23142314

23152315
for (unsigned r = 0, e = cst.getNumEqualities(); r < e; r++) {
@@ -2320,7 +2320,7 @@ static void getIndependentConstraints(const IntegerRelation &cst, unsigned pos,
23202320
break;
23212321
}
23222322
if (c == pos + num)
2323-
nbEqIndices.push_back(r);
2323+
nbEqIndices.emplace_back(r);
23242324
}
23252325
}
23262326

mlir/lib/Analysis/Presburger/LinearTransform.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ IntegerRelation LinearTransform::applyTo(const IntegerRelation &rel) const {
5151
const DynamicAPInt &c = eq.back();
5252

5353
SmallVector<DynamicAPInt, 8> newEq = preMultiplyWithRow(eq.drop_back());
54-
newEq.push_back(c);
54+
newEq.emplace_back(c);
5555
result.addEquality(newEq);
5656
}
5757

@@ -61,7 +61,7 @@ IntegerRelation LinearTransform::applyTo(const IntegerRelation &rel) const {
6161
const DynamicAPInt &c = ineq.back();
6262

6363
SmallVector<DynamicAPInt, 8> newIneq = preMultiplyWithRow(ineq.drop_back());
64-
newIneq.push_back(c);
64+
newIneq.emplace_back(c);
6565
result.addInequality(newIneq);
6666
}
6767

mlir/lib/Analysis/Presburger/PWMAFunction.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static SmallVector<DynamicAPInt, 8> subtractExprs(ArrayRef<DynamicAPInt> vecA,
4646
SmallVector<DynamicAPInt, 8> result;
4747
result.reserve(vecA.size());
4848
for (unsigned i = 0, e = vecA.size(); i < e; ++i)
49-
result.push_back(vecA[i] - vecB[i]);
49+
result.emplace_back(vecA[i] - vecB[i]);
5050
return result;
5151
}
5252

@@ -78,7 +78,7 @@ MultiAffineFunction::valueAt(ArrayRef<DynamicAPInt> point) const {
7878
// function of; we have computed one possible set of values and use them here.
7979
pointHomogenous.reserve(pointHomogenous.size() + divValues.size());
8080
for (const std::optional<DynamicAPInt> &divVal : divValues)
81-
pointHomogenous.push_back(*divVal);
81+
pointHomogenous.emplace_back(*divVal);
8282
// The matrix `output` has an affine expression in the ith row, corresponding
8383
// to the expression for the ith value in the output vector. The last column
8484
// of the matrix contains the constant term. Let v be the input point with
@@ -295,7 +295,7 @@ void PWMAFunction::addPiece(const Piece &piece) {
295295
assert(piece.isConsistent() && "Piece should be consistent");
296296
assert(piece.domain.intersect(getDomain()).isIntegerEmpty() &&
297297
"Piece should be disjoint from the function");
298-
pieces.push_back(piece);
298+
pieces.emplace_back(piece);
299299
}
300300

301301
void PWMAFunction::print(raw_ostream &os) const {

0 commit comments

Comments
 (0)