Skip to content

Commit

Permalink
Merge branch 'nightly' into int-from-and-to-bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
msaelices authored Nov 26, 2024
2 parents 21384ec + 91ef935 commit bc22870
Show file tree
Hide file tree
Showing 9 changed files with 1,466 additions and 1,441 deletions.
7 changes: 7 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ what we publish.
memory allocation and performance. These options allow for optimized memory usage
and reduced buffer reallocations, providing flexibility based on application requirements.

- A new `StringLiteral.from_string[someString]()` method is available. It
allows forming a runtime-constant StringLiteral from a compile-time-dynamic
`String` value.

### 🦋 Changed

- The argument convention for `__init__` methods has been changed from `inout`
Expand Down Expand Up @@ -590,3 +594,6 @@ what we publish.

- Tooling now prints the origins of `ref` arguments and results correctly, and
prints `self` instead of `self: Self` in methods.

- The LSP and generated documentation now print parametric result types
correctly, e.g. showing `SIMD[type, simd_width]` instead of `SIMD[$0, $1]`.
698 changes: 341 additions & 357 deletions examples/magic.lock

Large diffs are not rendered by default.

698 changes: 341 additions & 357 deletions examples/notebooks/magic.lock

Large diffs are not rendered by default.

700 changes: 342 additions & 358 deletions examples/operators/magic.lock

Large diffs are not rendered by default.

700 changes: 342 additions & 358 deletions magic.lock

Large diffs are not rendered by default.

36 changes: 35 additions & 1 deletion stdlib/src/builtin/constrained.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,38 @@ fn constrained[cond: Bool, msg: StringLiteral = "param assertion failed"]():
__mlir_op.`kgen.param.assert`[
cond = cond.__mlir_i1__(), message = msg.value
]()
return


@always_inline("nodebug")
fn constrained[cond: Bool, msg: String]():
"""Compile time checks that the condition is true.
The `constrained` is similar to `static_assert` in C++ and is used to
introduce constraints on the enclosing function. In Mojo, the assert places
a constraint on the function. The message is displayed when the assertion
fails, and takes a generalized string.
Parameters:
cond: The bool value to assert.
msg: The message to display on failure.
Example:
```mojo
from sys.info import num_physical_cores
def main():
alias cores_to_use = 2
multicore_check[cores_to_use]()
def multicore_check[cores: Int]():
constrained[
cores <= num_physical_cores(),
"build failed: not enough cores"
]()
constrained[
cores >= 2,
"at least two cores are required"
]()
"""
constrained[cond, StringLiteral.from_string[msg]()]()
2 changes: 1 addition & 1 deletion stdlib/src/builtin/simd.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -3368,7 +3368,7 @@ fn _write_scalar[
elif dtype.is_integral():

@parameter
if is_gpu():
if is_gpu() or dtype.is_integral():
var err = _try_write_int(writer, value)
if err:
abort(
Expand Down
34 changes: 30 additions & 4 deletions stdlib/src/builtin/string_literal.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,29 @@ struct StringLiteral(
"""
self = other

# TODO(MOCO-1460): This should be: fn __init__[*, value: String](out self):
# but Mojo tries to bind the parameter in `StringLiteral["foo"]()` to the
# type instead of the initializer. Use a static method to work around this
# for now.
@always_inline("nodebug")
@staticmethod
fn from_string[value: String]() -> StringLiteral:
"""Form a string literal from an arbitrary compile-time String value.
Parameters:
value: The string value to use.
Returns:
The string value as a StringLiteral.
"""
return __mlir_attr[
`#kgen.param.expr<data_to_str,`,
value.byte_length().value,
`,`,
value.unsafe_ptr().address,
`> : !kgen.string`,
]

# ===-------------------------------------------------------------------===#
# Operator dunders
# ===-------------------------------------------------------------------===#
Expand Down Expand Up @@ -902,9 +925,12 @@ struct StringLiteral(
return str(self).lstrip()


fn _to_string_literal(i: Int) -> StringLiteral:
return __mlir_op.`pop.string.create`(i)
fn _to_string_literal[val: Int]() -> StringLiteral:
alias s = StringLiteral.from_string[str(val)]()
return s


fn _to_string_literal(i: SIMD) -> StringLiteral:
return __mlir_op.`pop.string.create`(i)
fn _to_string_literal[val: SIMD]() -> StringLiteral:
constrained[val.type.is_integral(), "input type must be integral"]()
alias s = StringLiteral.from_string[str(val)]()
return s
32 changes: 27 additions & 5 deletions stdlib/src/builtin/type_aliases.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,40 @@ alias OriginSet = __mlir_type.`!lit.origin.set`
"""A set of origin parameters."""


# Helper to build a value of !lit.origin type.
# TODO: Should be a parametric alias.
@value
@register_passable("trivial")
struct Origin[is_mutable: Bool]:
"""This represents a origin reference of potentially parametric type.
TODO: This should be replaced with a parametric type alias.
"""This represents a origin reference for a memory value.
Parameters:
is_mutable: Whether the origin reference is mutable.
is_mutable: Whether the origin is mutable.
"""

alias type = __mlir_type[
`!lit.origin<`,
is_mutable.value,
`>`,
]

# ===-------------------------------------------------------------------===#
# Fields
# ===-------------------------------------------------------------------===#

var _mlir_origin: Self.type

# ===-------------------------------------------------------------------===#
# Life cycle methods
# ===-------------------------------------------------------------------===#

# NOTE:
# Needs to be @implicit convertible for the time being so that
# `__origin_of(..)` can implicilty convert to `Origin` in use cases like:
# Span[Byte, __origin_of(self)]
@implicit
@always_inline("nodebug")
fn __init__(out self, mlir_origin: Self.type):
"""Initialize an Origin from a raw MLIR `!lit.origin` value.
Args:
mlir_origin: The raw MLIR origin value."""
self._mlir_origin = mlir_origin

0 comments on commit bc22870

Please sign in to comment.