-
Notifications
You must be signed in to change notification settings - Fork 3
/
instruction.rs
343 lines (306 loc) · 12.4 KB
/
instruction.rs
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
// Copyright 2022 Rigetti Computing
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::convert::{TryFrom, TryInto};
// Functions which operate on and extract information from `inkwell` `InstructionValue`s
use either::Either;
use eyre::{eyre, Result, WrapErr};
use inkwell::{
basic_block::BasicBlock,
types::AnyTypeEnum,
values::{
BasicValue, BasicValueEnum, FloatValue, InstructionOpcode, InstructionValue, IntValue,
PhiValue, PointerValue,
},
};
use crate::context::QCSCompilerContext;
pub(crate) fn get_called_function_name(instruction: InstructionValue) -> Result<Option<String>> {
let intrinsic_function_target = instruction
.get_operand(instruction.get_num_operands() - 1)
.ok_or_else(|| eyre!("expected a final operand in Call instruction"))?;
match intrinsic_function_target {
Either::Left(BasicValueEnum::PointerValue(ptr_value)) => Ok(ptr_value
.get_name()
.to_str()
.ok()
.map(std::borrow::ToOwned::to_owned)),
// TODO: Implement
_ => Err(eyre!(
"BasicBlock target of function call is not yet implemented"
)),
}
}
#[derive(Debug)]
pub(crate) enum OperationArgument<'ctx> {
Qubit(u64),
Result(u64),
Parameter(FloatValue<'ctx>),
Instruction(InstructionValue<'ctx>),
}
/// Return the arguments used to invoke a quantum runtime intrinsic, `@__quantum__qis__*__body`, in order.
pub(crate) fn get_qis_function_arguments<'ctx>(
context: &QCSCompilerContext,
instruction: InstructionValue<'ctx>,
) -> Result<Vec<OperationArgument<'ctx>>> {
let operand_count = instruction.get_num_operands();
// The final operand of a call instruction is the function being called
(0..operand_count - 1)
.map(|operand_index| {
let target = instruction
.get_operand(operand_index)
.ok_or_else(|| eyre!("expected a first operand in Call instruction"))?;
if let Either::Left(BasicValueEnum::PointerValue(ptr_value)) = target {
if let AnyTypeEnum::StructType(struct_type) =
ptr_value.get_type().get_element_type()
{
let type_name = struct_type
.get_name()
.ok_or_else(|| eyre!("expected struct type to have name"))?;
match type_name
.to_str()
.wrap_err("unable to convert C String to string")?
{
"Qubit" => {
let qubit_index =
pointer_value_to_u64(context, ptr_value).ok_or_else(|| {
eyre!("qubit index must be a non-negative number")
})?;
Ok(OperationArgument::Qubit(qubit_index))
}
"Result" => {
let result_index = pointer_value_to_u64(context, ptr_value)
.ok_or_else(|| {
eyre!("unable to derive Result index from pointer")
})?;
Ok(OperationArgument::Result(result_index))
}
// TODO: Ensure all required types are supported
other => Err(eyre!(
"got unexpected type {} as argument to {:?}",
other,
instruction
)),
}
} else if let Some(inst) = ptr_value.as_instruction() {
Ok(OperationArgument::Instruction(inst))
} else {
// TODO: Support more pointer value types
Err(eyre!(
"unexpected pointer value {:?} as operand {} of instruction {:?}",
ptr_value,
operand_index,
instruction
))
}
} else if let Either::Left(BasicValueEnum::FloatValue(value)) = target {
Ok(OperationArgument::Parameter(value))
} else {
// TODO: Support non-pointer/float function arguments
Err(eyre!(
"non-pointer/float function argument in {:?}",
instruction
))
}
})
.collect()
}
/// Attempt to extract an integer value from an operand, and return that integer value if successful
pub(crate) fn operand_to_integer<'ctx>(
operand: Either<BasicValueEnum<'ctx>, BasicBlock<'ctx>>,
) -> Option<IntValue<'ctx>> {
if let Either::Left(BasicValueEnum::IntValue(int_value)) = operand {
Some(int_value)
} else {
None
}
}
/// Attempt to cast a pointer to an immediate int and return that value if successful
pub(crate) fn pointer_value_to_u64(
context: &QCSCompilerContext,
value: PointerValue,
) -> Option<u64> {
value
.const_to_int(context.base_context.i64_type())
.get_sign_extended_constant()
.and_then(|value| u64::try_from(value).ok())
}
/// Attempt to cast a pointer to an immediate int and return that value if successful
pub(crate) fn integer_value_to_u64(_context: &QCSCompilerContext, value: IntValue) -> Option<u64> {
value
.get_sign_extended_constant()
.and_then(|value| u64::try_from(value).ok())
}
pub(crate) fn get_conditional_branch_else_target(
instruction: InstructionValue,
) -> Option<BasicBlock> {
if let Some(Either::Right(target)) = instruction.get_operand(1) {
Some(target)
} else {
None
}
}
/// Given a conditional branch (`br`) instruction, replace its then and/or else targets with the specified basic blocks.
///
/// Note: this function moves the builder's position and does not restore it.
pub(crate) fn replace_conditional_branch_target(
context: &mut QCSCompilerContext,
instruction: InstructionValue,
replace_then: Option<&BasicBlock>,
replace_else: Option<&BasicBlock>,
) -> Result<()> {
context.builder.position_at(
instruction
.get_parent()
.ok_or_else(|| eyre!("Expected instruction to have a parent"))?,
&instruction,
);
let original_then_block = if let Some(Either::Right(target)) = instruction.get_operand(2) {
target
} else {
return Err(eyre!("expected basic block target for branch"));
};
let original_else_block = if let Some(Either::Right(target)) = instruction.get_operand(1) {
target
} else {
return Err(eyre!("expected basic block target for branch"));
};
let (then_block, else_block) = (
replace_then.unwrap_or(&original_then_block),
replace_else.unwrap_or(&original_else_block),
);
let comparison = if let Some(Either::Left(BasicValueEnum::IntValue(comparison))) =
instruction.get_operand(0)
{
comparison
} else {
return Err(eyre!("expected integer comparison for branch"));
};
let new_instruction =
context
.builder
.build_conditional_branch(comparison, *then_block, *else_block);
instruction.replace_all_uses_with(&new_instruction);
instruction.remove_from_basic_block();
Ok(())
}
/// Given a `phi` instruction, replace the existing matching block with the new one specified.
///
/// Parameters:
/// * `reverse_match`: whether to match all incoming clauses that _aren't_ from the specified original basic block instead
/// of those that _are_.
pub(crate) fn replace_phi_clause(
context: &mut QCSCompilerContext,
instruction: PhiValue,
old_basic_block: BasicBlock,
new_basic_block: BasicBlock,
reverse_match: bool,
) -> Result<()> {
let basic_block_parent = instruction
.as_instruction()
.get_parent()
.ok_or_else(|| eyre!("Expected instruction to have a parent"))?;
// We have to ensure that we're writing all phi instructions at the start of the basic block;
// in LLVM IR no non-phi instructions may precede any phi instruction in the block.
context.builder.position_before(
&basic_block_parent
.get_first_instruction()
.ok_or_else(|| eyre!("Expected basic block to have at least one instruction"))?,
);
let mut new_incoming: Vec<(BasicValueEnum, BasicBlock)> = vec![];
// FromIterator not implemented
// let new_incoming = (0..instruction.count_incoming()).map(|index| {
// let value = instruction.get_incoming(index).unwrap();
// TODO: Rework this so it's less clumsy.
// The trick is that get_incoming gives you an owned value but add_incoming wants a &dyn,
// so you need to own the values somewhere long enough to be able to supply them to `add_incoming`.
for index in 0..instruction.count_incoming() {
let value = instruction
.get_incoming(index)
.ok_or_else(|| eyre!("Expected phi instruction to have incoming values"))?;
if reverse_match ^ (value.1 == old_basic_block) {
new_incoming.push((value.0, new_basic_block));
} else {
new_incoming.push((value.0, value.1));
}
}
let mut new_incoming_ref: Vec<(&dyn BasicValue, BasicBlock)> = vec![];
for element in &new_incoming {
new_incoming_ref.push((&element.0, element.1));
}
// TODO: derive the type from the actual instruction instead of assuming i64
let new_instruction = context
.builder
.build_phi(context.base_context.i64_type(), "");
new_instruction.add_incoming(new_incoming_ref.as_slice());
instruction.replace_all_uses_with(&new_instruction);
instruction.as_instruction().remove_from_basic_block();
Ok(())
}
/// Print each of the operands of an instruction in debug format to stdout on its own labeled line.
#[allow(dead_code)]
pub(crate) fn print_all_operands(instruction: InstructionValue) {
println!("instruction: {:?}", instruction);
for i in 0..instruction.get_num_operands() {
println!("operand {}: {:?}", i, instruction.get_operand(i));
}
}
pub(crate) fn replace_phi_clauses(
context: &mut QCSCompilerContext,
within_basic_block: BasicBlock,
old_basic_block: BasicBlock,
new_basic_block: BasicBlock,
reverse_match: bool,
) -> Result<()> {
let mut instruction = within_basic_block.get_first_instruction();
while let Some(current_instruction) = instruction {
// We have to get a valid handle on the next instruction before replace_phi_clause deletes this one.
let next_instruction = current_instruction.get_next_instruction();
if current_instruction.get_opcode() == InstructionOpcode::Phi {
replace_phi_clause(
context,
current_instruction
.try_into()
.map_err(|_| eyre!("Expected phi instruction"))?,
old_basic_block,
new_basic_block,
reverse_match,
)?;
}
instruction = next_instruction;
}
Ok(())
}
/// Remove instructions in topological order such that none is removed while any other instruction uses it.
/// Note that this will panic if there is a cycle in the use graph or
pub(crate) fn remove_instructions_in_safe_order(instructions: Vec<InstructionValue>) {
let mut instructions = instructions;
loop {
let mut instructions_removed_in_round = false;
instructions.retain(|instr| {
if instr.get_first_use().is_some() {
true
} else {
instr.remove_from_basic_block();
instructions_removed_in_round = true;
false
}
});
if instructions.is_empty() {
return;
}
assert!(
instructions_removed_in_round,
"Unable to remove remaining instructions safely"
);
}
}