Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Residualized Intermediate Representation (RIR) #1292

Merged
merged 3 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ members = [
"compiler/qsc_parse",
"compiler/qsc_passes",
"compiler/qsc_project",
"compiler/qsc_rir",
"fuzz",
"katas",
"language_service",
Expand Down
14 changes: 14 additions & 0 deletions compiler/qsc_rir/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "qsc_rir"
authors.workspace = true
homepage.workspace = true
repository.workspace = true
edition.workspace = true
license.workspace = true
version.workspace = true

[dependencies]
qsc_data_structures = { path = "../qsc_data_structures" }

[lints]
workspace = true
4 changes: 4 additions & 0 deletions compiler/qsc_rir/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

pub mod rir;
85 changes: 85 additions & 0 deletions compiler/qsc_rir/src/rir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use qsc_data_structures::index_map::IndexMap;

/// The root of the RIR.
pub struct Program {
pub blocks: IndexMap<BlockId, Block>,
pub callables: IndexMap<CallableId, Callable>,
pub entry: CallableId,
}

/// A unique identifier for a block in a RIR program.
#[derive(Clone, Copy, Default)]
pub struct BlockId(u32);

/// A block is a collection of instructions.
pub struct Block(pub Vec<Instruction>);

/// A unique identifier for a callable in a RIR program.
#[derive(Clone, Copy, Default)]
pub struct CallableId(u32);

/// A callable.
pub struct Callable {
/// The ID of the callable.
pub id: CallableId,
/// The name of the callable.
pub name: String,
/// The input type of the callable.
pub input_type: Vec<Ty>,
/// The output type of the callable.
pub output_type: Option<Ty>,
/// The callable body.
/// N.B. `None` bodys represent an intrinsic.
pub body: Option<BlockId>,
}

pub enum Instruction {
cesarzc marked this conversation as resolved.
Show resolved Hide resolved
Store(Value, Variable),
Call(CallableId, Vec<Value>),
Jump(BlockId),
Branch(Value, BlockId, BlockId),
Add(Value, Value, Variable),
Sub(Value, Value, Variable),
Mul(Value, Value, Variable),
Div(Value, Value, Variable),
LogicalNot(Value, Variable),
LogicalAnd(Value, Value, Variable),
LogicalOr(Value, Value, Variable),
BitwiseNot(Value, Variable),
BitwiseAnd(Value, Value, Variable),
BitwiseOr(Value, Value, Variable),
BitwiseXor(Value, Value, Variable),
}

#[derive(Clone, Copy, Default)]
pub struct VariableId(u32);

pub struct Variable {
pub variable_id: VariableId,
pub ty: Ty,
}

pub enum Ty {
Qubit,
Result,
Boolean,
Integer,
Double,
}

pub enum Value {
Literal(Literal),
Variable(Variable),
}

#[derive(Clone, Copy)]
pub enum Literal {
Qubit(u32),
Result(u32),
Bool(bool),
Integer(i64),
Double(f64),
}
Loading