Skip to content

Add .lower() methods to reduction and replication expressions - #9123

Merged
kroening merged 1 commit into
developfrom
kroening/bitvector-expr-lower-methods
Sep 23, 2026
Merged

kroening merged 1 commit into
developfrom
kroening/bitvector-expr-lower-methods

Conversation

@kroening

Copy link
Copy Markdown
Collaborator

Add lower() methods to reduction_and_exprt, reduction_nand_exprt, reduction_or_exprt, reduction_nor_exprt, reduction_xor_exprt, reduction_xnor_exprt, and replication_exprt in bitvector_expr.h/bitvector_expr.cpp.

These methods lower high-level bitvector reduction and replication operators to simpler expression forms:

  • reduction_and(a) → (a = all_ones)
  • reduction_nand(a) → (a != all_ones)
  • reduction_or(a) → (a != 0)
  • reduction_nor(a) → (a = 0)
  • reduction_xor(a) → a[0] ^ a[1] ^ ... ^ a[n-1]
  • reduction_xnor(a) → !(a[0] ^ a[1] ^ ... ^ a[n-1])
  • replication(n, x) → x :: x :: ... :: x (n times)

Use the new .lower() methods in smt2_conv.cpp to replace inline lowering code for reduction_and, reduction_nand, reduction_or, reduction_nor, and reduction_xnor. The reduction_xor case retains its native SMT-LIB encoding using extract + bvxor for efficiency.

The boolbv layer is not changed since it operates at the propositional literal level, and the SMT2 replication keeps its native repeat operator.

Companion to diffblue/hw-cbmc#2037 which will use these methods via .lower() instead of free functions.

@kroening
kroening force-pushed the kroening/bitvector-expr-lower-methods branch 4 times, most recently from 3961543 to 67842c6 Compare July 21, 2026 12:22
@codecov

codecov Bot commented Jul 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.85%. Comparing base (b31f61b) to head (82e0fce).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #9123      +/-   ##
===========================================
+ Coverage    80.84%   80.85%   +0.01%     
===========================================
  Files         1717     1717              
  Lines       190084   190153      +69     
  Branches        73       73              
===========================================
+ Hits        153664   153751      +87     
+ Misses       36420    36402      -18     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread src/util/bitvector_expr.cpp
Comment thread src/util/bitvector_expr.cpp
Comment on lines +392 to +400
exprt replication_exprt::lower() const
{
auto count = numeric_cast_v<std::size_t>(times());
exprt::operandst ops;
ops.reserve(count);
for(std::size_t i = 0; i < count; i++)
ops.push_back(op());
return concatenation_exprt{std::move(ops), type()};
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we want PRECONDITION(count >= 1).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verilog does allow zero replications in concatenations; I realise this makes lowering to SMT-LIB a bit harder. I'll add a comment.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might just need to fix it on the SMT-LIB output side (which already tries to catch zero-width expressions in some places).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Comment thread unit/solvers/smt2/smt2_conv.cpp
@kroening
kroening force-pushed the kroening/bitvector-expr-lower-methods branch 2 times, most recently from 7c32259 to 7e9f532 Compare September 23, 2026 20:17
Add lower() methods to reduction_and_exprt, reduction_nand_exprt,
reduction_or_exprt, reduction_nor_exprt, reduction_xor_exprt,
reduction_xnor_exprt, and replication_exprt in bitvector_expr.h/cpp.

These methods lower high-level bitvector reduction and replication
operators to simpler expression forms:
- reduction_and(a)  → (a = all_ones)
- reduction_nand(a) → (a != all_ones)
- reduction_or(a)   → (a != 0)
- reduction_nor(a)  → (a = 0)
- reduction_xor(a)  → a[0] ^ a[1] ^ ... ^ a[n-1]
- reduction_xnor(a) → !(a[0] ^ a[1] ^ ... ^ a[n-1])
- replication(n, x) → x :: x :: ... :: x (n times)

Use the new .lower() methods in smt2_conv.cpp to replace inline
lowering code for reduction_and, reduction_nand, reduction_or,
reduction_nor, and reduction_xnor. The reduction_xor case retains
its native SMT-LIB encoding using extract + bvxor for efficiency.

Co-authored-by: Michael Tautschnig <tautschn@amazon.com>
@kroening
kroening force-pushed the kroening/bitvector-expr-lower-methods branch from 7e9f532 to 82e0fce Compare September 23, 2026 20:21
@kroening
kroening enabled auto-merge September 23, 2026 20:27
@kroening
kroening merged commit fd5dcee into develop Sep 23, 2026
43 of 44 checks passed
@kroening
kroening deleted the kroening/bitvector-expr-lower-methods branch September 23, 2026 20:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants