lean4-htt/src/initialize/init.cpp
Henrik Böving 52b1b342ab
feat: zero cost BaseIO (#10625)
This PR implements zero cost `BaseIO` by erasing the `IO.RealWorld`
parameter from argument lists and structures. This is a **major breaking
change for FFI**.

Concretely:
- `BaseIO` is defined in terms of `ST IO.RealWorld`
- `EIO` (and thus `IO`) is defined in terms of `EST IO.RealWorld`
- The opaque `Void` type is introduced and the trivial structure
optimization updated to account for it. Furthermore, arguments of type
`Void s` are removed from the argument lists of the C functions.
- `ST` is redefined as `Void s -> ST.Out s a` where `ST.Out` is a pair
of `Void s` and `a`

This together has the following major effects on our generated code:
- Functions that return `BaseIO`/`ST`/`EIO`/`IO`/`EST` now do not take
the dummy world parameter anymore. To account for this FFI code needs to
delete the dummy world parameter from the argument lists.
- Functions that return `BaseIO`/`ST` now return their wrapped value
directly. In particular `BaseIO UInt32` now returns a `uint32_t` instead
of a `lean_object*`. To account for this FFI code might have to change
the return type and does not need to call `lean_io_result_mk_ok` anymore
but can instead just `return` values right away (same with extracting
values from `BaseIO` computations.
- Functions that return `EIO`/`IO`/`EST` now only return the equivalent
of an `Except` node which reduces the allocation size. The
`lean_io_result_mk_ok`/`lean_io_result_mk_error` functions were updated
to account for this already so no change is required.

Besides improving performance by dropping allocation (sizes) we can now
also do fun new things such as:
```lean
@[extern "malloc"]
opaque malloc (size : USize) : BaseIO USize
```
2025-10-22 10:55:12 +02:00

66 lines
2.2 KiB
C++

/*
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
*/
#include "runtime/stackinfo.h"
#include "runtime/thread.h"
#include "runtime/init_module.h"
#include "util/init_module.h"
#include "util/io.h"
#include "kernel/init_module.h"
#include "library/init_module.h"
#include "library/constructions/init_module.h"
#include "library/print.h"
#include "initialize/init.h"
namespace lean {
extern "C" object* initialize_Init(uint8_t);
extern "C" object* initialize_Std(uint8_t);
extern "C" object* initialize_Lean(uint8_t);
/* Initializes the Lean runtime. Before executing any code which uses the Lean package,
you must first call this function, and then `lean::io_mark_end_initialization`. In between
these two calls, you may also have to run additional initializers for your own modules. */
extern "C" LEAN_EXPORT void lean_initialize() {
save_stack_info();
initialize_util_module();
uint8_t builtin = 1;
// Initializing the core libs explicitly is necessary because of references to them other than
// via `import`, such as:
// * calling exported Lean functions from C++
// * calling into native code of the current module from a previous stage when `prefer_native`
// is set
consume_io_result(initialize_Init(builtin));
consume_io_result(initialize_Std(builtin));
consume_io_result(initialize_Lean(builtin));
initialize_kernel_module();
init_default_print_fn();
initialize_library_core_module();
initialize_library_module();
initialize_constructions_module();
}
void finalize() {
run_thread_finalizers();
finalize_constructions_module();
finalize_library_module();
finalize_library_core_module();
finalize_kernel_module();
finalize_util_module();
run_post_thread_finalizers();
delete_thread_finalizer_manager();
}
initializer::initializer() {
lean_initialize();
/* Remark: We used to call `lean::io_mark_end_initialization` here, however this prevented
plugins from setting up global state such as environment extensions in their initializers.
See also `lean_initialize`. */
}
initializer::~initializer() {
finalize();
}
}