Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 13 additions & 6 deletions source/compiler/qsc_circuit/src/rir_to_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use qsc_fir::fir::PackageId;
use qsc_partial_eval::{
Callable, CallableType, ConditionCode, FcmpConditionCode, Instruction, Literal, Operand,
VariableId,
rir::{Block, BlockId, Program, Ty, Variable},
rir::{Block, BlockId, Prim, Program, Ty, Variable},
};
use qsc_rir::debug::{DbgInfo, DbgLocationId, DbgScope, DbgScopeId};
use std::{fmt::Display, vec};
Expand Down Expand Up @@ -372,7 +372,8 @@ fn process_variables(
instruction @ (Instruction::Store(..)
| Instruction::BitwiseNot(..)
| Instruction::Alloca(..)
| Instruction::Load(..)) => {
| Instruction::Load(..)
| Instruction::Index(..)) => {
return Err(Error::UnsupportedFeature(format!(
"unsupported instruction in block: {instruction:?}"
)));
Expand Down Expand Up @@ -911,7 +912,7 @@ fn store_expr_in_variable(
panic!("variable {variable_id:?} already stored {old_value:?}, cannot store {expr:?}");
}
if let Expr::Bool(condition_expr) = &expr {
if let Ty::Boolean = var.ty {
if let Ty::Prim(Prim::Boolean) = var.ty {
} else {
return Err(Error::UnsupportedFeature(format!(
"variable {variable_id:?} has type {var_ty:?} but is being assigned a condition expression: {condition_expr:?}",
Expand Down Expand Up @@ -1278,17 +1279,23 @@ fn callable_spec<'a>(
match o {
Operand::Literal(Literal::Integer(_) | Literal::Double(_))
| Operand::Variable(Variable {
ty: Ty::Boolean | Ty::Integer | Ty::Double,
ty: Ty::Prim(Prim::Boolean | Prim::Integer | Prim::Double),
..
}) => {
operand_types.push(OperandType::Arg);
}
Operand::Literal(Literal::Qubit(_))
| Operand::Variable(Variable { ty: Ty::Qubit, .. }) => {
| Operand::Variable(Variable {
ty: Ty::Prim(Prim::Qubit),
..
}) => {
operand_types.push(OperandType::TargetQubit);
}
Operand::Literal(Literal::Result(_))
| Operand::Variable(Variable { ty: Ty::Result, .. }) => {
| Operand::Variable(Variable {
ty: Ty::Prim(Prim::Result),
..
}) => {
operand_types.push(OperandType::TargetResult);
}
o => {
Expand Down
60 changes: 43 additions & 17 deletions source/compiler/qsc_codegen/src/qir/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl ToQir<String> for rir::Literal {
}
}
rir::Literal::Integer(i) => format!("i64 {i}"),
rir::Literal::Pointer => "i8* null".to_string(),
rir::Literal::NullPointer => "i8* null".to_string(),
rir::Literal::Qubit(q) => format!("%Qubit* inttoptr (i64 {q} to %Qubit*)"),
rir::Literal::Result(r) => format!("%Result* inttoptr (i64 {r} to %Result*)"),
rir::Literal::Tag(idx, len) => {
Expand All @@ -43,23 +43,30 @@ impl ToQir<String> for rir::Literal {
"i8* getelementptr inbounds ([{len} x i8], [{len} x i8]* @{idx}, i64 0, i64 0)"
)
}
rir::Literal::Array(_) => {
panic!("array literals are not supported in QIR v1 generation")
}
}
}
}

impl ToQir<String> for rir::Ty {
fn to_qir(&self, _program: &rir::Program) -> String {
fn to_qir(&self, program: &rir::Program) -> String {
match self {
rir::Ty::Boolean => "i1".to_string(),
rir::Ty::Double => "double".to_string(),
rir::Ty::Integer => "i64".to_string(),
rir::Ty::Pointer => "i8*".to_string(),
rir::Ty::Qubit => "%Qubit*".to_string(),
rir::Ty::Result => "%Result*".to_string(),
rir::Ty::Prim(prim) => ToQir::<String>::to_qir(prim, program),
rir::Ty::Array(..) => {
unimplemented!("array types are not supported in QIR v1 generation")
}
}
}
}

impl ToQir<String> for rir::Prim {
fn to_qir(&self, _program: &rir::Program) -> String {
get_prim_ty(*self).to_owned()
}
}

impl ToQir<String> for Option<rir::Ty> {
fn to_qir(&self, program: &rir::Program) -> String {
match self {
Expand Down Expand Up @@ -213,7 +220,9 @@ impl ToQir<String> for rir::Instruction {
rir::Instruction::Sub(lhs, rhs, variable) => {
binop_to_qir("sub", lhs, rhs, *variable, program)
}
rir::Instruction::Alloca(..) | rir::Instruction::Load(..) => {
rir::Instruction::Alloca(..)
| rir::Instruction::Load(..)
| rir::Instruction::Index(..) => {
unimplemented!("advanced instructions are not supported in QIR v1 generation")
}
}
Expand Down Expand Up @@ -522,12 +531,15 @@ fn get_value_as_str(value: &rir::Operand, program: &rir::Program) -> String {
}
}
rir::Literal::Integer(i) => format!("{i}"),
rir::Literal::Pointer => "null".to_string(),
rir::Literal::NullPointer => "null".to_string(),
rir::Literal::Qubit(q) => format!("{q}"),
rir::Literal::Result(r) => format!("{r}"),
rir::Literal::Tag(..) => panic!(
"tag literals should not be used as string values outside of output recording"
),
rir::Literal::Array(..) => {
panic!("array literals are not supported in QIR v1 generation")
}
},
rir::Operand::Variable(var) => ToQir::<String>::to_qir(&var.variable_id, program),
}
Expand All @@ -541,20 +553,30 @@ fn get_value_ty(lhs: &rir::Operand) -> &str {
rir::Literal::Double(_) => get_f64_ty(),
rir::Literal::Qubit(_) => "%Qubit*",
rir::Literal::Result(_) => "%Result*",
rir::Literal::Pointer | rir::Literal::Tag(..) => "i8*",
rir::Literal::NullPointer | rir::Literal::Tag(..) => "i8*",
rir::Literal::Array(_) => {
panic!("array literals are not supported in QIR v1 generation")
}
},
rir::Operand::Variable(var) => get_variable_ty(*var),
}
}

fn get_variable_ty(variable: rir::Variable) -> &'static str {
match variable.ty {
rir::Ty::Integer => "i64",
rir::Ty::Boolean => "i1",
rir::Ty::Double => get_f64_ty(),
rir::Ty::Qubit => "%Qubit*",
rir::Ty::Result => "%Result*",
rir::Ty::Pointer => "i8*",
rir::Ty::Prim(prim) => get_prim_ty(prim),
rir::Ty::Array(..) => unimplemented!("array types are not supported in QIR v1 generation"),
}
}

fn get_prim_ty(prim: rir::Prim) -> &'static str {
match prim {
rir::Prim::Integer => "i64",
rir::Prim::Boolean => "i1",
rir::Prim::Double => get_f64_ty(),
rir::Prim::Qubit => "%Qubit*",
rir::Prim::Result => "%Result*",
rir::Prim::Pointer => "i8*",
}
}

Expand Down Expand Up @@ -646,6 +668,10 @@ impl ToQir<String> for rir::Program {
} else {
"adaptive_profile"
};
assert!(
self.array_literals.is_empty(),
"array literals are not supported in QIR v1 generation"
);
let mut constants = String::default();
for (idx, tag) in self.tags.iter().enumerate() {
// We need to add the tag as a global constant.
Expand Down
24 changes: 12 additions & 12 deletions source/compiler/qsc_codegen/src/qir/v1/instruction_tests/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn logical_and_literals() {
rir::Operand::Literal(rir::Literal::Bool(false)),
rir::Variable {
variable_id: rir::VariableId(0),
ty: rir::Ty::Boolean,
ty: rir::Ty::Prim(rir::Prim::Boolean),
},
);
expect![" %var_0 = and i1 true, false"].assert_eq(&inst.to_qir(&rir::Program::default()));
Expand All @@ -23,15 +23,15 @@ fn logical_and_variables() {
let inst = rir::Instruction::LogicalAnd(
rir::Operand::Variable(rir::Variable {
variable_id: rir::VariableId(1),
ty: rir::Ty::Boolean,
ty: rir::Ty::Prim(rir::Prim::Boolean),
}),
rir::Operand::Variable(rir::Variable {
variable_id: rir::VariableId(2),
ty: rir::Ty::Boolean,
ty: rir::Ty::Prim(rir::Prim::Boolean),
}),
rir::Variable {
variable_id: rir::VariableId(0),
ty: rir::Ty::Boolean,
ty: rir::Ty::Prim(rir::Prim::Boolean),
},
);
expect![" %var_0 = and i1 %var_1, %var_2"].assert_eq(&inst.to_qir(&rir::Program::default()));
Expand All @@ -43,7 +43,7 @@ fn logical_not_true_literal() {
rir::Operand::Literal(rir::Literal::Bool(true)),
rir::Variable {
variable_id: rir::VariableId(0),
ty: rir::Ty::Boolean,
ty: rir::Ty::Prim(rir::Prim::Boolean),
},
);
expect![" %var_0 = xor i1 true, true"].assert_eq(&inst.to_qir(&rir::Program::default()));
Expand All @@ -54,11 +54,11 @@ fn logical_not_variables() {
let inst = rir::Instruction::LogicalNot(
rir::Operand::Variable(rir::Variable {
variable_id: rir::VariableId(1),
ty: rir::Ty::Boolean,
ty: rir::Ty::Prim(rir::Prim::Boolean),
}),
rir::Variable {
variable_id: rir::VariableId(0),
ty: rir::Ty::Boolean,
ty: rir::Ty::Prim(rir::Prim::Boolean),
},
);
expect![" %var_0 = xor i1 %var_1, true"].assert_eq(&inst.to_qir(&rir::Program::default()));
Expand All @@ -70,7 +70,7 @@ fn logical_not_false_literal() {
rir::Operand::Literal(rir::Literal::Bool(false)),
rir::Variable {
variable_id: rir::VariableId(0),
ty: rir::Ty::Boolean,
ty: rir::Ty::Prim(rir::Prim::Boolean),
},
);
expect![" %var_0 = xor i1 false, true"].assert_eq(&inst.to_qir(&rir::Program::default()));
Expand All @@ -83,7 +83,7 @@ fn logical_or_literals() {
rir::Operand::Literal(rir::Literal::Bool(false)),
rir::Variable {
variable_id: rir::VariableId(0),
ty: rir::Ty::Boolean,
ty: rir::Ty::Prim(rir::Prim::Boolean),
},
);
expect![" %var_0 = or i1 true, false"].assert_eq(&inst.to_qir(&rir::Program::default()));
Expand All @@ -94,15 +94,15 @@ fn logical_or_variables() {
let inst = rir::Instruction::LogicalOr(
rir::Operand::Variable(rir::Variable {
variable_id: rir::VariableId(1),
ty: rir::Ty::Boolean,
ty: rir::Ty::Prim(rir::Prim::Boolean),
}),
rir::Operand::Variable(rir::Variable {
variable_id: rir::VariableId(2),
ty: rir::Ty::Boolean,
ty: rir::Ty::Prim(rir::Prim::Boolean),
}),
rir::Variable {
variable_id: rir::VariableId(0),
ty: rir::Ty::Boolean,
ty: rir::Ty::Prim(rir::Prim::Boolean),
},
);
expect![" %var_0 = or i1 %var_1, %var_2"].assert_eq(&inst.to_qir(&rir::Program::default()));
Expand Down
Loading
Loading