Commit Graph

3095 Commits

Author SHA1 Message Date
Leandro Pacheco
4be51aa95a Powdr openvm extension with new hints (#3100)
Extend our openvm guest/host with support for new hints.
Includes hints for `k256` affine coordinate inverse and sqrt.
2025-07-30 15:50:23 +00:00
Georg Wiese
5c8ecd2a46 Remove free variables (#3098)
This PR adds a new optimizer step:

5568270f77/autoprecompiles/src/constraint_optimizer.rs (L96-L108)

This can be useful to remove "left-over" range constraints, e.g. from
removed memory bus interactions:
- Imagine a memory bus interaction receiving a previous time stamp and
then having a range constraint like `[current_timestamp - prev_timestamp
- 1] in [BIT16]` to enforce that it is smaller than the current
timestamp.
- Then, `prev_timestamp` would only be mentioned in this stateless bus
interaction after the memory bus interaction is removed by the memory
optimizer.
- The `remove_disconnected_columns` step would not remove it, because
`current_timestamp` is also mentioned and it is connected to a stateful
bus interaction.
- The constraint is still redundant: For any value of
`current_timestamp`, the prover could pick a satisfying value for
`current_timestamp - prev_timestamp - 1` (e.g. $0$) and solve for
`prev_timestamp`

---------

Co-authored-by: Thibaut Schaeffer <schaeffer.thibaut@gmail.com>
2025-07-30 15:03:39 +00:00
chriseth
eff797efe7 Only transform non-affine bus interactions. (#3101)
Bus interaction variables were introduced in order to be able to derive
knowledge about complex expressions used in bus interactions. The system
is quite good in handling affine expressions, though, so in order to
keep the number of variables (and constraints) down, we only replace
non-affine expressions in bus interactions.
2025-07-30 14:01:44 +00:00
Thibaut Schaeffer
7003d6a453 Return early from cell pgo if no apcs are requested (#3109) 2025-07-30 13:59:53 +00:00
chriseth
ef97650646 Use negative numbers in range constraints. (#3115) 2025-07-30 08:53:22 +00:00
Steve Wang
a3fc1ee854 Create APC with constraint on max number of basic block instructions (#3114)
Reth test will be fixed after we merge this.
2025-07-30 08:45:35 +00:00
chriseth
581a9916fe Group into btreeset directly (#3105)
Co-authored-by: Georg Wiese <georgwiese@gmail.com>
2025-07-29 13:58:45 +00:00
Thibaut Schaeffer
4700c1815f Rename extract_boolean to try_extract_boolean (#3110)
It can fail and returns an Option, so `try_` seems appropriate.
2025-07-29 12:26:23 +00:00
Georg Wiese
83cd1ecc3e Add optimize() benchmark (#3104)
```
$ cd openvm
$ cargo bench --bench optimizer_benchmark
Benchmarking optimize-keccak/optimize: Warming up for 3.0000 s
Warning: Unable to complete 10 samples in 5.0s. You may wish to increase target time to 44.2s.
optimize-keccak/optimize
                        time:   [4.3703 s 4.3856 s 4.4020 s]
                        change: [-1.2781% -0.5240% +0.2094%] (p = 0.21 > 0.05)
                        No change in performance detected.
```
2025-07-29 09:34:06 +00:00
Georg Wiese
92429bf364 Variable Occurrences: Use BTreeSet (#3102)
This way, we guarantee that there are no duplicates, which can cause
performance issues.

Although, it seems like on the OpenVM Keccak test case (#3104), there
are not many duplicates and performance decreases slightly:
```
$ cargo bench --bench optimizer_benchmark
Benchmarking optimize-keccak/optimize: Warming up for 3.0000 s
Warning: Unable to complete 10 samples in 5.0s. You may wish to increase target time to 45.8s.
optimize-keccak/optimize
                        time:   [4.5420 s 4.5712 s 4.6035 s]
                        change: [+3.5320% +4.2324% +5.0959%] (p = 0.00 < 0.05)
                        Performance has regressed.
```
2025-07-29 09:33:27 +00:00
Steve Wang
eef681c225 Derive traits for evaluation structs (#3103)
Needed for https://github.com/succinctlabs/sp1-wip/pull/633.
2025-07-29 09:32:07 +00:00
chriseth
448debf109 Solver queue (#3092)
Implements a queue over constraint system items such that items are
re-queued when their variables are updated.

---------

Co-authored-by: Georg Wiese <georgwiese@gmail.com>
2025-07-29 07:26:23 +00:00
Georg Wiese
11cb3d6803 Fix typo on comment (#3099)
Added a wrong comment in #3097 :/
2025-07-28 19:06:44 +00:00
Georg Wiese
45398d41a9 Test stack accesses (#3097) 2025-07-28 17:33:25 +00:00
Thibaut Schaeffer
86bd7c5db3 Update reth hash in CI (#3096) 2025-07-28 16:20:47 +00:00
Thibaut Schaeffer
9f36102a16 Keep openvm program intact (#3075)
Since #3055, we don't need to change the program anymore.
This PR keeps the program intact in openvm, passing the apc opcodes to
openvm through a new API in openvm.
See https://github.com/powdr-labs/openvm/pull/36
Related: https://github.com/powdr-labs/openvm-reth-benchmark/pull/26
2025-07-28 14:48:10 +00:00
chriseth
0d43b411b3 Turn solver into trait. (#3087)
Turns `Solver` into a trait to abstract away the bus interaction handler
and also allow other implementations in the future.

It also makes the solver persistent across optimization loops so that we
do not have to derive the same range constraints all over again.

This is also a preparatory PR to make the solver more independent from
the constraint system: The constraint system inside the solver will have
way more redundancies in the future.

---------

Co-authored-by: Georg Wiese <georgwiese@gmail.com>
2025-07-28 14:18:02 +00:00
chriseth
5aaa375a70 Move derive_more... to exhaustive search module. (#3090)
It is not really an intrinsic task of the constraint system to derive
assignments.
2025-07-26 10:55:15 +00:00
chriseth
362d3f9fdb Remove redundant constraints. (#3074)
Removes algebraic constraints that are implied by other algebraic
constraints.

In order to do that, grouped expressions are turned into products of
factors such that these factors are as much normalized as possible. Then
we try to find algebraic constraints that are divisors of other
algebraic constraints (i.e. the factors are subsets).
2025-07-26 10:54:15 +00:00
Georg Wiese
0a1a7556e7 Add tests for pseudo instructions (#3073)
Done by Claude, not reviewed yet

---------

Co-authored-by: chriseth <chriseth.github@gmail.com>
2025-07-25 14:59:25 +00:00
Georg Wiese
6329f0ea65 Remove opcode (#3091)
We used to need the Opcode of the APC in order to hard-code it in the PC
lookup. However, for a while, we have removed PC lookups entirely,
hard-coding the PC in the execution bridge receive instead.

This removes the necessity to store the opcode. It was only used as an
ID of the APC. But for this, we can use the PC instead. This has the
added benefit that we can more easily relate it to the original program.
2025-07-25 14:33:43 +00:00
Georg Wiese
694450fdfe Include instructions & evaluation in APC snapshots (#3088)
Includes an evaluation of APCs in the snapshots.
2025-07-25 13:39:20 +00:00
chriseth
c79e4c17f0 Substitute bus vars at start and end. (#3078) 2025-07-25 11:08:22 +00:00
Steve Wang
1f43bce579 Expose Prog as public (#3089)
Small patch so that we can update reth here:
https://github.com/powdr-labs/openvm-reth-benchmark/pull/27.
2025-07-25 10:49:44 +00:00
chriseth
993a1ac81b Avoid quadratic terms that sum to zero. (#3084) 2025-07-25 09:25:23 +00:00
Steve Wang
786642c0bc Generalize execution profile (#3086)
Move execution data (pc execution map) collection from OVM to APC.
2025-07-25 09:21:56 +00:00
chriseth
46549ee718 Replace xor bus interaction by algebraic constraint (#3058)
Optimize xor lookups such that if the inputs are already
boolean-constrained, the xor operation is computed using an algebraic
constraint instead.
2025-07-25 08:16:59 +00:00
chriseth
efe3652837 Use update-expect also for columns saved. (#3085) 2025-07-25 08:14:17 +00:00
chriseth
d2d8160711 Use expect-test (#3081) 2025-07-24 14:59:00 +00:00
chriseth
1b30e81d78 Use UPDATE_EXPECT feature also for apc builder tests. (#3082) 2025-07-24 12:54:36 +00:00
Georg Wiese
032297368f Make UniqueReferences public (#3080) 2025-07-23 21:52:19 +00:00
Georg Wiese
eeb6776e85 Generalize bus type (#3070)
The `BusType` enum in the `autoprecompiles` crate still listed the
OpenVM bus types. Now, the bus types that we actually rely on are
hard-coded (e.g. execution bridge, memory, etc), but there is an `Other`
variant where the integration can add their own types:

92ae7469e8/autoprecompiles/src/bus_map.rs (L5-L19)

---------

Co-authored-by: Thibaut Schaeffer <schaeffer.thibaut@gmail.com>
2025-07-23 13:50:57 +00:00
Thibaut Schaeffer
d308ae166e Make rv64 label detection more idiomatic (#3072)
Use iterators instead of while loop and globa state.
2025-07-22 11:54:22 +00:00
Leo
4446c7298f Replace broken udeps action by direct calls (#3071)
The action itself seems broken with JS errors in every PR recently.
Only merge if the `udeps` job passes.
2025-07-22 11:12:28 +00:00
Leo
e6ff8810b8 initial support for label reading for rv64 (#3060)
This PR adds a module to the `riscv-elf` crate that collects labels,
their addresses, and jumpdests from a RV64 ELF binary. It does not
compute debug infos and all the other stuff that the current 32-bit
module does.

---------

Co-authored-by: Steve Wang <qian.wang.wg24@wharton.upenn.edu>
2025-07-22 10:34:13 +00:00
chriseth
9a97fca7c6 Take other bus interactions into account when computing xor range constraints (#3012) 2025-07-22 09:32:46 +00:00
Georg Wiese
d74aaa1a83 Generalize memory (#3065)
This PR generalizes the memory optimizer to not make any specific
assumptions about the structure of memory bus interactions. This is done
by turning `MemoryBusInteraction` into a trait, which is an associated
type in the `Adapter`. Currently, there is one concrete implementation,
`OpenVmMemoryBusInteraction`.
2025-07-22 08:19:13 +00:00
chriseth
ad9c6c0b3e Also return determined range constraints. (#3068)
Sometimes we want to run an optimization that depends on range
constraints. Using the solver is the best way to determine range
constraints, so it should also return them.
2025-07-21 18:59:07 +00:00
chriseth
46a9e7e634 Extract reachability. (#3069)
This change extracts the reachability algorithm and adds a new feature
that we will need for the seqz optimization: Find all variables that are
connected to a stateful bus interaction in any way that does not go
through a certain set of variables. The idea will be that we know of a
more efficient way to compute a certain relation between several input
variables and one output variable. This reachability routine can be used
to determine which constraints are used to compute the relation and can
be replaced by the more efficient routine.
2025-07-21 15:44:51 +00:00
chriseth
bcae937044 Add memcpy block test. (#3067)
Co-authored-by: Georg Wiese <georgwiese@gmail.com>
2025-07-21 15:38:51 +00:00
chriseth
1480efd132 Store column count. (#3066)
In order to get a feel for the optimizer, I think it is good to also
output the column count. It is rather difficult to get the column count
before optimization, but in the end we are only interested in the effect
of a new optimization step anyway, so I think this is fine.

---------

Co-authored-by: Georg Wiese <georgwiese@gmail.com>
2025-07-21 11:03:06 +00:00
Georg Wiese
dbda4050c2 Better panic message for BusMap::bus_type (#3064) 2025-07-20 12:41:33 +00:00
Georg Wiese
caf069eb85 Generalize PC lookup & execution bus handling (#3055)
Generalizations that make fewer assumptions about the structure of bus
interactions like PC lookups and bus interactions. See comments below.

One change is that we know inline the initial PC (because we know it for
any given basic block), which leads to *all* PC lookups being removed,
slightly improving performance (at least reducing one witness column &
one bus interaction).

---------

Co-authored-by: schaeff <thibaut@powdrlabs.com>
2025-07-20 08:09:32 +00:00
Thibaut Schaeffer
3ab2dba41f Update openvm (#3062) 2025-07-19 22:41:42 +00:00
Georg Wiese
c856986f75 BasicBlock: Store start_pc (#3059)
Suggested alternative to #3057: Instead of `start_idx`, we store the
`start_pc` to identify a basic block. Both should uniquely identify the
basic block, but the start PC is useful information on its own (whereas
the `start_idx` only makes sense to someone who knows the entire
program).

See also
https://github.com/powdr-labs/powdr/pull/3055#discussion_r2215738707.
2025-07-18 15:16:03 +00:00
Thibaut Schaeffer
c5d2b13e31 Partially remove opcode concept from apc crate, use VmOpcode in openvm crate (#3054)
This PR does two things:
- it partially removes the notion of opcode in the autoprecompile crate,
and instead relies on user provided functions to know if an instruction
is allowed/branching
- it uses VmOpcode as much as possible in the openvm crate instead of
usize

After this PR, the only remaining notion of opcode in the apc crate is
in the `SymbolicInstructionStatement` where it is still a single field
element. @georgwiese shared ideas to remove that too, or at least shift
it to the client, see #3055
2025-07-18 09:28:58 +00:00
Thibaut Schaeffer
3e239cb965 Simplify SymbolicInstructionStatement treatment (#3052)
`SymbolicInstructionStatement` is only used to constraint the pc lookup.
Reuse the same type for the pc bus lookup and the instruction.
2025-07-16 17:09:34 +00:00
Thibaut Schaeffer
2f723f5e62 Remove clone bound on InstructionMachineHandler (#3051) 2025-07-16 11:05:12 +00:00
Thibaut Schaeffer
56acc3d858 Use full instruction when getting the instruction air (#3048) 2025-07-16 10:19:03 +00:00
Thibaut Schaeffer
cb5c1df549 Make pc 64bit (#3050) 2025-07-16 09:39:08 +00:00