Skip to content

Commit 69eea4b

Browse files
committed
Reduce unneeded imports
1 parent 42d489d commit 69eea4b

7 files changed

Lines changed: 47 additions & 25 deletions

File tree

crates/backend/src/builder.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
use calyx_ir as ir;
44
use malachite::Natural;
55

6-
use crate::components::{ComponentManager, Constant};
6+
use crate::components::Constant;
7+
use crate::{ComponentManager, Import};
78

89
pub struct IrBuilder<'a> {
910
pub component: &'a mut ir::Component,
@@ -165,6 +166,24 @@ impl<'a> IrBuilder<'a> {
165166
}
166167
}
167168

169+
pub fn std_reg(&mut self, width: u64) -> ir::RRC<ir::Cell> {
170+
self.cm.import(Import::Core);
171+
172+
self.add_primitive("r", "std_reg", &[width])
173+
}
174+
175+
pub fn std_mux(&mut self, width: u64) -> ir::RRC<ir::Cell> {
176+
self.cm.import(Import::Core);
177+
178+
self.add_primitive("mux", "std_mux", &[width])
179+
}
180+
181+
pub fn std_const(&mut self, value: u64, width: u64) -> ir::RRC<ir::Cell> {
182+
self.cm.import(Import::Core);
183+
184+
self.add_primitive("c", "std_const", &[width, value])
185+
}
186+
168187
pub fn big_constant(
169188
&mut self,
170189
value: &Natural,

crates/backend/src/compile.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ struct Builder<'a, 'src> {
4444

4545
impl Builder<'_, '_> {
4646
fn compile_number(&mut self, number: &hir::Number) -> Option<CompiledExpr> {
47-
let width = u64::from(self.format.width);
4847
let rounded = (&number.value).round_convergent(self.format.lsb());
4948

5049
let Some(value) = rounded.to_fixed_point(self.format) else {
@@ -63,16 +62,14 @@ impl Builder<'_, '_> {
6362
return None;
6463
};
6564

66-
let cell = self.builder.big_constant(&value, width);
65+
let cell = self.builder.big_constant(&value, self.format.width.into());
6766
let port = cell.borrow().get(self.builder.cm.ids.out);
6867

6968
Some(CompiledExpr::from_port(port))
7069
}
7170

7271
fn compile_boolean(&mut self, value: bool) -> Option<CompiledExpr> {
73-
let params = [1, u64::from(value)];
74-
75-
let cell = self.builder.add_primitive("c", "std_const", &params);
72+
let cell = self.builder.std_const(value.into(), 1);
7673
let port = cell.borrow().get(self.builder.cm.ids.out);
7774

7875
Some(CompiledExpr::from_port(port))
@@ -354,11 +351,11 @@ impl Builder<'_, '_> {
354351
let true_branch = self.compile_expression(expr.if_true)?;
355352
let false_branch = self.compile_expression(expr.if_false)?;
356353

357-
let params = [true_branch.out.borrow().width];
354+
let width = true_branch.out.borrow().width;
358355

359356
match (&true_branch.control, &false_branch.control) {
360357
(ir::Control::Empty(_), ir::Control::Empty(_)) => {
361-
let mux = self.builder.add_primitive("mux", "std_mux", &params);
358+
let mux = self.builder.std_mux(width);
362359
let out = mux.borrow().get(self.builder.cm.ids.out);
363360

364361
let inputs = [
@@ -385,7 +382,7 @@ impl Builder<'_, '_> {
385382
})
386383
}
387384
_ => {
388-
let reg = self.builder.add_primitive("r", "std_reg", &params);
385+
let reg = self.builder.std_reg(width);
389386
let out = reg.borrow().get(self.builder.cm.ids.out);
390387

391388
let store_true = self.builder.invoke_with(
@@ -433,8 +430,7 @@ impl Builder<'_, '_> {
433430
let write = &self.hir[write];
434431
let expr = self.compile_expression(write.val)?;
435432

436-
let params = [expr.out.borrow().width];
437-
let reg = self.builder.add_primitive("r", "std_reg", &params);
433+
let reg = self.builder.std_reg(expr.out.borrow().width);
438434

439435
let invoke = self.builder.invoke_with(
440436
reg.clone(),
@@ -474,8 +470,7 @@ impl Builder<'_, '_> {
474470
let write = &self.hir[write];
475471
let init = self.compile_expression(write.val)?;
476472

477-
let params = [init.out.borrow().width];
478-
let reg = self.builder.add_primitive("r", "std_reg", &params);
473+
let reg = self.builder.std_reg(init.out.borrow().width);
479474

480475
let invoke = self.builder.invoke_with(
481476
reg.clone(),

crates/backend/src/components/cast.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ impl ComponentBuilder for Cast<'_> {
5959

6060
("ext", "std_signext")
6161
} else {
62+
builder.cm.import(Import::Core);
63+
6264
("pad", "std_pad")
6365
};
6466

@@ -99,6 +101,8 @@ impl ComponentBuilder for Cast<'_> {
99101
};
100102

101103
let (cell, port) = if msb_out < msb_in || lsb_out > lsb_in {
104+
builder.cm.import(Import::Core);
105+
102106
let in_width = width;
103107
let out_width = u64::from(self.to.width);
104108

crates/backend/src/components/horner.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ impl ComponentBuilder for Horner<'_> {
127127
let mut component = ir::Component::new(name, ports, true, false, None);
128128
let mut builder = IrBuilder::new(&mut component, cm);
129129

130+
builder.cm.import(Import::Core);
130131
builder.cm.import(Import::Numbers);
131132

132133
assert!(self.spec.sum_scale <= self.spec.lut_scale);

crates/backend/src/components/lookup.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use calyx_libm_utils::rational::FixedPoint;
1212
use calyx_libm_utils::{Diagnostic, Format};
1313

1414
use super::{ComponentBuilder, ComponentManager, Ids, Rom};
15-
use crate::IrBuilder;
15+
use crate::{Import, IrBuilder};
1616

1717
/// Packs a sequence of values into a single bit vector. The first element of
1818
/// the sequence occupies the most-significant position.
@@ -137,6 +137,8 @@ impl ComponentBuilder for LookupTable<'_> {
137137
let rom = builder.add_primitive("rom", rom, &[]);
138138
let subtrahend = builder.big_constant(&self.spec.subtrahend, global);
139139

140+
builder.cm.import(Import::Core);
141+
140142
structure!(builder;
141143
let sub = prim std_sub(global);
142144
let slice = prim std_bit_slice(

crates/backend/src/stdlib/importer.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@ pub struct Importer {
99
}
1010

1111
impl Importer {
12-
#[allow(clippy::new_without_default)]
1312
pub fn new() -> Importer {
14-
let mut imports = ImportSet::new();
15-
16-
imports.insert(Import::Core);
17-
18-
Importer { imports }
13+
Importer {
14+
imports: ImportSet::new(),
15+
}
1916
}
2017

2118
pub fn import(&mut self, file: Import) {
@@ -27,14 +24,20 @@ impl Importer {
2724
}
2825

2926
pub fn not(&mut self) -> &'static Primitive<'static> {
27+
self.import(Import::Core);
28+
3029
&primitives::core::STD_NOT
3130
}
3231

3332
pub fn and(&mut self) -> &'static Primitive<'static> {
33+
self.import(Import::Core);
34+
3435
&primitives::core::STD_AND
3536
}
3637

3738
pub fn or(&mut self) -> &'static Primitive<'static> {
39+
self.import(Import::Core);
40+
3841
&primitives::core::STD_OR
3942
}
4043

@@ -105,11 +108,9 @@ impl Importer {
105108
}
106109

107110
pub fn neg(&mut self, _format: &Format) -> &'static Primitive<'static> {
108-
let primitive = &primitives::numbers::NUM_SNEG;
109-
110-
self.import(primitive.import);
111+
self.import(Import::Numbers);
111112

112-
primitive
113+
&primitives::numbers::NUM_SNEG
113114
}
114115

115116
pub fn abs(&mut self, format: &Format) -> &'static Primitive<'static> {

crates/backend/src/stdlib/primitives.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl Primitive<'_> {
7474
pub mod compile {
7575
use super::*;
7676

77-
pub const IMPORT: Import = Import::Compile;
77+
pub const IMPORT: Import = Import::Core; // access through transitive import
7878

7979
pub const STD_WIRE: Primitive = Primitive {
8080
name: "std_wire",

0 commit comments

Comments
 (0)