Commit graph

272 commits

Author SHA1 Message Date
Sebastian Ullrich
1248a55d32
perf: avoid taking mutex on already-resolved promises (#6984) 2025-02-07 10:14:35 +00:00
Sebastian Ullrich
ac9708051a
feat: respect Task.map/bind (sync := true) after waiting (#6976)
This PR extends the behavior of the `sync` flag for `Task.map/bind` etc.
to encompass synchronous execution even when they first have to wait on
completion of the first task, drastically lowering the overhead of such
tasks. Thus the flag is now equivalent to e.g. .NET's
`TaskContinuationOptions.ExecuteSynchronously`.
2025-02-07 09:06:57 +00:00
Eric Wieser
0d7e126a01
chore: re-land "perf: use C23's free_sized when available" (#6844)
Unreverts #6598

I'll combine #6825 into this before merging.
2025-02-04 12:43:56 +00:00
Sebastian Ullrich
a35bf7ee4c
chore: revert "perf: use C23's free_sized when available" (#6841)
Reverts leanprover/lean4#6598, which broke Windows CI
2025-01-29 09:11:23 +00:00
Eric Wieser
6aa6407af1
perf: use C23's free_sized when available (#6598)
See https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2699.htm for an
explanation of this feature.

---------

Co-authored-by: Chris Kennelly <ckennelly@google.com>
2025-01-28 10:17:15 +00:00
Sebastian Ullrich
214093e6c4
fix: prevent Task.get deadlocks from threadpool starvation (#6758)
This PR prevents deadlocks from non-cyclical task waits that may
otherwise occur during parallel elaboration with small threadpool sizes.
2025-01-23 23:01:39 +00:00
Leonardo de Moura
2e11b8ac88
feat: add support for Float32 to the Lean runtime (#6348)
This PR adds support for `Float32` to the Lean runtime.

We need an update stage0, and then uncomment `Float32.lean` file.
2024-12-09 21:33:43 +00:00
Thomas Köppe
91c14c7ee9
fix: only consider salient bytes in sharecommon eq, hash (#5840)
This PR changes `lean_sharecommon_{eq,hash}` to only consider the
salient bytes of an object, and not any bytes of any
unspecified/uninitialized unused capacity.

Accessing uninitialized storage results in undefined behaviour.

This does not seem to have any semantics disadvantages: If objects
compare equal after this change, their salient bytes are still equal. By
contrast, if the actual identity of allocations needs to be
distinguished, that can be done by just comparing pointers to the
storage.

If we wanted to retain the current logic, we would need initialize the
otherwise unused parts to some specific value to avoid the undefined
behaviour.

Closes #5831
2024-11-19 13:56:46 +00:00
Leonardo de Moura
f13e5ca852
chore: naming convention and NaN normalization (#6097)
Changes:
- `Float.fromBits` => `Float.ofBits`
- NaN normalization
2024-11-16 00:14:28 +00:00
Leonardo de Moura
ecbaeff24b
feat: add Float.toBits and Float.fromBits (#6094)
This PR adds raw transmutation of floating-point numbers to and from
`UInt64`. Floats and UInts share the same endianness across all
supported platforms. The IEEE 754 standard precisely specifies the bit
layout of floats. Note that `Float.toBits` is distinct from
`Float.toUInt64`, which attempts to preserve the numeric value rather
than the bitwise value.

closes #6071
2024-11-15 19:45:19 +00:00
Henrik Böving
c77b6a2c64
feat: define ISize and basic operations on it (#5961) 2024-11-05 15:08:19 +00:00
Henrik Böving
93dd6f2b36
feat: add Int16/Int32/Int64 (#5885)
This adds all fixed width integers with the exception of `ssize_t` so
the code is quick to review as everything just behaves the same.
2024-11-04 13:18:05 +00:00
Henrik Böving
844e7ae7eb
chore: remove native code for UInt8.modn (#5901)
Closes #5818
2024-10-31 12:42:24 +00:00
Henrik Böving
193b6f2bec
feat: define Int8 (#5790) 2024-10-25 06:06:40 +00:00
Kim Morrison
e41e305479 chore: rename Array.data to Array.toList 2024-09-10 15:24:23 +10:00
Sebastian Ullrich
dd4e26f247
feat: output panics into Lean's redirected stderr (#4952)
...unless we are about to kill the process anyway (which is not the
default)

Ensures panics are visible as regular messages in the language server
and properly ordered in relation to other messages on the cmdline
2024-08-12 12:15:15 +00:00
Mario Carneiro
0a1a855ba8
fix: validate UTF-8 at C++ -> Lean boundary (#3963)
Continuation of #3958. To ensure that lean code is able to uphold the
invariant that `String`s are valid UTF-8 (which is assumed by the lean
model), we have to make sure that no lean objects are created with
invalid UTF-8. #3958 covers the case of lean code creating strings via
`fromUTF8Unchecked`, but there are still many cases where C++ code
constructs strings from a `const char *` or `std::string` with unclear
UTF-8 status.

To address this and minimize accidental missed validation, the
`(lean_)mk_string` function is modified to validate UTF-8. The original
function is renamed to `mk_string_unchecked`, with several other
variants depending on whether we know the string is UTF-8 or ASCII and
whether we have the length and/or utf8 char count on hand. I reviewed
every function which leads to `mk_string` or its variants in the C code,
and used the appropriate validation function, defaulting to `mk_string`
if the provenance is unclear.

This PR adds no new error handling paths, meaning that incorrect UTF-8
will still produce incorrect results in e.g. IO functions, they are just
not causing unsound behavior anymore. A subsequent PR will handle adding
better error reporting for bad UTF-8.
2024-06-19 14:05:48 +00:00
Mac Malone
25e94f916f
feat: IO.TaskState (#4097)
Adds `IO.getTaskState` which returns the state of a `Task` in the Lean
runtime's task manager. The `TaskState` inductive has 3 constructors:
`waiting`, `running`, and `finished`. The `waiting` constructor
encompasses the waiting and queued states within the C task object
documentation, because the task object does not provide a low cost way
to distinguish these different forms of waiting. Furthermore, it seems
unlikely for consumers to wish to distinguish between these internal
states. The `running` constructor encompasses both the running and
promised states in C docs. While not ideal, the C implementation does
not provide a way to distinguish between a running `Task` and a waiting
`Promise.result` (they both have null closures).
2024-05-10 23:04:54 +00:00
Mario Carneiro
62cdb51ed5
feat: UTF-8 string validation (#3958)
Previously, there was a function `opaque fromUTF8Unchecked : ByteArray
-> String` which would convert a list of bytes into a string, but as the
name implies it does not validate that the string is UTF-8 before doing
so and as a result it produces unsound results in the compiler (because
the lean model of `String` indirectly asserts UTF-8 validity). This PR
replaces that function by
```lean
opaque validateUTF8 (a : @& ByteArray) : Bool

opaque fromUTF8 (a : @& ByteArray) (h : validateUTF8 a) : String
```
so that while the function is still "unchecked", we have a proof witness
that the string is valid. To recover the original, actually unchecked
version, use `lcProof` or other unsafe methods to produce the proof
witness.

Because this was the only `ByteArray -> String` conversion function, it
was used in several places in an unsound way (e.g. reading untrusted
input from IO and treating it as UTF-8). These have been replaced by
`fromUTF8?` or `fromUTF8!` as appropriate.
2024-04-20 18:36:37 +00:00
Mario Carneiro
aeacb7b69e
feat: String.Pos.isValid (#3959)
This adds a function that can be used to check whether a position is on
a UTF-8 byte boundary.
2024-04-20 14:57:35 +00:00
Joachim Breitner
504336822f
perf: faster Nat.repr implementation in C (#3876)
`Nat.repr` was implemented by generating a list of `Chars`, each created
by a 10-way if-then-else. This can cause significant slow down in some
particular use cases.

Now `Nat.repr` is `implemented_by` a faster implementation that uses
C++’s `std::to_string` on small numbers (< USize.size) and maintains an
array of pre-allocated strings for the first 128 numbers.

The handling of big numbers (≥ USize.size) remains as before.
2024-04-17 18:11:05 +00:00
Sebastian Ullrich
afbf8759e1
fix: deadlock in IO.Promise.resolve (#3693) 2024-03-20 12:47:52 +00:00
Sebastian Ullrich
380dd9e6e7 fix: free threadpool threads before process exit 2024-03-03 20:12:46 +01:00
Sebastian Ullrich
908b98dad8 fix: task_manager termination under Emscripten 2024-03-03 20:12:46 +01:00
Joe Hendrix
e2b3b34d14
feat: introduce native functions for Int.ediv / Int.emod (#3376)
These still need tests, but I thought I'd upstream so I can use
benchmarking and check for build errors.
2024-02-19 15:04:51 +00:00
Sebastian Ullrich
b548b4faae
refactor: make Promise implementation opaque (#3273)
This follows the standard `Ref` recipe and moves the `unsafeCast` into
C++
2024-02-09 10:43:41 +00:00
Sebastian Ullrich
6b0e7e1f46
feat: synchronous execution of task continuations (#3013)
In the new snapshot design, we have a tree of `Task`s that represents
the asynchronously processed document structure. When transforming this
tree in response to a user edit, we want to quickly run through
reusable, already computed nodes of the tree synchronously and then
spawn new tasks for the new parts. The new flag allows us to do such
mixed sync/async tree transformations uniformly. This flag exists as
e.g.
[`ExecuteSynchronously`](https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.taskcontinuationoptions?view=net-8.0)
in other runtimes.
2024-01-25 13:54:20 +00:00
Sebastian Ullrich
78200b309f
fix: run_task/deactivate_task race condition on m_imp->m_closure (#2959)
Fixes #2853, unblocking my work before I get to refactoring this part of
the task manager.
2023-12-12 02:01:40 +00:00
int-y1
ce4ae37c19 chore: fix more typos in comments 2023-10-08 14:37:34 -07:00
Leonardo de Moura
b5a736708f fix: fixes #2419 2023-08-14 16:18:30 -07:00
Gabriel Ebner
681bbe5cf4 feat: ByteArray.hash 2022-12-01 20:18:14 -08:00
Gabriel Ebner
9b416667e7 chore: replace all hashes by murmurhash 2022-12-01 20:18:14 -08:00
Leonardo de Moura
5eaa0fa2df chore: leftovers 2022-11-09 17:03:08 -08:00
Leonardo de Moura
92c03c0050 perf: prepare do add String.next' 2022-11-09 12:00:31 -08:00
Leonardo de Moura
20eeb4202f perf: fast String.get' without runtime bounds check
TODO: naming convention `String.get'` should be called `String.get`,
and we should rename the old `String.get`
2022-11-09 12:00:30 -08:00
Leonardo de Moura
dc750d143e chore: remove test/optimization that is essentially dead code 2022-10-27 16:45:50 -07:00
Gabriel Ebner
c2f1e01b3b feat: Promise 2022-09-05 08:52:46 -07:00
Sebastian Ullrich
af7f5aa2a0 feat: dbgStackTrace 2022-08-26 20:52:51 -07:00
Mario Carneiro
9de477ecf9 feat: add more float functions 2022-08-12 13:12:59 -07:00
Mario Carneiro
94f85ae649 fix: don't show NaN sign info in Float.toString 2022-08-12 08:21:47 -07:00
Leonardo de Moura
386b0a75bc fix: bug at lean_nat_mod
fixes at #1433
2022-08-06 08:07:25 -07:00
Leonardo de Moura
949dddbf63 fix: lean_float_array_data 2022-07-24 17:05:28 -07:00
Leonardo de Moura
757171db1f feat: add String.get! and s[i]! notation for String 2022-07-03 14:59:44 -07:00
Leonardo de Moura
e8935d996b chore: String.get?, String.getOp?, and remove String.getOp 2022-07-02 09:59:04 -07:00
Sebastian Ullrich
c8fb72195b feat: print panic backtraces on Linux 2022-06-29 16:29:35 +02:00
Leonardo de Moura
8d9428261e chore: remove Fix.lean
see #1208
2022-06-16 15:30:47 -07:00
Sebastian Ullrich
eb170d1f43 fix: compiled string literals containing null bytes 2022-05-17 09:24:34 -07:00
Gabriel Ebner
88e26b75b0 fix: actually abort with LEAN_ABORT_ON_PANIC
The previous null-pointer dereference was UB and therefore optimized
away.
2022-05-03 09:42:45 -07:00
Gabriel Ebner
412691c958 feat: support LEAN_NUM_THREADS environment variable 2021-12-21 17:01:08 +01:00
Sebastian Ullrich
b732484663 fix: do not consider worker threads as idle during startup
Without this change, enqueuing multiple tasks before the first worker
was started led to only a single worker being created. Now the first
increment and decrement happen under the task manager mutex, so
effectively the worker is never idle until it is out of tasks.
2021-12-21 12:01:23 +01:00