llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Add tiering or configuration of Solvers for Clang Dataflow Framework

Open jvoung opened this issue 5 months ago • 4 comments

Currently, the CDF checkers default to WatchedLiteralsSolver, which is okay for simple problems but doesn't scale well to more complex problems (see reports like issue #69369).

Internally, we've been using tiers of solvers (a) start with WatchedLiteralsSolver for only a few iterations, and then (b) switch to a production-grade solver after that, and that has worked better. For (b), we could for example use Z3 w/ some limits set as well.

The default should be improved in the diagnoseFunction entry point, but we should also be able to take a custom solver (e.g., it is currently configurable though, e.g., DataflowAnalysisContext if you instead use the runDataflowAnalysis entry point)

jvoung avatar Jun 18 '26 13:06 jvoung

A rough attempt in draft PR #222378 , which does show promising speed up for the ccache project (issue #69369), reducing overall checker time from ~78s -> ~5s in the whole project (~129 .cc TUs).

  • The draft code uses Z3 if available, otherwise has the existing behavior.
  • An alternative is have a ClangTidy checker option to opt in (but users would need to know to do that).
  • Or a middle ground could be a checker option, but the default is on if building with LLVM_WITH_Z3 and off if not (it wouldn't work anyway, but if someone tries to turn it on would find out)
  • For some distributions like Ubuntu, it seems Z3 is enabled in the build (I assume for CSA support?). Not sure about the official releases.

@vbvictor @5chmidti @localspook @zeyi2 -- in case you have thoughts on this (explicit checker option, vs implicitly enable if possible), or know how if the official release builds with LLVM_WITH_Z3 or not.

jvoung avatar Sep 10 '26 18:09 jvoung

Is the Z3 solver always better then current one or they have pros/cons?

Then, there is a runtime error if Z3 was not actually available. However, it is not clear users will know to enable this.

Can we just always build it in clang-tidy? I mean add to CMAKE as a dependency, then all distributions will build z3 anyway.

vbvictor avatar Sep 16 '26 14:09 vbvictor

if the official release builds with LLVM_WITH_Z3 or not.

I think currently it is disabled in the release build. But Homebrew's LLVM package also enables it and links libLLVM.dylib to Z3.

Can we just always build it in clang-tidy? I mean add to CMAKE as a dependency, then all distributions will build z3 anyway.

I tried it on my setup, it seems that turning on Z3 slows down compilation time of clang-tidy a lot.


Personally I'm supportive of using Z3 with clang-tidy, I personally also have a check that utilizes Z3 for fast detection of lock misuse bugs. IMO, this integration would significantly improve clang-tidy’s analysis capabilities.

zeyi2 avatar Sep 17 '26 05:09 zeyi2

Thanks for taking a look

Is the Z3 solver always better then current one or they have pros/cons?

Z3 does more pre-processing / simplification than the WatchedLiteralsSolver, so for simple functions, WatchedLiteralsSolver can be faster. The tiering was meant to adjust dynamically and do better on average than just Z3. A couple samples from another CDF checker (just z3 vs with tiering)

  • lld/MachO/SyntheticSections.cpp (3s vs 1s)
  • clang/lib/StaticAnalyzer/Core/BugReporter.cpp (7s vs 4s)
  • llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp (15s vs 12s)

Can we just always build it in clang-tidy? I mean add to CMAKE as a dependency, then all distributions will build z3 anyway.

That could help indicate to the distros, but I'm not sure if that would mean having Z3 be required even for general Clang other parts of LLVM (e.g, JIT use cases). Maybe with some refactoring it could be possible to isolate the dependency more (currently CreateZ3Solver is part of llvm/Support lib).

That said, there are other options for CDF:

  • simpler model that avoids SAT https://github.com/llvm/llvm-project/issues/224638, but wouldn't handle some correlated conditions
  • improve the current simple builtin solver
    • add CDCL
    • the formulas we generate have a pretty specific structure and am curious if we can specialize the solver to this problem domain (thinking of https://c.mov/lymphosat), or do more simplification of the formulas first
  • improve the use of the solver (we don't cache anything right now, etc)

Personally I'm supportive of using Z3 with clang-tidy, I personally also have a check that utilizes Z3 for fast detection of lock misuse bugs ...

Interesting! Are you using just the SAT portion of Z3 (like this dataflow framework) or more general SMT constraints?

jvoung avatar Sep 18 '26 14:09 jvoung