282 Commits

Author SHA1 Message Date
Mehdi Amini
6c7a3f80e7
Fix LLVM_ENABLE_ABI_BREAKING_CHECKS macro check: use #if instead of #ifdef (#110938)
This macros is always defined: either 0 or 1. The correct pattern is to
use #if.

Re-apply #110185 with more fixes for debug build with the ABI breaking
checks disabled.
2024-10-03 01:24:14 +02:00
Amy Wang
2740273505
[MLIR][Presburger] Make printing aligned to assist in debugging (#107648)
Hello Arjun! Please allow me to contribute this patch as it helps me
debugging significantly! When the 1's and 0's don't line up when
debugging farkas lemma of numerous polyhedrons using simplex lexmin
solver, it is truly straining on the eyes. Hopefully this patch can help
others!

The unfortunate part is the lack of testcase as I'm not sure how to add
testcase for debug dumps. :) However, you can add this testcase to the
SimplexTest.cpp to witness the nice printing!

```c++
TEST(SimplexTest, DumpTest) {
  int COLUMNS = 2;
  int ROWS = 2;
  LexSimplex simplex(COLUMNS * 2);
  IntMatrix m1(ROWS, COLUMNS * 2 + 1);
  // Adding LHS columns.
  for (int i = 0; i < ROWS; i++) {
    // an arbitrary formula to test all kinds of integers
    for (int j = 0; j < COLUMNS; j++) 
      m1(i, j) = i + (2 << (i % 3)) * (-1 * ((i + j) % 2));
  }
  // Adding RHS columns.
  for (int i = 0; i < ROWS; i++) {
    for (int j = 0; j < COLUMNS; j++)
      m1(i, j + COLUMNS) = j - (3 << (j % 4)) * (-1 * ((i + j * 2) % 2));
  }
  for (int i = 0; i < m1.getNumRows(); i++) {
    ArrayRef<DynamicAPInt> curRow = m1.getRow(i);
    simplex.addInequality(curRow);
  }
  IntegerRelation rel =
      parseRelationFromSet("(x, y, z)[] : (z - x - 17 * y == 0, x - 11 * z >= 1)",2);
  simplex.dump();
  m1.dump();
  rel.dump();
}
```

```
rows = 2, columns = 7
var: c3, c4, c5, c6
con: r0 [>=0], r1 [>=0]
r0: -1, r1: -2
c0: denom, c1: const, c2: 2147483647, c3: 0, c4: 1, c5: 2, c6: 3
  1  0  1  0 -2  0  1
  1  0 -8 -3  1  3  7

  0 -2  0  1  0
 -3  1  3  7  0
Domain: 2, Range: 1, Symbols: 0, Locals: 0
2 constraints
 -1  -17  1   0   = 0
  1   0  -11 -1  >= 0

```
2024-09-11 23:22:54 -04:00
Kazu Hirata
b2dbcf4dc1
[Presburger] Avoid repeated hash lookups (NFC) (#107426) 2024-09-05 11:43:19 -07:00
Kazu Hirata
165f45354a
[mlir] Use llvm::is_contained (NFC) (#102714) 2024-08-09 21:42:19 -07:00
Kazu Hirata
5262865aac
[mlir] Construct SmallVector with ArrayRef (NFC) (#101896) 2024-08-04 11:43:05 -07:00
Ramkumar Ramachandra
266a5a9cb9
mlir/Presburger: optimize to avoid creating copies (#97897)
Optimize the Presburger library to avoid unnecessarily creating copies.
While at it, fix some other minor issues in the codebase.
2024-07-15 19:42:27 +01:00
Ramkumar Ramachandra
64740edac8
mlir/Presburger: optimize normalizeDiv when gcd=1 (#97893) 2024-07-08 08:28:53 +01:00
Ramkumar Ramachandra
f819302a09
mlir/Presburger: reinstate use of LogicalResult (#97415)
Follow up on a desire post-landing d0fee98 (mlir/Presburger: strip
dependency on MLIRSupport) to reinstate the use of LogicalResult in
Presburger. Since db791b2 (mlir/LogicalResult: move into llvm),
LogicalResult is in LLVM, and fulfilling this desire is possible while
still maintaining the goal of stripping the Presburger library of mlir
dependencies.
2024-07-03 10:51:25 +01:00
Ramkumar Ramachandra
d0fee98e0c
mlir/Presburger: strip dependency on MLIRSupport (#96517)
Strip the Presburger library's dependency on the MLIR Support library,
as well as the headers, in the interest of making it leaner.

This patch is part of a project to move the Presburger library into
LLVM.
2024-06-29 12:23:20 +01:00
Jay Foad
d4a0154902
[llvm-project] Fix typo "seperate" (#95373) 2024-06-13 20:20:27 +01:00
Ramkumar Ramachandra
1a0e67d730
Reland "mlir/Presburger/MPInt: move into llvm/ADT" (#95254)
Change: remove guards on debug-printing, to allow Release builds without
LLVM_ENABLE_DUMP to pass.

MPInt is an arbitrary-precision integer library that builds on top of
APInt, and has a fast-path when the number fits within 64 bits. It was
originally written for the Presburger library in MLIR, but seems useful
to the LLVM project in general, independently of the Presburger library
or MLIR. Hence, move it into LLVM/ADT under the name DynamicAPInt.

This patch is part of a project to move the Presburger library into
LLVM.
2024-06-12 18:09:16 +01:00
Maksim Levental
cb5d1b52ad
Revert #95218 and #94953 (#95244) 2024-06-12 08:55:48 -05:00
Ramkumar Ramachandra
76030dc157
mlir/Presburger/MPInt: move into llvm/ADT (#94953)
MPInt is an arbitrary-precision integer library that builds on top of
APInt, and has a fast-path when the number fits within 64 bits. It was
originally written for the Presburger library in MLIR, but seems useful
to the LLVM project in general, independently of the Presburger library
or MLIR. Hence, move it into LLVM/ADT under the name DynamicAPInt.

This patch is part of a project to move the Presburger library into
LLVM.
2024-06-12 09:19:21 +01:00
Bharathi Ramana Joshi
24da7fa029
[MLIR][Presburger] Use Identifiers outside Presburger library (#77316)
The pull request includes the following changes.
1. Refactors the interface to `PresburgerSpace::identifiers` to `setId` and a
const `getId`, instead of previous `getId` which returned a mutable
reference. `resetIds` does not need to be called to use identifiers, `setId`
calls `resetIds` if identifiers are not enabled.
2. Deprecates `FlatAffineRelation` by refactoring all usages of
`FlatAffineRelation` to `IntegerRelation`. To achieve this,
`FlatAffineRelation::compose` is refactored into
`IntegerRelation::mergeAndCompose`.
3. Deletes unneeded overrides of virtual functions `hasConsistentState`,
`clearAndCopyFrom` and `fourierMotzkinEliminate` from
`FlatLinearValueConstraints` as these were only used through
`FlatAffineRelation` and we now use `IntegerRelation`'s member functions
instead.
4. Fixes an existing bug in FlatLinearValueConstraints' constructor
which caused
identifiers set by superclass FlatLinearConstraints' constructor to be
erased.
5. Fixes `IntegerRelation::convertVarKind` not preserving identifiers.
2024-04-18 21:56:53 +05:30
Mehdi Amini
1837579bbc Apply clang-tidy fixes for readability-simplify-boolean-expr in IntegerRelation.cpp (NFC) 2024-03-04 23:15:10 -08:00
Mehdi Amini
1934fc6a0c Apply clang-tidy fixes for readability-container-size-empty in Barvinok.cpp (NFC) 2024-03-04 23:15:10 -08:00
Mehdi Amini
2db8b94138 Apply clang-tidy fixes for performance-unnecessary-value-param in Barvinok.cpp (NFC) 2024-03-04 23:15:10 -08:00
Mehdi Amini
9915418144 Apply clang-tidy fixes for modernize-use-emplace in Barvinok.cpp (NFC) 2024-03-04 23:15:10 -08:00
Mehdi Amini
a36b73e5a7 Apply clang-tidy fixes for modernize-loop-convert in Barvinok.cpp (NFC) 2024-03-04 23:15:10 -08:00
Adrian Kuegel
654e65d3b2 [mlir] Apply ClangTidy performance fix.
Use const reference for loop variable.
2024-02-21 13:17:43 +00:00
Abhinav271828
562790f371
[MLIR][Presburger] Implement vertex enumeration and chamber decomposition for polytope generating function computation. (#78987)
We implement a function to compute the generating function corresponding
to a full-dimensional parametric polytope whose tangent cones are all
unimodular.
We fix a bug in unimodGenFunc to check the absolute value of the index.
We also implement Matrix<T>::negateMatrix() and Matrix<T>::scaleRow for
convenience.
2024-02-15 11:03:32 +05:30
Jie Fu
3c94154c86 [mlir] Fix -Wunused-variable in Barvinok.cpp (NFC)
llvm-project/mlir/lib/Analysis/Presburger/Barvinok.cpp:262:21:
 error: unused variable 'd' [-Werror,-Wunused-variable]
  for (const Point &d : ds)
                    ^
1 error generated.
2024-01-22 20:12:39 +08:00
Abhinav271828
68a5261d26
[MLIR][Presburger] Implement function to evaluate the number of terms in a generating function. (#78078)
We implement `computeNumTerms()`, which counts the number of terms in a
generating function by substituting the unit vector in it.
This is the main function in Barvinok's algorithm – the number of points
in a polytope is given by the number of terms in the generating function
corresponding to it.
We also modify the GeneratingFunction class to have `const` getters and
improve the simplification of QuasiPolynomials.
2024-01-22 14:22:01 +05:30
Bharathi Ramana Joshi
d70bfeb4e1
[MLIR][Presburger] Implement IntegerRelation::setId (#77872) 2024-01-20 15:19:10 +05:30
Benjamin Kramer
9c33a2e9a3 [MLIR][Presburger] Fold loop into assert
This way it doesn't trigger -Wunused-variable when assertions are disabled.
2024-01-13 17:52:58 +01:00
Abhinav271828
850f713e80
[MLIR][Presburger] Helper functions to compute the constant term of a generating function (#77819)
We implement two functions that are needed to compute the constant term
of a GF.
One finds a vector not orthogonal to all the non-null vectors in a given
set.
One computes the coefficient of any term in an arbitrary rational
function (quotient of two polynomials).
2024-01-13 21:30:06 +05:30
Bharathi Ramana Joshi
66786a79d6
[MLIR][Presburger] Implement Matrix::moveColumns (#68362) 2024-01-13 18:51:26 +05:30
Bharathi Ramana Joshi
c39926e679
[MLIR][Presburger] Fix style violations in ff80414 (NFC) (#76720)
Use preincrement not postincrement; use `Identifier::getIds` not
`getVarKindOffset`
2024-01-12 10:36:28 +05:30
Abhinav271828
2dde029df8
[MLIR][Presburger] Implement computation of generating function for unimodular cones (#77235)
We implement a function that computes the generating function
corresponding to a unimodular cone.
The generating function for a polytope is obtained by summing these
generating functions over all tangent cones.
2024-01-11 01:28:36 +05:30
Bharathi Ramana Joshi
3eb9fd8ac8
[MLIR][Presburger] Implement IntegerRelation::mergeAndAlignSymbols (#76736) 2024-01-07 17:06:52 +05:30
Abhinav271828
4c8dbb6813
[MLIR][Presburger] Definitions for basic functions related to cones (#76650)
We add some basic type aliases and function definitions relating to
cones for Barvinok's algorithm.
These include functions to get the dual of a cone and find its index.
2024-01-07 10:30:22 +00:00
Abhinav271828
bd0dc357af
[MLIR][Presburger] Shift GeneratingFunction.h to includes (#77114)
We shift the GeneratingFunction.h header file to the include/ directory
and wrap it in a `detail` namespace.
2024-01-06 17:08:25 +05:30
Adrian Kuegel
b238a0d989 [mlir] Apply ClangTidy findings.
- Remove redundant return
- Use .empty() instead of size() == 0.
2024-01-02 08:53:01 +00:00
Bharathi Ramana Joshi
ff80414620
[MLIR][Presburger] Implement PresburgerSpace::mergeAndAlignSymbols (#76397) 2024-01-01 23:40:57 +05:30
Bharathi Ramana Joshi
b8e4053c06
[MLIR][Presburger] Fix bug in Identifier::isEqual assert (#76380)
Make identifiers::isEqual return false instead of failing assertion when
identifiers are not equal.
2023-12-31 11:02:13 +05:30
Abhinav271828
e213af78b2
[MLIR][Presburger] Fix a bug with determinant of IntMatrix (#76622)
Fixed a bug where IntMatrix determinant() had a bug where it would try to assign to a null
pointer.
Added a test case that triggers this bug to avoid regressions.
2023-12-30 22:03:01 +02:00
Balaji V. Iyer
36fd7291cd
[mlir][Quasipolynomial] Fixed -Wunused-variable in GeneratorFunction.h (#76419)
```
llvm-project/mlir/lib/Analysis/Presburger/GeneratingFunction.h:56:28:
error: unused variable 'term' [-Werror,-Wunused-variable]
   56 |     for (const ParamPoint &term : numerators)
      |                            ^~~~
1 error generated.
```
2023-12-26 19:29:04 -06:00
Jie Fu
c86fe3ee0b [mlir][Quasipolynomials] Fix -Wunused-variable in QuasiPolynomial.cpp (NFC)
llvm-project/mlir/lib/Analysis/Presburger/QuasiPolynomial.cpp:29:39:
 error: unused variable 'aff' [-Werror,-Wunused-variable]
    for (const SmallVector<Fraction> &aff : term) {
                                      ^
1 error generated.
2023-12-27 08:00:58 +08:00
Balaji V. Iyer
532d4845ed
[mlir][Quasipolynomials] Fixed type issues in GeneratorFuunction.h (#76413)
Fixed two issues: A SmallVector size that caused size-differences issue
(8 vs. 12). Thus removed this size restriction. Also a constant
parameter was causing an issue in a function not marked constant.
2023-12-26 16:34:32 -06:00
Abhinav271828
1022febd9d
[MLIR][Presburger] Generating functions and quasi-polynomials for Barvinok's algorithm (#75702)
Define basic types and classes for Barvinok's algorithm, including
polyhedra, generating functions and quasi-polynomials.
The class definitions include methods for arithmetic manipulation,
printing, logical relations, etc.
2023-12-26 21:29:26 +02:00
Abhinav271828
cfd51fbadd
[MLIR][Presburger] Add LLL basis reduction (#75565)
Add a method for LLL basis reduction to the FracMatrix class.
This needs an abs() method for Fractions, which is added to Fraction.h.
2023-12-19 17:31:38 +01:00
Bharathi Ramana Joshi
8d7c979815
[MLIR][Presburger] Fix IntegerRelation::swapVar not swapping identifiers (#74407)
This commit fixes a bug where identifiers were not swapped when doing a
IntegerRelation::swapVar.
2023-12-13 22:47:19 +05:30
Abhinav271828
84ab06ba2f
[MLIR][Presburger] Add Gram-Schmidt (#70843)
Implement Gram-Schmidt orthogonalisation for the FracMatrix class.
This requires dotProduct, which has been added as a util.
2023-12-13 08:28:47 +00:00
Adrian Kuegel
b4c1421466 [mlir] Apply ClangTidy fix
Remove redundant return.
2023-11-17 09:56:07 +00:00
Mehdi Amini
2ee87cd610 [MLIR] Apply clang-tidy fixes for misc-include-cleaner in Presburger library (NFC) 2023-10-31 21:24:57 -07:00
gilsaia
39b939555f
[MLIR][Presburger] Add simplify function (#69107)
Added the simplify function to reduce the size of the constraint system,
referencing the ISL implementation.

Tested it on a simple Benchmark implemented by myself, calling SImplify
before the operation and calling Simplify on the result after Subtract
were tested, respectively.

The Benchmark can be found here:
[benchmark](https://github.com/gilsaia/llvm-project-test-fpl/blob/develop_benchmark/mlir/benchmark/presburger/Benchmark.cpp)

For the case of calling Simplify before each operation, the overall
result is shown in the following figure.

![image](https://github.com/llvm/llvm-project/assets/38588948/7099286e-b9a2-42e0-bc2a-1ed6627ead00)

A comparison of the constraint system sizes and time for each operation
is as follows

![image](https://github.com/llvm/llvm-project/assets/38588948/e5d0e488-f76e-4438-b19e-f6163699c526)

![image](https://github.com/llvm/llvm-project/assets/38588948/119a08de-4ee1-4cde-886c-50a91b502d93)

![image](https://github.com/llvm/llvm-project/assets/38588948/7a8b69ac-6cdb-41ab-9a75-cd016664fa5a)

![image](https://github.com/llvm/llvm-project/assets/38588948/c84b6eb1-62dc-4bae-a771-67d97ebf514a)

![image](https://github.com/llvm/llvm-project/assets/38588948/cdbfa3ed-0155-481e-9273-9d6dba3a2d7b)

![image](https://github.com/llvm/llvm-project/assets/38588948/8c945cff-a0a4-472a-a178-6b6a70a1b16a)

![image](https://github.com/llvm/llvm-project/assets/38588948/0bfe3a2b-3568-4d31-bebf-bd1b3c4e734e)

![image](https://github.com/llvm/llvm-project/assets/38588948/f1a99d56-edf5-45de-a506-512c0584f1d8)

![image](https://github.com/llvm/llvm-project/assets/38588948/ffef3312-6c99-494c-bb52-73aa8df275bb)

![image](https://github.com/llvm/llvm-project/assets/38588948/3e5924a7-8e1f-49d1-bd27-02a2e10a5cc4)

![image](https://github.com/llvm/llvm-project/assets/38588948/cec8be0e-dd19-46fa-88b4-2585d4031c9e)

![image](https://github.com/llvm/llvm-project/assets/38588948/3cb68e89-82c7-4cd2-b6bc-70f15e495ce8)

For the case of calling Simplify on the result after Subtract, the
overall results are as follows

![image](https://github.com/llvm/llvm-project/assets/38588948/be5b9c50-7417-42c8-abbf-8a50f093c3f5)

A comparison of the constraint system sizes and time for subtract is as
follows

![image](https://github.com/llvm/llvm-project/assets/38588948/fafe10ba-f8bd-43cd-b281-aaebf09af0af)

![image](https://github.com/llvm/llvm-project/assets/38588948/24662b40-42fc-47ee-a0a3-1b8b8f5778d2)
2023-10-28 16:25:44 +05:30
Abhinav271828
f08fe1f1dd
[MLIR][Presburger] Implement matrix inverse (#67382)
Shift the `determinant()` function from LinearTransform to Matrix.
Implement a FracMatrix class, inheriting from Matrix<Fraction>, for inverses.
Implement inverse for FracMatrix and intInverse for IntMatrix.
Make Matrix internals protected instead of private so that Int/FracMatrix can access them.
2023-10-20 17:03:45 +01:00
Arjun P
7025ff6fa3 [MLIR][Presburger] clang-format and clang-tidy
Fix formatting issues mostly introduced in recent commits.
(This was possibly missed due to GitHub not having formatting checks at
the time, but it's unclear.)
2023-10-13 11:41:51 +01:00
Kunwar Grover
7ce800ba31
[MLIR][Presburger] Fix bug in PresburgerSpace::convertVarKind (#67267)
This patch fixes a bug in PresburgerSpace::convertVarKind where the
identifiers were not moved properly due to offset being invalidated.
2023-09-25 12:55:58 +05:30
Abhinav271828
c1b997464b
[MLIR][Presburger] Template Matrix to allow MPInt and Fraction; use IntMatrix for integer matrices (#66897)
Matrix has been templated to Matrix (for MPInt and Fraction) with
explicit instantiation for both these types.
IntMatrix, inheriting from Matrix<MPInt>, has been created to allow for
integer-only methods.
makeMatrix has been duplicated to makeIntMatrix and makeFracMatrix.

This was already landed previously but was reverted in
98c994c8e22d7f38cc04f56ee5cdeb337734414d due to build failure. This
fixes the failure.
2023-09-20 14:29:33 +01:00