Skip to main content

chomsky_context/
lib.rs

1#![warn(missing_docs)]
2
3use chomsky_uir::IKun;
4use chomsky_uir::egraph::{Analysis, EGraph, Id};
5
6pub struct ContextInjector;
7
8impl ContextInjector {
9    /// 在意图图中注入 GPU 上下文
10    pub fn inject_gpu<A: Analysis<IKun>>(egraph: &EGraph<IKun, A>, id: Id) -> Id {
11        let gpu_ctx = egraph.add(IKun::GpuContext);
12        egraph.add(IKun::WithContext(gpu_ctx, id))
13    }
14
15    /// 在意图图中注入 CPU 上下文
16    pub fn inject_cpu<A: Analysis<IKun>>(egraph: &EGraph<IKun, A>, id: Id) -> Id {
17        let cpu_ctx = egraph.add(IKun::CpuContext);
18        egraph.add(IKun::WithContext(cpu_ctx, id))
19    }
20
21    /// 在意图图中注入 Async 上下文
22    pub fn inject_async<A: Analysis<IKun>>(egraph: &EGraph<IKun, A>, id: Id) -> Id {
23        let async_ctx = egraph.add(IKun::AsyncContext);
24        egraph.add(IKun::WithContext(async_ctx, id))
25    }
26
27    /// 在意图图中注入 Spatial 上下文
28    pub fn inject_spatial<A: Analysis<IKun>>(egraph: &EGraph<IKun, A>, id: Id) -> Id {
29        let spatial_ctx = egraph.add(IKun::SpatialContext);
30        egraph.add(IKun::WithContext(spatial_ctx, id))
31    }
32
33    /// 注入通用的上下文
34    pub fn inject_context<A: Analysis<IKun>>(
35        egraph: &EGraph<IKun, A>,
36        id: Id,
37        context: IKun,
38    ) -> Id {
39        let ctx_id = egraph.add(context);
40        egraph.add(IKun::WithContext(ctx_id, id))
41    }
42}