-
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 zext and trunc to builder (#291)
- Loading branch information
Showing
6 changed files
with
156 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
; ModuleID = 'trunc' | ||
source_filename = "trunc" | ||
|
||
define void @main() #0 { | ||
entry: | ||
%0 = call i32 @random_int(i32 0) | ||
%1 = trunc i32 %0 to i16 | ||
ret void | ||
} | ||
|
||
declare i32 @random_int(i32) | ||
|
||
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,20 @@ | ||
; ModuleID = 'zext' | ||
source_filename = "zext" | ||
|
||
define void @main() #0 { | ||
entry: | ||
%0 = call i16 @random_int(i16 0) | ||
%1 = zext i16 %0 to i32 | ||
ret void | ||
} | ||
|
||
declare i16 @random_int(i16) | ||
|
||
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,33 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
|
||
import os | ||
|
||
import pytest | ||
from pathlib import Path | ||
|
||
import pyqir | ||
|
||
|
||
def test_trunc() -> None: | ||
module = pyqir.SimpleModule("trunc", 1, 1) | ||
context = module.context | ||
builder = module.builder | ||
entry = module.entry_block | ||
builder.insert_at_end(entry) | ||
i16 = pyqir.IntType(context, 16) | ||
i32 = pyqir.IntType(context, 32) | ||
random_int = module.add_external_function( | ||
"random_int", | ||
pyqir.FunctionType( | ||
i32, | ||
[i32], | ||
), | ||
) | ||
const = builder.call(random_int, [pyqir.const(i32, 0)]) | ||
builder.trunc(const, i16) | ||
ir = module.ir() | ||
|
||
file = os.path.join(os.path.dirname(__file__), "resources/test_trunc.ll") | ||
expected = Path(file).read_text() | ||
assert ir == expected |
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,33 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
|
||
import os | ||
|
||
import pytest | ||
from pathlib import Path | ||
|
||
import pyqir | ||
|
||
|
||
def test_zext() -> None: | ||
module = pyqir.SimpleModule("zext", 1, 1) | ||
context = module.context | ||
builder = module.builder | ||
entry = module.entry_block | ||
builder.insert_at_end(entry) | ||
i16 = pyqir.IntType(context, 16) | ||
i32 = pyqir.IntType(context, 32) | ||
random_int = module.add_external_function( | ||
"random_int", | ||
pyqir.FunctionType( | ||
i16, | ||
[i16], | ||
), | ||
) | ||
const1 = builder.call(random_int, [pyqir.const(i16, 0)]) | ||
builder.zext(const1, i32) | ||
ir = module.ir() | ||
|
||
file = os.path.join(os.path.dirname(__file__), "resources/test_zext.ll") | ||
expected = Path(file).read_text() | ||
assert ir == expected |