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

Shrink the instruction size, function size #208

Merged
merged 5 commits into from
Jun 11, 2024
Merged
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
1 change: 0 additions & 1 deletion crates/steel-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ anyhow = { version = "1", optional = true }
stacker = { version = "0.1.15", optional = true }



[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
which = { version = "4.4.0" }

Expand Down
10 changes: 5 additions & 5 deletions crates/steel-core/src/compiler/code_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
Captured, Free, Global, HeapAllocated, LetVar, Local, LocallyDefinedFunction,
},
core::{
instructions::Instruction,
instructions::{u24, Instruction},
labels::{resolve_labels, LabeledInstruction},
opcode::OpCode,
},
Expand Down Expand Up @@ -325,14 +325,14 @@ impl<'a> VisitorMut for CodeGenerator<'a> {
// }

if let Some(elem) = self.instructions.get_mut(false_start - 1) {
elem.payload_size = j3;
elem.payload_size = u24::from_usize(j3);
// (*elem).payload_size = false_start;
} else {
stop!(Generic => "out of bounds jump");
}

if let Some(elem) = self.instructions.get_mut(if_idx) {
elem.payload_size = false_start;
elem.payload_size = u24::from_usize(false_start);
// (*elem).payload_size = false_start;
} else {
stop!(Generic => "out of bounds jump");
Expand Down Expand Up @@ -581,7 +581,7 @@ impl<'a> VisitorMut for CodeGenerator<'a> {
self.push(LabeledInstruction::builder(OpCode::ECLOSURE).payload(arity));

if let Some(elem) = self.instructions.get_mut(idx) {
elem.payload_size = closure_body_size;
elem.payload_size = u24::from_usize(closure_body_size);
} else {
stop!(Generic => "out of bounds closure len");
}
Expand Down Expand Up @@ -1011,7 +1011,7 @@ mod code_gen_tests {
let mut found = code_gen
.instructions
.iter()
.map(|x| (x.op_code, x.payload_size))
.map(|x| (x.op_code, x.payload_size.to_usize()))
.collect::<Vec<_>>();

// Wipe out the syntax object id from the PASS
Expand Down
18 changes: 5 additions & 13 deletions crates/steel-core/src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
reader::MultipleArityFunctions, shadow::RenameShadowedVariables, VisitorMutRefUnit,
},
},
core::labels::Expr,
core::{instructions::u24, labels::Expr},
parser::{
expand_visitor::{expand_kernel_in_env, expand_kernel_in_env_with_change},
interner::InternedString,
Expand Down Expand Up @@ -100,7 +100,7 @@ impl DebruijnIndicesInterner {
// }

if let Some(x) = instructions.get_mut(i) {
x.payload_size = idx;
x.payload_size = u24::from_usize(idx);
}
}
(
Expand All @@ -123,7 +123,7 @@ impl DebruijnIndicesInterner {
// }

if let Some(x) = instructions.get_mut(i) {
x.payload_size = idx;
x.payload_size = u24::from_usize(idx);
}
}
_ => {}
Expand Down Expand Up @@ -215,7 +215,7 @@ impl DebruijnIndicesInterner {

// TODO commenting this for now
if let Some(x) = instructions.get_mut(i) {
x.payload_size = idx;
x.payload_size = u24::from_usize(idx);
}
}
Instruction {
Expand Down Expand Up @@ -251,7 +251,7 @@ impl DebruijnIndicesInterner {

// TODO commenting this for now
if let Some(x) = instructions.get_mut(i) {
x.payload_size = idx;
x.payload_size = u24::from_usize(idx);
}
}
_ => {}
Expand Down Expand Up @@ -879,14 +879,6 @@ impl Compiler {
// let mut analysis = Analysis::from_exprs(&expanded_statements);
analysis.populate_captures(&expanded_statements);

// let mut semantic = SemanticAnalysis::from_analysis(&mut expanded_statements, analysis);

// println!("MARKER HERE--------------------------");

// expanded_statements.pretty_print();

// println!("END MARKER---------------------------");

let mut semantic = SemanticAnalysis::from_analysis(&mut expanded_statements, analysis);

// This is definitely broken still
Expand Down
42 changes: 29 additions & 13 deletions crates/steel-core/src/compiler/passes/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,6 @@
lambda_function: &LambdaFunction,
captured_vars: &[(InternedString, ScopeInfo)],
depth: usize,
// arguments: &FxHashMap<InternedString, ScopeInfo>,
arguments: &[(InternedString, ScopeInfo)],
) {
for var in &lambda_function.args {
Expand Down Expand Up @@ -1043,14 +1042,26 @@
impl<'a> VisitorMutUnitRef<'a> for AnalysisPass<'a> {
// TODO: define expressions are not handled by this for stack offset purposes
fn visit_define(&mut self, define: &'a crate::parser::ast::Define) {
self.visit_define_without_body(define, IdentifierStatus::Local);
if matches!(&define.body, ExprKind::LambdaFunction(_)) {
self.visit_define_without_body(define, IdentifierStatus::Local);

let define_ctx = self.defining_context.take();
let define_ctx = self.defining_context.take();

// Mark defining context
self.defining_context = define.name_id();
self.visit_with_tail_call_eligibility(&define.body, false);
self.defining_context = define_ctx;
// Mark defining context
self.defining_context = define.name_id();
self.visit_with_tail_call_eligibility(&define.body, false);
self.defining_context = define_ctx;
} else {
// self.visit_define_without_body(define, IdentifierStatus::Local);

// let define_ctx = self.defining_context.take();

// Mark defining context
// self.defining_context = define.name_id();
self.visit_with_tail_call_eligibility(&define.body, false);
self.visit_define_without_body(define, IdentifierStatus::Local);
// self.defining_context = define_ctx;
}
}

// Quoted values are just constants - lets ignore them for now?
Expand Down Expand Up @@ -1701,6 +1712,10 @@
// Mark that we've seen this one
self.vars_used.push(*ident);

// if ident.resolve().ends_with("website.scm__%#__parser") {
// println!("Visiting {}", ident);
// }

// Check if its a global var - otherwise, we want to check if its a free
// identifier
if let Some(depth) = self.info.scope.height_of(ident) {
Expand Down Expand Up @@ -2102,7 +2117,7 @@
None
}

struct FindCallSiteById<'a, F> {

Check warning on line 2120 in crates/steel-core/src/compiler/passes/analysis.rs

View workflow job for this annotation

GitHub Actions / Test Suite

struct `FindCallSiteById` is never constructed
id: SyntaxObjectId,
analysis: &'a Analysis,
func: F,
Expand All @@ -2120,7 +2135,7 @@
}

// TODO: clean this up a bit
pub fn is_required_call_site(&self, l: &List) -> bool {

Check warning on line 2138 in crates/steel-core/src/compiler/passes/analysis.rs

View workflow job for this annotation

GitHub Actions / Test Suite

method `is_required_call_site` is never used
if let Some(refers_to) = l
.args
.first()
Expand Down Expand Up @@ -4006,9 +4021,6 @@
{
if *func == module_get_interned || *func == proto_hash_get {
// If this is found inside of a macro, do not remove it

// println!("REMOVING: {}", define);

found = true;
break;
}
Expand All @@ -4035,7 +4047,7 @@
if *func == module_get_interned
|| *func == proto_hash_get
{
// println!("REMOVING: {}", define);
// dbg!(format!("REMOVING: {}", define));
found = true;
break;
}
Expand Down Expand Up @@ -4099,7 +4111,7 @@
return true;
}

// println!("REMOVING: {}", name);
// dbg!(format!("REMOVING: {}", name));

return false;
}
Expand Down Expand Up @@ -4141,7 +4153,11 @@
return true;
}

// println!("REMOVING: {}", name);
// dbg!(format!("REMOVING: {}", name));

// if name.resolve().ends_with("parser") {
// println!("Removing: {}", name);
// }

offset += 1;
return false;
Expand Down
35 changes: 19 additions & 16 deletions crates/steel-core/src/compiler/program.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::core::instructions::u24;
use crate::core::labels::Expr;
use crate::parser::span_visitor::get_span;
use crate::rvals::{Result, SteelComplex};
Expand Down Expand Up @@ -95,7 +96,7 @@ pub fn specialize_read_local(instructions: &mut [Instruction]) {
payload_size,
..
}) => {
let op_code = match payload_size {
let op_code = match payload_size.to_u32() {
0 => OpCode::MOVEREADLOCAL0,
1 => OpCode::MOVEREADLOCAL1,
2 => OpCode::MOVEREADLOCAL2,
Expand All @@ -113,7 +114,7 @@ pub fn specialize_read_local(instructions: &mut [Instruction]) {
payload_size,
..
}) => {
let op_code = match payload_size {
let op_code = match payload_size.to_u32() {
0 => OpCode::READLOCAL0,
1 => OpCode::READLOCAL1,
2 => OpCode::READLOCAL2,
Expand Down Expand Up @@ -187,15 +188,15 @@ pub fn convert_call_globals(instructions: &mut [Instruction]) {
..
}),
) => {
let arity = *arity;
let arity = arity.to_usize();
let index = *index;

if let TokenType::Identifier(ident) = ident.ty {
match ident {
_ if ident == *PRIM_CONS_SYMBOL && arity == 2 => {
if let Some(x) = instructions.get_mut(i) {
x.op_code = OpCode::CONS;
x.payload_size = 2;
x.payload_size = u24::from_u32(2);
continue;
}
}
Expand Down Expand Up @@ -279,7 +280,7 @@ pub fn convert_call_globals(instructions: &mut [Instruction]) {
if let Some(x) = instructions.get_mut(i + 1) {
// Leave this as the OpCode::FUNC;
// x.op_code = OpCode::Arity;
x.payload_size = arity;
x.payload_size = u24::from_usize(arity);
}
}
(
Expand All @@ -303,7 +304,7 @@ pub fn convert_call_globals(instructions: &mut [Instruction]) {
_ if ident == *PRIM_CONS_SYMBOL => {
if let Some(x) = instructions.get_mut(i) {
x.op_code = OpCode::CONS;
x.payload_size = 2;
x.payload_size = u24::from_u32(2);
continue;
}
}
Expand Down Expand Up @@ -473,24 +474,26 @@ pub fn inline_num_operations(instructions: &mut [Instruction]) {
}),
) = (push, func)
{
let payload_size = payload_size.to_u32();

let replaced = match *ident {
x if x == *PRIM_PLUS && *payload_size == 2 => Some(OpCode::BINOPADD),
x if x == *PRIM_PLUS && *payload_size > 0 => Some(OpCode::ADD),
x if x == *PRIM_PLUS && payload_size == 2 => Some(OpCode::BINOPADD),
x if x == *PRIM_PLUS && payload_size > 0 => Some(OpCode::ADD),
// x if x == *PRIM_MINUS && *payload_size == 2 => Some(OpCode::BINOPSUB),
x if x == *PRIM_MINUS && *payload_size > 0 => Some(OpCode::SUB),
x if x == *PRIM_DIV && *payload_size > 0 => Some(OpCode::DIV),
x if x == *PRIM_STAR && *payload_size > 0 => Some(OpCode::MUL),
x if x == *PRIM_NUM_EQUAL && *payload_size == 2 => Some(OpCode::NUMEQUAL),
x if x == *PRIM_EQUAL && *payload_size > 0 => Some(OpCode::EQUAL),
x if x == *PRIM_LTE && *payload_size > 0 => Some(OpCode::LTE),
x if x == *PRIM_MINUS && payload_size > 0 => Some(OpCode::SUB),
x if x == *PRIM_DIV && payload_size > 0 => Some(OpCode::DIV),
x if x == *PRIM_STAR && payload_size > 0 => Some(OpCode::MUL),
x if x == *PRIM_NUM_EQUAL && payload_size == 2 => Some(OpCode::NUMEQUAL),
x if x == *PRIM_EQUAL && payload_size > 0 => Some(OpCode::EQUAL),
x if x == *PRIM_LTE && payload_size > 0 => Some(OpCode::LTE),
_ => None,
};

if let Some(new_op_code) = replaced {
let payload_size = *payload_size;
// let payload_size = *payload_size;
if let Some(x) = instructions.get_mut(i) {
x.op_code = new_op_code;
x.payload_size = payload_size;
x.payload_size = u24::from_u32(payload_size);
}

if let Some(x) = instructions.get_mut(i + 1) {
Expand Down
Loading
Loading