Skip to content

Commit e1f2ec1

Browse files
committed
Use-elim and loop-uni on fragments
1 parent 0bd5039 commit e1f2ec1

File tree

5 files changed

+43
-49
lines changed

5 files changed

+43
-49
lines changed

compiler/qsc_passes/src/lib.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub mod loop_unification;
1212
pub mod replace_qubit_allocation;
1313
pub mod spec_gen;
1414

15+
use loop_unification::LoopUni;
1516
use miette::Diagnostic;
1617
use qsc_frontend::{
1718
compile::{CompileUnit, PackageStore},
@@ -21,7 +22,9 @@ use qsc_hir::{
2122
assigner::Assigner,
2223
global::Table,
2324
hir::{Item, ItemKind},
25+
mut_visit::MutVisitor,
2426
};
27+
use replace_qubit_allocation::ReplaceQubitAllocation;
2528
use thiserror::Error;
2629

2730
#[derive(Clone, Debug, Diagnostic, Error)]
@@ -38,8 +41,11 @@ pub fn run_default_passes(core: &Table, unit: &mut CompileUnit) -> Vec<Error> {
3841
let spec_errors = spec_gen::generate_specs(core, unit);
3942
let conjugate_errors = conjugate_invert::invert_conjugate_exprs(core, unit);
4043

41-
loop_unification::loop_unification(core, unit);
42-
replace_qubit_allocation::replace_qubit_allocation(unit, store.core());
44+
LoopUni {
45+
assigner: &mut unit.assigner,
46+
}
47+
.visit_package(&mut unit.package);
48+
ReplaceQubitAllocation::new(&mut unit.assigner, store.core()).visit_package(&mut unit.package);
4349

4450
spec_errors
4551
.into_iter()
@@ -49,12 +55,16 @@ pub fn run_default_passes(core: &Table, unit: &mut CompileUnit) -> Vec<Error> {
4955
}
5056

5157
pub fn run_core_passes(unit: &mut CompileUnit) {
52-
loop_unification::loop_unification(unit);
58+
LoopUni {
59+
assigner: &mut unit.assigner,
60+
}
61+
.visit_package(&mut unit.package);
5362
}
5463

5564
pub fn run_default_passes_for_fragment(
5665
core: &Table,
5766
assigner: &mut Assigner,
67+
core_table: &Table,
5868
fragment: &mut Fragment,
5969
) -> Vec<Error> {
6070
let mut errors = Vec::new();
@@ -66,7 +76,8 @@ pub fn run_default_passes_for_fragment(
6676
.into_iter()
6777
.map(Error::ConjInvert),
6878
);
69-
loop_unification::loop_unification_for_stmt(assigner, stmt);
79+
LoopUni { assigner }.visit_stmt(stmt);
80+
ReplaceQubitAllocation::new(assigner, core_table).visit_stmt(stmt);
7081
}
7182
Fragment::Item(Item {
7283
kind: ItemKind::Callable(decl),
@@ -82,7 +93,8 @@ pub fn run_default_passes_for_fragment(
8293
.into_iter()
8394
.map(Error::ConjInvert),
8495
);
85-
loop_unification::loop_unification_for_callable(assigner, decl);
96+
LoopUni { assigner }.visit_callable_decl(decl);
97+
ReplaceQubitAllocation::new(assigner, core_table).visit_callable_decl(decl);
8698
}
8799
Fragment::Item(_) | Fragment::Error(_) => {}
88100
}

compiler/qsc_passes/src/loop_unification.rs

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ use core::panic;
55
use std::{mem::take, rc::Rc};
66

77
use qsc_data_structures::span::Span;
8-
use qsc_frontend::compile::CompileUnit;
98
use qsc_hir::{
109
assigner::Assigner,
1110
global::Table,
1211
hir::{
13-
BinOp, Block, CallableDecl, Expr, ExprKind, Lit, Mutability, NodeId, Pat, PrimField,
14-
PrimTy, Stmt, StmtKind, Ty, UnOp,
12+
BinOp, Block, Expr, ExprKind, Lit, Mutability, NodeId, Pat, PrimField, PrimTy, Stmt,
13+
StmtKind, Ty, UnOp,
1514
},
1615
mut_visit::{walk_expr, MutVisitor},
1716
};
@@ -21,29 +20,9 @@ use crate::common::{create_gen_core_ref, IdentTemplate};
2120
#[cfg(test)]
2221
mod tests;
2322

24-
pub fn loop_unification(core: &Table, unit: &mut CompileUnit) {
25-
let mut pass = LoopUni {
26-
core,
27-
assigner: &mut unit.assigner,
28-
};
29-
pass.visit_package(&mut unit.package);
30-
}
31-
32-
#[allow(clippy::module_name_repetitions)]
33-
pub fn loop_unification_for_stmt(assigner: &mut Assigner, stmt: &mut Stmt) {
34-
let mut pass = LoopUni { assigner };
35-
pass.visit_stmt(stmt);
36-
}
37-
38-
#[allow(clippy::module_name_repetitions)]
39-
pub fn loop_unification_for_callable(assigner: &mut Assigner, decl: &mut CallableDecl) {
40-
let mut pass = LoopUni { assigner };
41-
pass.visit_callable_decl(decl);
42-
}
43-
44-
struct LoopUni<'a> {
45-
core: &'a Table,
46-
assigner: &'a mut Assigner,
23+
pub(crate) struct LoopUni<'a> {
24+
pub(crate) core: &'a Table,
25+
pub(crate) assigner: &'a mut Assigner,
4726
}
4827

4928
impl LoopUni<'_> {

compiler/qsc_passes/src/loop_unification/tests.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@
66
use expect_test::{expect, Expect};
77
use indoc::indoc;
88
use qsc_frontend::compile::{self, compile, PackageStore, SourceMap};
9+
use qsc_hir::mut_visit::MutVisitor;
910

10-
use crate::loop_unification::loop_unification;
11+
use crate::loop_unification::LoopUni;
1112

1213
fn check(file: &str, expect: &Expect) {
1314
let store = PackageStore::new(compile::core());
1415
let sources = SourceMap::new([("test".into(), file.into())], None);
1516
let mut unit = compile(&store, &[], sources);
1617
assert!(unit.errors.is_empty(), "{:?}", unit.errors);
17-
loop_unification(store.core(), &mut unit);
18+
LoopUni {
19+
assigner: &mut unit.assigner,
20+
}
21+
.visit_package(&mut unit.package);
1822
expect.assert_eq(&unit.package.to_string());
1923
}
2024

compiler/qsc_passes/src/replace_qubit_allocation.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
mod tests;
66

77
use qsc_data_structures::span::Span;
8-
use qsc_frontend::compile::CompileUnit;
98
use qsc_hir::{
109
assigner::Assigner,
1110
global::Table,
@@ -19,31 +18,30 @@ use std::{mem::take, rc::Rc};
1918

2019
use crate::common::{create_gen_core_ref, IdentTemplate};
2120

22-
pub fn replace_qubit_allocation(unit: &mut CompileUnit, core_table: &Table) {
23-
let mut pass = ReplaceQubitAllocation {
24-
assigner: &mut unit.assigner,
25-
core_table,
26-
qubits_curr_callable: Vec::new(),
27-
qubits_curr_block: Vec::new(),
28-
prefix_qubits: Vec::new(),
29-
};
30-
pass.visit_package(&mut unit.package);
31-
}
32-
3321
struct QubitIdent {
3422
id: IdentTemplate,
3523
is_array: bool,
3624
}
3725

38-
struct ReplaceQubitAllocation<'a> {
26+
pub(crate) struct ReplaceQubitAllocation<'a> {
3927
assigner: &'a mut Assigner,
4028
core_table: &'a Table,
4129
qubits_curr_callable: Vec<Vec<QubitIdent>>,
4230
qubits_curr_block: Vec<QubitIdent>,
4331
prefix_qubits: Vec<QubitIdent>,
4432
}
4533

46-
impl ReplaceQubitAllocation<'_> {
34+
impl<'a> ReplaceQubitAllocation<'a> {
35+
pub(crate) fn new(assigner: &'a mut Assigner, core_table: &'a Table) -> Self {
36+
Self {
37+
assigner,
38+
core_table,
39+
qubits_curr_callable: Vec::new(),
40+
qubits_curr_block: Vec::new(),
41+
prefix_qubits: Vec::new(),
42+
}
43+
}
44+
4745
fn process_qubit_stmt(
4846
&mut self,
4947
stmt_span: Span,

compiler/qsc_passes/src/replace_qubit_allocation/tests.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
use crate::replace_qubit_allocation::replace_qubit_allocation;
4+
use crate::replace_qubit_allocation::ReplaceQubitAllocation;
55
use expect_test::{expect, Expect};
66
use indoc::indoc;
77
use qsc_frontend::compile::{self, compile, PackageStore, SourceMap};
8+
use qsc_hir::mut_visit::MutVisitor;
89

910
fn check(file: &str, expect: &Expect) {
1011
let store = PackageStore::new(compile::core());
1112
let sources = SourceMap::new([("test".into(), file.into())], None);
1213
let mut unit = compile(&store, &[], sources);
1314
assert!(unit.errors.is_empty(), "{:?}", unit.errors);
14-
replace_qubit_allocation(&mut unit, store.core());
15+
ReplaceQubitAllocation::new(&mut unit.assigner, store.core()).visit_package(&mut unit.package);
1516
expect.assert_eq(&unit.package.to_string());
1617
}
1718

0 commit comments

Comments
 (0)