Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/nightly' into make-splitlines-…
Browse files Browse the repository at this point in the history
…return-list-stringslice
  • Loading branch information
martinvuyk committed Jan 9, 2025
2 parents 02c8738 + 79c4236 commit 66af789
Show file tree
Hide file tree
Showing 35 changed files with 4,462 additions and 4,454 deletions.
46 changes: 46 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,47 @@ what we publish.
a `StringSlice` from a buffer containing UTF-8 encoded data. This method will
raise if the buffer contents are not valid UTF-8.
- Several standard library functions have been changed to take `StringSlice`
instead of `String`. This generalizes them to be used for any appropriately
encoded string in memory, without requiring that the string be heap allocated.
- `atol()`
- `atof()`
- `ord()`
- `ascii()`
- `b64encode()`
- Additionally, the `b64encode()` overload that previously took `List` has
been changed to
take a `Span`.
- `b64decode()`
- `b16encode()`
- `b16decode()`
- Various functionality has moved from `String` and `StringRef` to the more
general `StringSlice` type.
- `StringSlice` now implements `Representable`, and that implementation is now
used by `String.__repr__()` and `StringRef.__repr__()`.
- `StringSlice` now implements `EqualityComparable`.
Up until now, `StringSlice` has implemented a more general `__eq__` and
`__ne__` comparision with `StringSlice` types that had arbitrary other
origins. However, to satisfy `EqualityComparable`, `StringSlice` now also
has narrower comparison methods that support comparing only with
`StringSlice`'s with the exact same origin.
- Removed `@implicit` decorator from some standard library initializer methods
that perform allocation. This reduces places where Mojo code could implicitly
allocate where the user may not be aware.
Remove `@implicit` from:
- `String.__init__(out self, StringRef)`
- `String.__init__(out self, StringSlice)`
- `List.__init__(out self, owned *values: T)`
- `List.__init__(out self, span: Span[T])`
- The `ExplicitlyCopyable` trait has changed to require a
`fn copy(self) -> Self` method. Previously, an initializer with the signature
`fn __init__(out self, *, other: Self)` had been required by
Expand All @@ -117,7 +158,9 @@ what we publish.
- `StringRef` is being deprecated. Use `StringSlice` instead.
- Changed `sys.argv()` to return list of `StringSlice`.
- Added `Path` explicit constructor from `StringSlice`.
- removed `StringRef.startswith()` and `StringRef.endswith()`
- removed `StringRef.strip()`
### 🛠️ Fixed
Expand All @@ -132,5 +175,8 @@ what we publish.
- [Issue #3540](https://github.com/modularml/mojo/issues/3540) - Using named
output slot breaks trait conformance
- [Issue #3617](https://github.com/modularml/mojo/issues/3617) - Can't generate
the constructors for a type wrapping `!lit.ref`
- The Mojo Language Server doesn't crash anymore on empty **init**.mojo files.
[Issue #3826](https://github.com/modularml/mojo/issues/3826).
8 changes: 3 additions & 5 deletions docs/manual/get-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ Hello, World!
Let's extend this basic program by prompting the user for their name and
including that in the greeting printed. The built-in
[`input()`](/mojo/stdlib/builtin/io/input) function accepts an optional
[`String`](/mojo/stdlib/collections/string/String) argument to use as a prompt,
and returns a `String` consisting of the characters the user entered (with the
newline character at the end stripped off).
[`String`](/mojo/stdlib/collections/string/string/String) argument to use as a
prompt, and returns a `String` consisting of the characters the user entered
(with the newline character at the end stripped off).

So let's declare a variable, assign the return value from `input()` to it, and
build a customized greeting.
Expand Down Expand Up @@ -786,8 +786,6 @@ struct Grid(StringableRaising):
<summary>Click here to see the complete `gridv1.mojo` so far:</summary>

```mojo title="gridv1.mojo"
import random
@value
struct Grid(StringableRaising):
var rows: Int
Expand Down
3 changes: 2 additions & 1 deletion docs/manual/variables.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,11 @@ floating-point type, it converts the value instead of giving a compiler error:

```mojo
var number: Float64 = Int(1)
print(number)
```

```output
1
1.0
```

As shown above, value assignment can be converted into a constructor call if the
Expand Down
1,589 changes: 802 additions & 787 deletions examples/life/magic.lock

Large diffs are not rendered by default.

1,494 changes: 757 additions & 737 deletions examples/magic.lock

Large diffs are not rendered by default.

1,673 changes: 856 additions & 817 deletions examples/notebooks/magic.lock

Large diffs are not rendered by default.

1,494 changes: 757 additions & 737 deletions examples/operators/magic.lock

Large diffs are not rendered by default.

1,494 changes: 757 additions & 737 deletions magic.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions stdlib/benchmarks/collections/bench_dict.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ from bit import next_power_of_two
fn make_dict[size: Int]() -> Dict[Int, Int]:
var d = Dict[Int, Int]()
for i in range(0, size):
d[i] = random.random_si64(0, size).value
d[i] = int(random.random_si64(0, size))
return d


Expand Down Expand Up @@ -62,7 +62,7 @@ fn bench_dict_insert[size: Int](mut b: Bencher) raises:
@parameter
fn call_fn() raises:
for key in range(size, size + 100):
items[key] = random.random_si64(0, size).value
items[key] = int(random.random_si64(0, size))

b.iter[call_fn]()
keep(bool(items))
Expand Down
6 changes: 3 additions & 3 deletions stdlib/src/base64/_b64encode.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ from collections import InlineArray
from math.math import _compile_time_iota
from sys import llvm_intrinsic

from memory import UnsafePointer, bitcast, memcpy
from memory import Span, UnsafePointer, bitcast, memcpy

from utils import IndexList

Expand Down Expand Up @@ -195,10 +195,10 @@ fn load_incomplete_simd[
return result


# TODO: Use Span instead of List as input when Span is easier to use
@no_inline
fn b64encode_with_buffers(
input_bytes: List[UInt8, _], mut result: List[UInt8, _]
input_bytes: Span[Byte, _],
mut result: List[UInt8, _],
):
alias simd_width = sys.simdbytewidth()
alias input_simd_width = simd_width * 3 // 4
Expand Down
33 changes: 17 additions & 16 deletions stdlib/src/base64/base64.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ from base64 import b64encode
"""

from collections import List
from collections.string import StringSlice
from memory import Span
from sys import simdwidthof

import bit
Expand All @@ -32,7 +34,7 @@ from ._b64encode import b64encode_with_buffers as _b64encode_with_buffers


@always_inline
fn _ascii_to_value(char: String) -> Int:
fn _ascii_to_value(char: StringSlice) -> Int:
"""Converts an ASCII character to its integer value for base64 decoding.
Args:
Expand Down Expand Up @@ -64,8 +66,7 @@ fn _ascii_to_value(char: String) -> Int:
# ===-----------------------------------------------------------------------===#


# TODO: Use Span instead of List as input when Span is easier to use
fn b64encode(input_bytes: List[UInt8, _], mut result: List[UInt8, _]):
fn b64encode(input_bytes: Span[Byte, _], mut result: List[Byte, _]):
"""Performs base64 encoding on the input string.
Args:
Expand All @@ -76,7 +77,7 @@ fn b64encode(input_bytes: List[UInt8, _], mut result: List[UInt8, _]):


# For a nicer API, we provide those overloads:
fn b64encode(input_string: String) -> String:
fn b64encode(input_string: StringSlice) -> String:
"""Performs base64 encoding on the input string.
Args:
Expand All @@ -85,11 +86,10 @@ fn b64encode(input_string: String) -> String:
Returns:
The ASCII base64 encoded string.
"""
# Slicing triggers a copy, but it should work with Span later on.
return b64encode(input_string._buffer[:-1])
return b64encode(input_string.as_bytes())


fn b64encode(input_bytes: List[UInt8, _]) -> String:
fn b64encode(input_bytes: Span[Byte, _]) -> String:
"""Performs base64 encoding on the input string.
Args:
Expand All @@ -112,7 +112,7 @@ fn b64encode(input_bytes: List[UInt8, _]) -> String:


@always_inline
fn b64decode(str: String) -> String:
fn b64decode(str: StringSlice) -> String:
"""Performs base64 decoding on the input string.
Args:
Expand Down Expand Up @@ -150,19 +150,20 @@ fn b64decode(str: String) -> String:
p.append(((c & 0x03) << 6) | d)

p.append(0)
return p

return String(p^)


# ===-----------------------------------------------------------------------===#
# b16encode
# ===-----------------------------------------------------------------------===#


fn b16encode(str: String) -> String:
"""Performs base16 encoding on the input string.
fn b16encode(str: StringSlice) -> String:
"""Performs base16 encoding on the input string slice.
Args:
str: The input string.
str: The input string slice.
Returns:
Base16 encoding of the input string.
Expand All @@ -176,7 +177,7 @@ fn b16encode(str: String) -> String:
@parameter
@always_inline
fn str_bytes(idx: UInt8) -> UInt8:
return str._buffer[int(idx)]
return str._slice[int(idx)]

for i in range(length):
var str_byte = str_bytes(i)
Expand All @@ -196,7 +197,7 @@ fn b16encode(str: String) -> String:


@always_inline
fn b16decode(str: String) -> String:
fn b16decode(str: StringSlice) -> String:
"""Performs base16 decoding on the input string.
Args:
Expand All @@ -209,7 +210,7 @@ fn b16decode(str: String) -> String:
# TODO: Replace with dict literal when possible
@parameter
@always_inline
fn decode(c: String) -> Int:
fn decode(c: StringSlice) -> Int:
var char_val = ord(c)

if ord("A") <= char_val <= ord("Z"):
Expand All @@ -232,4 +233,4 @@ fn b16decode(str: String) -> String:
p.append(decode(hi) << 4 | decode(lo))

p.append(0)
return p
return String(p^)
17 changes: 8 additions & 9 deletions stdlib/src/builtin/_stubs.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,11 @@ fn parameter_for_generator[
fn _generator[
IteratorT: _IntIter
](it: IteratorT) -> _ParamForIterator[IteratorT]:
if it.__len__() == 0:
return _ParamForIterator[IteratorT](
__mlir_attr[`#kgen.unknown : !kgen.paramref<`, IteratorT, `>`],
0,
True,
)
var next_it = it
var value = next_it.__next__()
return _ParamForIterator(next_it, value, False)
if it.__len__() != 0:
var next_it = it
var value = next_it.__next__()
return _ParamForIterator(next_it, value, False)

var value: IteratorT
__mlir_op.`lit.ownership.mark_initialized`(__get_mvalue_as_litref(value))
return _ParamForIterator(value^, 0, True)
21 changes: 8 additions & 13 deletions stdlib/src/builtin/debug_assert.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ These are Mojo built-ins, so you don't need to import them.


from os import abort
from sys import is_gpu, is_nvidia_gpu, llvm_intrinsic
from sys import is_gpu, is_nvidia_gpu, is_amd_gpu, llvm_intrinsic
from sys._build import is_debug_build
from sys.ffi import c_char, c_size_t, c_uint, external_call
from sys.param_env import env_get_string
Expand Down Expand Up @@ -241,14 +241,15 @@ fn _debug_assert_msg(
var stdout = sys.stdout

@parameter
if is_gpu():
if is_amd_gpu():
# FIXME: debug_assert printing is disabled on AMD GPU, see KERN-1448
pass
elif is_nvidia_gpu():
# Count the total length of bytes to allocate only once
var arg_bytes = _ArgBytes()
arg_bytes.write(
"At ",
loc,
": ",
_ThreadContext(),
" Assert ",
"Warning: " if defined_mode == "warn" else " Error: ",
)
Expand All @@ -258,8 +259,6 @@ fn _debug_assert_msg(
buffer.write(
"At ",
loc,
": ",
_ThreadContext(),
" Assert ",
"Warning: " if defined_mode == "warn" else "Error: ",
)
Expand All @@ -269,10 +268,6 @@ fn _debug_assert_msg(
Span[Byte, ImmutableAnyOrigin](ptr=buffer.data, length=buffer.pos)
)

@parameter
if defined_mode != "warn":
abort()

else:
var buffer = _WriteBufferStack[4096](stdout)
buffer.write("At ", loc, ": ")
Expand All @@ -286,9 +281,9 @@ fn _debug_assert_msg(
write_args(buffer, messages, end="\n")
buffer.flush()

@parameter
if defined_mode != "warn":
abort()
@parameter
if defined_mode != "warn":
abort()


struct _ThreadContext(Writable):
Expand Down
11 changes: 6 additions & 5 deletions stdlib/src/builtin/file.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -472,11 +472,12 @@ struct FileHandle:
return self^

fn _get_raw_fd(self) -> Int:
var i64_res = external_call[
"KGEN_CompilerRT_IO_GetFD",
Int64,
](self.handle)
return Int(i64_res.value)
return int(
external_call[
"KGEN_CompilerRT_IO_GetFD",
Int64,
](self.handle)
)


fn open[
Expand Down
Loading

0 comments on commit 66af789

Please sign in to comment.