1- use std:: collections:: { BTreeSet , HashMap } ;
1+ use std:: collections:: HashMap ;
22
33use std:: fmt:: Display ;
44use std:: hash:: Hash ;
@@ -10,14 +10,14 @@ use crate::bus_map::OpenVmBusType;
1010use crate :: extraction_utils:: { get_air_metrics, AirWidthsDiff , OriginalAirs , OriginalVmConfig } ;
1111use crate :: instruction_formatter:: openvm_instruction_formatter;
1212use crate :: memory_bus_interaction:: OpenVmMemoryBusInteraction ;
13- use crate :: opcode:: branch_opcodes_bigint_set;
1413use crate :: powdr_extension:: chip:: PowdrAir ;
14+ use crate :: program:: Prog ;
1515use crate :: utils:: UnsupportedOpenVmReferenceError ;
1616use crate :: OriginalCompiledProgram ;
1717use crate :: { CompiledProgram , SpecializedConfig } ;
1818use itertools:: Itertools ;
1919use openvm_instructions:: instruction:: Instruction as OpenVmInstruction ;
20- use openvm_instructions:: program:: { Program as OpenVmProgram , DEFAULT_PC_STEP } ;
20+ use openvm_instructions:: program:: DEFAULT_PC_STEP ;
2121use openvm_instructions:: VmOpcode ;
2222use openvm_stark_backend:: {
2323 interaction:: SymbolicInteraction ,
@@ -27,7 +27,7 @@ use openvm_stark_sdk::p3_baby_bear::BabyBear;
2727use powdr_autoprecompiles:: adapter:: {
2828 Adapter , AdapterApc , AdapterApcWithStats , AdapterVmConfig , ApcWithStats , PgoAdapter ,
2929} ;
30- use powdr_autoprecompiles:: blocks:: { collect_basic_blocks , BasicBlock , Instruction , Program } ;
30+ use powdr_autoprecompiles:: blocks:: { BasicBlock , Instruction } ;
3131use powdr_autoprecompiles:: evaluation:: { evaluate_apc, EvaluationResult } ;
3232use powdr_autoprecompiles:: expression:: try_convert;
3333use powdr_autoprecompiles:: pgo:: { ApcCandidateJsonExport , Candidate , KnapsackItem } ;
@@ -76,16 +76,6 @@ impl<'a> Adapter for BabyBearOpenVmApcAdapter<'a> {
7676 }
7777}
7878
79- /// A newtype wrapper around `OpenVmProgram` to implement the `Program` trait.
80- /// This is necessary because we cannot implement a foreign trait for a foreign type.
81- pub struct Prog < ' a , F > ( & ' a OpenVmProgram < F > ) ;
82-
83- impl < ' a , F > From < & ' a OpenVmProgram < F > > for Prog < ' a , F > {
84- fn from ( program : & ' a OpenVmProgram < F > ) -> Self {
85- Prog ( program)
86- }
87- }
88-
8979/// A newtype wrapper around `OpenVmInstruction` to implement the `Instruction` trait.
9080/// This is necessary because we cannot implement a foreign trait for a foreign type.
9181#[ derive( Clone , Serialize , Deserialize ) ]
@@ -116,54 +106,21 @@ impl<F: PrimeField32> Instruction<F> for Instr<F> {
116106 }
117107}
118108
119- impl < ' a , F : PrimeField32 > Program < Instr < F > > for Prog < ' a , F > {
120- fn base_pc ( & self ) -> u64 {
121- self . 0 . pc_base as u64
122- }
123-
124- fn pc_step ( & self ) -> u32 {
125- DEFAULT_PC_STEP
126- }
127-
128- fn instructions ( & self ) -> Box < dyn Iterator < Item = Instr < F > > + ' _ > {
129- Box :: new (
130- self . 0
131- . instructions_and_debug_infos
132- . iter ( )
133- . filter_map ( |x| x. as_ref ( ) . map ( |i| Instr ( i. 0 . clone ( ) ) ) ) ,
134- )
135- }
136-
137- fn length ( & self ) -> u32 {
138- self . 0 . instructions_and_debug_infos . len ( ) as u32
139- }
140- }
141-
142109pub fn customize < ' a , P : PgoAdapter < Adapter = BabyBearOpenVmApcAdapter < ' a > > > (
143- OriginalCompiledProgram {
144- exe,
145- vm_config,
146- elf,
147- } : OriginalCompiledProgram ,
110+ original_program : OriginalCompiledProgram ,
148111 config : PowdrConfig ,
149112 pgo : P ,
150113) -> CompiledProgram {
151- let labels = elf. text_labels ( ) ;
152- let debug_info = elf. debug_info ( ) ;
153- let original_config = OriginalVmConfig :: new ( vm_config. clone ( ) ) ;
114+ let original_config = OriginalVmConfig :: new ( original_program. vm_config . clone ( ) ) ;
154115 let airs = original_config. airs ( config. degree_bound . identities ) . expect ( "Failed to convert the AIR of an OpenVM instruction, even after filtering by the blacklist!" ) ;
155116 let bus_map = original_config. bus_map ( ) ;
156117
157- let jumpdest_set = add_extra_targets (
158- & exe. program ,
159- labels. clone ( ) ,
160- exe. program . pc_base ,
161- DEFAULT_PC_STEP ,
162- ) ;
163-
164- let program = Prog ( & exe. program ) ;
165-
166- let range_tuple_checker_sizes = vm_config. sdk . rv32m . unwrap ( ) . range_tuple_checker_sizes ;
118+ let range_tuple_checker_sizes = original_program
119+ . vm_config
120+ . sdk
121+ . rv32m
122+ . unwrap ( )
123+ . range_tuple_checker_sizes ;
167124 let vm_config = VmConfig {
168125 instruction_handler : & airs,
169126 bus_interaction_handler : OpenVmBusInteractionHandler :: new (
@@ -173,13 +130,9 @@ pub fn customize<'a, P: PgoAdapter<Adapter = BabyBearOpenVmApcAdapter<'a>>>(
173130 bus_map : bus_map. clone ( ) ,
174131 } ;
175132
176- // Convert the jump destinations to u64 for compatibility with the `collect_basic_blocks` function.
177- let jumpdest_set = jumpdest_set
178- . iter ( )
179- . map ( |& x| x as u64 )
180- . collect :: < BTreeSet < _ > > ( ) ;
181-
182- let blocks = collect_basic_blocks :: < BabyBearOpenVmApcAdapter > ( & program, & jumpdest_set, & airs) ;
133+ let blocks = original_program. collect_basic_blocks ( config. degree_bound . identities ) ;
134+ let exe = original_program. exe ;
135+ let debug_info = original_program. elf . debug_info ( ) ;
183136 tracing:: info!(
184137 "Got {} basic blocks from `collect_basic_blocks`" ,
185138 blocks. len( )
@@ -264,36 +217,6 @@ pub fn customize<'a, P: PgoAdapter<Adapter = BabyBearOpenVmApcAdapter<'a>>>(
264217 }
265218}
266219
267- /// Besides the base RISCV-V branching instructions, the bigint extension adds two more branching
268- /// instruction classes over BranchEqual and BranchLessThan.
269- /// Those instructions have the form <INSTR rs0 rs1 target_offset ...>, where target_offset is the
270- /// relative jump we're interested in.
271- /// This means that for a given program address A containing the instruction above,
272- /// we add A + target_offset as a target as well.
273- fn add_extra_targets < F : PrimeField32 > (
274- program : & OpenVmProgram < F > ,
275- mut labels : BTreeSet < u32 > ,
276- base_pc : u32 ,
277- pc_step : u32 ,
278- ) -> BTreeSet < u32 > {
279- let branch_opcodes_bigint = branch_opcodes_bigint_set ( ) ;
280- let new_labels = program
281- . instructions_and_debug_infos
282- . iter ( )
283- . enumerate ( )
284- . filter_map ( |( i, instr) | {
285- let instr = instr. as_ref ( ) . unwrap ( ) . 0 . clone ( ) ;
286- let adjusted_pc = base_pc + ( i as u32 ) * pc_step;
287- let op = instr. opcode ;
288- branch_opcodes_bigint
289- . contains ( & op)
290- . then_some ( adjusted_pc + instr. c . as_canonical_u32 ( ) )
291- } ) ;
292- labels. extend ( new_labels) ;
293-
294- labels
295- }
296-
297220pub fn openvm_bus_interaction_to_powdr < F : PrimeField32 > (
298221 interaction : & SymbolicInteraction < F > ,
299222 columns : & [ Arc < String > ] ,
0 commit comments