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

[stdlib] Add Pointer(to=) constructors #3606

Draft
wants to merge 6 commits into
base: nightly
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 12 additions & 0 deletions stdlib/src/memory/pointer.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,18 @@ struct Pointer[
"""
self._value = _mlir_value

# FIXME(#3617)
# @always_inline("nodebug")
# fn __init__(
# inout self, *, ref [origin, address_space._value.value]to: type
# ):
# """Create a pointer with the input value.

# Args:
# to: The value to construct a pointer to.
# """
# self = Self(_mlir_value=__get_mvalue_as_litref(to))

@staticmethod
@always_inline("nodebug")
fn address_of(ref [origin, address_space]value: type) -> Self:
Expand Down
11 changes: 11 additions & 0 deletions stdlib/src/memory/unsafe_pointer.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ struct UnsafePointer[
"""
self.address = value

@always_inline
fn __init__(
inout self, *, ref [origin, address_space._value.value]to: type
):
"""Create a pointer with the input value.

Args:
to: The value to construct a pointer to.
"""
self = Self(__mlir_op.`lit.ref.to_pointer`(__get_mvalue_as_litref(to)))

@always_inline
@implicit
fn __init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# limitations under the License.
# ===----------------------------------------------------------------------=== #
# RUN: %mojo %s
from testing import assert_equal, assert_true
from testing import assert_equal, assert_true, assert_not_equal


def test_copy_reference_explicitly():
Expand Down Expand Up @@ -41,7 +41,16 @@ def test_str():
assert_true(str(a_ref).startswith("0x"))


def test_pointer_to():
# FIXME(#3617)
# var local = 1
# assert_not_equal(0, int(Pointer(to=local)))
# _ = local
...


def main():
test_copy_reference_explicitly()
test_equality()
test_str()
test_pointer_to()
7 changes: 7 additions & 0 deletions stdlib/test/memory/test_unsafepointer.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ def test_address_of():
_ = local


def test_pointer_to():
var local = 1
assert_not_equal(0, int(UnsafePointer(to=local)))
_ = local


def test_explicit_copy_of_pointer_address():
var local = 1
var ptr = UnsafePointer[Int].address_of(local)
Expand Down Expand Up @@ -326,6 +332,7 @@ def test_volatile_load_and_store_simd():

def main():
test_address_of()
test_pointer_to()

test_refitem()
test_refitem_offset()
Expand Down
Loading