Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions conformance/sv-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ the initial 28 upstream checks passed. Arity and void-result rejection remain ch
Queue slices, queue operands in concatenations, element-reference persistence,
warning diagnostics, and queries on nonintegral pop results remain outside
this change's support contract.
Logical equivalence (`<->`) now passes six generated cases in both state modes.
The binary-operator case passes elaboration; the five logical-operator cases
execute simulations and pass their assertions. A full run before the container
method changes above changed only these twelve verdicts, with no passing case
regressing: 1,208 cases pass in 2state and 1,219 in 4state. Shared tests cover unknown operands and
evaluate both operands exactly once (IEEE 1800-2023 11.4.7).

Logical implication (`->`) now passes six generated cases in both state modes.
The binary-operator case passes elaboration; the five logical-operator cases
Expand All @@ -172,6 +178,22 @@ method changes above changed only these twelve verdicts, with no passing case
regressing: 1,208 cases pass in 2state and 1,219 in 4state. Shared tests also cover short-circuiting and
unknown operands (IEEE 1800-2023 11.4.7).

Integral array reductions (7.12.3) support `sum`, `product`, `and`, `or` and
`xor`, with or without empty parentheses. The five unmodified
`chapter-7/arrays/unpacked/reduction-methods` simulation cases pass the
official runner in both state modes at the pinned revision. Only those ten
reviewed expectations change from `fail` to `pass`; this is a focused run,
not a new full-corpus measurement. Shared regressions also exercise fixed
arrays, queues, integral- and string-indexed associative arrays, element-width
overflow, signed results, packed type queries and four-state propagation.
Empty reductions use the operation identity (`product`: one, `and`: all ones,
`sum` / `or` / `xor`: zero). An explicit `legacy_empty_reductions` build option
selects zero for all empty reductions. These empty-input cases are covered by
shared and simulator API regressions; the five upstream cases use nonempty
arrays and do not distinguish these modes.
`with` expressions, iterator arguments and
multidimensional unpacked receivers remain unsupported.

## Running locally

Use the locked development shell and install the pinned [`requirements.txt`](requirements.txt) in a Python environment.
Expand Down
44 changes: 22 additions & 22 deletions conformance/sv-tests/expected.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -1383,16 +1383,16 @@ chapter-7/arrays/unpacked/ordering-methods/shuffle.sv 2state fail
chapter-7/arrays/unpacked/ordering-methods/shuffle.sv 4state fail
chapter-7/arrays/unpacked/ordering-methods/sort.sv 2state fail
chapter-7/arrays/unpacked/ordering-methods/sort.sv 4state fail
chapter-7/arrays/unpacked/reduction-methods/and.sv 2state fail
chapter-7/arrays/unpacked/reduction-methods/and.sv 4state fail
chapter-7/arrays/unpacked/reduction-methods/or.sv 2state fail
chapter-7/arrays/unpacked/reduction-methods/or.sv 4state fail
chapter-7/arrays/unpacked/reduction-methods/product.sv 2state fail
chapter-7/arrays/unpacked/reduction-methods/product.sv 4state fail
chapter-7/arrays/unpacked/reduction-methods/sum.sv 2state fail
chapter-7/arrays/unpacked/reduction-methods/sum.sv 4state fail
chapter-7/arrays/unpacked/reduction-methods/xor.sv 2state fail
chapter-7/arrays/unpacked/reduction-methods/xor.sv 4state fail
chapter-7/arrays/unpacked/reduction-methods/and.sv 2state pass
chapter-7/arrays/unpacked/reduction-methods/and.sv 4state pass
chapter-7/arrays/unpacked/reduction-methods/or.sv 2state pass
chapter-7/arrays/unpacked/reduction-methods/or.sv 4state pass
chapter-7/arrays/unpacked/reduction-methods/product.sv 2state pass
chapter-7/arrays/unpacked/reduction-methods/product.sv 4state pass
chapter-7/arrays/unpacked/reduction-methods/sum.sv 2state pass
chapter-7/arrays/unpacked/reduction-methods/sum.sv 4state pass
chapter-7/arrays/unpacked/reduction-methods/xor.sv 2state pass
chapter-7/arrays/unpacked/reduction-methods/xor.sv 4state pass
chapter-7/arrays/unpacked/slice-equality.sv 2state pass
chapter-7/arrays/unpacked/slice-equality.sv 4state pass
chapter-7/arrays/unpacked/slice.sv 2state pass
Expand Down Expand Up @@ -1817,8 +1817,8 @@ generated/binary_op/11.4.6--binary_op_wild_neq.sv 2state pass
generated/binary_op/11.4.6--binary_op_wild_neq.sv 4state pass
generated/binary_op/11.4.7--binary_op_log_and.sv 2state pass
generated/binary_op/11.4.7--binary_op_log_and.sv 4state pass
generated/binary_op/11.4.7--binary_op_log_equiv.sv 2state fail
generated/binary_op/11.4.7--binary_op_log_equiv.sv 4state fail
generated/binary_op/11.4.7--binary_op_log_equiv.sv 2state pass
generated/binary_op/11.4.7--binary_op_log_equiv.sv 4state pass
generated/binary_op/11.4.7--binary_op_log_imp.sv 2state pass
generated/binary_op/11.4.7--binary_op_log_imp.sv 4state pass
generated/binary_op/11.4.7--binary_op_log_or.sv 2state pass
Expand Down Expand Up @@ -2451,14 +2451,14 @@ generated/keywords/5.6.2--keyword_xnor.sv 2state pass
generated/keywords/5.6.2--keyword_xnor.sv 4state pass
generated/keywords/5.6.2--keyword_xor.sv 2state pass
generated/keywords/5.6.2--keyword_xor.sv 4state pass
generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_0.sv 2state fail
generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_0.sv 4state fail
generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_1.sv 2state fail
generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_1.sv 4state fail
generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_2.sv 2state fail
generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_2.sv 4state fail
generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_3.sv 2state fail
generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_3.sv 4state fail
generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_0.sv 2state pass
generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_0.sv 4state pass
generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_1.sv 2state pass
generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_1.sv 4state pass
generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_2.sv 2state pass
generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_2.sv 4state pass
generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_3.sv 2state pass
generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_3.sv 4state pass
generated/logical_implication_operator_sim/11.4.7--logical_impl_operator_0.sv 2state pass
generated/logical_implication_operator_sim/11.4.7--logical_impl_operator_0.sv 4state pass
generated/logical_implication_operator_sim/11.4.7--logical_impl_operator_1.sv 2state pass
Expand Down Expand Up @@ -2647,8 +2647,8 @@ generated/sampled_functions/20.13--steady_gclk.sv 2state fail
generated/sampled_functions/20.13--steady_gclk.sv 4state fail
generated/simple_logical_operators_sim/11.4.7--simple_and_logical_operator.sv 2state pass
generated/simple_logical_operators_sim/11.4.7--simple_and_logical_operator.sv 4state pass
generated/simple_logical_operators_sim/11.4.7--simple_equiv_logical_operator.sv 2state fail
generated/simple_logical_operators_sim/11.4.7--simple_equiv_logical_operator.sv 4state fail
generated/simple_logical_operators_sim/11.4.7--simple_equiv_logical_operator.sv 2state pass
generated/simple_logical_operators_sim/11.4.7--simple_equiv_logical_operator.sv 4state pass
generated/simple_logical_operators_sim/11.4.7--simple_impl_logical_operator.sv 2state pass
generated/simple_logical_operators_sim/11.4.7--simple_impl_logical_operator.sv 4state pass
generated/simple_logical_operators_sim/11.4.7--simple_or_logical_operator.sv 2state pass
Expand Down
118 changes: 118 additions & 0 deletions crates/celox-frontend-sv/src/lowering/ff/containers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ impl Ff<'_, '_> {
string_key,
);
let code = |opcode| encode(opcode, parameter);
if op.reduction().is_some() {
return self.container_reduction(op, layout, &args[0], parameter);
}
let zero = self.constant64(0);
let (width, signed) = op.result_type(layout);
let element_result = matches!(op, Get | PopFront | PopBack);
Expand Down Expand Up @@ -298,6 +301,121 @@ impl Ff<'_, '_> {
Ok(sv::ir::Expr::Ident(name))
}

fn container_reduction(
&mut self,
op: ContainerOp,
layout: ContainerLayout,
receiver: &sv::ir::Expr,
parameter: u64,
) -> Result<sv::ir::Expr, sv::AnalyzerError> {
let width = layout.width as usize;
let four_state = !layout.two_state && self.m.four_state;
let alloc = |b: &mut SIRBuilder<Addr>| {
if four_state {
b.alloc_logic(width)
} else {
b.alloc_bit(width, layout.signed)
}
};
let code = |opcode| encode(opcode, parameter);
// Keep one immutable snapshot of the receiver throughout the traversal.
let handle = self.handle(receiver)?;
let zero = self.constant64(0);
let one = self.constant64(1);
let length = self.heap_call(code(heap::LEN), &[handle]);
let associative = matches!(layout.kind, ContainerKind::Associative { .. });
let first = if associative {
self.heap_call(code(heap::FIRST), &[handle, zero])
} else {
zero
};
let seed = alloc(&mut self.b);
let identity = match op {
ContainerOp::ReduceProduct | ContainerOp::LegacyReduceProduct => BigUint::from(1u8),
ContainerOp::ReduceAnd | ContainerOp::LegacyReduceAnd => {
(BigUint::from(1u8) << width) - BigUint::from(1u8)
}
_ => BigUint::zero(),
};
self.b
.emit(SIRInstruction::Imm(seed, SIRValue::new(identity)));
let remaining = self.b.alloc_bit(64, false);
let key = self.b.alloc_bit(64, false);
let accumulator = alloc(&mut self.b);
let header = self.b.new_block_with(vec![remaining, key, accumulator]);
let body = self.b.new_block();
let result = alloc(&mut self.b);
let exit = self.b.new_block_with(vec![result]);
self.b
.seal_block(SIRTerminator::Jump(header, vec![length, first, seed]));
self.b.switch_to_block(header);
let more = self.b.alloc_bit(1, false);
self.b
.emit(SIRInstruction::Binary(more, remaining, BinaryOp::Ne, zero));
self.b.seal_block(SIRTerminator::Branch {
cond: more,
true_block: (body, Vec::new()),
false_block: (exit, vec![accumulator]),
});
self.b.switch_to_block(body);
let raw = self.heap_call(code(heap::GET), &[handle, key, zero]);
let mask = self.heap_call(code(heap::GET_MASK), &[handle, key, zero]);
let element = self.container_join(raw, mask, width, four_state);
let combined = alloc(&mut self.b);
let binary = match op {
ContainerOp::ReduceSum => BinaryOp::Add,
ContainerOp::ReduceProduct | ContainerOp::LegacyReduceProduct => BinaryOp::Mul,
ContainerOp::ReduceAnd | ContainerOp::LegacyReduceAnd => BinaryOp::And,
ContainerOp::ReduceOr => BinaryOp::Or,
ContainerOp::ReduceXor => BinaryOp::Xor,
_ => unreachable!("reduction method"),
};
self.b.emit(SIRInstruction::Binary(
combined,
accumulator,
binary,
element,
));
let next = if associative {
self.heap_call(code(heap::NEXT), &[handle, key])
} else {
let next = self.b.alloc_bit(64, false);
self.b
.emit(SIRInstruction::Binary(next, key, BinaryOp::Add, one));
next
};
let fewer = self.b.alloc_bit(64, false);
self.b
.emit(SIRInstruction::Binary(fewer, remaining, BinaryOp::Sub, one));
self.b
.seal_block(SIRTerminator::Jump(header, vec![fewer, next, combined]));
self.b.switch_to_block(exit);
// The accumulator starts at the operation's identity. Only explicit
// legacy compatibility replaces an empty product/AND with zero.
let value = if matches!(
op,
ContainerOp::LegacyReduceProduct | ContainerOp::LegacyReduceAnd
) {
let empty = self.b.alloc_bit(1, false);
self.b
.emit(SIRInstruction::Binary(empty, length, BinaryOp::Eq, zero));
let empty_value = alloc(&mut self.b);
self.b
.emit(SIRInstruction::Imm(empty_value, SIRValue::new(0u8)));
let value = alloc(&mut self.b);
self.b
.emit(SIRInstruction::Mux(value, empty, empty_value, result));
value
} else {
result
};
let (temp, name) = self
.m
.temp("array_reduction", width, layout.signed, four_state);
self.store(temp, SIROffset::Static(0), width, value);
Ok(sv::ir::Expr::Ident(name))
}

fn container_element(
&mut self,
expr: &sv::ir::Expr,
Expand Down
7 changes: 7 additions & 0 deletions crates/celox-sir-opt/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
#![cfg_attr(
test,
allow(
clippy::disallowed_macros,
reason = "thread-local verification counters exist only in unit tests; non-test builds enforce the ban"
)
)]
//! Backend-independent SIR optimization policy and pass pipeline.

pub type HashMap<K, V> = fxhash::FxHashMap<K, V>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ pub(super) fn branchify_coupled_state_updates(
applied += plans.len();
apply_coupled_state_update_batch(eu, plans, next_block_id, reg_counter);
}

if applied != 0 {
verify_coupled_rewrites(eu);
}
applied
}

Expand All @@ -43,9 +45,25 @@ pub(super) fn branchify_coupled_priority_chains(
applied += 1;
worklist.push_front(merge);
}
if applied != 0 {
verify_coupled_rewrites(eu);
}
applied
}

/// Verify the complete rewrite once, rather than revisiting every unrelated
/// block and definition after each local state update or priority chain.
fn verify_coupled_rewrites(eu: &ExecutionUnit<RegionedAbsoluteAddr>) {
debug_assert_eq!(
{
#[cfg(test)]
super::tests::COUPLED_BATCH_VERIFICATIONS.with(|count| count.set(count.get() + 1));
eu.verify_result()
},
Ok(())
);
}

fn find_coupled_priority_chain_in_block(
eu: &ExecutionUnit<RegionedAbsoluteAddr>,
block_id: BlockId,
Expand Down Expand Up @@ -286,7 +304,6 @@ fn apply_coupled_priority_chain(
terminator: original.terminator,
},
);
debug_assert_eq!(eu.verify_result(), Ok(()));
merge_id
}

Expand Down Expand Up @@ -758,5 +775,4 @@ fn apply_coupled_state_update_batch(
terminator: original.terminator,
},
);
debug_assert_eq!(eu.verify_result(), Ok(()));
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ use crate::ir::{InstanceId, RegisterType, SIRValue};
use celox_design::StateObjectId as VarId;
use num_bigint::BigUint;

thread_local! {
pub(super) static COUPLED_BATCH_VERIFICATIONS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}

fn addr(id: usize) -> RegionedAbsoluteAddr {
RegionedAbsoluteAddr {
region: 0,
Expand Down
Loading
Loading