Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Linking an object file into an executable

vxc -c hello.vx -o hello.o writes an object file, not an executable. To get a program that you can run, link the object file with the libraries that it calls. vxc --action emit-exe does this link step for you. This page shows how to do it yourself.

What -c gives you

-c is the short form of --action emit-obj. It compiles your program through MLIR to a native object file, and then it stops. An object file contains the functions that your code defines. It also lists the functions that your code calls. The linker must find each called function that the object file does not define.

A compiled Vx program calls functions from three kinds of library:

FunctionsLibrary
vx_init_signals, vx_print_str and the other functions of the Vx runtimelibvx_std_core, the Vx runtime library
printMemrefF32 and the other functions that print a tensorlibmlir_runner_utils and libmlir_c_runner_utils, the MLIR runner libraries from LLVM 22
vx_plugin_dispatch_async and the other vx_plugin_ functionsthe dispatch library, only for a program with spawn on

First, set two variables to the folders that contain these libraries:

  • VX_LIB is the folder of the Vx runtime library. For an installed toolchain, use $HOME/.vx/current/lib. In a source checkout, run cargo build, then use the absolute path of target/debug (or of target/release after cargo build --release).
  • LLVM_LIB is the lib folder of LLVM 22. A toolchain that carries its own LLVM keeps it in $HOME/.vx/current/llvm/lib. Otherwise, use the output of llvm-config --libdir. In a source checkout, source config.local puts the correct llvm-config on PATH.

Use absolute paths. The linker writes the two folders into the executable, and the program loads the libraries from them when it starts.

Then compile and link:

vxc -c hello.vx -O2 -o hello.o
cc hello.o -o hello \
    -L"$VX_LIB" -lvx_std_core \
    -L"$LLVM_LIB" -lmlir_runner_utils -lmlir_c_runner_utils \
    -Wl,-rpath,"$VX_LIB" -Wl,-rpath,"$LLVM_LIB" \
    -lm
./hello

Each -l flag names a library without its file extension. So the same line works on macOS, where the files end in .dylib, and on Linux, where they end in .so.

Two errors show that the line is not complete:

  1. The link fails with an undefined symbol, such as vx_print_str or printMemrefF32. The line does not name the library of that symbol. The table above gives the library. This is not a compiler bug.
  2. The link passes, but the program stops when it starts. macOS says “Library not loaded”, and Linux says “cannot open shared object file”. The line does not give an -rpath for the folder of that library.

A program that does not print a tensor does not call the MLIR runner libraries. For such a program, you can leave out the LLVM_LIB flags and the two MLIR libraries.

A program with spawn on

A program with spawn on also calls the dispatch library, and the dispatch library calls libffi. Add both to the link line:

  • An installed toolchain keeps the dispatch library in $VX_LIB. It is the file whose name ends in dispatch.dylib or dispatch.so. On macOS, add -lvx_npu_dispatch -lffi.
  • In a source checkout, the build writes the dispatch library into a folder target/debug/build/vxc-*/out/. Give the full path of the library, and add an -rpath for its folder.

On Linux, also add -rdynamic. The dispatch library finds each kernel by its name in the running program. Linux makes those names visible only when the link asks for it. The gpu-norms build script in the Vx-starters repository links a GPU program on Linux.

A shared library

To call Vx from C, or from Python’s ctypes, link the object file into a shared library. Unsafe and FFI says which functions the shared library exports.

vxc add_one.vx --action emit-obj -o add_one.o
cc -shared -o libadd_one.so add_one.o -L"$VX_LIB" -lvx_std_core -Wl,-rpath,"$VX_LIB"

On macOS, name the file libadd_one.dylib.

Code that calls the Vx runtime needs the Vx runtime library in the shared library. A main function calls the runtime, and so does print. Without the Vx runtime library, the link fails on macOS. On Linux, the link passes, but Python cannot load the shared library: undefined symbol: vx_init_signals. Code that calls no function from the table above needs no library, and cc -shared -o libadd_one.so add_one.o is enough.

Let the compiler do it

vxc --run hello.vx compiles your program, links it in a temporary folder, and runs it. vxc --action emit-exe hello.vx -o hello does the same steps, and it keeps the executable:

MLIR --mlir-translate--> LLVM IR --opt--> LLVM IR --llc--> object file --clang--> executable

--run and emit-exe continue to the executable. -c stops at the object file. The benchmark harness in benchmarks/stdlib links an object file with the same libraries as --run. See build_vx in src/bin/cargo-vx-bench.rs.