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

Add ability to set name on values #298

Merged
merged 5 commits into from
Oct 15, 2024
Merged
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ resolver = "2"

[workspace.package]
authors = ["Microsoft"]
version = "0.10.5"
version = "0.10.6"
edition = "2021"
license = "MIT"
homepage = "https://github.com/qir-alliance/pyqir"
Expand Down
4 changes: 2 additions & 2 deletions pyqir/NOTICE-WHEEL.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5595,7 +5595,7 @@ limitations under the License.



qirlib 0.10.5 - SPDX: MIT - MIT License
qirlib 0.10.6 - SPDX: MIT - MIT License
https://github.com/qir-alliance/pyqir

/*
Expand Down Expand Up @@ -5970,7 +5970,7 @@ SOFTWARE.



pyqir 0.10.5 - SPDX: MIT - MIT License
pyqir 0.10.6 - SPDX: MIT - MIT License
https://github.com/qir-alliance/pyqir

MIT License
Expand Down
2 changes: 1 addition & 1 deletion pyqir/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "pyqir"
version = "0.10.5"
version = "0.10.6"
requires-python = ">= 3.8"
classifiers = [
"License :: OSI Approved :: MIT License",
Expand Down
5 changes: 5 additions & 0 deletions pyqir/pyqir/_native.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,11 @@ class Value:
"""The name of this value or the empty string if this value is anonymous."""
...

@name.setter
def name(self, value: str) -> None:
"""Sets the name of the value."""
...

def __richcmp__(self, other: Value, op: int) -> bool:
"""
Compares this value to another value.
Expand Down
8 changes: 8 additions & 0 deletions pyqir/src/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ impl Value {
}
}

#[setter]
fn set_name(&self, value: &str) {
unsafe {
let c_name = &CString::new(value).unwrap();
LLVMSetValueName2(self.as_ptr(), value.as_ptr().cast(), c_name.as_bytes().len());
}
}

fn __str__(&self) -> String {
unsafe {
Message::from_raw(LLVMPrintValueToString(self.as_ptr()))
Expand Down
73 changes: 73 additions & 0 deletions pyqir/tests/test_value_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

from pathlib import Path

import pyqir


current_file_path = Path(__file__)
# Get the directory of the current file
current_dir = current_file_path.parent

from pyqir import (
Context,
is_entry_point,
)


def read_file(file_name: str) -> str:
return Path(current_dir / file_name).read_text(encoding="utf-8")

def test_setting_function_name_changes_name() -> None:
context = Context()
ir = read_file("random_bit.ll")
mod = pyqir.Module.from_ir(context, ir)
entry_point = next(filter(is_entry_point, mod.functions))
assert entry_point.name == "random_bit"
expected = "new_name"
entry_point.name = expected
entry_point = next(filter(is_entry_point, mod.functions))
assert entry_point.name == expected


def test_setting_constant_name_does_not_do_anything() -> None:
context = pyqir.Context()
const0 = pyqir.const(pyqir.IntType(context, 64), 42)
const0.name = "my_value"
assert const0.name == ""


def test_setting_block_name_changes_name() -> None:
mod = pyqir.SimpleModule("test_type_mismatch", 0, 0)
mod.entry_block.name = "my_block"
ir = mod.ir()
assert "my_block:" in ir


def test_int_variable() -> None:
mod = pyqir.SimpleModule("test", 0, 0)
i64 = pyqir.IntType(mod.context, 64)

source = mod.add_external_function("source", pyqir.FunctionType(i64, []))
sink = mod.add_external_function("sink", pyqir.FunctionType(i64, [i64]))

source_res = mod.builder.call(source, [])
source_res.name = "my_var"

sink_res = mod.builder.call(sink, [source_res])
sink_res.name = "my_res"
ir = mod.ir()
assert "%my_var = call i64 @source()" in ir
assert "%my_res = call i64 @sink(i64 %my_var)" in ir


def test_function_name_can_contain_spaces_and_chars() -> None:
mod = pyqir.SimpleModule("test", 0, 0)
expected = "Some - ; name fin"
idavis marked this conversation as resolved.
Show resolved Hide resolved
mod.entry_point.name = expected

assert f"@\"{expected}\"() #0" in mod.ir()
# verify we can find it by name
func = next(filter(lambda f: f.name == expected, mod._module.functions))
assert func == mod.entry_point
Loading