This is an archive of the discontinued LLVM Phabricator instance.

[ConstraintSystem] Add helpers to deal with linear constraints.
ClosedPublic

Authored by fhahn on Jul 24 2020, 12:32 PM.

Details

Summary

This patch introduces a new ConstraintSystem class, that maintains a set
of linear constraints and uses Fourier–Motzkin elimination to eliminate
constraints to check if there are solutions for the system.

It also adds a convert-constraint-log-to-z3.py script, which can parse
the debug output of the constraint system and convert it to a python
script that feeds the constraints into Z3 and checks if it produces the
same result as the LLVM implementation. This is for verification
purposes.

Diff Detail

Event Timeline

fhahn created this revision.Jul 24 2020, 12:32 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 24 2020, 12:32 PM
fhahn updated this revision to Diff 289789.Sep 3 2020, 1:01 PM

Fold D86966 and D85336 into this one.

fhahn updated this revision to Diff 289796.Sep 3 2020, 1:26 PM

Remove unnecessary variable.

fhahn edited the summary of this revision. (Show Details)
spatel accepted this revision.Sep 8 2020, 9:54 AM

LGTM

llvm/include/llvm/Analysis/ConstraintSystem.h
39

typo - Returns

53

typo - Returns

llvm/lib/Analysis/ConstraintSystem.cpp
36

The incoming Constraints are not changing in this loop? Put size() into a loop local variable:
https://llvm.org/docs/CodingStandards.html#don-t-evaluate-end-every-time-through-a-loop

40

NumVariables instead of Constraints[0].size()?

49

The incoming Constraints are not changing in this loop? Put size() into a loop local variable:
https://llvm.org/docs/CodingStandards.html#don-t-evaluate-end-every-time-through-a-loop

This revision is now accepted and ready to land.Sep 8 2020, 9:54 AM
fhahn updated this revision to Diff 291195.Sep 11 2020, 6:15 AM
fhahn marked 2 inline comments as done.

address comments & some cleanups, thanks! I will land this shortly.

This revision was landed with ongoing or failed builds.Sep 11 2020, 6:43 AM
This revision was automatically updated to reflect the committed changes.

Unfortunately not all compilers support __builtin_mul_overflow. I will resubmit a version using APInt for the overflow computations.

I'm curious if we could make use of this in the Clang Static Analyzer.
We always hit the limitations of the current Constraint manager implementation.
What do you think about this @NoQ @xazax.hun @Szelethus?

llvm/lib/Analysis/ConstraintSystem.cpp
137

Is this unconditional dump intentional?

fhahn added a comment.Sep 13 2020, 1:23 PM

I'm curious if we could make use of this in the Clang Static Analyzer.
We always hit the limitations of the current Constraint manager implementation.
What do you think about this @NoQ @xazax.hun @Szelethus?

It would be great if this would be useful for other users as well! It is intended as relatively generic utility and I would be more than happy to collaborate/coordinate to make sure it works well for additional uses cases.

Please note that there is plenty of potential for additional improvements in the code. The aim is to get started with a relatively simple implementation and an initial user so there is decent test coverage as we work on improving both.

llvm/lib/Analysis/ConstraintSystem.cpp
137

dump only generates output with -debug. It might be worth to make this more explicit here, but I am not sure what the best way would be.