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] Implement Int.from_bytes() and Int.as_bytes() #3768

Closed
wants to merge 71 commits into from

Conversation

msaelices
Copy link
Contributor

@msaelices msaelices commented Nov 12, 2024

Similar to the Python's int.from_bytes() and int.to_bytes() one.

@msaelices msaelices requested a review from a team as a code owner November 12, 2024 23:04
@JoeLoser JoeLoser requested a review from lsh November 14, 2024 17:16
@msaelices msaelices requested review from jackos and a team as code owners November 20, 2024 18:31
@msaelices msaelices changed the title [stdlib] Implement Int.from_bytes() [stdlib] Implement Int.from_bytes() and Int.as_bytes() Nov 21, 2024
msaelices and others added 25 commits November 22, 2024 10:37
Similar to the Python int.from_bytes() one

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
…ular#3770)

Some new examples that are being added are being tested as standalone
`magic` projects, not with `lit`. Ignore these files from the lit
config.

We may need to also add tests in CI for these separately, that could be
a followup once we have some examples in place.

Signed-off-by: Alex Trotta <atrotta@modular.com>
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
…3771)

Lit config doesn't support wildcards _or_ directories, but it does
support entire folders, so here we're just searching for every folder
under `examples` and adding it to the exclude.

I ran a quick test by copying `hello_interop.mojo` to `examples/project`
(and renaming to `hello_interop2.mojo` to ignore the current excludes),
then running `magic run examples`. The new file was correctly ignored.

Signed-off-by: Alex Trotta <atrotta@modular.com>
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
functions

Also change to use `stack_allocation()` inside of
`check_and_get_or_convert_arg()` -- this simplifies the code needed on the
caller side.

MODULAR_ORIG_COMMIT_REV_ID: c9ec14f33bac7d1c464058dd78ce11f448afb527

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
* New Mojo manual page documenting operators and dunder methods
* An accompanying tutorial showing how to operators for a custom Mojo
type
* Complete example code for the tutorial

MODULAR_ORIG_COMMIT_REV_ID: 40762247b51da9d5b2b958738c846a504d538248

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
MODULAR_ORIG_COMMIT_REV_ID: 86e3e1b9f8ba59d1012f88d5da528ca6f165f30c

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
MODULAR_ORIG_COMMIT_REV_ID: c3553f83d8ff65680949a3119a312aeed7c3fa24

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
consistency

MODULAR_ORIG_COMMIT_REV_ID: 48e5472978812d786082d4c5906daf844e8a5da0

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
…1401

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
MODULAR_ORIG_COMMIT_REV_ID: e6882d6be8b161bf493fd1dafe0c727f4f4cb428

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
MODULAR_ORIG_COMMIT_REV_ID: cb3ebf296399f5166893aa5d6e7d5c8560bdfb49

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
MODULAR_ORIG_COMMIT_REV_ID: 85797bf247f55c80748fc56d468a9d8c2e0e2767

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
…1405

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
[External] [stdlib] Fix `math.math.mojo` examples

Part of modular#3572.

Co-authored-by: soraros <soraros@users.noreply.github.com>
Closes modular#3763
MODULAR_ORIG_COMMIT_REV_ID: 9b499c7e23f0fc7cccc3ab3ff75326364d032fe8

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
[External] [stdlib] Clean up `b64encode` (2/N)

Co-authored-by: soraros <soraros@users.noreply.github.com>
Closes modular#3746
MODULAR_ORIG_COMMIT_REV_ID: e5bf916a6cc953c18bcb23b482de4a86778d5f52

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
[External] [stdlib] Make `StringRef` Representable

- [X] Implement `StringRef.__repr__()`
- [X] Implement `Writable` for `os.stat_result`

Co-authored-by: Manuel Saelices <msaelices@gmail.com>
Closes modular#3736
MODULAR_ORIG_COMMIT_REV_ID: f8fe27aa7b0274fa27665b8cd0b19af22f3cac3b

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
This change passed tests initially but caused issues specifically on
linux + Intel after being merged internally. Reverting fixes the issue
for now while we investigate.

MODULAR_ORIG_COMMIT_REV_ID: 0aa612d4d89883645c0649593f8f306d31d35268

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
MODULAR_ORIG_COMMIT_REV_ID: 157fb1e14ce62552ea978397e3bc164925127ce4

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
MODULAR_ORIG_COMMIT_REV_ID: 151283eee2ad3d2b32a9a99c318fce87f2fb33f2

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
…1505

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
variables

* Also removed various unused imports of related types.

The Mojo standard library currently contains a pseudo-global-variables
mechanism based on
a global hashmap. This allows one to use functionality that behaves like
global variables, despite
the fact that Mojo does not currently have complete language-level
support for global
variables (i.e. `var` statements at the top level scope).

The current interface in the standard library is very low-level, error
prone, and requires juggling
`OpaquePointer` values.

With this new interface, declaring a global variable containing e.g. a
String looks like:

```mojo
alias MY_GLOBAL = _Global["MY_GLOBAL", String, _init_my_global]

fn _init_my_global() -> String:
    return "default value"
```

This new interface adds the following constraints that make it easier to
reason
about and use:

* The data type stored by the global variable must be declared.
* The initializer function cannot take any arguments (no "payload" data)
* The global variable is de-initialized by calling the data type's
`__del__()` method (not an arbitrary function).

MODULAR_ORIG_COMMIT_REV_ID: 40575002cd16e7ecb931a0143102f3956dfb8ef7

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
Ahajha and others added 24 commits November 22, 2024 10:38
This change passed tests initially but caused issues specifically on
linux + Intel after being merged internally. Reverting fixes the issue
for now while we investigate.

MODULAR_ORIG_COMMIT_REV_ID: 0aa612d4d89883645c0649593f8f306d31d35268

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
variables

* Also removed various unused imports of related types.

The Mojo standard library currently contains a pseudo-global-variables
mechanism based on
a global hashmap. This allows one to use functionality that behaves like
global variables, despite
the fact that Mojo does not currently have complete language-level
support for global
variables (i.e. `var` statements at the top level scope).

The current interface in the standard library is very low-level, error
prone, and requires juggling
`OpaquePointer` values.

With this new interface, declaring a global variable containing e.g. a
String looks like:

```mojo
alias MY_GLOBAL = _Global["MY_GLOBAL", String, _init_my_global]

fn _init_my_global() -> String:
    return "default value"
```

This new interface adds the following constraints that make it easier to
reason
about and use:

* The data type stored by the global variable must be declared.
* The initializer function cannot take any arguments (no "payload" data)
* The global variable is de-initialized by calling the data type's
`__del__()` method (not an arbitrary function).

MODULAR_ORIG_COMMIT_REV_ID: 40575002cd16e7ecb931a0143102f3956dfb8ef7
…1606

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
This refactors some implicit conversion emission logic in the compiler's
OverloadSet processing logic to follow the `CallSyntax` enum instead of
using a redundant `allowImplicitConversions` boolean.  Beyond
unifying the code this exposes a problem where we allowed implicit
conversions from implicit conversions (invalid "a->b->c" conversion)
in one case.  This fixes that, which requires tightening up some logic
in the standard library.

MODULAR_ORIG_COMMIT_REV_ID: fe26e7bfdb8d36b36f9537d5370a142076a58feb
…1705

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
This treats `ref x: Foo` the same as `ref [_] x: Foo`, inferring
the origin from the call site.  This was discussed in the arg
convention resyntaxing thread.

MODULAR_ORIG_COMMIT_REV_ID: 6a9d97b8ef01b87dab972a3e783315ac3db8ff12
This moves from `self: Self` to just `self` to clean things up.

MODULAR_ORIG_COMMIT_REV_ID: 1a26d34dcb9cc22b9d8a9da0e94402a2730e786c

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
This adds `@implicit` to all single-argument constructors to enable
explicit by default construction. Implicit conversions are used heavily
throughout the codebase, and so this is a required step to get it to
compile. This allows us to take off `@implicit` constructors where
they don't make sense, and fix the compiler errors one step at
a time.

MODULAR_ORIG_COMMIT_REV_ID: 931a2f38a0f9fe87007e334158f1fdf399f04bb2

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
Require `@implicit` decorator above single argument `__init__` function
to allow it to be implicitly constructed. See changelog entry in this
commit for an example and more details.

This fixes modular#1310

MODULAR_ORIG_COMMIT_REV_ID: ace1a1da978fd7acbd9a8ea60d807448f8889fcf
And slight rewording.

MODULAR_ORIG_COMMIT_REV_ID: cc40b7c83b539274076c83197a704c8367fc5fac

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
…1816

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
…ctions (#47479)

[External] [stdlib] Add full unicode support for character casing
functions

The code I used to generate the lookup tables can be found here
https://gist.github.com/mzaks/bbadaeebcf81a5200021af041568b26b

Co-authored-by: Maxim Zaks <maxim.zaks@gmail.com>
Closes modular#3496
MODULAR_ORIG_COMMIT_REV_ID: 971d447e40e3221b84df526393f984b7fe22fcba

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
Since `Span` is just a pointer and length, it's a trivial type, and
`StringSlice` is just a wrapper around `Span`. This should provide a
small speedup.

MODULAR_ORIG_COMMIT_REV_ID: 6ce887ad907e0851f353a9ee9f3cb84eaa53c2cb

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
Added conversion from float8 to float32

MODULAR_ORIG_COMMIT_REV_ID: 441ab0aaac7dbf2235f26dc14f0c38bc60f12c64

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
Makes mem-ops in memory.mojo more generic.

MODULAR_ORIG_COMMIT_REV_ID: 9ac40b0fb895905026ff386e194954bfb130d498

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
using llvm_intrinsic instead of _call_libm on AMD GPUs

MODULAR_ORIG_COMMIT_REV_ID: bbd8e506d72f335a62425dbfb62f94b72ec339f9

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
`__ceildiv__`.

The traits currently require `__floordiv__`, `__rfloordiv__`, and
`__neg__`. Modelling `CieldDivable{Raising}` using those traits ignores
unsigned numeric types where `__neg__` doesn't make sense. Having the
types implement `__ceildiv__` directly reduces the change of
implementing it wrong.

MODULAR_ORIG_COMMIT_REV_ID: f043dc093244082d03eb643c8080341c009b8855

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
Adds support for more GPUs to the sleep function.

MODULAR_ORIG_COMMIT_REV_ID: c1e1f113c7f51e1386f3bbce5860c4e778e8cdf5

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
…2020

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
This refactors some implicit conversion emission logic in the compiler's
OverloadSet processing logic to follow the `CallSyntax` enum instead of
using a redundant `allowImplicitConversions` boolean.  Beyond
unifying the code this exposes a problem where we allowed implicit
conversions from implicit conversions (invalid "a->b->c" conversion)
in one case.  This fixes that, which requires tightening up some logic
in the standard library.

MODULAR_ORIG_COMMIT_REV_ID: fe26e7bfdb8d36b36f9537d5370a142076a58feb

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
This adds `@implicit` to all single-argument constructors to enable
explicit by default construction. Implicit conversions are used heavily
throughout the codebase, and so this is a required step to get it to
compile. This allows us to take off `@implicit` constructors where
they don't make sense, and fix the compiler errors one step at
a time.

MODULAR_ORIG_COMMIT_REV_ID: 931a2f38a0f9fe87007e334158f1fdf399f04bb2

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
@msaelices msaelices changed the base branch from nightly to main November 22, 2024 09:48
@msaelices msaelices changed the base branch from main to nightly November 22, 2024 09:49
@msaelices
Copy link
Contributor Author

Closed in favor of #3795

This PR ends up in a mess because of conflicts with the nightly branch

@msaelices msaelices closed this Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.