Skip to content

Commit de718d9

Browse files
authored
Create bus.asm call to expose native BusInteraction to the backend (#2510)
Currently just the bus.asm call is created. Next step will be editing `bus.rs` linker to do this call for backends that needs the native bus interaction. A key quesiton remains whether we need to import the backend crate or create another linker mode in the linker crate in order to distinguish which cases to pass down the native bus interaction vs which cases to call `bus_multi_linker`. A third solution is to make both `bus_multi_linker` and `bus_native_linker` call in `bus.rs`, and this will create both `BusInteraction` and `PhantomBusInteraction` for all backends, and some backends can just ignore the native `BusInteraction`, similar to what we've done for native lookup and permutation.
1 parent f3a56cc commit de718d9

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

linker/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ powdr-analysis.workspace = true
1212
powdr-ast.workspace = true
1313
powdr-number.workspace = true
1414
powdr-parser-util.workspace = true
15+
powdr-pil-analyzer.workspace = true
16+
powdr-pilopt.workspace = true
1517
strum = { version = "0.24.1", features = ["derive"] }
1618

1719
pretty_assertions = "1.4.0"

linker/src/bus.rs

+30
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ impl BusLinker {
312312
mod test {
313313
use std::{fs, path::PathBuf};
314314

315+
use powdr_ast::analyzed::{Analyzed, Identity};
315316
use powdr_ast::object::MachineInstanceGraph;
316317
use powdr_number::{FieldElement, GoldilocksField};
317318

@@ -377,4 +378,33 @@ namespace main__rom(4);
377378
let pil = BusLinker::link(graph, crate::DegreeMode::Vadcop).unwrap();
378379
assert_eq!(extract_main(&format!("{pil}")), expectation);
379380
}
381+
382+
#[test]
383+
fn pass_native_bus_interaction_to_backend() {
384+
let file_name = "../test_data/asm/empty_vm.asm";
385+
let graph = parse_analyze_and_compile_file::<GoldilocksField>(file_name);
386+
let pil = BusLinker::link(graph, crate::DegreeMode::Vadcop).unwrap();
387+
let analyzed: Analyzed<GoldilocksField> = powdr_pil_analyzer::analyze_ast(pil).unwrap();
388+
let optimized = powdr_pilopt::optimize(analyzed);
389+
let native_bus_interactions = optimized
390+
.identities
391+
.iter()
392+
.filter_map(|i| match i {
393+
Identity::BusInteraction(interaction) => Some(interaction),
394+
_ => None,
395+
})
396+
.collect::<Vec<_>>();
397+
398+
let expectation = r#"Constr::BusInteraction(1, 0, [0, main::pc, main::instr__jump_to_operation, main::instr__reset, main::instr__loop, main::instr_return], 1);
399+
Constr::BusInteraction(-main__rom::multiplicity, 0, [0, main__rom::p_line, main__rom::p_instr__jump_to_operation, main__rom::p_instr__reset, main__rom::p_instr__loop, 0], 1);
400+
"#;
401+
let native_bus_interactions =
402+
native_bus_interactions
403+
.iter()
404+
.fold(String::new(), |mut acc, i| {
405+
acc.push_str(&format!("{i}\n"));
406+
acc
407+
});
408+
assert_eq!(native_bus_interactions, expectation);
409+
}
380410
}

std/protocols/bus.asm

+11
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,17 @@ let bus_multi_interaction: expr[], expr[][], expr[], expr[] -> () = constr |ids,
202202
)
203203
}
204204
);
205+
206+
// Add array of native bus interactions
207+
array::new(
208+
input_len,
209+
|i| Constr::BusInteraction(
210+
multiplicities[i],
211+
ids[i],
212+
payloads[i],
213+
latches[i]
214+
)
215+
);
205216
};
206217

207218
/// Compute acc' = acc * (1 - is_first') + multiplicity' / fingerprint(id, payload...),

0 commit comments

Comments
 (0)