-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add API for inserting phi nodes (#279)
- Loading branch information
Showing
7 changed files
with
218 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
; ModuleID = 'phi_add' | ||
source_filename = "phi_add" | ||
|
||
define void @main() #0 { | ||
entry: | ||
br i1 true, label %body, label %footer | ||
|
||
body: ; preds = %entry | ||
br label %footer | ||
|
||
footer: ; preds = %body, %entry | ||
%0 = phi i32 [ 2, %entry ], [ 3, %body ] | ||
%1 = add i32 %0, 1 | ||
ret void | ||
} | ||
|
||
attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="custom" "required_num_qubits"="1" "required_num_results"="1" } | ||
|
||
!llvm.module.flags = !{!0, !1, !2, !3} | ||
|
||
!0 = !{i32 1, !"qir_major_version", i32 1} | ||
!1 = !{i32 7, !"qir_minor_version", i32 0} | ||
!2 = !{i32 1, !"dynamic_qubit_management", i1 false} | ||
!3 = !{i32 1, !"dynamic_result_management", i1 false} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
; ModuleID = 'phi_constants' | ||
source_filename = "phi_constants" | ||
|
||
define void @main() #0 { | ||
entry: | ||
br i1 false, label %body, label %footer | ||
|
||
body: ; preds = %entry | ||
br label %footer | ||
|
||
footer: ; preds = %body, %entry | ||
%0 = phi i32 [ 4, %body ], [ 100, %entry ] | ||
ret void | ||
} | ||
|
||
attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="custom" "required_num_qubits"="1" "required_num_results"="1" } | ||
|
||
!llvm.module.flags = !{!0, !1, !2, !3} | ||
|
||
!0 = !{i32 1, !"qir_major_version", i32 1} | ||
!1 = !{i32 7, !"qir_minor_version", i32 0} | ||
!2 = !{i32 1, !"dynamic_qubit_management", i1 false} | ||
!3 = !{i32 1, !"dynamic_result_management", i1 false} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
|
||
import os | ||
|
||
import pytest | ||
from pathlib import Path | ||
|
||
import pyqir | ||
|
||
|
||
def test_constants() -> None: | ||
module = pyqir.SimpleModule("phi_constants", 1, 1) | ||
context = module.context | ||
builder = module.builder | ||
entry_point = module.entry_point | ||
|
||
entry = module.entry_block | ||
body = pyqir.BasicBlock(context, "body", entry_point) | ||
footer = pyqir.BasicBlock(context, "footer", entry_point) | ||
|
||
builder.insert_at_end(entry) | ||
const0 = pyqir.Constant.null(pyqir.IntType(context, 1)) | ||
builder.condbr(const0, body, footer) | ||
|
||
builder.insert_at_end(body) | ||
builder.br(footer) | ||
|
||
builder.insert_at_end(footer) | ||
i32 = pyqir.IntType(context, 32) | ||
phi = builder.phi(i32) | ||
const_taken = pyqir.const(i32, 4) | ||
const_not_taken = pyqir.const(i32, 100) | ||
phi.add_incoming(const_taken, body) | ||
phi.add_incoming(const_not_taken, entry) | ||
|
||
ir = module.ir() | ||
|
||
file = os.path.join(os.path.dirname(__file__), "resources/test_phi_constants.ll") | ||
expected = Path(file).read_text() | ||
assert ir == expected | ||
|
||
|
||
def test_add() -> None: | ||
module = pyqir.SimpleModule("phi_add", 1, 1) | ||
context = module.context | ||
builder = module.builder | ||
entry_point = module.entry_point | ||
|
||
entry = module.entry_block | ||
body = pyqir.BasicBlock(context, "body", entry_point) | ||
footer = pyqir.BasicBlock(context, "footer", entry_point) | ||
|
||
builder.insert_at_end(entry) | ||
i32 = pyqir.IntType(context, 32) | ||
const1 = pyqir.const(i32, 1) | ||
const2 = pyqir.const(i32, 2) | ||
sum_two = builder.add(const1, const1) | ||
cmp = builder.icmp(pyqir.IntPredicate.EQ, sum_two, const2) | ||
builder.condbr(cmp, body, footer) | ||
|
||
builder.insert_at_end(body) | ||
sum_three = builder.add(sum_two, const1) | ||
builder.br(footer) | ||
|
||
builder.insert_at_end(footer) | ||
phi = builder.phi(i32) | ||
phi.add_incoming(sum_two, entry) | ||
phi.add_incoming(sum_three, body) | ||
sum_four = builder.add(phi, const1) | ||
|
||
ir = module.ir() | ||
|
||
file = os.path.join(os.path.dirname(__file__), "resources/test_phi_add.ll") | ||
expected = Path(file).read_text() | ||
assert ir == expected |