Skip to content

Add SpecializedFunction. #7718

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

Draft
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions crates/cairo-lang-lowering/src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,7 @@ impl FunctionCached {
FunctionLongId::Generated(id) => {
FunctionCached::Generated(GeneratedFunctionCached::new(id, ctx))
}
FunctionLongId::Specialized(_) => todo!(),
}
}
fn embed(self, ctx: &mut CacheLoadingContext<'_>) -> FunctionId {
Expand Down
1 change: 1 addition & 0 deletions crates/cairo-lang-lowering/src/concretize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fn concretize_function(
})
.intern(db))
}
FunctionLongId::Specialized(_) => unreachable!("This should not be called."),
}
}

Expand Down
85 changes: 77 additions & 8 deletions crates/cairo-lang-lowering/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ use cairo_lang_utils::unordered_hash_map::UnorderedHashMap;
use cairo_lang_utils::unordered_hash_set::UnorderedHashSet;
use cairo_lang_utils::{Intern, LookupIntern, Upcast};
use defs::ids::NamedLanguageElementId;
use itertools::{Itertools, chain};
use itertools::{Itertools, chain, zip_eq};
use num_traits::ToPrimitive;

use crate::add_withdraw_gas::add_withdraw_gas;
use crate::blocks::Blocks;
use crate::blocks::{Blocks, BlocksBuilder};
use crate::borrow_check::{
PotentialDestructCalls, borrow_check, borrow_check_possible_withdraw_gas,
};
Expand All @@ -29,16 +29,18 @@ use crate::concretize::concretize_lowered;
use crate::destructs::add_destructs;
use crate::diagnostic::{LoweringDiagnostic, LoweringDiagnosticKind};
use crate::graph_algorithms::feedback_set::flag_add_withdraw_gas;
use crate::ids::{FunctionId, FunctionLongId};
use crate::ids::{FunctionId, FunctionLongId, LocationId};
use crate::inline::get_inline_diagnostics;
use crate::lower::context::{VarRequest, VariableAllocator};
use crate::lower::{MultiLowering, lower_semantic_function};
use crate::optimizations::config::OptimizationConfig;
use crate::optimizations::scrub_units::scrub_units;
use crate::optimizations::strategy::{OptimizationStrategy, OptimizationStrategyId};
use crate::panic::lower_panics;
use crate::utils::InliningStrategy;
use crate::{
BlockId, DependencyType, FlatBlockEnd, FlatLowered, Location, MatchInfo, Statement, ids,
BlockId, DependencyType, FlatBlockEnd, FlatLowered, Location, MatchInfo, Statement,
StatementCall, StatementConst, VarUsage, VariableId, ids,
};

// Salsa database interface.
Expand Down Expand Up @@ -432,6 +434,9 @@ fn priv_function_with_body_lowering(
ids::FunctionWithBodyLongId::Generated { key, .. } => {
multi_lowering.generated_lowerings[key].clone()
}
ids::FunctionWithBodyLongId::Specialized(_specialized) => {
unreachable!("There is no generic version of a specialized function.")
}
};
Ok(Arc::new(lowered))
}
Expand Down Expand Up @@ -471,6 +476,7 @@ fn priv_concrete_function_with_body_lowered_flat(
function: ids::ConcreteFunctionWithBodyId,
) -> Maybe<Arc<FlatLowered>> {
let semantic_db = db;

let generic_function_id = function.function_with_body_id(db);
db.function_with_body_lowering_diagnostics(generic_function_id)?.check_error_free()?;
let mut lowered = (*db.function_with_body_lowering(generic_function_id)?).clone();
Expand All @@ -485,11 +491,74 @@ fn concrete_function_with_body_postpanic_lowered(
db: &dyn LoweringGroup,
function: ids::ConcreteFunctionWithBodyId,
) -> Maybe<Arc<FlatLowered>> {
let mut lowered = (*db.priv_concrete_function_with_body_lowered_flat(function)?).clone();
let mut lowered = if let ids::ConcreteFunctionWithBodyLongId::Specialized(specialized) =
function.lookup_intern(db)
{
let base = db.concrete_function_with_body_postpanic_lowered(specialized.base)?;

let base_semantic = specialized.base.base_semantic_function(db);

let mut variables = VariableAllocator::new(
db,
base_semantic.function_with_body_id(db),
Default::default(),
)?;
let mut statement = vec![];
let mut parameters = vec![];
for (param, arg) in zip_eq(&base.parameters, specialized.args.iter()) {
let var_id = variables.variables.alloc(base.variables[*param].clone());
if let Some(arg) = arg {
statement
.push(Statement::Const(StatementConst { value: arg.clone(), output: var_id }));
continue;
}
parameters.push(var_id);
}

add_withdraw_gas(db, function, &mut lowered)?;
lower_panics(db, function, &mut lowered)?;
add_destructs(db, function, &mut lowered);
let location = LocationId::from_stable_location(
db,
specialized.base.base_semantic_function(db).stable_location(db),
);
let inputs =
variables.variables.iter().map(|(var_id, _)| VarUsage { var_id, location }).collect();

let outputs: Vec<VariableId> = chain!(
base.signature.extra_rets.iter().map(|ret| ret.ty()),
[base.signature.return_type]
)
.map(|ty| variables.new_var(VarRequest { ty, location }))
.collect_vec();

let mut block_builder = BlocksBuilder::new();

let ret_usage =
outputs.iter().map(|var_id| VarUsage { var_id: *var_id, location }).collect_vec();

statement.push(Statement::Call(StatementCall {
function: specialized.base.function_id(db)?,
with_coupon: false,
inputs,
outputs,
location,
}));
block_builder.alloc(crate::FlatBlock {
statements: statement,
end: FlatBlockEnd::Return(ret_usage, location),
});
FlatLowered {
signature: function.signature(db)?,
variables: variables.variables,
blocks: block_builder.build().unwrap(),
parameters,
diagnostics: Default::default(),
}
} else {
let mut lowered = (*db.priv_concrete_function_with_body_lowered_flat(function)?).clone();
add_withdraw_gas(db, function, &mut lowered)?;
lower_panics(db, function, &mut lowered)?;
add_destructs(db, function, &mut lowered);
lowered
};
scrub_units(db, &mut lowered);

Ok(Arc::new(lowered))
Expand Down
Loading
Loading