Skip to content

Use different cycle prevention in inlining. #7742

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

Merged
merged 1 commit into from
May 14, 2025
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
9 changes: 9 additions & 0 deletions crates/cairo-lang-lowering/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,15 @@ pub trait LoweringGroup: SemanticGroup + Upcast<dyn SemanticGroup> {
dependency_type: DependencyType,
) -> Maybe<bool>;

/// A concrete version of `in_cycle`.
#[salsa::invoke(crate::graph_algorithms::cycles::concrete_in_cycle)]
fn concrete_in_cycle(
&self,
function_id: ids::ConcreteFunctionWithBodyId,
dependency_type: DependencyType,
stage: LoweringStage,
) -> Maybe<bool>;

// ### Strongly connected components ###

/// Returns the representative of the concrete function's strongly connected component. The
Expand Down
16 changes: 16 additions & 0 deletions crates/cairo-lang-lowering/src/graph_algorithms/cycles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,19 @@ pub fn in_cycle(
}
Ok(db.function_with_body_scc(function_id, dependency_type).len() > 1)
}

/// Query implementation of [LoweringGroup::concrete_in_cycle].
pub fn concrete_in_cycle(
db: &dyn LoweringGroup,
function_id: ConcreteFunctionWithBodyId,
dependency_type: DependencyType,
stage: LoweringStage,
) -> Maybe<bool> {
if db
.lowered_direct_callees_with_body(function_id, dependency_type, stage)?
.contains(&function_id)
{
return Ok(true);
}
Ok(db.lowered_scc(function_id, dependency_type, stage).len() > 1)
}
10 changes: 3 additions & 7 deletions crates/cairo-lang-lowering/src/inline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use crate::ids::{
use crate::lower::context::{VarRequest, VariableAllocator};
use crate::utils::{InliningStrategy, Rebuilder, RebuilderEx};
use crate::{
Block, BlockEnd, BlockId, Lowered, LoweringStage, Statement, StatementCall, VarRemapping,
VariableId,
Block, BlockEnd, BlockId, DependencyType, Lowered, LoweringStage, Statement, StatementCall,
VarRemapping, VariableId,
};

pub fn get_inline_diagnostics(
Expand Down Expand Up @@ -59,11 +59,7 @@ pub fn priv_should_inline(
function_id: ConcreteFunctionWithBodyId,
) -> Maybe<bool> {
// Breaks cycles.
// TODO(ilya): consider #[inline(never)] attributes for feedback set.
if db
.function_with_body_feedback_set(function_id, LoweringStage::Monomorphized)?
.contains(&function_id)
{
if db.concrete_in_cycle(function_id, DependencyType::Call, LoweringStage::Monomorphized)? {
return Ok(false);
}

Expand Down
80 changes: 12 additions & 68 deletions crates/cairo-lang-lowering/src/inline/test_data/inline
Original file line number Diff line number Diff line change
Expand Up @@ -982,8 +982,8 @@ End:
//! > test #[inline] with indirect self call.

//! > Motivation:
//! #[inline] functions should inline a function unless it is recursively called by itself, even
//! indirectly. In this case, the function bar should be inlined, but not the inner call to foo.
//! #[inline] functions should inline a function unless they are on a call cycle.
//! in this test `bar` should not be inlined into `foo`.

//! > test_runner_name
test_function_inlining
Expand Down Expand Up @@ -1066,8 +1066,12 @@ End:

blk1:
Statements:
(v3: core::panics::PanicResult::<(core::felt252,)>) <- test::bar(v0)
End:
Goto(blk7, {})
Match(match_enum(v3) {
PanicResult::Ok(v4) => blk4,
PanicResult::Err(v6) => blk5,
})

blk2:
Statements:
Expand Down Expand Up @@ -1095,38 +1099,6 @@ Statements:
End:
Return(v11)

blk6:
Statements:
End:
Match(match_enum(v3) {
PanicResult::Ok(v4) => blk4,
PanicResult::Err(v6) => blk5,
})

blk7:
Statements:
End:
Match(match core::felt252_is_zero(v0) {
IsZeroResult::Zero => blk8,
IsZeroResult::NonZero(v12) => blk9,
})

blk8:
Statements:
(v13: core::felt252) <- 1
(v14: core::felt252) <- core::felt252_sub(v0, v13)
(v15: core::panics::PanicResult::<(core::felt252,)>) <- test::foo(v14)
End:
Goto(blk6, {v15 -> v3})

blk9:
Statements:
(v16: core::felt252) <- 1
(v17: (core::felt252,)) <- struct_construct(v16)
(v18: core::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Ok(v17)
End:
Goto(blk6, {v18 -> v3})

//! > lowering_diagnostics

//! > ==========================================================================
Expand Down Expand Up @@ -1621,8 +1593,12 @@ End:

blk1:
Statements:
(v3: core::panics::PanicResult::<(core::felt252,)>) <- test::bar(v0)
End:
Goto(blk7, {})
Match(match_enum(v3) {
PanicResult::Ok(v4) => blk4,
PanicResult::Err(v6) => blk5,
})

blk2:
Statements:
Expand Down Expand Up @@ -1650,38 +1626,6 @@ Statements:
End:
Return(v11)

blk6:
Statements:
End:
Match(match_enum(v3) {
PanicResult::Ok(v4) => blk4,
PanicResult::Err(v6) => blk5,
})

blk7:
Statements:
End:
Match(match core::felt252_is_zero(v0) {
IsZeroResult::Zero => blk8,
IsZeroResult::NonZero(v12) => blk9,
})

blk8:
Statements:
(v13: core::felt252) <- 1
(v14: core::felt252) <- core::felt252_sub(v0, v13)
(v15: core::panics::PanicResult::<(core::felt252,)>) <- test::foo(v14)
End:
Goto(blk6, {v15 -> v3})

blk9:
Statements:
(v16: core::felt252) <- 1
(v17: (core::felt252,)) <- struct_construct(v16)
(v18: core::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Ok(v17)
End:
Goto(blk6, {v18 -> v3})

//! > lowering_diagnostics

//! > ==========================================================================
Expand Down