Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
8788eda
start porting graph optimizations to use the analysis info system
BramOtte Sep 19, 2025
8c407e2
Use bram's ss_range_analyser
BramOtte Sep 19, 2025
008fa34
Use better signatures
BramOtte Sep 19, 2025
25dd323
use bitset instead of low and high for SSRange
BramOtte Sep 24, 2025
9ea0cad
Speedup coalesce2 pass by not operating on the CompileGraph directly
BramOtte Dec 27, 2025
d1ff52e
Use normalized distance instead of signature and construct new range_…
BramOtte Dec 28, 2025
10b31a7
run cargo fmt
BramOtte Dec 28, 2025
d35b317
Optimize coalesce2 pass by reducing allocations and clones
BramOtte Dec 29, 2025
6588ce9
run cargo fmt
BramOtte Dec 29, 2025
d8999ca
Coalesce output components and keep a list of all the blocks that hav…
BramOtte Dec 29, 2025
a9942f4
Optimize ss_range_analysis by only iterating over nodes that are actu…
BramOtte Dec 30, 2025
952ab14
coalesce2: Use split_at_mut instead of from_raw_parts to allocate inp…
BramOtte Dec 31, 2025
f2a0626
run cargo fmt
BramOtte Dec 31, 2025
4a9d3c8
coalesce2: remove unsafe clone
BramOtte Dec 31, 2025
2c8bcb2
coalesce2: use single insert on nod_map instead of get + insert
BramOtte Dec 31, 2025
c0b32eb
coalesce2: use or_insert instead of insert for nod_map
BramOtte Jan 1, 2026
5b4a299
run cargo fmt
BramOtte Jan 1, 2026
20a5c29
coalesce2: use vec of bool to dedup outputs being selected for the ne…
BramOtte Jan 1, 2026
aa1428d
Incorporate pending ticks into compile_graph::NodeState
BramOtte Feb 13, 2026
2261c96
run cargo fmt
BramOtte Feb 13, 2026
a493996
Fix breakage from rebase
BramOtte Mar 25, 2026
81ee7ee
make coalesce2 dedup links
BramOtte Mar 26, 2026
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
/target
/test_proxy
/generators
/.direnv
/.direnv
/Config.toml
/world
45 changes: 45 additions & 0 deletions crates/redpiler/ril_tests/opt/constant_fold2.ril
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
test_args "--passes=constant-fold-2"

circuit @simple_input {
%constant = constant 1
%repeater1 = repeater 1, false, false, true, [%constant:0], []
%repeater2 = repeater 1, false, false, true, [%repeater1:0], []
%lamp = lamp true, [%repeater2:0]
}

test(@simple_input) circuit @simple {
%lamp = lamp true, [%GLOBAL_CONSTANT15:0]
%GLOBAL_CONSTANT15 = constant 15
}

circuit @comparator_input {
%constant5 = constant 5
%constant4 = constant 4
%constant10 = constant 10
# 5 - 4
%comp_sub1 = comparator subtract, none, false, 1, [%constant5:0], [%constant4:0]
# 4 - 5
%comp_sub2 = comparator subtract, none, false, 1, [%constant4:0], [%constant5:0]
# (10 - 1) - (10 - 5)
%comp_sub3 = comparator subtract, none, false, 1, [%constant10:1], [%constant10:5]
}

test(@comparator_input) circuit @comparator {
%comp_sub2 = comparator subtract, none, false, 1, [%GLOBAL_CONSTANT15:11], [%GLOBAL_CONSTANT15:10]
%comp_sub3 = comparator subtract, none, false, 1, [%GLOBAL_CONSTANT15:6], [%GLOBAL_CONSTANT15:10]
%GLOBAL_CONSTANT15 = constant 15
}

circuit @locking_rep_input {
%constant = constant 1
%lever = lever false
%side_rep = repeater 1, true, false, false, [%lever:0], []
# This repeater should be untouched
%locking_rep = repeater 1, false, false, true, [%constant:0], [%side_rep:0]
}

test(@locking_rep_input) circuit @locking_rep {
%lever = lever false
%side_rep = repeater 1, true, false, false, [%lever:0], []
%GLOBAL_CONSTANT15 = constant 15
}
6 changes: 5 additions & 1 deletion crates/redpiler/src/backend/direct/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,11 @@ fn get_all_input(node: &Node) -> (u8, u8) {

// This function is optimized for input values from 0 to 15 and does not work correctly outside that
// range
fn calculate_comparator_output(mode: ComparatorMode, input_strength: u8, power_on_sides: u8) -> u8 {
pub fn calculate_comparator_output(
mode: ComparatorMode,
input_strength: u8,
power_on_sides: u8,
) -> u8 {
let difference = input_strength.wrapping_sub(power_on_sides);
if difference <= 15 {
match mode {
Expand Down
24 changes: 21 additions & 3 deletions crates/redpiler/src/compile_graph.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use mchprs_blocks::blocks::{ComparatorMode, Instrument};
use mchprs_blocks::BlockPos;
use mchprs_world::{TickEntry, TickPriority};
use petgraph::stable_graph::{NodeIndex, StableGraph};
use smallvec::SmallVec;

Expand Down Expand Up @@ -46,11 +47,18 @@ impl NodeType {
}
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
impl NodeType {
pub fn is_bool(&self) -> bool {
!matches!(self, NodeType::Wire | NodeType::Comparator { .. })
}
}

#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
pub struct NodeState {
pub powered: bool,
pub repeater_locked: bool,
pub output_strength: u8,
pending_ticks: Option<(TickPriority, u32)>,
}

impl NodeState {
Expand All @@ -67,6 +75,7 @@ impl NodeState {
powered,
repeater_locked: locked,
output_strength: if powered { 15 } else { 0 },
..Default::default()
}
}

Expand All @@ -86,10 +95,10 @@ impl NodeState {
}
}

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct Annotations {}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct CompileNode {
pub ty: NodeType,
pub block: SmallVec<[(BlockPos, u32); 1]>,
Expand All @@ -105,6 +114,15 @@ impl CompileNode {
pub fn is_removable(&self) -> bool {
!self.is_input && !self.is_output
}

pub fn add_pending_tick(&mut self, tick: &TickEntry) {
assert!(!self.has_pending_ticks());
self.state.pending_ticks = Some((tick.tick_priority, tick.ticks_left));
}

pub fn has_pending_ticks(&self) -> bool {
self.state.pending_ticks.is_some()
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down
7 changes: 6 additions & 1 deletion crates/redpiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ impl Compiler {
debug!("Starting compile");
let start = Instant::now();

let input = CompilerInput { world, bounds };
let input = CompilerInput {
world,
bounds,
pending_ticks: &ticks,
};
let registry = PassRegistry::default();
let pass_pipeline = passes::build_pass_pipeline::<W>(&registry, &options);
let graph =
Expand Down Expand Up @@ -256,6 +260,7 @@ impl Compiler {
pub struct CompilerInput<'w, W: World> {
pub world: &'w W,
pub bounds: (BlockPos, BlockPos),
pub pending_ticks: &'w [TickEntry],
}

#[cfg(test)]
Expand Down
Loading
Loading