LLAM v3.0.1

Managed C

Edit pageReport a problemLast modified: 03 December 2026

llam_runtime_create() and llam_runtime_init() select required asynchronous preemption by default. On a qualified native profile, compiler-qualified CPU loops are interrupted without an application poll or yield. Unsupported profiles and unqualified task entries fail with ENOTSUP; the default does not fall back to cooperative execution. llam_runtime_create_managed() remains the advanced interface for an explicit policy and capsule/native-affine limits.

On this page

Use llam_runtime_create_compat() or llam_runtime_init_compat() for ordinary native C callbacks that need the former boundary/safepoint contract. The caller-sized singleton form is llam_runtime_init_compat_ex(). These explicit compatibility runtimes retain the AUTO/STRICT cooperative request policies.

Release workflows package the managed producer and native verifier alongside the runtime SDK on qualified targets. Install the matching LLVM tools separately.

The managed compiler emits LLPM2 records after instruction selection and validates the final linked image. The runtime discovers these records, checks the loaded image and retains an independent loader reference. Passing an ordinary function pointer or copying metadata into heap memory does not qualify that function for asynchronous preemption.

Library use and preemption

Static or shared linkage does not determine whether a task is asynchronously preemptible. LLAM can provide asynchronous preemption as a library when the task image is qualified, the runtime's native profile is supported, and the requested policy has passed startup admission.

How LLAM is used CPU preemption contract
Static/shared LLAM plus a qualified task image and a supported native profile, using a default constructor Required asynchronous execution; qualified CPU loops need no application poll or yield.
Ordinary, unqualified task code linked against LLAM, using a default constructor Unqualified task entries are refused with ENOTSUP. Unsupported native profiles also fail creation or startup.
Ordinary C tasks using a *_compat constructor Boundary/safepoint execution. Arbitrary CPU loops have no asynchronous interruption guarantee. Runtime I/O can still park tasks.
Another language calling LLAM through the C ABI The host can submit qualified C tasks, but its own foreign-language callbacks need separate qualification before they can be admitted as asynchronously preemptible tasks.

Compile host setup code in native mode and task code through the managed producer. The task's reachable calls must have supported effects and call contracts; wrapping an arbitrary foreign body in one managed function does not qualify that body. The final link of the task image must pass the LLPM2 verifier. The runtime then discovers or registers that image, retains its loader lifetime, and checks the selected native workers before user dispatch. Follow the module lifetime rules when loading or unloading task libraries.

Installing the standard runtime SDK and linking with a normal compiler does not perform these compiler steps. Build the matching managed producer as described below. The profile matrix lists the native worker probes for each backend.

Use llam_runtime_preempt_info_ex() from host code to inspect requested/effective policy, the reason, and RUNNING/COMMITTED state. A pending startup probe is not an active asynchronous profile. Extending this compiler path to another LLVM language is the next LLVM frontend integration milestone.

The managed producer requires LLVM 23.1.2 or a later 23.1 patch release. The installed producer records the LLVM version it was built against. The driver uses tools that match that version. Build the runtime and its matching compiler plugin together; see the build guide for platform dependencies. A local SDK prefix can be prepared with:

sh
cmake -S . -B build-managed \
  -DLLAM_EXPERIMENTAL_ASYNC_PREEMPT=ON \
  -DLLAM_MANAGED_TOOLING=required \
  -DLLVM_DIR=/path/to/llvm/lib/cmake/llvm
cmake --build build-managed -j4
cmake --install build-managed --prefix "$PWD/sdk"

Compile task translation units with the installed driver and use the driver again for the final link. Compile host setup code in native mode. For a POSIX shared-library SDK:

sh
export LLAM_LLVM_BIN=/path/to/llvm/bin
sdk/bin/llam-clang --llam-managed=off -Isdk/include -O2 -c host.c -o host.o
sdk/bin/llam-clang -Isdk/include -c tasks.c -o tasks.o
sdk/bin/llam-clang host.o tasks.o \
  -Lsdk/lib -lllam_runtime -Wl,-rpath,"$PWD/sdk/lib" -o managed-demo
./managed-demo
--llam-managed= Accepted by Effect
required (default) llam-clang, llam-managed-clang Rejects unsupported effects.
auto llam-clang, llam-managed-clang Leaves functions with unknown effects in the legacy class.
off llam-clang only Runs the native compiler. llam-managed-clang exits with status 2.

Both managed modes require the matching plugin and final-image verifier. With -flto=full, the driver combines the C inputs and standalone LLVM bitcode objects, optimizes the combined module, then qualifies and seals the generated machine code. On Mach-O and ELF, -flto=thin uses an LLD matching the LLVM plugin to resolve symbols and optimize each bitcode module. Compile separate objects with the same LTO flag used at link time. The driver also accepts --llam-isel=fast and --llam-isel=global on targets where LLVM supports those selectors. LLVM can fall back to SelectionDAG for functions its GlobalISel backend cannot lower. All selectors use the same final-machine and image checks. UBSan can use -fsanitize=undefined -fsanitize-trap=undefined on AArch64 and x86_64. On Linux and macOS, a matching compiler-rt also provides a fatal diagnostic with -fsanitize=undefined -fsanitize-minimal-runtime -fno-sanitize-recover=all. Use the same flags when compiling and linking separate objects. The diagnostic executes under a native guard and terminates the process. UBSan traps are excluded from capture. LLD's -Wl,--icf=safe can fold native code outside managed extents. The final check rejects any overlap of managed witnesses. Section rewriting and unrestricted folding require additional profiles.

On a qualified Linux or macOS host, build LLAM with Clang and pass -fsanitize=address or -fsanitize=thread to CMake through CMAKE_C_FLAGS. Use the matching flag with llam-managed-clang to compile task code and link the application against that instrumented runtime. Keep the default LLAM_PREEMPT_POLICY_ASYNC_REQUIRED policy for asynchronous execution. The producer qualifies instrumented code, and the runtime records sanitizer fiber switches outside the suspended-thread and signal-handler paths.

The C producer qualifies ASan and TSan per translation unit. Build these profiles without C driver LTO. Cooperative diagnostic builds still work with the native Clang frontend and LLAM_PREEMPT_POLICY_COOPERATIVE. See Platform support.

For separate linking pipelines, llam-managed-verify --seal-and-verify IMAGE combines sealing, macOS ad hoc signing and verification. The native verifier uses LLVM's in-process decoder and the runtime's LLPM parser. It checks the final executable mappings, exact call targets and guard control flow for ELF, Mach-O and PE. The existing managed_verify.py command uses the native verifier from the installed SDK when available.

llam-managed-verify --verify-object OBJECT checks relocatable ELF, Mach-O and COFF objects before link. Its JSON report includes the object SHA-256 and the unresolved symbol count. This stage checks metadata relocations and decoded continuations. The final verifier checks resolved calls and loader-bound guards and grants the image seal. The installed managed driver checks generated objects and the linked image automatically.

The Linux x86_64, Linux AArch64, and macOS release jobs build and qualify the managed producer before adding it to the SDK. The Intel macOS job builds LLVM 23.1.2 with its shared LLVM library from the pinned upstream sources. Use that shared-library build of LLVM with the Intel producer. Local POSIX release packaging reads build-managed/llam_managed_pass.so or the explicit LLAM_MANAGED_PLUGIN_PATH. The Windows release pairs the Visual Studio runtime with the producer and verifier built against MSYS2 UCRT64 LLVM 23.1. Put that matching toolchain on PATH for the DLL dependencies. The integration workflow exercises sealed PE admission through the ordinary constructor in both Visual Studio and MinGW runtime builds.

Required compiler and SDK CI

The ordinary Linux push/PR workflow includes linux-managed-compiler-sdk as a required prerequisite of its native build matrix. It downloads the exact LLVM 23.1.2 release archive with a pinned SHA-256, configures LLAM_MANAGED_TOOLING=required, and checks the compiler, public runtime import contracts, a real admitted image and the relocated installed SDK. Missing, duplicated, skipped or failed stages fail the job; automatic fallback cannot turn an absent compiler into a green check. Its result is explicitly propagated to the existing native check names, so a failed prerequisite is not reported as successful skipped matrix checks. The LLVM 14 legacy metadata lane remains separate.

The SDK check installs the complete package, relocates it into a path containing spaces, and compiles/links/runs a cross-translation-unit consumer using only its installed headers, shared runtime, driver, plugin and verifier. It also exercises blocking callback integration, unknown-effect rejection and missing-plugin errors.

To run this same chain against an existing required-mode build:

sh
PYTHONDONTWRITEBYTECODE=1 python3 scripts/verify_managed_ci.py \
  --build-dir build-managed --report-dir .artifacts/managed-ci --parallel 4

The build must have been configured with LLAM_MANAGED_TOOLING=required and the matching LLVM development package. Reports preserve the registered CTest inventory, build log and JUnit results.

The plugin follows the selected LLVM package's linkage model. Shared LLVM packages link its shared target; official Linux bundles with static tools use LLVM's loadable-module rules and the tools' exported plugin API. The plugin does not embed a separate LLVM registry. Both forms must pass the same compiler and installed-SDK checks.

Create and run tasks

An ordinary task can use calls, local arrays, atomics and supported arithmetic. For example, tasks.c can contain:

C
#include <stdint.h>

void cpu_task(void *argument) {
    uint64_t *values = argument;
    uint64_t value = values[0];
    for (uint64_t i = 0; i < 2000000; ++i)
        value = value * UINT64_C(6364136223846793005) + i;
    values[1] = value;
}

The host keeps task arguments alive until completion:

C
#include <llam/managed.h>
#include <stdint.h>
#include <stdio.h>

void cpu_task(void *);

int main(void) {
    llam_runtime_opts_t runtime_options;
    llam_runtime_t *runtime = NULL;
    uint64_t first[2] = {7, 0}, second[2] = {11, 0};
    if (llam_runtime_opts_init(&runtime_options, sizeof runtime_options))
        return 1;
    runtime_options.deterministic = 1;
    if (llam_runtime_create(&runtime_options, sizeof runtime_options, &runtime)) {
        perror("managed create");
        return 1;
    }
    llam_task_t *a = llam_runtime_spawn_ex(runtime, cpu_task, first, NULL, 0);
    llam_task_t *b = llam_runtime_spawn_ex(runtime, cpu_task, second, NULL, 0);
    if (!a || !b || llam_runtime_run_handle(runtime) || llam_join(a) || llam_join(b)) {
        perror("managed execution");
        llam_runtime_destroy(runtime);
        return 1;
    }
    printf("%llu %llu\n", (unsigned long long)first[1],
           (unsigned long long)second[1]);
    llam_runtime_destroy(runtime);
    return 0;
}

The default ASYNC_REQUIRED policy rejects legacy entries, incompatible whole-task NO_PREEMPT and unavailable native profiles before user entry. The refusals are ENOTSUP, except that creation with a preempt_mode of OFF or COOPERATIVE and spawn with LLAM_SPAWN_F_NO_PREEMPT fail with EINVAL. With ASYNC_AUTO, qualified tasks use asynchronous preemption when available. Accepted legacy tasks run on an isolated compatibility lane and the effective policy reports MIXED. An unqualified legacy busy loop has no asynchronous scheduling guarantee.

Calls, native TLS, and blocking

The compiler checks exact runtime API signatures and calling conventions. Portable code preserves its M's native signal mask. Signal-set creation and destruction carry native-affine effects because their subscription mask and thread cookie belong to an OS thread. Foreign-fast contracts must preserve native thread state; use a native-affine callback for persistent changes. The Darwin native profile owns asynchronous signal masks on managed Ms. It blocks asynchronous delivery while G continuations can be saved, reapplies that policy after native callbacks, and restores a returning host M's original mask after native roots drain. Signal-set APIs consume blocked signals; synchronous fault diagnostics remain enabled. Native-affine TLS destructors retain the owned mask until their dedicated OS thread exits. Calls between translation units in the same final image can use LLAM_MANAGED_TARGET(function_name)(arguments). The target must have a matching managed definition in that image. Unknown dynamic function pointers, escaping markers and unresolved targets are rejected in required mode. Statically bounded local target sets are checked before lowering to direct calls.

On ELF, Mach-O and Windows x64, annotate a portable C definition with LLAM_MANAGED_EXPORT and call its declaration with LLAM_MANAGED_IMPORT(function_name)(arguments). Seal both images. On Windows, link the caller against the provider's import library with eager loading. The caller's verified bridge uses the loader's immutable binding to the managed body. The runtime checks the exact entry, calling ABI and portable class against its pinned image catalog before starting the run. A missing provider or mismatched type returns EXDEV before dispatch.

Execution class Placement and native effects
PORTABLE Shared managed workers. Linux, macOS and Windows may restore an unpinned snapshot on an idle peer with compatible native state. Each restore acquires a fresh P lease.
NATIVE_AFFINE A dedicated OS thread preserves native TLS and thread identity through native thread exit.
LEGACY Compatibility execution; rejected by ASYNC_REQUIRED.

LLAM_SPAWN_F_NATIVE_AFFINE can reserve a dedicated M for an already-qualified entry. It cannot qualify arbitrary native code. PINNED retains its existing placement meaning and does not promise native thread identity.

Cooperative portable continuations can move between Ms. LLAM restores logical task errno through a fresh native accessor after each switch. Native TLS addresses retained by application C frames require the native-affine class.

Use llam_call_blocking_result() to offload foreign work. A native-affine task that must call foreign code on its own OS thread can use llam_call_native_affine_result(). This API drains earlier native operations, returns P before the callback and reacquires a fresh P on the same M before returning its result. The callback has no managed G/P authority; LLAM task APIs are rejected from it. Callback completion preserves its result, errno and Windows LastError. Cancellation before callback entry returns ECANCELED; after callback entry the runtime first completes the native return handoff.

What each context can call

required mode checks every call a qualified function makes. A call with no contract stops the compile with LLAM managed effect/ABI is unqualified; auto mode instead leaves that function in the legacy class.

API group Host thread Compatibility task Qualified function
Spawn and join, sync, channels, timers, cancellation, byte I/O, owned buffers, DNS, object constructors, stop requests, llam_runtime_get_resources_ex, llam_call_blocking_result, llam_now_ns, llam_wait_opts_init, llam_io_buffer_opts_init Per API; see Threading Yes Yes
Signal sets Yes Yes Yes; the function becomes NATIVE_AFFINE
llam_task_local_set_owned ENOTSUP Yes Yes; a portable task's cleanup must work on any scheduler worker
llam_call_native_affine_result ENOTSUP ENOTSUP Yes; the function becomes NATIVE_AFFINE
llam_poll_fd, llam_poll_handle Yes Yes Rejected: narrow integer ABI
llam_enter_blocking, llam_leave_blocking No-op Yes Rejected
Stats, JSON stats, dumps, llam_runtime_preempt_info_ex Yes Yes Rejected
Runtime create and init, llam_runtime_default, run, drain, shutdown, destroy, module registration, Windows opt-in Yes Per API: constructors and run drivers get EBUSY, except the *_compat constructors; shutdown and destroy only request a stop Rejected
llam_abi_version, llam_abi_get_info, llam_version_string, llam_runtime_opts_init, llam_spawn_opts_init, llam_preempt_opts_init Yes Yes Rejected

llam_managed_native_enter and llam_managed_native_leave are for the code the compiler generates, in every context.

Callbacks have their own rules:

Callback What it can call
llam_call_blocking_result It runs on a helper OS thread with no task, so the host-thread rules apply.
llam_call_native_affine_result Only llam_now_ns, llam_abi_version, llam_abi_get_info, llam_version_string, llam_runtime_opts_init, llam_spawn_opts_init, llam_wait_opts_init and llam_io_buffer_opts_init. Every other public function fails with ENOTSUP.

Keep setup and reporting in host code compiled with --llam-managed=off. When task code needs something the first table rejects, use the replacement:

Instead of Use Why
llam_poll_fd llam_poll_fd_until or llam_poll_fd_ex These take an int mask.
llam_poll_handle llam_poll_fd_until on POSIX, where a handle is a descriptor; a blocking callback on Windows
llam_enter_blocking llam_call_blocking_result Keeps the worker free.
Foreign work tied to the task's OS thread llam_call_native_affine_result Blocks only this task's own thread.
printf, strlen, other unlisted calls A callback compiled with --llam-managed=off A callback in a managed file is checked too.

Use a short local for revents when the task needs to distinguish readable input from hangup or error events. Pass NULL when only the poll's return value is needed.

For example, this task waits for input and then logs a line. Compile callbacks.c with --llam-managed=off and tasks.c with the managed driver:

callbacks.c
#include <stdio.h>

void *log_line(void *arg) {
    return puts(arg) >= 0 ? arg : NULL;
}
tasks.c
#include <llam/runtime.h>
#include <poll.h>

void *log_line(void *);

/* 0 once fd is readable and the line is logged, -1 otherwise. */
int wait_then_log(llam_fd_t fd, char *line) {
    uint64_t deadline = llam_now_ns() + UINT64_C(1000000000);
    void *logged = NULL;
    short revents = 0;

    if (llam_poll_fd_until(fd, POLLIN, deadline, &revents) <= 0 ||
        (revents & POLLIN) == 0) {
        return -1;
    }
    if (llam_call_blocking_result(log_line, line, &logged) != 0) {
        return -1;
    }
    return logged != NULL ? 0 : -1;
}

Foreign calls and effects

Only malloc, calloc, realloc, free, memcpy, memmove, memset and memcmp are recognized libc calls, and they need their standard signatures. Reading or writing errno, or any thread-local variable, makes the function NATIVE_AFFINE.

A foreign declaration can state its effect with an annotation. The compiler cannot check the function behind it, so the annotation is your promise:

C
__attribute__((annotate("llam-foreign-fast")))
extern long fast_mix(long);
Annotation Applies to Result
llam-foreign-fast Foreign declaration Callable; the caller stays PORTABLE. The function must return promptly, must not block, and must preserve native thread state.
llam-native-affine Declaration or definition Callable; the caller becomes NATIVE_AFFINE.
llam-foreign-blocking Foreign declaration Rejected: an inline call cannot park. Use llam_call_blocking_result.
llam-legacy Definition Compiles in the legacy class even in required. ASYNC_REQUIRED then refuses it at spawn.
llam-no-preempt Definition Rejected in required.

Policy, resources, and module lifetime

The field tables list each option, counter, and flag.

llam_runtime_preempt_info_ex() uses the usual caller-size handshake. Inspect requested and effective policies, the reason, RUNNING/COMMITTED flags, capsule charges, entry counts and available native-affine capacity. A pending or failed startup probe is not a committed asynchronous profile.

prepared_capsules and available_capsules describe the bounded prepared pool. reserved_capsule_bytes includes both free and in-use slots; capsule_charge_bytes counts slots retained by admitted tasks. These are storage-accounting values, not RSS. Preparation scales with the selected M count and budget. A native cold worker replenishes the pool without holding P; managed spawn returns EAGAIN if a prepared slot is temporarily unavailable, or ENOMEM at the declared quota, before publishing G.

An affine slot becomes available again after its previous native thread has exited, TLS destructors and native readers have drained, and a fresh OS thread has passed its native probe. A delayed join of the old G cannot release a newer slot reservation. EAGAIN during that refill is retryable admission failure.

Dynamic workers soft-park idle portable Ms and offline only empty, unowned Ps. Queues, timers, live allocation owners and in-flight I/O prevent P offlining; wake resources remain open, and publication or pressure can reactivate P. Portable native threads remain bound until run end because saved C frames may retain native TLS addresses. Affine M replacement happens only after native thread exit and complete G retirement.

An idle portable M can defer a P claim while other portable Ms own P or have made cooperative progress within the existing 100µs retry interval. This avoids contending on every P return. Origin-bound READY work and dedicated affine Ms retain their wait positions; all successful claims still validate a fresh lease.

effective_quantum_ns[3] is indexed by llam_task_class_t and uses the same calculation as native capture requests. Zero means no active qualified native profile. This is a request budget: native delivery, PC eligibility and reader drain can delay actual preemption. It is not a hard scheduling deadline.

Creation and module mutation are host operations. Pass at least the two metadata fields of llam_module_opts_t, 16 bytes, and keep the caller's loader reference alive during llam_runtime_module_register_ex(); LLAM obtains its own reference. Mutation requires stopped dispatch and no retained G/native/catalog references. EBUSY preserves the module handle; success consumes it. Another runtime's handle returns EXDEV. A loader may retain an image for its own TLS lifetime even after LLAM has released its final independent reference.

The public API is an ABI 2.x minor extension. New structures retain fixed-width fields, compatible-prefix reads and writes, and defaults for omitted fields. See the ABI contract and platform support for the applicable contracts and gates.

↑↓ Navigate↵ Openesc CloseSearching the 3.0.1 docs