Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto precompiles #2473

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
59 changes: 31 additions & 28 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,36 @@
resolver = "2"

members = [
"powdr",
"powdr-test",
"number",
"parser",
"cargo-powdr",
"cli",
"cli-rs",
"executor",
"jit-compiler",
"riscv",
"parser-util",
"pil-analyzer",
"pipeline",
"pilopt",
"plonky3",
"asm-to-pil",
"asmopt",
"backend",
"ast",
"analysis",
"linker",
"isa-utils",
"airgen",
"riscv-executor",
"riscv-syscalls",
"schemas",
"backend-utils",
"executor-utils",
"powdr",
"powdr-test",
"number",
"parser",
"cargo-powdr",
"cli",
"cli-rs",
"executor",
"jit-compiler",
"riscv",
"parser-util",
"pil-analyzer",
"pipeline",
"pilopt",
"plonky3",
"asm-to-pil",
"asmopt",
"backend",
"ast",
"analysis",
"linker",
"isa-utils",
"airgen",
"riscv-executor",
"riscv-syscalls",
"schemas",
"backend-utils",
"executor-utils",
"precompile_macro",
"autoprecompiles",
]

exclude = ["riscv-runtime"]
Expand All @@ -51,6 +53,7 @@ powdr-asm-to-pil = { path = "./asm-to-pil", version = "0.1.4" }
powdr-isa-utils = { path = "./isa-utils", version = "0.1.4" }
powdr-analysis = { path = "./analysis", version = "0.1.4" }
powdr-asmopt = { path = "./asmopt", version = "0.1.4" }
powdr-autoprecompiles = { path = "./autoprecompiles/", version = "0.1.4" }
powdr-backend = { path = "./backend", version = "0.1.4" }
powdr-backend-utils = { path = "./backend-utils", version = "0.1.4" }
powdr-executor = { path = "./executor", version = "0.1.4" }
Expand Down
2 changes: 2 additions & 0 deletions analysis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ repository = { workspace = true }
[dependencies]
powdr-asm-to-pil.workspace = true
powdr-ast.workspace = true
powdr-autoprecompiles.workspace = true
powdr-number.workspace = true
powdr-parser.workspace = true
powdr-parser-util.workspace = true

itertools = "0.13"
lazy_static = "1.4.0"
Expand Down
194 changes: 194 additions & 0 deletions analysis/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
pub mod machine_check;
mod vm;

use std::collections::{BTreeMap, BTreeSet};
use std::ops::ControlFlow;

use powdr_ast::asm_analysis::InstructionDefinitionStatement;
use powdr_ast::parsed::asm::{
parse_absolute_path, AbsoluteSymbolPath, CallableRef, Instruction, InstructionBody,
LinkDeclaration, MachineParams, OperationId, Param, Params, Part, SymbolPath,
};
use powdr_ast::{asm_analysis::AnalysisASMFile, parsed::asm::ASMProgram};
use powdr_ast::{
asm_analysis::{
CallableSymbol, CallableSymbolDefinitions, FunctionStatement, FunctionStatements,
InstructionStatement, LabelStatement, LinkDefinition, Machine, MachineDegree, Module,
OperationSymbol, RegisterTy, SubmachineDeclaration,
},
parsed::{
visitor::{ExpressionVisitable, VisitOrder},
BinaryOperator, FunctionCall, NamespacedPolynomialReference, Number, PilStatement,
UnaryOperation, UnaryOperator,
},
};
use powdr_number::BigUint;
use powdr_number::FieldElement;
use powdr_parser_util::SourceRef;

type Expression = powdr_ast::asm_analysis::Expression<NamespacedPolynomialReference>;

const MAIN_MACHINE_STR: &str = "::Main";

pub fn convert_asm_to_pil<T: FieldElement>(
file: ASMProgram,
Expand All @@ -11,6 +37,38 @@ pub fn convert_asm_to_pil<T: FieldElement>(
Ok(powdr_asm_to_pil::compile::<T>(file))
}

pub fn check(file: ASMProgram) -> Result<AnalysisASMFile, Vec<String>> {
log::debug!("Run machine check analysis step");
let mut file = machine_check::check(file)?;
annotate_basic_blocks(&mut file);

Ok(file)
}

pub fn analyze_precompiles(
analyzed_asm: AnalysisASMFile,
selected: &BTreeSet<String>,
) -> AnalysisASMFile {
let main_machine_path = parse_absolute_path(MAIN_MACHINE_STR);
if analyzed_asm
.machines()
.all(|(path, _)| path != main_machine_path)
{
return analyzed_asm;
}

powdr_autoprecompiles::powdr::create_precompiles(analyzed_asm, selected)
}

pub fn analyze_only(file: AnalysisASMFile) -> Result<AnalysisASMFile, Vec<String>> {
// run analysis on virtual machines, batching instructions
log::debug!("Start asm analysis");
let file = vm::analyze(file)?;
log::debug!("End asm analysis");

Ok(file)
}

pub fn analyze(file: ASMProgram) -> Result<AnalysisASMFile, Vec<String>> {
log::debug!("Run machine check analysis step");
let file = machine_check::check(file)?;
Expand All @@ -36,3 +94,139 @@ pub mod utils {
PIL_STATEMENT_PARSER.parse(&ctx, input).unwrap()
}
}

pub fn annotate_basic_blocks(analyzed_asm: &mut AnalysisASMFile) {
let machine = analyzed_asm
.get_machine_mut(&parse_absolute_path("::Main"))
.unwrap();
let CallableSymbol::Function(ref mut main_function) =
&mut machine.callable.0.get_mut("main").unwrap()
else {
panic!("main function missing")
};

let program = &mut main_function.body.statements.inner;

let mut ghost_labels = 0;

let mut i = 0;
while i < program.len() {
match &program[i] {
FunctionStatement::Instruction(InstructionStatement {
source,
instruction,
inputs: _,
}) if instruction.starts_with("branch")
|| instruction.starts_with("jump")
|| instruction.starts_with("skip") =>
{
let curr_label = format!("ghost_label_{ghost_labels}");
let new_label = FunctionStatement::Label(LabelStatement {
source: source.clone(),
name: curr_label.clone(),
});
program.insert(i + 1, new_label);
ghost_labels += 1;
i += 1;
}
FunctionStatement::Return(_) | FunctionStatement::Assignment(_) => {
let curr_label = format!("ghost_label_{ghost_labels}");
let new_label = FunctionStatement::Label(LabelStatement {
source: Default::default(),
name: curr_label.clone(),
});
program.insert(i + 1, new_label);
ghost_labels += 1;
i += 1;
}
_ => {}
}
i += 1;
}
}

pub fn collect_basic_blocks(
analyzed_asm: &AnalysisASMFile,
) -> Vec<(String, Vec<FunctionStatement>)> {
let machine = analyzed_asm
.get_machine(&parse_absolute_path("::Main"))
.unwrap();
let CallableSymbol::Function(ref main_function) = &mut machine.callable.0.get("main").unwrap()
else {
panic!("main function missing")
};

let program = &main_function.body.statements.inner;

let mut blocks = Vec::new();
//let ghost_labels = 0;
//let mut curr_label = format!("ghost_label_{ghost_labels}");
//let mut curr_label = "block_init".to_string();
let mut curr_label: Option<String> = None;
let mut block_statements = Vec::new();

for op in program {
match &op {
FunctionStatement::Label(LabelStatement { source: _, name }) => {
if let Some(label) = curr_label {
assert!(!blocks.iter().any(|(l, _)| l == &label));
blocks.push((label.clone(), block_statements.clone()));
}
block_statements.clear();
curr_label = Some(name.clone());
}
FunctionStatement::Instruction(InstructionStatement {
source: _,
instruction,
inputs: _,
}) if instruction.starts_with("branch") || instruction.starts_with("jump") => {
block_statements.push(op.clone());
if let Some(label) = curr_label {
assert!(!blocks.iter().any(|(l, _)| l == &label));
blocks.push((label.clone(), block_statements.clone()));
}
block_statements.clear();
curr_label = None;
//assert!(!blocks.iter().any(|(label, _)| label == &curr_label));
//blocks.push((curr_label.clone(), block_statements.clone()));
//block_statements.clear();
//ghost_labels += 1;
//curr_label = format!("ghost_label_{ghost_labels}");
}
FunctionStatement::Return(_) | FunctionStatement::Assignment(_) => {
if let Some(label) = curr_label {
assert!(!blocks.iter().any(|(l, _)| l == &label));
blocks.push((label.clone(), block_statements.clone()));
}
block_statements.clear();
curr_label = None;
//blocks.push((curr_label.clone(), block_statements.clone()));
//block_statements.clear();
//ghost_labels += 1;
//curr_label = format!("ghost_label_{ghost_labels}");
}
FunctionStatement::Instruction(InstructionStatement {
source: _,
instruction,
inputs: _,
}) if instruction.starts_with("skip") => {
if let Some(label) = curr_label {
assert!(!blocks.iter().any(|(l, _)| l == &label));
blocks.push((label.clone(), block_statements.clone()));
}
block_statements.clear();
curr_label = None;
}
FunctionStatement::Instruction(InstructionStatement {
source: _,
instruction: _,
inputs: _,
}) => {
block_statements.push(op.clone());
}
_ => {}
}
}

blocks
}
1 change: 1 addition & 0 deletions asm-to-pil/src/vm_to_constrained.rs
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,7 @@ impl<T: FieldElement> VMConverter<T> {
for (coeff, item) in value {
match item {
AffineExpressionComponent::Register(reg) => {
println!("p_read_{assign_reg}_{reg}");
rom_constants
.get_mut(&format!("p_read_{assign_reg}_{reg}"))
.unwrap()[i] += *coeff;
Expand Down
1 change: 1 addition & 0 deletions asmopt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repository.workspace = true
[dependencies]
powdr-ast.workspace = true
powdr-analysis.workspace = true
powdr-number.workspace = true
powdr-pilopt.workspace = true
powdr-parser.workspace = true

Expand Down
4 changes: 2 additions & 2 deletions asmopt/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::collections::{HashMap, HashSet};
use std::iter::once;

use powdr_ast::parsed::asm::parse_absolute_path;
use powdr_ast::parsed::asm::{parse_absolute_path, AbsoluteSymbolPath};
use powdr_ast::{
asm_analysis::{AnalysisASMFile, Machine},
parsed::{asm::AbsoluteSymbolPath, NamespacedPolynomialReference},
parsed::NamespacedPolynomialReference,
};
use powdr_pilopt::referenced_symbols::ReferencedSymbols;

Expand Down
4 changes: 4 additions & 0 deletions ast/src/asm_analysis/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ impl Display for MachineDegree {

impl Display for Machine {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
let params = &self.params.0;
if !params.is_empty() {
write!(f, "({params}) ", params = params.iter().format(", "))?;
}
let props = std::iter::once(&self.degree)
.map(|d| format!("{d}"))
.chain(self.latch.as_ref().map(|s| format!("latch: {s}")))
Expand Down
8 changes: 7 additions & 1 deletion ast/src/asm_analysis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub fn combine_flags(instr_flag: Option<Expression>, link_flag: Expression) -> E

#[derive(Clone, Debug, Default)]
pub struct FunctionStatements {
inner: Vec<FunctionStatement>,
pub inner: Vec<FunctionStatement>,
batches: Option<Vec<BatchMetadata>>,
}

Expand Down Expand Up @@ -837,6 +837,12 @@ impl AnalysisASMFile {
let name = path.pop()?;
self.modules.get(&path)?.machines.get(&name)
}

pub fn get_machine_mut(&mut self, ty: &AbsoluteSymbolPath) -> Option<&mut Machine> {
let mut path = ty.clone();
let name = path.pop().unwrap();
self.modules.get_mut(&path).unwrap().machines.get_mut(&name)
}
}

#[derive(Clone, Debug)]
Expand Down
24 changes: 24 additions & 0 deletions autoprecompiles/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "powdr-autoprecompiles"
version.workspace = true
edition.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true

[dependencies]
powdr-asm-to-pil.workspace = true
powdr-ast.workspace = true
powdr-number.workspace = true
powdr-parser.workspace = true
powdr-parser-util.workspace = true

itertools = "0.13"
lazy_static = "1.4.0"
log = "0.4.18"

[package.metadata.cargo-udeps.ignore]
development = ["env_logger"]

[lints]
workspace = true
Loading
Loading