From 0ea984f3e8f435039dc8079e50bb3fea133ffce2 Mon Sep 17 00:00:00 2001 From: diviocity Date: Mon, 30 Nov 2026 04:25:03 +0100 Subject: [PATCH] Exercise LLVM 17 and 19 native code generation for Mesa --- tests/x11/README.md | 2 ++ tests/x11/llvm.v | 60 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 tests/x11/llvm.v diff --git a/tests/x11/README.md b/tests/x11/README.md index 8d4459a..3aea94c 100644 --- a/tests/x11/README.md +++ b/tests/x11/README.md @@ -36,3 +36,5 @@ The font test installed `mkfontscale` and both bitmap font packages by name. The The headless server test installed `xvfb`, `xauth`, `xset`, `xrdb` and BusyBox by name. The x86-64 root contained 36 PGP-validated packages, 5,738 owned file and link paths and 68 target ELFs. On the release kernel, `headless.sh` started the packaged Xvfb with TCP disabled, authenticated a client, drew a pixmap and checked the returned pixel. The client also wrote and read back an X resource. A new connection with an empty authority file correctly failed. The 384 MiB, one-vCPU guest transcript and ISO checksum are under `~/.local/state/vinix/ports/musl-busybox-CqkBEA/xorg/worker/guest-xvfb-03`. On the released AArch64 kernel, `modeline-font.sh` ran the source-built `cvt` and `bdftopcf` executables that require 16 KiB page alignment. It computed a 640×480 modeline and regenerated the release PCF cursor from its upstream BDF. The cursor matched SHA-256 `ad72bdd05b8baf48fd988dd08ee64a7b9ab4e04c3c72db3c4bc74ae01b7a6246`. The one-vCPU, 384 MiB guest used seven signed packages containing 559 owned paths and twelve target ELFs. Evidence is under `~/.local/state/vinix/ports/musl-busybox-CqkBEA/xorg/worker/arm16-guest-tools-02`. + +The LLVM fixture compiles a two-argument integer addition function from LLVM IR, obtains its native address and calls it with positive and negative inputs. Before using it on the candidate runtime, we ran it twice in the build-only Alpine environment, once with `libLLVM-17.so` and once with `libLLVM.so.19.1`. Both returned the expected sums. The per-version `DT_NEEDED` records and transcripts are under `~/.local/state/vinix/ports/musl-busybox-CqkBEA/xorg/worker/llvm-probe-fixture-02`. Runtime acceptance also requires repeating these executions with the source-built Vinix packages on each release kernel. diff --git a/tests/x11/llvm.v b/tests/x11/llvm.v new file mode 100644 index 0000000..abfc96a --- /dev/null +++ b/tests/x11/llvm.v @@ -0,0 +1,60 @@ +// Execute LLVM-generated machine code with the runtime used by software Gallium. +module main + +#include +#include +#include +#include + +fn C.LLVMContextCreate() voidptr +fn C.LLVMContextDispose(voidptr) +fn C.LLVMCreateMemoryBufferWithMemoryRangeCopy(&char, usize, &char) voidptr +fn C.LLVMParseIRInContext(voidptr, voidptr, voidptr, &&char) int +fn C.LLVMDisposeMessage(&char) +fn C.LLVMLinkInMCJIT() +fn C.LLVMInitializeNativeTarget() int +fn C.LLVMInitializeNativeAsmPrinter() int +fn C.LLVMCreateJITCompilerForModule(voidptr, voidptr, u32, &&char) int +fn C.LLVMDisposeExecutionEngine(voidptr) +fn C.LLVMGetFunctionAddress(voidptr, &char) u64 + +type Add = fn (i32, i32) i32 + +fn execute() ! { + C.LLVMLinkInMCJIT() + if C.LLVMInitializeNativeTarget() != 0 || C.LLVMInitializeNativeAsmPrinter() != 0 { + return error('LLVM did not initialize the native target and assembly printer') + } + context := C.LLVMContextCreate() + defer { C.LLVMContextDispose(context) } + ir := 'define i32 @add(i32 %a, i32 %b) { %sum = add i32 %a, %b ret i32 %sum }' + buffer := C.LLVMCreateMemoryBufferWithMemoryRangeCopy(ir.str, usize(ir.len), c'vinix-guest') + mut program := voidptr(0) + mut message := &char(unsafe { nil }) + if C.LLVMParseIRInContext(context, buffer, voidptr(&program), &message) != 0 { + defer { C.LLVMDisposeMessage(message) } + return error(unsafe { cstring_to_vstring(message) }) + } + mut engine := voidptr(0) + if C.LLVMCreateJITCompilerForModule(voidptr(&engine), program, 1, &message) != 0 { + defer { C.LLVMDisposeMessage(message) } + return error(unsafe { cstring_to_vstring(message) }) + } + defer { C.LLVMDisposeExecutionEngine(engine) } + address := C.LLVMGetFunctionAddress(engine, c'add') + if address == 0 { + return error('LLVM did not emit the add function') + } + add := unsafe { Add(usize(address)) } + if add(19, 23) != 42 || add(-5, 2) != -3 { + return error('generated code returned incorrect sums') + } + println('LLVM generated and executed native addition for both argument pairs.') +} + +fn main() { + execute() or { + eprintln(err) + exit(1) + } +} -- 2.56.1