Skip to content

Commit

Permalink
Merge branch 'nightly' into add-uint-invert
Browse files Browse the repository at this point in the history
  • Loading branch information
martinvuyk authored Oct 13, 2024
2 parents 82299e6 + c1665f2 commit 3af321e
Show file tree
Hide file tree
Showing 54 changed files with 726 additions and 602 deletions.
2 changes: 1 addition & 1 deletion docs/changelog-released.md
Original file line number Diff line number Diff line change
Expand Up @@ -3137,7 +3137,7 @@ installation issues. Otherwise it is functionally identical to Mojo 24.1.
[`Copyable`](/mojo/stdlib/builtin/value/Copyable), and
[`CollectionElement`](/mojo/stdlib/builtin/value/CollectionElement).

- A new magic `__lifetime_of(expr)` call will yield the lifetime of a memory
- A new magic `__origin_of(expr)` call will yield the lifetime of a memory
value. We hope and expect that this will eventually be replaced by
`Reference(expr).lifetime` as the parameter system evolves, but this is
important in the meantime for use in function signatures.
Expand Down
16 changes: 13 additions & 3 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ what we publish.
- Added `Python.unsafe_get_python_exception()`, as an efficient low-level
utility to get the Mojo `Error` equivalent of the current CPython error state.

- The `__type_of(x)` and `__lifetime_of(x)` operators are much more general now:
- The `__type_of(x)` and `__origin_of(x)` operators are much more general now:
they allow arbitrary expressions inside of them, allow referring to dynamic
values in parameter contexts, and even allow referring to raising functions
in non-raising contexts. These operations never evaluate their expression, so
Expand Down Expand Up @@ -166,9 +166,9 @@ what we publish.

- `ref` argument and result specifiers now allow providing a memory value
directly in the lifetime specifier, rather than requiring the use of
`__lifetime_of`. It is still fine to use `__lifetime_of` explicitly though,
`__origin_of`. It is still fine to use `__origin_of` explicitly though,
and this is required when specifying lifetimes for parameters (e.g. to the
`Reference` type). For example, this is now valid without `__lifetime_of`:
`Reference` type). For example, this is now valid without `__origin_of`:

```mojo
fn return_ref(a: String) -> ref [a] String:
Expand Down Expand Up @@ -264,6 +264,13 @@ what we publish.
- `String.as_bytes()` now returns a `Span[UInt8]` instead of a `List[Int8]`. The
old behavior can be achieved by using `List(s.as_bytes())`.

- `Lifetime` and related types has been renamed to `Origin` in the standard
library to better clarify that parameters of this type indicate where a
reference is derived from, not the more complicated notion of where a variable
is initialized and destroyed. Please see [the proposal](https://github.com/modularml/mojo/blob/main/proposals/lifetimes-keyword-renaming.md)
for more information and rationale. As a consequence `__origin_of` is now
named `__origin_of`.

### ❌ Removed

### 🛠️ Fixed
Expand All @@ -280,6 +287,9 @@ what we publish.
- [Issue #3559](https://github.com/modularml/mojo/issues/3559) - VariadicPack
doesn't extend the lifetimes of the values it references.

- [Issue #3627](https://github.com/modularml/mojo/issues/3627) - Compiler
overlooked exclusivity violation caused by `ref [MutableAnyLifetime] T`

- The VS Code extension now auto-updates its private copy of the MAX SDK.

- The variadic initializer for `SIMD` now works in parameter expressions.
4 changes: 2 additions & 2 deletions docs/manual/lifecycle/index.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@
"\n",
":::note Lifetime type\n",
"\n",
"The concept of lifetimes is related to the `lifetime` type, a Mojo primitive\n",
"The concept of lifetimes is related to the `origin` type, a Mojo primitive\n",
"used to track ownership. For most Mojo programming, you won't need to work with\n",
"`lifetime` values directly. For information, see [Lifetimes and\n",
"`origin` values directly. For information, see [Lifetimes and\n",
"references](/mojo/manual/values/lifetimes).\n",
"\n",
":::\n",
Expand Down
30 changes: 15 additions & 15 deletions docs/manual/pointers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
":::note \n",
"\n",
"In addition to unsafe pointers, Mojo supports a safe \n",
"[`Reference`](/mojo/stdlib/memory/reference/Reference) type. See\n",
"[`UnsafePointer` and `Reference`](#unsafepointer-and-reference) for a brief\n",
"[`Pointer`](/mojo/stdlib/memory/pointer/Pointer) type. See\n",
"[`UnsafePointer` and `Pointer`](#unsafepointer-and-pointer) for a brief\n",
"comparison of the types.\n",
"\n",
":::\n",
Expand Down Expand Up @@ -708,25 +708,25 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## `UnsafePointer` and `Reference`\n",
"## `UnsafePointer` and `Pointer`\n",
"\n",
"The [`Reference`](/mojo/stdlib/memory/reference/Reference) type is essentially a \n",
"safe pointer type. Like a pointer, you can derference a `Reference` using the \n",
"dereference operator, `[]`. However, the `Reference` type has several\n",
"The [`Pointer`](/mojo/stdlib/memory/pointer/Pointer) type is essentially a \n",
"safe pointer type. Like a pointer, you can derference a `Pointer` using the \n",
"dereference operator, `[]`. However, the `Pointer` type has several\n",
"differences from `UnsafePointer` which make it safer:\n",
"\n",
"- A `Reference` is _non-nullable_. A reference always points to something.\n",
"- You can't allocate or free memory using a `Reference`—only point to an\n",
"- A `Pointer` is _non-nullable_: it always points to something.\n",
"- You can't allocate or free memory using a `Pointer`—only point to an\n",
" existing value.\n",
"- A `Reference` only refers to a single value. You can't do pointer arithmetic\n",
" with a `Reference`.\n",
"- A `Reference` has an associated _lifetime_, which connects it back to an\n",
" original, owned value. The lifetime ensures that the value won't be destroyed\n",
" while the reference exists.\n",
"- A `Pointer` only refers to a single value. You can't do pointer arithmetic\n",
" with a `Pointer`.\n",
"- A `Pointer` has an associated _origin_, which connects it back to an\n",
" original, owned value. The origin ensures that the value won't be destroyed\n",
" while the pointer exists.\n",
"\n",
"The `Reference` type shouldn't be confused with the immutable and mutable\n",
"The `Pointer` type shouldn't be confused with the immutable and mutable\n",
"references used with the `borrowed` and `inout` argument conventions. Those\n",
"references do not require explicit dereferencing, unlike a `Reference` or \n",
"references do not require explicit dereferencing, unlike a `Pointer` or \n",
"`UnsafePointer`."
]
}
Expand Down
13 changes: 8 additions & 5 deletions docs/manual/python/index.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,15 @@
" code than in the Mojo standard library, which \n",
" [limits their use for performance reasons](/mojo/roadmap#the-standard-library-has-limited-exceptions-use).)\n",
"\n",
":::note\n",
"\n",
"Mojo loads the Python interpreter and Python modules at runtime, so\n",
"wherever you run a Mojo program, it must be able to access a compatible Python\n",
"interpreter, and to locate any imported Python modules. For more information,\n",
"see [Create a Python environment](#create-a-python-environment).\n",
":::caution\n",
"\n",
"[`mojo build`](/mojo/cli/build) doesn't include the Python packages used by\n",
"your Mojo project. Instead, Mojo loads the Python interpreter and Python\n",
"packages at runtime, so they must be provided in the environment where you run\n",
"the Mojo program (such as inside the Magic environment where you built the\n",
"executable). For more information, see the section above to [create a Python\n",
"environment](#create-a-python-environment).\n",
"\n",
":::"
]
Expand Down
6 changes: 3 additions & 3 deletions docs/manual/values/lifetimes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
"\n",
"- Static lifetimes. The `ImmutableStaticLifetime` and `MutableStaticLifetime`\n",
" aliases are lifetimes that last for the duration of the program. \n",
"- The `__lifetime_of()` magic function, which returns the lifetime associated\n",
"- The `__origin_of()` magic function, which returns the lifetime associated\n",
" with the value (or values) passed in.\n",
"- Inferred lifetime. You can use inferred parameters to capture the lifetime\n",
" of a value passed in to a function.\n",
Expand Down Expand Up @@ -213,7 +213,7 @@
"\n",
"#### Derived lifetimes\n",
"\n",
"Use the `__lifetime_of()` magic function to obtain a value's lifetime. This can \n",
"Use the `__origin_of()` magic function to obtain a value's lifetime. This can \n",
"be useful, for example, when creating a container type. Consider the `List`\n",
"type. Subscripting into a list (`list[4]`) returns a reference to the item at\n",
"the specified position. The signature of the `__getitem__()` method that's\n",
Expand Down Expand Up @@ -298,7 +298,7 @@
"\n",
"The syntax for a `ref` argument is:\n",
"\n",
"<code><strong>ref [</strong><var>lifetime</var><strong>]</strong> <var>argName</var>: <var>argType</var></code>\n",
"<code><strong>ref [</strong><var>origin</var><strong>]</strong> <var>argName</var>: <var>argType</var></code>\n",
"\n",
"The `lifetime` parameter passed inside the square brackets can be replaced with\n",
"an underscore character (`_`) to indicate that the parameter is _unbound_. Think\n",
Expand Down
8 changes: 4 additions & 4 deletions examples/reduce.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ alias scalar = Scalar[type]

# Use the https://en.wikipedia.org/wiki/Kahan_summation_algorithm
# Simple summation of the array elements
fn naive_reduce_sum[size: Int](buffer: Buffer[type, size]) -> scalar:
fn naive_reduce_sum[size: Int](buffer: Buffer[type, size]) raises -> scalar:
var my_sum: scalar = 0
var c: scalar = 0
for i in range(buffer.size):
Expand All @@ -47,7 +47,7 @@ fn naive_reduce_sum[size: Int](buffer: Buffer[type, size]) -> scalar:
return my_sum


fn stdlib_reduce_sum[size: Int](array: Buffer[type, size]) -> scalar:
fn stdlib_reduce_sum[size: Int](array: Buffer[type, size]) raises -> scalar:
var my_sum = sum(array)
return my_sum

Expand All @@ -62,12 +62,12 @@ def pretty_print(name: String, elements: Int, time: Float64):


fn bench[
func: fn[size: Int] (buffer: Buffer[type, size]) -> scalar,
func: fn[size: Int] (buffer: Buffer[type, size]) raises -> scalar,
size: Int,
name: String,
](buffer: Buffer[type, size]) raises:
@parameter
fn runner():
fn runner() raises:
var result = func[size](buffer)
keep(result)

Expand Down
2 changes: 1 addition & 1 deletion proposals/ref-convention.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ You would now use an argument convention:
```mojo
fn __refitem__(ref [_] self, index: Int) -> Reference[
# This is a bit yuck, but is simplified further below.
Self.ElementType, __lifetime_of(self).is_mutable, __lifetime_of(self)
Self.ElementType, __origin_of(self).is_mutable, __origin_of(self)
]:
# Alternatively, name the Lifetime:
Expand Down
4 changes: 2 additions & 2 deletions proposals/value-ownership.md
Original file line number Diff line number Diff line change
Expand Up @@ -638,11 +638,11 @@ and MLIR design. For example, you could imagine things like this:

```mojo
# Take a non-copyable SomeTy as a borrow and return owned copy
fn life_ex1['a: lifetime](value: 'a SomeTy) -> SomeTy:
fn life_ex1['a: origin](value: 'a SomeTy) -> SomeTy:
return value.copy()
# Take a non-copyable SomeTy and return the reference
fn life_ex2['a: lifetime](value: 'a SomeTy) -> borrowed 'a SomeTy:
fn life_ex2['a: origin](value: 'a SomeTy) -> borrowed 'a SomeTy:
return value
```

Expand Down
2 changes: 1 addition & 1 deletion stdlib/COMPATIBLE_COMPILER_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024.10.1105
2024.10.1305
6 changes: 3 additions & 3 deletions stdlib/benchmarks/hashlib/bench_hash.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ from memory import UnsafePointer
from hashlib.hash import hash as old_hash
from hashlib._ahash import (
AHasher,
hash as ahash,
_folded_multiply,
_read_small,
U256,
U128,
MULTIPLE,
ROT,
)
from hashlib._hasher import _hash_with_hasher

# Source: https://www.101languages.net/arabic/most-common-arabic-words/
alias words_ar = """
Expand Down Expand Up @@ -618,7 +618,7 @@ fn bench_small_keys_new_hash_function[s: String](inout b: Bencher) raises:
@parameter
fn call_fn():
for w in words:
var h = ahash(w[].unsafe_ptr(), w[].byte_length())
var h = _hash_with_hasher(w[].unsafe_ptr(), w[].byte_length())
benchmark.keep(h)

b.iter[call_fn]()
Expand All @@ -640,7 +640,7 @@ fn bench_long_key_new_hash_function[s: String](inout b: Bencher) raises:
@always_inline
@parameter
fn call_fn():
var h = ahash(s.unsafe_ptr(), s.byte_length())
var h = _hash_with_hasher(s.unsafe_ptr(), s.byte_length())
benchmark.keep(h)

b.iter[call_fn]()
Expand Down
10 changes: 5 additions & 5 deletions stdlib/src/builtin/_pybind.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ fn fail_initialization(owned err: Error) -> PythonObject:


alias MutableGlobalLifetime = __mlir_attr[
`#lit.lifetime.field<`,
`#lit.static.lifetime : !lit.lifetime<1>`,
`, "__python_globals__"> : !lit.lifetime<1>`,
`#lit.origin.field<`,
`#lit.static.origin : !lit.origin<1>`,
`, "__python_globals__"> : !lit.origin<1>`,
]


# FIXME(MOCO-1308): Workaround crash by adding explicit `alignment=1`.
alias PyGlobalPtr = UnsafePointer[lifetime=MutableGlobalLifetime, alignment=1]
alias PyGlobalPtr = UnsafePointer[origin=MutableGlobalLifetime, alignment=1]


@always_inline
Expand All @@ -79,7 +79,7 @@ fn global_alloc[T: AnyType, len: Int]() -> PyGlobalPtr[T]:

fn pointer_bitcast[
To: AnyType
](ptr: Pointer) -> Pointer[To, ptr.lifetime, ptr.address_space, *_, **_] as out:
](ptr: Pointer) -> Pointer[To, ptr.origin, ptr.address_space, *_, **_] as out:
return __type_of(out)(
_mlir_value=__mlir_op.`lit.ref.from_pointer`[
_type = __type_of(out)._mlir_type
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/builtin/anytype.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ trait AnyType:
an instance of the object reaches the end of its lifetime. Hence, only
non-trivial types may have destructors.
Any composition of types that have lifetimes is also an object with a
Any composition of types that have origins is also an object with a
lifetime, and the resultant type receives a destructor regardless of whether
the user explicitly defines one.
Expand Down
Loading

0 comments on commit 3af321e

Please sign in to comment.