cmtc 0.1.2

The cmtc compiler providing cmtir-based passes to generate backends including FIRRTL, SystemVerilog and simulators.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
use std::{
  arch::x86_64::_SIDD_LEAST_SIGNIFICANT, fmt::Debug, path::Path,
  thread::current,
};

use indexmap::IndexMap;

use crate::{TimingExpr, TimingPartialOrder};

use super::*;

#[derive(Clone, Default)]
pub struct RuleSchedStat {
  pub is_combinational: bool,
  // Some if static, None if dynamic
  pub num_cycles: Option<u32>,
  // if the guard op is always true
  pub guard_always_true: bool,
}

impl ToString for RuleSchedStat {
  fn to_string(&self) -> String {
    format!(
      "is_comb? {};\tnum_cycle={:?};\tguard_always_true={}",
      self.is_combinational, self.num_cycles, self.guard_always_true
    )
  }
}

#[derive(Clone)]
pub struct IndexedOp {
  pub idx: Option<usize>,
  pub op: ir::Op,
}

#[derive(Clone, Default)]
pub struct OpsPerStep {
  pub time_expr: ir::TimingExpr,
  pub ops: Vec<IndexedOp>,
}

impl OpsPerStep {
  pub fn new(time_expr: ir::TimingExpr) -> Self {
    Self {
      time_expr,
      ops: Vec::new(),
    }
  }

  pub fn into_inner(self) -> (ir::TimingExpr, Vec<IndexedOp>) {
    (self.time_expr, self.ops)
  }

  pub fn extend(&mut self, ops: impl IntoIterator<Item = IndexedOp>) {
    self.ops.extend(ops);
  }
}

#[derive(Clone)]
pub struct RuleSched {
  pub stat: RuleSchedStat,
  pub hy_steps: IndexMap<ir::TimingExpr, OpsPerStep>,
  pub timing_po: TimingPartialOrder,
  pub index_to_deps: AutoVec<(ir::TimingExpr, Vec<Dep>)>,
  pub texpr_to_deps: IndexMap<ir::TimingExpr, Vec<Dep>>,
  pub end_cycle: AutoVec<TimingExpr>,
  pub exit_cycle: TimingExpr,
}

impl Debug for RuleSched {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    write!(f, "{}", self.stat.to_string())
  }
}

impl Default for RuleSched {
  fn default() -> Self {
    Self {
      stat: RuleSchedStat {
        is_combinational: true,
        num_cycles: Some(0),
        guard_always_true: true,
      },
      hy_steps: IndexMap::new(),
      timing_po: TimingPartialOrder::default(),
      index_to_deps: AutoVec::new(),
      texpr_to_deps: IndexMap::new(),
      end_cycle: AutoVec::new(),
      exit_cycle: TimingExpr::const_(0),
    }
  }
}

impl RuleSched {
  pub fn is_combinational(&self) -> bool {
    self.stat.is_combinational
  }

  // NOTE: this can not be used when MSchedPass during any visit_rule_impl,
  // since it uses module.rule_by_name
  pub fn dump(&self, rule_name: &str, data: &VisitorData) -> String {
    let mut string = String::new();
    string.push_str(&format!("{}: --- \n", rule_name));
    string.push_str(&format!("{}\n", self.stat.to_string()));

    for (texpr, ops) in self.hy_steps.iter() {
      string.push_str(&format!("@{}:\n", texpr.to_string()));
      for op in ops.ops.iter() {
        string.push_str(&format!(
          "\t[{}-th]{}\n",
          op.idx
            .map(|idx| format!("{}", idx))
            .unwrap_or("".to_string()),
          op.op.ir_dump_with(&data.module.values)
        ));
      }
    }

    for (idx, (texpr, deps)) in self.index_to_deps.iter() {
      string.push_str(&format!("{}-th op@{}:\n", idx, texpr.to_string()));
      for dep in deps.iter() {
        string.push_str(&format!("\t{}\n", dep.to_string()));
      }
    }

    for (texpr, deps) in self.texpr_to_deps.iter() {
      string.push_str(&format!("texpr {}:\n", texpr.to_string()));
      for dep in deps.iter() {
        string.push_str(&format!("\t{}\n", dep.to_string()));
      }
    }

    string
  }
}

pub type RuleSchedTable = HashMap<RuleId, RuleSched>;
pub type ValueContextTable = HashMap<RuleId, ValueContext>;

pub struct MSchedPass {
  pub rule_sched_table: RuleSchedTable,
  pub value_context_table: ValueContextTable,
}

impl Visitor for MSchedPass {
  fn name() -> &'static str {
    "msched"
  }

  /// tb module should have its own msched pass
  fn skip_tb() -> bool {
    true
  }

  fn dump_results(&self, data: &mut VisitorData) {
    for (rule_path, rule_sched_info) in self.rule_sched_table.iter() {
      if rule_path.module_name == data.module.name {
        log::debug!(
          "\n{}: {}",
          rule_path.to_string(),
          rule_sched_info.dump(&rule_path.rule_name, data)
        );
      }
    }
  }

  fn prepare_visit_module_impl(
    &mut self,
    data: &mut VisitorData,
  ) -> anyhow::Result<()> {
    self.infer_rule_relations(data)?;
    Ok(())
  }

  fn get_rules_in_order(&mut self, data: &mut VisitorData) -> Vec<ir::Rule> {
    // NOTE: this will change the order of rules in the module, which is not
    // what we want. Thereby, we use before_visit_rules and after_visit_rules to
    // backup and restore the rules.
    self.get_rules_in_topo_order(data)
  }

  // the below two functions are used to backup and restore rules
  // because msched should not change the order of rules in the module
  fn before_visit_rules(
    &mut self,
    data: &mut VisitorData,
  ) -> anyhow::Result<()> {
    data.backup_rules();
    Ok(())
  }

  fn after_visit_rules(
    &mut self,
    data: &mut VisitorData,
  ) -> anyhow::Result<()> {
    data.pop_backup_rules();
    log::debug!(
      "after visiting rules in {}, rule sched infos: \n{:#?}",
      data.module.name(),
      self.rule_sched_table
    );
    Ok(())
  }

  fn visit_rule_impl(
    &mut self,
    data: &mut VisitorData,
  ) -> Result<(Vec<ir::Rule>, Vec<ir::RuleRel>), anyhow::Error> {

    let ops = data.rule().ops.clone();
    let mut new_tl_counter = 0;
    let to_schedule_items: Vec<_> = Self::collect_sched_items(
      ops.into_iter().map(|op| SchedItem::Op(op)),
      &|item, _counter| match item {
        x @ SchedItem::Tl(_, _) => ItemRecursive::Leaf(vec![x]),
        SchedItem::Op(op) => match op.inner() {
          ir::OpEnum::Block(block) => ItemRecursive::Recursive(
            block
              .ops
              .clone()
              .into_iter()
              .map(|op| SchedItem::Op(op))
              .collect(),
          ),
          ir::OpEnum::Timed(ir::TimingIntv { start, end }, op) => {
            ItemRecursive::Recursive(
              std::iter::once(SchedItem::Tl(false, start.clone()))
                .chain(std::iter::once(SchedItem::Op((**op).clone())))
                .chain(std::iter::once(SchedItem::Tl(true, end.clone())))
                .collect(),
            )
          }
          // IfOp should not be recursively flattened, it should be treated as a
          // whole
          // ir::Op::If(ifop) => {}
          _ => ItemRecursive::Leaf(vec![SchedItem::Op(op)]),
        },
      },
      &mut new_tl_counter,
    )
    .collect();

    let (to_schedule_items, _num_cycles) =
      Self::apply_rule_timing(to_schedule_items, data.rule().timing.clone());

    // log::debug!("{:#?}", data.module.values);
    log::debug!(
      "to_schedule_ops: \n{}",
      to_schedule_items
        .iter()
        .enumerate()
        .map(|(i, op)| format!("{}: {}", i, op.dump_with(&data.module.values)))
        .collect::<Vec<_>>()
        .join("\n")
    );

    // let rule_name = data.rule().name().to_string();
    let mut rule_sched_info = RuleSched::default();
    if data.rule().is_single_cycle() {
      rule_sched_info.stat = RuleSchedStat {
        is_combinational: true,
        num_cycles: Some(0),
        guard_always_true: is_true(&mut data.rule().guard_ops.iter()),
      };
      for (idx, sched_item) in to_schedule_items.into_iter().enumerate() {
        if let SchedItem::Op(op) = sched_item {
          rule_sched_info
            .hy_steps
            .entry(TimingExpr::const_(0))
            .or_insert(OpsPerStep {
              time_expr: TimingExpr::const_(0),
              ops: Vec::new(),
            })
            .ops
            .push(IndexedOp {
              idx: Some(idx),
              op: op.clone(),
            });
          rule_sched_info.end_cycle.insert(idx, TimingExpr::const_(0));
        }
      }
    } else {
      anyhow::bail!("multi-cycle rule is not supported");
    }

    let rule_path = data.rule_id();

    let value_context = ValueContext::build(
      &rule_sched_info.hy_steps,
      &rule_sched_info.end_cycle,
      &self.rule_sched_table,
      &self.value_context_table,
      !(rule_sched_info.stat.num_cycles.is_some()
        && rule_sched_info.stat.guard_always_true),
      rule_sched_info.exit_cycle.clone(),
      data,
    )?;

    log::debug!("value_context: \n{}", value_context.dump());

    // add the value_context
    self
      .value_context_table
      .insert(rule_path.clone(), value_context);

    // add the rule_sched_info
    self.rule_sched_table.insert(rule_path, rule_sched_info);
    Ok((vec![data.take_rule()], vec![]))
  }
}

pub enum ItemRecursive {
  Leaf(Vec<SchedItem>),
  Recursive(Vec<SchedItem>),
}

#[derive(Debug, Clone)]
pub enum SchedItem {
  Op(ir::Op),                       // Op(is_or_not, op)
  Tl(bool, Option<ir::TimingExpr>), // Tl(0--open / 1--close, texpr)
}

impl SchedItem {
  pub fn duration(
    &self,
    data: &VisitorData,
    msched: &MSchedPass,
  ) -> Option<u32> {
    match self {
      SchedItem::Op(op) => match op.inner() {
        ir::OpEnum::Invoke(InvokeOp { inst_rule, .. }) => {
          let module_name = data.resolve_path(&inst_rule.path);
          let rule_path: RuleId =
            RuleId::from(&module_name, &inst_rule.rule_name);
          let rule_sched_info = msched.get_sched_info(&rule_path);
          if rule_sched_info.stat.guard_always_true {
            rule_sched_info.stat.num_cycles
          } else {
            None
          }
        }
        _ => Some(0),
      },
      SchedItem::Tl(_, _) => Some(0),
    }
  }

  pub fn dump_with(
    &self,
    value_map: &SlotMap<ir::ValueId, ir::Value>,
  ) -> String {
    match self {
      SchedItem::Op(op) => {
        format!("{}", op.ir_dump_with(value_map))
      }
      SchedItem::Tl(close_or_open, tl) => {
        if *close_or_open {
          format!("{}]", tl.ir_dump_with(value_map))
        } else {
          format!("[{}", tl.ir_dump_with(value_map))
        }
      }
    }
  }
}

impl MSchedPass {
  pub fn new() -> Self {
    Self {
      rule_sched_table: HashMap::new(),
      value_context_table: HashMap::new(),
    }
  }

  pub fn into_inner(self) -> RuleSchedTable {
    self.rule_sched_table
  }

  pub fn get_sched_info(&self, rule_path: &RuleId) -> &RuleSched {
    if let Some(info) = self.rule_sched_table.get(rule_path) {
      info
    } else {
      panic!("rule {} not found", rule_path.to_string());
    }
  }

  fn collect_sched_items(
    items: impl Iterator<Item = SchedItem>,
    flatten_fn: &impl Fn(SchedItem, &mut u32) -> ItemRecursive,
    counter: &mut u32,
  ) -> impl Iterator<Item = SchedItem> {
    let mut collected_items = Vec::new();
    for item in items {
      let recursive = flatten_fn(item, counter);
      match recursive {
        ItemRecursive::Leaf(items) => collected_items.extend(items),
        ItemRecursive::Recursive(items) => collected_items.extend(
          Self::collect_sched_items(items.into_iter(), flatten_fn, counter),
        ),
      }
    }
    collected_items.into_iter()
  }

  fn apply_rule_timing(
    items: Vec<SchedItem>,
    timing: ir::RuleTiming,
  ) -> (Vec<SchedItem>, Option<u32>) {
    match timing {
      ir::RuleTiming::SingleCycle => (items, Some(0)),
      ir::RuleTiming::MultiCycle { num_cycles, intv } => {
        let TimingIntv { start, end } = intv;
        (
          std::iter::once(SchedItem::Tl(false, start))
            .chain(items)
            .chain(std::iter::once(SchedItem::Tl(true, end)))
            .collect(),
          num_cycles,
        )
      }
      _ => unimplemented!(),
    }
  }

  fn setup_directive_deps(
    &mut self,
    items: &[SchedItem],
    data: &VisitorData,
  ) -> DirectiveDepModel {
    let mut dep_model = DirectiveDepModel::new(items, data, self);
    dep_model
  }

  fn setup_defuse_deps(
    &mut self,
    items: &[SchedItem],
    data: &VisitorData,
  ) -> DefUseDepModel {
    let mut dep_model = DefUseDepModel::new(items, data, self);
    dep_model
  }
}

// TODO: the is_true() is a temporary solution (too simple, what about
// true?), should be fixed later
fn is_true(guard: &mut dyn Iterator<Item = &ir::Op>) -> bool {
  guard.next().is_none_or(|op: &ir::Op| {
    if let ir::OpEnum::Lit(ir::LitOp { value, .. }) = op.inner() {
      value.is_true()
    } else {
      false
    }
  })
}