use core::{
borrow::{Borrow, BorrowMut},
mem::{size_of, MaybeUninit},
};
use hashbrown::HashMap;
use itertools::Itertools;
use slop_air::{Air, BaseAir};
use slop_algebra::{AbstractField, PrimeField32};
use slop_matrix::Matrix;
use slop_maybe_rayon::prelude::*;
use sp1_core_executor::{
events::{AluEvent, ByteLookupEvent, ByteRecord},
ExecutionRecord, Opcode, Program, CLK_INC, PC_INC,
};
use sp1_derive::AlignedBorrow;
use sp1_hypercube::{air::MachineAir, Word};
use crate::{
adapter::{
register::alu_type::{ALUTypeReader, ALUTypeReaderInput},
state::{CPUState, CPUStateInput},
},
air::{SP1CoreAirBuilder, SP1Operation},
operations::{LtOperationSigned, LtOperationSignedInput},
utils::next_multiple_of_32,
};
pub const NUM_LT_COLS: usize = size_of::<LtCols<u8>>();
#[derive(Default)]
pub struct LtChip;
#[derive(AlignedBorrow, Default, Clone, Copy)]
#[repr(C)]
pub struct LtCols<T> {
pub state: CPUState<T>,
pub adapter: ALUTypeReader<T>,
pub is_slt: T,
pub is_sltu: T,
pub lt_operation: LtOperationSigned<T>,
}
impl LtCols<u32> {
pub fn from_trace_row<F: PrimeField32>(row: &[F]) -> Self {
let sized: [u32; NUM_LT_COLS] =
row.iter().map(|x| x.as_canonical_u32()).collect::<Vec<u32>>().try_into().unwrap();
*sized.as_slice().borrow()
}
}
impl<F: PrimeField32> MachineAir<F> for LtChip {
type Record = ExecutionRecord;
type Program = Program;
fn name(&self) -> &'static str {
"Lt"
}
fn num_rows(&self, input: &Self::Record) -> Option<usize> {
let nb_rows =
next_multiple_of_32(input.lt_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 nb_rows = input.lt_events.len();
let padded_nb_rows = <LtChip as MachineAir<F>>::num_rows(self, input).unwrap();
let chunk_size = std::cmp::max((nb_rows + 1) / num_cpus::get(), 1);
unsafe {
let padding_start = nb_rows * NUM_LT_COLS;
let padding_size = (padded_nb_rows - nb_rows) * NUM_LT_COLS;
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, nb_rows * NUM_LT_COLS) };
values.chunks_mut(chunk_size * NUM_LT_COLS).enumerate().par_bridge().for_each(
|(i, rows)| {
rows.chunks_mut(NUM_LT_COLS).enumerate().for_each(|(j, row)| {
let idx = i * chunk_size + j;
let cols: &mut LtCols<F> = row.borrow_mut();
if idx < nb_rows {
let mut byte_lookup_events = Vec::new();
let event = &input.lt_events[idx];
cols.adapter.populate(&mut byte_lookup_events, event.1);
self.event_to_row(&event.0, cols, &mut byte_lookup_events);
cols.state.populate(&mut byte_lookup_events, event.0.clk, event.0.pc);
}
});
},
);
}
fn generate_dependencies(&self, input: &Self::Record, output: &mut Self::Record) {
let chunk_size = std::cmp::max(input.lt_events.len() / num_cpus::get(), 1);
let blu_batches = input
.lt_events
.par_chunks(chunk_size)
.map(|events| {
let mut blu: HashMap<ByteLookupEvent, usize> = HashMap::new();
events.iter().for_each(|event| {
let mut row = [F::zero(); NUM_LT_COLS];
let cols: &mut LtCols<F> = row.as_mut_slice().borrow_mut();
cols.adapter.populate(&mut blu, event.1);
self.event_to_row(&event.0, cols, &mut blu);
cols.state.populate(&mut blu, event.0.clk, event.0.pc);
});
blu
})
.collect::<Vec<_>>();
output.add_byte_lookup_events_from_maps(blu_batches.iter().collect_vec());
}
fn included(&self, shard: &Self::Record) -> bool {
if let Some(shape) = shard.shape.as_ref() {
shape.included::<F, _>(self)
} else {
!shard.lt_events.is_empty()
}
}
}
impl LtChip {
fn event_to_row<F: PrimeField32>(
&self,
event: &AluEvent,
cols: &mut LtCols<F>,
blu: &mut impl ByteRecord,
) {
cols.lt_operation.populate_signed(
blu,
event.a,
event.b,
event.c,
event.opcode == Opcode::SLT,
);
cols.is_slt = F::from_bool(event.opcode == Opcode::SLT);
cols.is_sltu = F::from_bool(event.opcode == Opcode::SLTU);
}
}
impl<F> BaseAir<F> for LtChip {
fn width(&self) -> usize {
NUM_LT_COLS
}
}
impl<AB> Air<AB> for LtChip
where
AB: SP1CoreAirBuilder,
{
fn eval(&self, builder: &mut AB) {
let main = builder.main();
let local = main.row_slice(0);
let local: &LtCols<AB::Var> = (*local).borrow();
let is_real = local.is_slt + local.is_sltu;
builder.assert_bool(local.is_slt);
builder.assert_bool(local.is_sltu);
builder.assert_bool(is_real.clone());
<LtOperationSigned<AB::F> as SP1Operation<AB>>::eval(
builder,
LtOperationSignedInput::<AB>::new(
local.adapter.b().map(|x| x.into()),
local.adapter.c().map(|x| x.into()),
local.lt_operation,
local.is_slt.into(),
is_real.clone(),
),
);
<CPUState<AB::F> as SP1Operation<AB>>::eval(
builder,
CPUStateInput {
cols: local.state,
next_pc: [
local.state.pc[0] + AB::F::from_canonical_u32(PC_INC),
local.state.pc[1].into(),
local.state.pc[2].into(),
],
clk_increment: AB::Expr::from_canonical_u32(CLK_INC),
is_real: is_real.clone(),
},
);
let opcode = local.is_slt * AB::F::from_canonical_u32(Opcode::SLT as u32)
+ local.is_sltu * AB::F::from_canonical_u32(Opcode::SLTU as u32);
let funct3 = local.is_slt * AB::Expr::from_canonical_u8(Opcode::SLT.funct3().unwrap())
+ local.is_sltu * AB::Expr::from_canonical_u8(Opcode::SLTU.funct3().unwrap());
let funct7 = local.is_slt * AB::Expr::from_canonical_u8(Opcode::SLT.funct7().unwrap_or(0))
+ local.is_sltu * AB::Expr::from_canonical_u8(Opcode::SLTU.funct7().unwrap_or(0));
let (slt_base, slt_imm) = Opcode::SLT.base_opcode();
let slt_imm = slt_imm.expect("SLT immediate opcode not found");
let (sltu_base, sltu_imm) = Opcode::SLTU.base_opcode();
let sltu_imm = sltu_imm.expect("SLTU immediate opcode not found");
let imm_base_difference = slt_base.checked_sub(slt_imm).unwrap();
assert_eq!(imm_base_difference, sltu_base.checked_sub(sltu_imm).unwrap());
let slt_base_expr = AB::Expr::from_canonical_u32(slt_base);
let sltu_base_expr = AB::Expr::from_canonical_u32(sltu_base);
let calculated_base_opcode = local.is_slt * slt_base_expr + local.is_sltu * sltu_base_expr
- AB::Expr::from_canonical_u32(imm_base_difference) * local.adapter.imm_c;
let slt_instr_type = Opcode::SLT.instruction_type().0 as u32;
let slt_instr_type_imm =
Opcode::SLT.instruction_type().1.expect("SLT immediate instruction type not found")
as u32;
let sltu_instr_type = Opcode::SLTU.instruction_type().0 as u32;
let sltu_instr_type_imm =
Opcode::SLTU.instruction_type().1.expect("SLTU immediate instruction type not found")
as u32;
let instr_type_difference = slt_instr_type.checked_sub(slt_instr_type_imm).unwrap();
assert_eq!(
instr_type_difference,
sltu_instr_type.checked_sub(sltu_instr_type_imm).unwrap()
);
let calculated_instr_type = local.is_slt * AB::Expr::from_canonical_u32(slt_instr_type)
+ local.is_sltu * AB::Expr::from_canonical_u32(sltu_instr_type)
- AB::Expr::from_canonical_u32(instr_type_difference) * local.adapter.imm_c;
let alu_reader_input = ALUTypeReaderInput::<AB, AB::Expr>::new(
local.state.clk_high::<AB>(),
local.state.clk_low::<AB>(),
local.state.pc,
opcode,
[calculated_instr_type, calculated_base_opcode, funct3, funct7],
Word::extend_var::<AB>(local.lt_operation.result.u16_compare_operation.bit),
local.adapter,
is_real,
);
ALUTypeReader::<AB::F>::eval(builder, alu_reader_input);
}
}