Commit Graph

951 Commits

Author SHA1 Message Date
Oleksandr "Alex" Zinenko
11140cc238 [mlir] mark ChangeResult as nodiscard (#76147)
This enum is used by dataflow analyses to indicate whether further
propagation is necessary to reach the fix point. Accidentally discarding
such a value will likely lead to propagation stopping early, leading to
incomplete or incorrect results. The most egregious example is the
duality between `join` on the analysis class, which triggers propagation
internally, and `join` on the lattice class that does not and expects
the caller to trigger it depending on the returned `ChangeResult`.
2023-12-21 17:58:53 +01: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
Oleksandr "Alex" Zinenko
32a4e3fcca [mlir] support non-interprocedural dataflow analyses (#75583)
The core implementation of the dataflow anlysis framework is
interpocedural by design. While this offers better analysis precision,
it also comes with additional cost as it takes longer for the analysis
to reach the fixpoint state. Add a configuration mechanism to the
dataflow solver to control whether it operates inteprocedurally or not
to offer clients a choice.

As a positive side effect, this change also adds hooks for explicitly
processing external/opaque function calls in the dataflow analyses,
e.g., based off of attributes present in the the function declaration or
call operation such as alias scopes and modref available in the LLVM
dialect.

This change should not affect existing analyses and the default solver
configuration remains interprocedural.

Co-authored-by: Jacob Peng <jacobmpeng@gmail.com>
2023-12-18 14:16:52 +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
Victor Perez
13c648f6bd [MLIR][IntegerRangeAnalysis] Avoid crash reached when loop bound is uninitialized (#74832)
If the loop bound is not initialized, the analysis crashed, as it only checked for nullity. Also checking for initialization fixes the issue.

Signed-off-by: Victor Perez <victor.perez@codeplay.com>
Co-authored-by: Tsang, Whitney <whitney.tsang@intel.com>
2023-12-11 10:36:03 +01:00
long.chen
dc4786b487 [mlir][affine] remove divide zero check when simplifer affineMap (#64622) (#68519)
When performing constant folding on the affineApplyOp, there is a
division of 0 in the affine map.
[related issue](https://github.com/llvm/llvm-project/issues/64622)

---------

Co-authored-by: Javier Setoain <jsetoain@users.noreply.github.com>
2023-11-19 02:14:53 +08:00
Adrian Kuegel
b4c1421466 [mlir] Apply ClangTidy fix
Remove redundant return.
2023-11-17 09:56:07 +00:00
Uday Bondhugula
c79ffb02bb Generalize affine fusion to work at all depths and inside other region-holding ops (#72288)
Generalize affine fusion to work at any inner depth; fusing loops inside
other
affine.for or even inside scf.for or scf.while nests. Apply in post
order on
all affine nests on the pass' top-level operation.

Fix MDG init for blocks inside other affine nests.

Relax unnecessary requirement for unique vars during merge and align of
FlatLinearValueConstraints. There are several cases where
FlatLinearValueConstraints need to have duplicate Values for the
dimensions:
for eg. in dependence relation systems with source and destination
accesses
could have common loop IVs. `mergeAndAlign` can be done even in the
presence
of Values reappearing by simply aligning from left to right in that
order.

While at this, drop outdated comments; improve some debug messages.
2023-11-16 08:52:12 +05:30
long.chen
1609f1c2a5 [mlir][affine][nfc] cleanup deprecated T.cast style functions (#71269)
detail see the docment: https://mlir.llvm.org/deprecation/

Not all changes are made manually, most of them are made through a clang
tool I wrote https://github.com/lipracer/cpp-refactor.
2023-11-14 13:01:19 +08:00
Jeremy Kun
f778eafdd8 [IntegerRangeAnalysis] remove constraint on integer-typed results (#72007) 2023-11-13 22:10:47 -06:00
Mehdi Amini
285a229f20 [MLIR] Apply clang-tidy fixes for misc-include-cleaner (NFC) 2023-11-12 20:35:46 -08:00
Mehdi Amini
06927848da Apply clang-tidy fixes for performance-unnecessary-value-param in SliceAnalysis.cpp (NFC) 2023-10-31 21:25:47 -07: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
Mehdi Amini
ebc2c4bde3 Apply clang-tidy fixes for misc-include-cleaner in SparseAnalysis.cpp (NFC) 2023-10-28 21:39:30 -07:00
Mehdi Amini
37d1d9fe41 Apply clang-tidy fixes for misc-include-cleaner in LivenessAnalysis.cpp (NFC) 2023-10-28 21:39:30 -07:00
Mehdi Amini
383f2bd597 Apply clang-tidy fixes for misc-include-cleaner in IntegerRangeAnalysis.cpp (NFC) 2023-10-28 21:39:30 -07:00
Mehdi Amini
a5a908654e Apply clang-tidy fixes for misc-include-cleaner in DataFlowFramework.cpp (NFC) 2023-10-28 21:39:30 -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
Mehdi Amini
fba3951b28 Apply clang-tidy fixes for misc-include-cleaner in DenseAnalysis.cpp (NFC) 2023-10-26 23:58:10 -07:00
Mehdi Amini
e48038a84b Apply clang-tidy fixes for misc-include-cleaner in DeadCodeAnalysis.cpp (NFC) 2023-10-26 23:58:09 -07:00
Mehdi Amini
8a51518025 Apply clang-tidy fixes for misc-include-cleaner in ConstantPropagationAnalysis.cpp (NFC) 2023-10-26 23:58:09 -07:00
Mehdi Amini
9dd06a1259 Apply clang-tidy fixes for misc-include-cleaner in CallGraph.cpp (NFC) 2023-10-26 23:58:09 -07:00
Mehdi Amini
a2e69c5708 Apply clang-tidy fixes for misc-include-cleaner in LocalAliasAnalysis.cpp (NFC) 2023-10-26 23:58:09 -07:00
Mehdi Amini
a723a5b637 Apply clang-tidy fixes for misc-include-cleaner in AliasAnalysis.cpp (NFC) 2023-10-26 23:58:09 -07:00
Justin Fargnoli
6833a3808f [mlir][DeadCodeAnalysis] Don't Require RegionBranchTerminatorOpInterface in visitRegionTerminator() (#69043)
Fix for a crash reported in #64975. 

The crash occurs in the cast located
[here](ece5dd101c/mlir/lib/Analysis/DataFlow/DeadCodeAnalysis.cpp (L262))
because `llvm.unreachable` doesn't implement
`RegionBranchTerminatorOpInterface`.

The crash is caused by `DeadCodeAnalysis` assuming that
`isa<RegionBranchOpInterface>(op->getParentOp())` implies
`isa<RegionBranchTerminatorOpInterface>(op)` in
`DeadCodeAnalysis::visit()`.

This patch tried to fix this by enabling the analysis to proceed
regardless of whether `op` is a `RegionBranchTerminatorOpInterface`.
2023-10-22 15:34:39 -07:00
Mehdi Amini
a2e572099c Apply clang-tidy fixes for llvm-qualified-auto in CallGraph.cpp (NFC) 2023-10-21 17:31:37 -07:00
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
Jacob Mai Peng
4732b0cbc0 [mlir][dataflow] Remove early exit in dead code analysis for zero-operand returns (#68151)
The PredecessorState in dead code analysis does not register
zero-operand returns as predecessors of their corresponding call ops.
This causes bugs with dense dataflow analyses that use dead code
analysis to query for the predecessors of CallOpInterfaces.

This was reasonable for sparse analyses, but isn't for dense ones.
2023-10-05 17:09:44 +02: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
Kazu Hirata
3f0bddb56a Use llvm::find (NFC) 2023-09-23 16:27:02 -07:00
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
98c994c8e2 due to build failure. This
fixes the failure.
2023-09-20 14:29:33 +01:00
Arjun P
98c994c8e2 Revert "[MLIR][Presburger] Template Matrix to allow MPInt and Fraction (#65272)"
This reverts commit efca035c6d.

Reverting due to windows build bot failure:
https://lab.llvm.org/buildbot/#/builders/13/builds/40242/steps/6/logs/stdio
2023-09-18 19:06:32 +01:00
Abhinav271828
efca035c6d [MLIR][Presburger] Template Matrix to allow MPInt and Fraction (#65272)
The method implementations remain in the .cpp file; explicit instantiations have been added for these two types. 
makeMatrix has been duplicated to makeIntMatrix and makeFracMatrix.
2023-09-18 22:52:22 +05:30
Bharathi Ramana Joshi
ccf194b845 [MLIR][Presburger] Implement convertVarKind for PresburgerRelation 2023-09-17 20:02:16 +05:30
vic
a9d0f5e2f0 [mlir] Allow loop-like operations in AbstractDenseForwardDataFlowAnalysis (#66179)
Remove assertion violated by loop-like operations.

Signed-off-by: Victor Perez <victor.perez@codeplay.com>
2023-09-14 10:30:40 +02:00
Groverkss
d222b69093 [MLIR][Presburger] Add Identifier class to store identifiers and their type
This patch adds a new class Identifier to store identifiers in PresburgerSpace
and their types.

Identifiers were added earlier and were stored as a void pointer, and their type
in the form of mlir::TypeId in PresburgerSpace. To get an identifier, a user of
PresburgerSpace needed to know the type of identifiers. This was a problem for
users of PresburgerSpace like IntegerRelation, which want to work on
identifiers without knowing their type.

The Identifier class allows users like IntegerRelation to work on identifiers
without knowing their type, and also exposes an easier way to work with
Identifiers.

Reviewed By: arjunp

Differential Revision: https://reviews.llvm.org/D146909
2023-09-05 12:46:00 +05:30
iambrj
6c6a2d3445 [MLIR][Presburger] Minor bug-fix in PresburgerRelation::intersectRange from 3dd9931
Fix a typo (incorrectly calling getNumDomainVars instead of
getNumRangeVars) in intersectRange from 3dd9931.

Reviewed By: Groverkss

Differential Revision: https://reviews.llvm.org/D159413
2023-09-05 11:51:19 +05:30
Fangrui Song
7557530f42 [mlir] Fix duplicate word typos; NFC
Those fixes were taken from https://reviews.llvm.org/D137338
2023-09-01 20:53:08 -07:00
Martin Erhart
34a35a8b24 [mlir] Move FunctionInterfaces to Interfaces directory and inherit from CallableOpInterface
Functions are always callable operations and thus every operation
implementing the `FunctionOpInterface` also implements the
`CallableOpInterface`. The only exception was the FuncOp in the toy
example. To make implementation of the `FunctionOpInterface` easier,
this commit lets `FunctionOpInterface` inherit from
`CallableOpInterface` and merges some of their methods. More precisely,
the `CallableOpInterface` has methods to get the argument and result
attributes and a method to get the result types of the callable region.
These methods are always implemented the same way as their analogues in
`FunctionOpInterface` and thus this commit moves all the argument and
result attribute handling methods to the callable interface as well as
the methods to get the argument and result types. The
`FuntionOpInterface` then does not have to declare them as well, but
just inherits them from the `CallableOpInterface`.
Adding the inheritance relation also required to move the
`FunctionOpInterface` from the IR directory to the Interfaces directory
since IR should not depend on Interfaces.

Reviewed By: jpienaar, springerm

Differential Revision: https://reviews.llvm.org/D157988
2023-08-31 11:28:23 +00:00
Markus Böck
4dd744ac9c Reland "[mlir] Use a type for representing branch points in RegionBranchOpInterface"
This reverts commit b26bb30b46.
2023-08-30 09:31:54 +02:00
Markus Böck
b26bb30b46 Revert "[mlir] Use a type for representing branch points in RegionBranchOpInterface"
This reverts commit 024f562da6.

Forgot to update flang
2023-08-29 20:17:50 +02:00
Markus Böck
024f562da6 [mlir] Use a type for representing branch points in RegionBranchOpInterface
The current implementation is not very ergonomic or descriptive: It uses `std::optional<unsigned>` where `std::nullopt` represents the parent op and `unsigned` is the region number.
This doesn't give us any useful methods specific to region control flow and makes the code fragile to changes due to now taking the region number into account.

This patch introduces a new type called `RegionBranchPoint`, replacing all uses of `std::optional<unsigned>` in the interface. It can be implicitly constructed from a region or a `RegionSuccessor`, can be compared with a region to check whether the branch point is branching from the parent, adds `isParent` to check whether we are coming from a parent op and adds `RegionSuccessor::parent` as a descriptive way to indicate branching from the parent.

Differential Revision: https://reviews.llvm.org/D159116
2023-08-29 20:02:23 +02:00
iambrj
866391580b [MLIR][Presburger] Implement getDomainSet and getRangeSet for PresburgerRelation
This patch implements getDomainSet and getRangeSet for PresburgerRelation

Reviewed By: Groverkss

Differential Revision: https://reviews.llvm.org/D158263
2023-08-19 16:12:43 +05:30
iambrj
a67072eeee [MLIR][Presburger] Fix incorrect doc comment and add const 2023-08-19 15:42:48 +05:30
Srishti Srivastava
232f8eadae [MLIR][analysis] Fix call op handling in sparse backward dataflow
Currently, data in `AbstractSparseBackwardDataFlowAnalysis` is
considered to flow one-to-one, in order, from the operands of an op
implementing `CallOpInterface` to the arguments of the function it is
calling.

This understanding of the data flow is inaccurate. The operands of such
an op that forward to the function arguments are obtained using a
method provided by `CallOpInterface` called `getArgOperands()`.

This commit fixes this bug by using `getArgOperands()` instead of
`getOperands()` to get the mapping from operands to function arguments
because not all operands necessarily forward to the function arguments
and even if they do, they don't necessarily have to be in the order in
which they appear in the op. The operands that don't get forwarded are
handled by the newly introduced `visitCallOperand()` function, which
works analogous to the `visitBranchOperand()` function.

This fix is also propagated to liveness analysis that earlier relied on
this incorrect implementation of the sparse backward dataflow analysis
framework and corrects some incorrect assumptions made in it.

Extra cleanup: Improved a comment and removed an unnecessary code line.

Signed-off-by: Srishti Srivastava <srishtisrivastava.ai@gmail.com>

Reviewed By: matthiaskramm, jcai19

Differential Revision: https://reviews.llvm.org/D157261
2023-08-11 17:26:58 +00:00
Markus Böck
138df29820 [mlir] Revamp RegionBranchOpInterface successor mechanism
The `RegionBranchOpInterface` had a few fundamental issues caused by the API design of `getSuccessorRegions`.

It always required passing values for the `operands` parameter. This is problematic as the operands parameter actually changes meaning depending on which predecessor `index` is referring to. If coming from a region, you'd have to find a `RegionBranchTerminatorOpInterface` in that region, get its operand count, and then create a `SmallVector` of that size.
This is not only inconvenient, but also error-prone, which has lead to a bug in the implementation of a previously existing `getSuccessorRegions` overload.

Additionally, this made the method dual-use, trying to serve two different use-cases: 1) Trying to determine possible control flow edges between regions and 2) Trying to determine the region being branched to based on constant operands.

This patch fixes these issues by changing the interface methods and adding new ones:
* The `operands` argument of `getSuccessorRegions` has been removed. The method is now only responsible for returning possible control flow edges between regions.
* An optional `getEntrySuccessorRegions` method has been added. This is used to determine which regions are branched to from the parent op based on constant operands of the parent op. By default, it calls `getSuccessorRegions`. This is analogous to `getSuccessorForOperands` from `BranchOpInterface`.
* Add `getSuccessorRegions` to `RegionBranchTerminatorOpInterface`. This is used to get the possible successors of the terminator based on constant operands. By default, it calls the containing `RegionBranchOpInterface`s `getSuccessorRegions` method.
* `getSuccessorEntryOperands` was renamed to `getEntrySuccessorOperands` for consistency.

Differential Revision: https://reviews.llvm.org/D157506
2023-08-10 10:27:27 +02:00
Markus Böck
10ae8ae837 [mlir][NFC] Make ReturnLike trait imply RegionBranchTerminatorOpInterface
This implication was already done de-facto and there were plenty of users and wrapper functions specifically used to handle the "return-like or RegionBranchTerminatorOpInterface" case. These simply existed due to up until recently missing features in ODS.

With the new capabilities of traits, we can make `ReturnLike` imply `RegionBranchTerminatorOpInterface` and auto generate proper definitions for its methods.
Various occurrences and wrapper methods used for `isa<RegionBranchTerminatorOpInterface>() || hasTrait<ReturnLike>()` have all been removed.

Differential Revision: https://reviews.llvm.org/D157402
2023-08-08 22:11:39 +02:00
iambrj
ca213983cc [MLIR][Presburger] Implement findSymbolicIntegerLexMin/Max for PresburgerRelation
This patch implements findSymbolicIntegerLexMin/Max for PresburgerRelation

Reviewed By: Groverkss

Differential Revision: https://reviews.llvm.org/D156623
2023-08-08 11:46:27 +05:30