Skip to content

Commit 9a01494

Browse files
authored
Refactored const-folding specialization code. (#7992)
1 parent 6f1c706 commit 9a01494

File tree

1 file changed

+19
-29
lines changed

1 file changed

+19
-29
lines changed

crates/cairo-lang-lowering/src/optimizations/const_folding.rs

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ impl<'a> ConstFoldingContext<'a> {
571571
return None;
572572
}
573573

574-
let Ok(Some(func_with_body)) = call_stmt.function.body(self.db) else {
574+
let Ok(Some(mut base)) = call_stmt.function.body(self.db) else {
575575
return None;
576576
};
577577

@@ -584,18 +584,18 @@ impl<'a> ConstFoldingContext<'a> {
584584
return None;
585585
}
586586

587-
let mut const_arg = vec![];
587+
let mut const_args = vec![];
588588
let mut new_args = vec![];
589589
for arg in &call_stmt.inputs {
590590
if let Some(var_info) = self.var_info.get(&arg.var_id) {
591591
if let Some(c) =
592592
self.try_get_specialization_arg(var_info.clone(), self.variables[arg.var_id].ty)
593593
{
594-
const_arg.push(Some(c.clone()));
594+
const_args.push(Some(c.clone()));
595595
continue;
596596
}
597597
}
598-
const_arg.push(None);
598+
const_args.push(None);
599599
new_args.push(*arg);
600600
}
601601

@@ -604,39 +604,29 @@ impl<'a> ConstFoldingContext<'a> {
604604
return None;
605605
}
606606

607-
let (base, args) = match func_with_body.lookup_intern(self.db) {
608-
ConcreteFunctionWithBodyLongId::Semantic(_)
609-
| ConcreteFunctionWithBodyLongId::Generated(_) => {
610-
(func_with_body, const_arg.into_iter().collect())
611-
}
612-
ConcreteFunctionWithBodyLongId::Specialized(specialized_function) => {
613-
// Canonicalize the specialization rather than adding a specialization of a
614-
// specializaed function.
615-
let mut new_args_iter = chain!(const_arg.into_iter(), std::iter::once(None));
616-
let args = specialized_function
617-
.args
618-
.iter()
619-
.map(|arg| {
620-
if arg.is_none() {
621-
return new_args_iter.next().unwrap();
622-
}
623-
arg.clone()
624-
})
625-
.collect();
626-
627-
(specialized_function.base, args)
607+
if let ConcreteFunctionWithBodyLongId::Specialized(specialized_function) =
608+
base.lookup_intern(self.db)
609+
{
610+
// Canonicalize the specialization rather than adding a specialization of a specialized
611+
// function.
612+
base = specialized_function.base;
613+
let mut new_args_iter = const_args.into_iter();
614+
const_args = specialized_function.args.to_vec();
615+
for arg in &mut const_args {
616+
if arg.is_none() {
617+
*arg = new_args_iter.next().unwrap_or_default();
618+
}
628619
}
629-
};
620+
}
630621

631622
// Avoid specializing with the same base as the current function as it may lead to infinite
632623
// specialization.
633624
if base == self.caller_base {
634625
return None;
635626
}
636-
627+
let specialized = SpecializedFunction { base, args: const_args.into() };
637628
let specialized_func_id =
638-
ConcreteFunctionWithBodyLongId::Specialized(SpecializedFunction { base, args })
639-
.intern(self.db);
629+
ConcreteFunctionWithBodyLongId::Specialized(specialized).intern(self.db);
640630

641631
if self.db.priv_should_specialize(specialized_func_id) == Ok(false) {
642632
return None;

0 commit comments

Comments
 (0)