Repository navigation
Port from stdpp bitvectors to stdlib bits - #2
Conversation
|
Please also take a look at https://github.com/mit-plv/quartz/tree/andreser/monomorphize-Zmod (an older branch that already uses Zmod) and consider any differences between this PR and that branch -- keep the best option in each case, report back here. |
71f9958 to
3c6aeb5
Compare
|
Compared this PR with the
Result: Written by Claude Fable 5.1 via Claude Code (session https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm). |
|
Do all differences between the two Zmod ports have tests for both C++ and SystemVerilog backends? If not, add appropriate tests that distinguish between these behaviors in this PR. EDIT: I mean the observable differences, no need to test internal implementation differences. |
| From Stdlib Require Import Bits. | ||
| (*! sv:reject *) |
There was a problem hiding this comment.
The test-harness-configuration comment should always be on the first line if there is one.
There was a problem hiding this comment.
Fixed in every test file (this one and test_either_unit_unit.v); the harness directive is line 1 again, and the new tests have none.
Written by Claude Fable 5.1 via Claude Code (session https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm).
|
It would be nice to rename "BV" to "Zmod". Would it be straightforward to refactor so that no stdlib names are shadowed, just new definitions are added to the Zmod namespace? |
Use rocq-stdlib's `bits n` (`Zmod (2^n)`, stdlib >= 9.1) as the carrier of `type.Bits` instead of std++'s `bv n`, so that quartz shares one bit-vector type with the other mit-plv projects. This removes the rocq-stdpp-bitvector dependency and, since `vec` was the only remaining use, std++ altogether. Widths are `Z` again, as they were before the std++ port: stdlib's `bits` is indexed by `Z`, and `type.wf` already carries the `0 <= sz` side condition that the stdlib range lemmas need. The operations map directly: `Zmod.add/sub/opp/and/or/xor/not`, shifts `Zmod.slu/sru/srs` by the unsigned value of the amount, `Zmod.eqb` for `IsZero`/`EqBits`, `bits.of_Z` of the signed/unsigned value for `Resize`, and `Zmod.firstn l (Zmod.skipn s v)` for `Slice s l`. `App` is `Zmod.app` again, with result type `Bits (n + m)` and the first operand in the low bits. The std++ port had made `App` take an explicit result width and put the first operand in the high bits, which contradicted the C++ and SystemVerilog printers (both emit the first operand low); the printers were right, so the Rocq semantics returns to them and `Concat` in Processor.v resizes `App lo hi`. `type.pack` is built from bare `Zmod.app` in the same order. The boolean helpers (`embed_bool`, `nonzero`, `bool_cases`, ...) live in a module named `Zmod` in domain.v, so they read as `Zmod.embed_bool` next to the stdlib operations. That module only adds names: it defines nothing that stdlib's `Zmod` modules define, so `Zmod.add`, `Zmod.eqb`, `Zmod.of_Z`, ... still resolve to the stdlib constants without any `ZmodDef.` prefix. The case analyses come from `Zmod.in_elements`. `unsigned_resize_id`, which restated `Zmod.of_Z_unsigned`, is gone. The tests use `bits.of_Z` and `Zmod.zero` for constants, keep the harness directive comment on their first line, and gain the bit-semantics tests from the andreser/monomorphize-Zmod branch. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm
Every test is checked on all three evaluators (the Rocq interpreter, slang on the SystemVerilog output, and the compiled C++ output), so a test whose expected value differs between two candidate semantics pins down which one the language has. Add tests for the operations where the stdlib port and the andreser/monomorphize-Zmod branch differ, with operands chosen so that the other reading would produce another value: - test_app_asym: `App` of a 2-bit 1 and a 3-bit 5 is 5'h15, so the first operand is in the low bits and the width is n + m (the other operand order gives 5'hd). - test_app_resize: the same `App` under an `UnsignedResize` to 8 bits is 8'h15, the shape `Concat` in Processor.v uses. - test_slice_mid: `Slice 3 4` of 8'hb5 is 4'h6, so the slice starts at bit s and takes l bits (swapping firstn and skipn gives 0). - test_slice_overrun: `Slice 5 8` of 8'hb5 is 8'h5, so a slice that reaches past the operand zero-extends. - test_xor: 4'hc xor 4'ha is 4'h6 (and gives 4'h8, or gives 4'he); the branch has no `Xor`. test_all_ops shifted by the full 32-bit operand 0xbad1dea5. The interpreter computes `Zmod.slu` through `Z.shiftl`, so producing its expected output meant building a three-billion-bit integer, and the test's `.out` rule ran out of memory (on main as well). Shift by the 16-bit resized value 0xbeef instead, as the branch's version of this test does: the amount is still larger than the width, so the test keeps checking that oversized shifts saturate to zero and to the sign bit on both backends. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm
3c6aeb5 to
47dfe79
Compare
|
Tests (commit 47dfe79): every observable difference between the two Zmod ports now has a test that both backends check against the interpreter, with values chosen so that the other behaviour fails. Full run: 40 SystemVerilog and 42 C++ tests agree with the interpreter (clang 19, UBSan; ASan could not run under this machine's virtual-memory limit), and the 3 sv:reject / 1 cpp:reject tests are rejected.
Harness limits: none of the constructs above is unsupported on a backend; the only tests not checked on a backend are the pre-existing reject tests. Processor.v's Rename: done. Written by Claude Fable 5.1 via Claude Code (session https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm). |
|
create a new PR stacked on top of this one to bring remaining changes from https://github.com/mit-plv/quartz/tree/andreser/monomorphize-stdpp up to date with latest work on this PR and granite |
|
On it: rebasing andreser/monomorphize-stdpp (4 commits over main) onto this PR's tip, then checking granite against it; the stacked PR will follow. Written by Claude Fable 5.1 via Claude Code (session https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm). |
|
Stacked PR: #3 (monomorphize-stdpp rebased onto this tip, one extra commit sanitizing binder names for the printers; 66 tests pass). granite is being built against it now; any granite fallout goes into a PR stacked on mit-plv/granite#1. Written by Claude Fable 5.1 via Claude Code (session https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm). |
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm
Monomorphized CPU and shared pretty-printing on stdlib bits (stacked on #2)
Uses rocq-stdlib's
bits n(Zmod (2^n), rocq-stdlib >= 9.1) as the carrier oftype.Bitsinstead of std++'sbv n, so quartz shares one bit-vector type with the other mit-plv projects; std++ is no longer a dependency at all (vecisVector.t). Widths areZagain, as before the std++ port.Appis stdlib'sZmod.appwith the first operand in the low bits and result widthn + m, which is what the C++ and SystemVerilog printers already emitted (main's Rocq semantics put the first operand high, so generated code disagreed with the spec);type.packfollows the same layout, and Processor.v'sConcatkeeps its hi/lo meaning asUnsignedResize (App lo hi).Slice s lisZmod.firstn l (Zmod.skipn s v). TheBVmodule is nowModule Zmod, adding only new names (embed_bool,nonzero,bool_cases, ...) to the stdlib namespace and shadowing nothing; constants arebits.of_Z; the printers printZwidths; a lemma that restated a stdlib one is gone; ten bit-semantics tests from theandreser/monomorphize-Zmodbranch are added, plus five tests that pin down every observable difference between the two ports on both backends (the comparison and the test table are in comments below).dune build(lang, examples, all tests) passes in 14 s, as on main, with the same warning kinds and no admits; the harness run agrees with the interpreter on all 40 SystemVerilog and 42 C++ tests. Requested by @andres-erbsen on mit-plv/kami#51; the matching granite port follows and will pin this branch.Written by Claude Fable 5.1.
🤖 Generated with Claude Code
https://claude.ai/code/session_01YJbiRBkJtM7x6PvYV6hmEm