3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2026-07-24 07:52:33 +00:00
Commit graph

67 commits

Author SHA1 Message Date
Michael Tautschnig
09aaadf963
Sequence AST-creating arguments in rewriters for cross-compiler determinism (#10165)
### Problem

The order of evaluation of function arguments is unspecified in C++
(arguments are indeterminately sequenced since C++17). Compilers use
this freedom differently:

```c++
static int f(int i) { printf("%d ", i); return i; }
static void g(int, int, int) { printf("\n"); }
int main() { g(f(1), f(2), f(3)); }
```
| compiler/target | output |
|---|---|
| gcc 13, x86_64 | `3 2 1` |
| gcc 13, aarch64 | `1 2 3` |
| clang 18, x86_64 | `1 2 3` |

Z3 has many call sites where **two or more arguments each create AST
nodes**, e.g. (before this PR, `bv_rewriter.cpp:876`):

```c++
result = m.mk_ite(c, m_mk_extract(high, low, t), m_mk_extract(high, low, e));
```

The two extract nodes are hash-consed and receive their AST ids in
evaluation order, so the id assignment differs between
compilers/targets. AST ids feed heuristic tie-breaking throughout the
solver (`bool_rewriter`'s `m_order_eq` equality-operand ordering,
id-based sorts in `array_rewriter`, case-split ordering, ...), so
**byte-identical input takes different solver paths depending on the
compiler and architecture z3 was built with**.

### Evidence

Investigated while chasing cross-platform proof-time instability in
CBMC/mldsa-native CI (diffblue/cbmc#8991), on byte-identical ~12 MB SMT2
instances (bit-vectors + arrays + quantifiers), with the `string_hash`
fix from #10163 applied to isolate this effect. Z3 4.15.3, gcc 13 on
x86_64 Linux and aarch64 Linux (Graviton):

* one instance: **17 s on x86_64 vs 1633 s on aarch64** (both `unsat`; a
sibling instance shows the reverse direction). Run-to-run within one
host: ±1 %.
* Instrumenting `ast_manager::register_node_core` with an order
fingerprint (running hash over `(node hash, node id)`) shows both
architectures construct **identical AST sequences up to registration
#41,789**, where x86_64 creates `(extract[0:0] #xFFFFFFFF)` before
`(extract[0:0] #xFFFFFFFE)` and aarch64 the other way around — from
identical call stacks at the `mk_ite`-over-two-`mk_extract` site quoted
above. All divergence between the two hosts flows from such events
(pointer/ASLR effects experimentally excluded: fingerprints are
invariant under `setarch -R` and across repeated runs).
* Sequencing that one site by hand moved the first divergence to
#248,118 — the analogous `mk_ite(c, mk_select(...), mk_select(...))`
site in `array_rewriter.cpp`. Sequencing that one, too, moved it to
#248,411, inside `nnf:👿:process_iff_xor` — i.e. the next layer of
the same onion.
* With the whole `ast/rewriter` layer swept (this PR), the instrumented
builds produce **identical AST construction traces on both architectures
throughout the entire rewriter phase** of this 546k-line industrial
instance; the first divergence left is the NNF one.

### Fix

Following the precedent of 37904b9e8, e113d39aa, 360193098, 93ff8c76d,
9b88aaf13 ("parameter evaluation order", `bool_rewriter`/`seq_rewriter`)
and the existing comments in `seq_rewriter.cpp` ("introduce temporaries
to ensure deterministic evaluation order..."), this PR hoists
AST-creating arguments into named temporaries with a defined evaluation
order, across `src/ast/rewriter/` — 126 call sites in 17 files. The
transformation is purely sequencing: it selects one of the two valid C++
evaluation orders and makes it the same everywhere. (Temporaries are raw
pointers in rewriter-local scope, matching the precedent commits;
nothing can trigger GC between creation and consumption.)

The sites were found with a small AST-argument scanner (statement-level
call sites whose argument list contains ≥ 2 top-level arguments that
each contain an AST-creating call); I am happy to share/contribute the
script. Known remaining work, deliberately out of scope here to keep the
diff reviewable:

* 41 sites in `src/ast/rewriter/` that need manual treatment (inside
`if` conditions, ternaries, or multi-statement expressions) — list
available on request;
* `src/ast/normal_forms/nnf.cpp` (`process_iff_xor`, proven divergent by
the trace above), ~7 sites in `src/ast/simplifiers/`, ~3 in
`src/ast/converters/`, ~9 in `src/ast/`;
* other theory/solver layers (`src/smt/`, `src/sat/`, ...) — divergences
there only matter after search starts, where paths have usually already
split, but a full sweep would be needed for bit-reproducibility across
compilers.

Together with #10163, this is a step towards z3 builds whose behaviour
does not depend on the compiler or target architecture — which matters
for verification CI that runs identical proofs on heterogeneous
platforms and expects comparable runtimes.

---------

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
2026-07-21 19:03:02 -07:00
Nuno Lopes
617c621cc0 replace some copies with moves 2026-02-09 22:45:28 +00:00
Copilot
058a8c082d
Refactor mk_and and mk_app to use std::span API (#8285)
* Initial plan

* Refactor mk_and and mk_app to use std::span

- Made mk_and(unsigned num_args, expr * const * args) private
- Added new public mk_and(std::span<expr* const> args) method
- Added new public mk_app(family_id fid, decl_kind k, std::span<expr* const> args) method
- Updated all convenience overloads to use std::span version
- Updated all external call sites to use the new std::span API

Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>

* Fix remaining test files to use std::span API

- Updated src/test/sorting_network.cpp
- Updated src/test/ho_matcher.cpp with explicit cast to resolve ambiguity

Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>

* Revert overlapping changes superseded by PR #8286

Reverted 30 files to match the state from PR #8286 (commit ebc0688) which refactored mk_and/mk_or call sites to use vector overloads. This supersedes the std::span changes in those files.

Retained std::span changes in files unique to this PR:
- Core API changes (ast.h, ast.cpp)
- Files not affected by PR #8286 (api_context.cpp, ast_util.cpp, bool_rewriter.h, datatype_rewriter.cpp, dom_simplifier.cpp, factor_rewriter.cpp, pb2bv_rewriter.cpp, quant_hoist.cpp, spacer_cluster_util.cpp, sortmax.cpp, array_axioms.cpp, smtfd_solver.cpp, goal.cpp, ho_matcher.cpp, qe_arith.cpp, sorting_network.cpp)
- Special case in hnf.cpp where both PRs modified different lines

Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>
2026-01-22 16:58:38 -08:00
LeeYoungJoon
0a93ff515d
Centralize and document TRACE tags using X-macros (#7657)
* Introduce X-macro-based trace tag definition
- Created trace_tags.def to centralize TRACE tag definitions
- Each tag includes a symbolic name and description
- Set up enum class TraceTag for type-safe usage in TRACE macros

* Add script to generate Markdown documentation from trace_tags.def
- Python script parses trace_tags.def and outputs trace_tags.md

* Refactor TRACE_NEW to prepend TraceTag and pass enum to is_trace_enabled

* trace: improve trace tag handling system with hierarchical tagging

- Introduce hierarchical tag-class structure: enabling a tag class activates all child tags
- Unify TRACE, STRACE, SCTRACE, and CTRACE under enum TraceTag
- Implement initial version of trace_tag.def using X(tag, tag_class, description)
  (class names and descriptions to be refined in a future update)

* trace: replace all string-based TRACE tags with enum TraceTag
- Migrated all TRACE, STRACE, SCTRACE, and CTRACE macros to use enum TraceTag values instead of raw string literals

* trace : add cstring header

* trace : Add Markdown documentation generation from trace_tags.def via mk_api_doc.py

* trace : rename macro parameter 'class' to 'tag_class' and remove Unicode comment in trace_tags.h.

* trace : Add TODO comment for future implementation of tag_class activation

* trace : Disable code related to tag_class until implementation is ready (#7663).
2025-05-28 14:31:25 +01:00
Nikolaj Bjorner
eba0732629 fix #6675
disable remove_unused_defs from pb-solver until it is integrated with model reconstruction.
2023-04-12 19:50:13 -07:00
Nikolaj Bjorner
527fb18366 add doc for card2bv 2022-12-11 09:51:49 -08:00
Nikolaj Bjorner
4a6083836a call it data instead of c_ptr for approaching C++11 std::vector convention. 2021-04-13 18:17:35 -07:00
Nikolaj Bjorner
8e0a2c9e77 fix #4910
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2020-12-20 11:29:13 -08:00
Nikolaj Bjorner
72d407a49f
mbp (#4741)
* adding dt-solver

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* dt

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* move mbp to self-contained module

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* files

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* Create CMakeLists.txt

* dt

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* rename to bool_var2expr to indicate type class

* mbp

* na

* add projection

* na

* na

* na

* na

* na

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* deps

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* testing arith/q

* na

* newline for model printing

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2020-10-21 15:48:40 -07:00
Nikolaj Bjorner
ee909b6374 random compiler nits 2020-09-29 13:43:51 -07:00
Nikolaj Bjorner
dbe2c9b305 encoding options #4665
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2020-08-30 10:24:42 -07:00
Nikolaj Bjorner
f7a7b9e1f4 fix #4108
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2020-04-26 21:04:28 -07:00
Nikolaj Bjorner
10768bd005 fix #3727
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2020-04-04 18:41:13 -07:00
Nikolaj Bjorner
9f386306ef na
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2020-03-29 10:55:47 -07:00
Nikolaj Bjorner
1a995da0ae fix #3538, turn on proof checking assertions in goal.cpp for earlier coverage
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2020-03-29 10:55:47 -07:00
Nikolaj Bjorner
a9a26e5f2e review comments by Elffers
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2019-07-21 06:52:02 -07:00
Bruce Mitchener
a12de12515 Use const& to reduce copies. 2019-06-02 09:58:32 +07:00
Nuno Lopes
cd4b53500c avoid a few str copies + symbol hiding 2019-03-08 10:13:46 +00:00
Nikolaj Bjorner
4c76d43670 add binary_merge encoding option
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2019-02-28 08:35:22 -08:00
Bruce Mitchener
7bc283b84e Avoid unnecessary copies in for-range loops. 2018-10-02 10:38:41 +07:00
Nikolaj Bjorner
3ae0ea8246 add circuit and unate encoding besides sorting option
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2018-07-06 21:09:13 -07:00
Nikolaj Bjorner
0d668e1428 fix #1661
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2018-05-30 03:18:22 -07:00
Nikolaj Bjorner
753b9dd734 fix #1650 fix #1648
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2018-05-25 08:56:13 -07:00
Nikolaj Bjorner
0708ecb543 dealing with compilers that don't take typename in non-template classes
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2018-05-23 09:11:33 -07:00
Nikolaj Bjorner
78b9f0686a merge
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2018-05-01 07:43:29 -07:00
Nikolaj Bjorner
859c68c2ac merge with opt
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2018-04-30 08:27:54 -07:00
Nikolaj Bjorner
f7e49501af updates
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2018-04-13 16:22:36 -07:00
Nikolaj Bjorner
c513f3ca09 merge with master
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2018-03-25 14:57:01 -07:00
Bruce Mitchener
76eb7b9ede Use nullptr. 2018-02-12 14:05:55 +07:00
Nikolaj Bjorner
8fb7fb9f98 add missing caching of PB/cardinality constraints, increase limit for compiling cardinalities to circuits
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2018-02-11 19:27:00 -08:00
Nikolaj Bjorner
4695ca16c8 perf improvements
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2018-02-10 11:43:33 -08:00
Nikolaj Bjorner
10894069b0 fix compiler error reported by Luca
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2018-02-07 13:19:40 -08:00
Nikolaj Bjorner
354c16454a fix bug in translation of pbeq into sat
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2018-02-03 22:19:25 -08:00
Nikolaj Bjorner
7b8101c502 fix bugs related to model-converter
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2018-01-17 12:25:24 -08:00
Nikolaj Bjorner
7afbf8165e snapshot
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2017-12-12 01:36:44 -08:00
Nikolaj Bjorner
1101c927c9 prepare for transitive reduction / hyper-binary clause addition
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2017-11-22 13:46:02 -08:00
Nikolaj Bjorner
da5c8c0667 update pb rewriter to be non-full on assertions
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2017-09-29 08:00:01 -07:00
Nikolaj Bjorner
ced2029ae9 local changes
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2017-09-25 16:37:15 -07:00
Nikolaj Bjorner
edb3569599 updates to sorting networks
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2017-09-23 22:36:19 -05:00
Nikolaj Bjorner
651587ce01 merge with master branch
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2017-09-19 09:39:22 -07:00
Nikolaj Bjorner
b19f94ae5b make include paths uniformly use path relative to src. #534
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2017-07-31 13:24:11 -07:00
Nikolaj Bjorner
5752830f71 bug fixes
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2017-06-22 15:01:12 -07:00
Nikolaj Bjorner
7d245be4e1 enable exposing internal solver state on interrupted solvers
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2017-05-30 17:33:27 -07:00
Nikolaj Bjorner
77b2a015a2 rewrite pb
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2017-05-22 17:47:46 -07:00
Nikolaj Bjorner
f9f0b2221d bug fixes
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2017-05-22 14:27:34 -07:00
Nikolaj Bjorner
71eb7e81b5 bug fixes
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2017-05-22 12:52:53 -07:00
Nikolaj Bjorner
d1fec7c029 bug fixes
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2017-05-21 15:39:05 -07:00
Nikolaj Bjorner
c49550ff2d enable pb solver
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2017-05-07 18:03:38 -07:00
Nikolaj Bjorner
aedabfff7a disable newer pb encoding
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2017-04-27 11:24:30 -07:00
Nikolaj Bjorner
41e1b9f3fe gt encoding of pb constraints
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
2017-04-16 12:07:16 +09:00