Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/nightly' into refactor-string-…
Browse files Browse the repository at this point in the history
…stringslice-constructor
  • Loading branch information
martinvuyk committed Dec 11, 2024
2 parents 17d4b79 + 7ea103c commit a44855f
Show file tree
Hide file tree
Showing 23 changed files with 1,641 additions and 1,305 deletions.
736 changes: 736 additions & 0 deletions docs/changelog-released.md

Large diffs are not rendered by default.

688 changes: 10 additions & 678 deletions docs/changelog.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/manual/decorators/parameter.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ this will be included in the binary

## Parametric for statement

You can add the `@parameter` decorator to an `for` loop to create a loop that's
You can add the `@parameter` decorator to a `for` loop to create a loop that's
evaluated at compile time. The loop sequence and induction values must be
a valid parameter expressions (that is, an expressions that evaluate at compile
valid parameter expressions (that is, expressions that evaluate at compile
time).

This has the effect of "unrolling" the loop.
Expand Down
260 changes: 151 additions & 109 deletions examples/life/magic.lock

Large diffs are not rendered by default.

260 changes: 151 additions & 109 deletions examples/magic.lock

Large diffs are not rendered by default.

258 changes: 150 additions & 108 deletions examples/notebooks/magic.lock

Large diffs are not rendered by default.

260 changes: 151 additions & 109 deletions examples/operators/magic.lock

Large diffs are not rendered by default.

260 changes: 151 additions & 109 deletions magic.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion stdlib/scripts/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ TEST_UTILS_PATH="${REPO_ROOT}/stdlib/test/test_utils"
# uses the stdlib that's given in the nightly, and will fail compilation
# if some breaking changes are made.
export MODULAR_MOJO_NIGHTLY_IMPORT_PATH=$BUILD_DIR
mojo package "${TEST_UTILS_PATH}" -o "${BUILD_DIR}/test_utils.mojopkg"
mojo package "${TEST_UTILS_PATH}" -I ${BUILD_DIR} -o "${BUILD_DIR}/test_utils.mojopkg"

TEST_PATH="${REPO_ROOT}/stdlib/test"
if [[ $# -gt 0 ]]; then
Expand Down
16 changes: 8 additions & 8 deletions stdlib/src/builtin/sort.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn _insertion_sort[
cmp_fn: fn (_SortWrapper[type], _SortWrapper[type]) capturing [_] -> Bool,
](span: Span[type, origin]):
"""Sort the array[start:end] slice"""
var array = span.unsafe_ptr()
var array = span.unsafe_ptr().bitcast[origin=MutableAnyOrigin]()
var size = len(span)

for i in range(1, size):
Expand All @@ -72,7 +72,7 @@ fn _quicksort_partition_right[
origin: MutableOrigin, //,
cmp_fn: fn (_SortWrapper[type], _SortWrapper[type]) capturing [_] -> Bool,
](span: Span[type, origin]) -> Int:
var array = span.unsafe_ptr()
var array = span.unsafe_ptr().bitcast[origin=MutableAnyOrigin]()
var size = len(span)

var left = 1
Expand Down Expand Up @@ -101,7 +101,7 @@ fn _quicksort_partition_left[
origin: MutableOrigin, //,
cmp_fn: fn (_SortWrapper[type], _SortWrapper[type]) capturing [_] -> Bool,
](span: Span[type, origin]) -> Int:
var array = span.unsafe_ptr()
var array = span.unsafe_ptr().bitcast[origin=MutableAnyOrigin]()
var size = len(span)

var left = 1
Expand All @@ -127,7 +127,7 @@ fn _heap_sort_fix_down[
origin: MutableOrigin, //,
cmp_fn: fn (_SortWrapper[type], _SortWrapper[type]) capturing [_] -> Bool,
](span: Span[type, origin], idx: Int):
var array = span.unsafe_ptr()
var array = span.unsafe_ptr().bitcast[origin=MutableAnyOrigin]()
var size = len(span)
var i = idx
var j = i * 2 + 1
Expand All @@ -148,7 +148,7 @@ fn _heap_sort[
origin: MutableOrigin, //,
cmp_fn: fn (_SortWrapper[type], _SortWrapper[type]) capturing [_] -> Bool,
](span: Span[type, origin]):
var array = span.unsafe_ptr()
var array = span.unsafe_ptr().bitcast[origin=MutableAnyOrigin]()
var size = len(span)
# heapify
for i in range(size // 2 - 1, -1, -1):
Expand Down Expand Up @@ -177,7 +177,7 @@ fn _delegate_small_sort[
origin: MutableOrigin, //,
cmp_fn: fn (_SortWrapper[type], _SortWrapper[type]) capturing [_] -> Bool,
](span: Span[type, origin]):
var array = span.unsafe_ptr()
var array = span.unsafe_ptr().bitcast[origin=MutableAnyOrigin]()
var size = len(span)
if size == 2:
_small_sort[2, type, cmp_fn](array)
Expand Down Expand Up @@ -209,7 +209,7 @@ fn _quicksort[
origin: MutableOrigin, //,
cmp_fn: fn (_SortWrapper[type], _SortWrapper[type]) capturing [_] -> Bool,
](span: Span[type, origin]):
var array = span.unsafe_ptr()
var array = span.unsafe_ptr().bitcast[origin=MutableAnyOrigin]()
var size = len(span)
if size == 0:
return
Expand Down Expand Up @@ -379,7 +379,7 @@ fn _partition[
if size <= 1:
return 0

var array = span.unsafe_ptr()
var array = span.unsafe_ptr().bitcast[origin=MutableAnyOrigin]()
var pivot = size // 2

var pivot_value = array[pivot]
Expand Down
27 changes: 18 additions & 9 deletions stdlib/src/builtin/string_literal.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,9 @@ struct StringLiteral(
return __mlir_op.`pop.string.size`(self.value)

@always_inline("nodebug")
# FIXME(MSTDL-956): This should return a pointer with StaticConstantOrigin.
fn unsafe_ptr(self) -> UnsafePointer[UInt8]:
fn unsafe_ptr(
self,
) -> UnsafePointer[Byte, is_mutable=False, origin=StaticConstantOrigin]:
"""Get raw pointer to the underlying data.
Returns:
Expand All @@ -497,11 +498,14 @@ struct StringLiteral(
# TODO(MSTDL-555):
# Remove bitcast after changing pop.string.address
# return type.
return ptr.bitcast[UInt8]()
return ptr.bitcast[
Byte, is_mutable=False, origin=StaticConstantOrigin
]()

@always_inline
# FIXME(MSTDL-956): This should return a pointer with StaticConstantOrigin.
fn unsafe_cstr_ptr(self) -> UnsafePointer[c_char]:
fn unsafe_cstr_ptr(
self,
) -> UnsafePointer[c_char, is_mutable=False, origin=StaticConstantOrigin]:
"""Retrieves a C-string-compatible pointer to the underlying memory.
The returned pointer is guaranteed to be NUL terminated, and not null.
Expand Down Expand Up @@ -889,8 +893,9 @@ struct StringLiteral(
return str(self).islower()

fn strip(self) -> String:
"""Return a copy of the string literal with leading and trailing whitespaces
removed.
"""Return a copy of the string literal with leading and trailing
whitespaces removed. This only takes ASCII whitespace into account:
`" \\t\\n\\v\\f\\r\\x1c\\x1d\\x1e"`.
Returns:
A string with no leading or trailing whitespaces.
Expand Down Expand Up @@ -922,7 +927,9 @@ struct StringLiteral(
return str(self).rstrip(chars)

fn rstrip(self) -> String:
"""Return a copy of the string with trailing whitespaces removed.
"""Return a copy of the string with trailing whitespaces removed. This
only takes ASCII whitespace into account:
`" \\t\\n\\v\\f\\r\\x1c\\x1d\\x1e"`.
Returns:
A copy of the string with no trailing whitespaces.
Expand All @@ -941,7 +948,9 @@ struct StringLiteral(
return str(self).lstrip(chars)

fn lstrip(self) -> String:
"""Return a copy of the string with leading whitespaces removed.
"""Return a copy of the string with leading whitespaces removed. This
only takes ASCII whitespace into account:
`" \\t\\n\\v\\f\\r\\x1c\\x1d\\x1e"`.
Returns:
A copy of the string with no leading whitespaces.
Expand Down
8 changes: 4 additions & 4 deletions stdlib/src/collections/deque.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ struct Deque[ElementType: CollectionElement](

return (self._data + self._head)[]

fn pop(mut self, out element: ElementType) raises:
fn pop(mut self) raises -> ElementType:
"""Removes and returns the element from the right side of the deque.
Returns:
Expand All @@ -816,9 +816,9 @@ struct Deque[ElementType: CollectionElement](
):
self._realloc(self._capacity >> 1)

return
return element

fn popleft(mut self, out element: ElementType) raises:
fn popleft(mut self) raises -> ElementType:
"""Removes and returns the element from the left side of the deque.
Returns:
Expand All @@ -840,7 +840,7 @@ struct Deque[ElementType: CollectionElement](
):
self._realloc(self._capacity >> 1)

return
return element

fn reverse(mut self):
"""Reverses the elements of the deque in-place."""
Expand Down
10 changes: 6 additions & 4 deletions stdlib/src/collections/dict.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ struct _DictValueIter[
# Cast through a pointer to grant additional mutability because
# _DictEntryIter.next erases it.
return Self.ref_type.address_of(
UnsafePointer.address_of(entry_ref[].value)[]
UnsafePointer.address_of(entry_ref[].value).bitcast[
origin=dict_origin
]()[]
)

@always_inline
Expand Down Expand Up @@ -242,14 +244,14 @@ struct DictEntry[K: KeyElement, V: CollectionElement](
self.key = other.key
self.value = other.value

fn reap_value(owned self) -> V:
fn reap_value(owned self) -> V as out:
"""Take the value from an owned entry.
Returns:
The value of the entry.
"""
__mlir_op.`lit.ownership.mark_destroyed`(__get_mvalue_as_litref(self))
return self.value^
out = self.value^
__disable_del self


alias _EMPTY = -1
Expand Down
12 changes: 8 additions & 4 deletions stdlib/src/collections/list.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ from collections import List

from os import abort
from sys import sizeof
from sys.intrinsics import _type_is_eq

from memory import Pointer, UnsafePointer, memcpy, Span

Expand Down Expand Up @@ -924,12 +923,17 @@ struct List[T: CollectionElement, hint_trivial_type: Bool = False](
if elt_idx_1 != elt_idx_2:
swap((self.data + elt_idx_1)[], (self.data + elt_idx_2)[])

@always_inline
fn unsafe_ptr(self) -> UnsafePointer[T]:
fn unsafe_ptr(
ref self,
) -> UnsafePointer[
T,
is_mutable = Origin(__origin_of(self)).is_mutable,
origin = __origin_of(self),
]:
"""Retrieves a pointer to the underlying memory.
Returns:
The UnsafePointer to the underlying memory.
The pointer to the underlying memory.
"""
return self.data

Expand Down
21 changes: 16 additions & 5 deletions stdlib/src/collections/string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -1589,13 +1589,19 @@ struct String(
buf.append(0)
return String(buf^)

fn unsafe_ptr(self) -> UnsafePointer[UInt8]:
fn unsafe_ptr(
ref self,
) -> UnsafePointer[
Byte,
is_mutable = Origin(__origin_of(self)).is_mutable,
origin = __origin_of(self),
]:
"""Retrieves a pointer to the underlying memory.
Returns:
The pointer to the underlying memory.
"""
return self._buffer.data
return self._buffer.unsafe_ptr()

fn unsafe_cstr_ptr(self) -> UnsafePointer[c_char]:
"""Retrieves a C-string-compatible pointer to the underlying memory.
Expand Down Expand Up @@ -1957,7 +1963,8 @@ struct String(

fn strip(self) -> StringSlice[__origin_of(self)]:
"""Return a copy of the string with leading and trailing whitespaces
removed.
removed. This only takes ASCII whitespace into account:
`" \\t\\n\\v\\f\\r\\x1c\\x1d\\x1e"`.
Returns:
A copy of the string with no leading or trailing whitespaces.
Expand All @@ -1977,7 +1984,9 @@ struct String(
return self.as_string_slice().rstrip(chars)

fn rstrip(self) -> StringSlice[__origin_of(self)]:
"""Return a copy of the string with trailing whitespaces removed.
"""Return a copy of the string with trailing whitespaces removed. This
only takes ASCII whitespace into account:
`" \\t\\n\\v\\f\\r\\x1c\\x1d\\x1e"`.
Returns:
A copy of the string with no trailing whitespaces.
Expand All @@ -1997,7 +2006,9 @@ struct String(
return self.as_string_slice().lstrip(chars)

fn lstrip(self) -> StringSlice[__origin_of(self)]:
"""Return a copy of the string with leading whitespaces removed.
"""Return a copy of the string with leading whitespaces removed. This
only takes ASCII whitespace into account:
`" \\t\\n\\v\\f\\r\\x1c\\x1d\\x1e"`.
Returns:
A copy of the string with no leading whitespaces.
Expand Down
11 changes: 8 additions & 3 deletions stdlib/src/memory/memory.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ fn memcmp[

@always_inline
fn _memcpy_impl(
dest_data: UnsafePointer[Byte, **_], src_data: __type_of(dest_data), n: Int
dest_data: UnsafePointer[Byte, is_mutable=True, **_],
src_data: __type_of(dest_data),
n: Int,
):
"""Copies a memory area.
Expand Down Expand Up @@ -412,9 +414,12 @@ fn stack_allocation[

@parameter
if is_gpu():
# On NVGPU, SHARED and PARAM address spaces lower to global memory.
# On NVGPU, SHARED and CONSTANT address spaces lower to global memory.
@parameter
if address_space in (_GPUAddressSpace.SHARED, _GPUAddressSpace.PARAM):
if address_space in (
_GPUAddressSpace.SHARED,
_GPUAddressSpace.CONSTANT,
):
alias global_name = name.value() if name else "_global_alloc"
return __mlir_op.`pop.global_alloc`[
name = global_name.value,
Expand Down
10 changes: 5 additions & 5 deletions stdlib/src/memory/pointer.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ from memory import Pointer
```
"""

from sys import is_nvidia_gpu


# ===-----------------------------------------------------------------------===#
# AddressSpace
Expand All @@ -36,13 +38,11 @@ struct _GPUAddressSpace(EqualityComparable):
"""Generic address space."""
alias GLOBAL = AddressSpace(1)
"""Global address space."""
alias CONSTANT = AddressSpace(2)
"""Constant address space."""
alias SHARED = AddressSpace(3)
"""Shared address space."""
alias PARAM = AddressSpace(4)
"""Param address space."""
alias LOCAL = AddressSpace(5)
alias CONSTANT = AddressSpace(4)
"""Constant address space."""
alias LOCAL = AddressSpace(5) if is_nvidia_gpu() else AddressSpace(3)
"""Local address space."""

@always_inline("nodebug")
Expand Down
12 changes: 6 additions & 6 deletions stdlib/src/memory/span.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ struct Span[
"""

# Field
var _data: UnsafePointer[T]
var _data: UnsafePointer[T, is_mutable=is_mutable, origin=origin]
var _len: Int

# ===------------------------------------------------------------------===#
Expand Down Expand Up @@ -250,14 +250,14 @@ struct Span[
# Methods
# ===------------------------------------------------------------------===#

fn unsafe_ptr(self) -> UnsafePointer[T]:
"""
Gets a pointer to the first element of this slice.
fn unsafe_ptr(
self,
) -> UnsafePointer[T, is_mutable=is_mutable, origin=origin]:
"""Retrieves a pointer to the underlying memory.
Returns:
A pointer pointing at the first element of this slice.
The pointer to the underlying memory.
"""

return self._data

fn as_ref(self) -> Pointer[T, origin]:
Expand Down
Loading

0 comments on commit a44855f

Please sign in to comment.