use slop_air::{Air, BaseAir};
use slop_matrix::Matrix;
use sp1_derive::AlignedBorrow;
use std::{
borrow::{Borrow, BorrowMut},
mem::{size_of, MaybeUninit},
};
use crate::{
adapter::{
register::i_type::{ITypeReader, ITypeReaderInput},
state::{CPUState, CPUStateInput},
},
air::{SP1CoreAirBuilder, SP1Operation},
memory::MemoryAccessCols,
operations::{AddressOperation, AddressOperationInput},
utils::next_multiple_of_32,
};
use hashbrown::HashMap;
use itertools::Itertools;
use rayon::iter::{ParallelBridge, ParallelIterator};
use slop_algebra::{AbstractField, PrimeField32};
use sp1_core_executor::{
events::{ByteLookupEvent, ByteRecord, MemInstrEvent, MemoryAccessPosition},
ExecutionRecord, Opcode, Program, CLK_INC, PC_INC,
};
use sp1_hypercube::air::MachineAir;
#[derive(Default)]
pub struct LoadDoubleChip;
pub const NUM_LOAD_DOUBLE_COLUMNS: usize = size_of::<LoadDoubleColumns<u8>>();
#[derive(AlignedBorrow, Default, Debug, Clone, Copy)]
#[repr(C)]
pub struct LoadDoubleColumns<T> {
pub state: CPUState<T>,
pub adapter: ITypeReader<T>,
pub address_operation: AddressOperation<T>,
pub memory_access: MemoryAccessCols<T>,
pub is_real: T,
}
impl<F> BaseAir<F> for LoadDoubleChip {
fn width(&self) -> usize {
NUM_LOAD_DOUBLE_COLUMNS
}
}
impl<F: PrimeField32> MachineAir<F> for LoadDoubleChip {
type Record = ExecutionRecord;
type Program = Program;
fn name(&self) -> &'static str {
"LoadDouble"
}
fn num_rows(&self, input: &Self::Record) -> Option<usize> {
let nb_rows = next_multiple_of_32(
input.memory_load_double_events.len(),
input.fixed_log2_rows::<F, _>(self),
);
Some(nb_rows)
}
fn generate_trace_into(
&self,
input: &ExecutionRecord,
output: &mut ExecutionRecord,
buffer: &mut [MaybeUninit<F>],
) {
let chunk_size =
std::cmp::max((input.memory_load_double_events.len()) / num_cpus::get(), 1);
let padded_nb_rows = <LoadDoubleChip as MachineAir<F>>::num_rows(self, input).unwrap();
let num_event_rows = input.memory_load_double_events.len();
unsafe {
let padding_start = num_event_rows * NUM_LOAD_DOUBLE_COLUMNS;
let padding_size = (padded_nb_rows - num_event_rows) * NUM_LOAD_DOUBLE_COLUMNS;
if padding_size > 0 {
core::ptr::write_bytes(buffer[padding_start..].as_mut_ptr(), 0, padding_size);
}
}
let buffer_ptr = buffer.as_mut_ptr() as *mut F;
let values = unsafe {
core::slice::from_raw_parts_mut(buffer_ptr, padded_nb_rows * NUM_LOAD_DOUBLE_COLUMNS)
};
let blu_events = values
.chunks_mut(chunk_size * NUM_LOAD_DOUBLE_COLUMNS)
.enumerate()
.par_bridge()
.map(|(i, rows)| {
let mut blu: HashMap<ByteLookupEvent, usize> = HashMap::new();
rows.chunks_mut(NUM_LOAD_DOUBLE_COLUMNS).enumerate().for_each(|(j, row)| {
let idx = i * chunk_size + j;
let cols: &mut LoadDoubleColumns<F> = row.borrow_mut();
if idx < input.memory_load_double_events.len() {
let event = &input.memory_load_double_events[idx];
self.event_to_row(&event.0, cols, &mut blu);
cols.state.populate(&mut blu, event.0.clk, event.0.pc);
cols.adapter.populate(&mut blu, event.1);
}
});
blu
})
.collect::<Vec<_>>();
output.add_byte_lookup_events_from_maps(blu_events.iter().collect_vec());
}
fn included(&self, shard: &Self::Record) -> bool {
if let Some(shape) = shard.shape.as_ref() {
shape.included::<F, _>(self)
} else {
!shard.memory_load_double_events.is_empty()
}
}
}
impl LoadDoubleChip {
fn event_to_row<F: PrimeField32>(
&self,
event: &MemInstrEvent,
cols: &mut LoadDoubleColumns<F>,
blu: &mut HashMap<ByteLookupEvent, usize>,
) {
cols.memory_access.populate(event.mem_access, blu);
cols.address_operation.populate(blu, event.b, event.c);
cols.is_real = F::one();
}
}
impl<AB> Air<AB> for LoadDoubleChip
where
AB: SP1CoreAirBuilder,
AB::Var: Sized,
{
#[inline(never)]
fn eval(&self, builder: &mut AB) {
let main = builder.main();
let local = main.row_slice(0);
let local: &LoadDoubleColumns<AB::Var> = (*local).borrow();
let clk_high = local.state.clk_high::<AB>();
let clk_low = local.state.clk_low::<AB>();
let opcode = AB::Expr::from_canonical_u32(Opcode::LD as u32);
let funct3 = AB::Expr::from_canonical_u8(Opcode::LD.funct3().unwrap());
let funct7 = AB::Expr::from_canonical_u8(Opcode::LD.funct7().unwrap_or(0));
let base_opcode = AB::Expr::from_canonical_u32(Opcode::LD.base_opcode().0);
let instr_type = AB::Expr::from_canonical_u32(Opcode::LD.instruction_type().0 as u32);
builder.assert_bool(local.is_real);
let aligned_addr = <AddressOperation<AB::F> as SP1Operation<AB>>::eval(
builder,
AddressOperationInput::new(
local.adapter.b().map(Into::into),
local.adapter.c().map(Into::into),
AB::Expr::zero(),
AB::Expr::zero(),
AB::Expr::zero(),
local.is_real.into(),
local.address_operation,
),
);
builder.eval_memory_access_read(
clk_high.clone(),
clk_low.clone() + AB::Expr::from_canonical_u32(MemoryAccessPosition::Memory as u32),
&aligned_addr.clone().map(Into::into),
local.memory_access,
local.is_real,
);
builder.assert_zero(local.adapter.op_a_0);
<CPUState<AB::F> as SP1Operation<AB>>::eval(
builder,
CPUStateInput::new(
local.state,
[
local.state.pc[0] + AB::F::from_canonical_u32(PC_INC),
local.state.pc[1].into(),
local.state.pc[2].into(),
],
AB::Expr::from_canonical_u32(CLK_INC),
local.is_real.into(),
),
);
<ITypeReader<AB::F> as SP1Operation<AB>>::eval(
builder,
ITypeReaderInput::new(
clk_high,
clk_low,
local.state.pc,
opcode,
[instr_type, base_opcode, funct3, funct7],
local.memory_access.prev_value.map(Into::into),
local.adapter,
local.is_real.into(),
),
);
}
}