Skip to main content

lellm_graph/compiler/
pass.rs

1//! CompilerPass trait — 优化 pass 的统一接口。
2
3use super::context::CompilerContext;
4use crate::Graph;
5use crate::MergeStrategy;
6use crate::state::workflow_state::WorkflowState;
7
8/// 编译器优化 pass trait。
9///
10/// 每个 pass 负责一个特定的优化,
11/// 例如:内联、死代码消除、Barrier 合并等。
12///
13/// # 示例
14///
15/// ```ignore
16/// struct MyPass;
17///
18/// impl<S: WorkflowState, M: MergeStrategy<S>> CompilerPass<S, M> for MyPass {
19///     fn name(&self) -> &str {
20///         "my_pass"
21///     }
22///
23///     fn run(&self, graph: &mut Graph<S, M>, ctx: &mut CompilerContext<S>) -> bool {
24///         // 优化逻辑
25///         false
26///     }
27/// }
28/// ```
29pub trait CompilerPass<S: WorkflowState, M: MergeStrategy<S>>: Send + Sync {
30    /// pass 的名称。
31    fn name(&self) -> &str;
32
33    /// 执行优化 pass。
34    ///
35    /// # 参数
36    ///
37    /// - `graph` — 要优化的图(可变引用)
38    /// - `ctx` — 编译上下文,包含配置和统计信息
39    ///
40    /// # 返回
41    ///
42    /// 如果 pass 修改了图,返回 `true`;
43    /// 否则返回 `false`。
44    fn run(&self, graph: &mut Graph<S, M>, ctx: &mut CompilerContext<S>) -> bool;
45}