# Compilation Modes and Optimization

`vex run` compiles and executes a program; `vex compile` produces a persistent
artifact when code generation and linking succeed. The options below describe
the CLI inspected for the 0.4.0 documentation. Check `vex --version`,
`vex compile --help` and `vex run --help` when using an older release or a
different candidate; their option sets can differ.

## Compilation Modes

```bash
vex run main.vx
vex compile main.vx
```

`run` executes a temporary native executable as a subprocess. `compile` emits a
native artifact; `-o <path>` selects its output location. A successful `lint`
check alone does not exercise native code generation, linking or execution.

## Optimization Levels

Both commands accept numeric levels `0` through `3` with `-O` or `--opt-level`.
In the current CLI, **compile defaults to O2 and run defaults to O1**. `--fast`
is a compile option for a no-optimization, no-LTO fast path; it is not a
universal performance guarantee.

```bash
vex compile -O 0 main.vx
vex compile -O 3 main.vx
vex run -O 2 main.vx
```

Higher levels can increase compilation time and change optimization choices;
measure the actual workload and target. Do not assume a particular LLVM pass,
instruction, speedup or binary size from a level alone. The current numeric CLI
schema does not define `-Oz` or `-Os` as optimization levels.

## Emit Flags

```bash
vex compile --emit-llvm main.vx
vex compile --emit-asm main.vx
```

These modes emit `.ll` and `.s` respectively and return without linking a
native executable. They are mutually exclusive. `vex run --emit-llvm main.vx`
is a separate run-time debugging option. The current compile CLI does not
provide `--emit-obj`, `--emit-llvm-ir` or `--emit-all` options; consult its
help for the available surface.

## LTO (Link-Time Optimization)

The development compile CLI exposes `--lto` and `--thin-lto`. The compiler's
link path checks LLVM/Clang compatibility before using bitcode. On the checked
path, ThinLTO emission can also be selected at O2 or higher when the compatible
Clang is not Apple Clang. Availability and final behavior depend on the linker,
compiler, target and other flags; requesting a flag does not by itself prove
that a given artifact used LTO. Inspect diagnostics and validate the artifact
on the intended environment.

```bash
vex compile --lto main.vx
vex compile --thin-lto main.vx
```

The current CLI exposes these as switches; `--lto=off` is not its documented
off switch. Benchmark different modes for your application rather than assuming
a fixed relative speed or optimization quality.

## Build Profiles

The development `vex compile` command does not currently expose the
`--profile` or `--release` options shown in some older examples. Set an
explicit numeric optimization level and other supported options instead. TOML
profile blocks are not a demonstrated configuration for this CLI. Check the
[package-manager reference](../../references/vex-pm-reference.md) for the manifest
and commands supported by the selected version.

## Incremental and Parallel Compilation

Caching and parallel work exist in parts of the compiler and toolchain, but
this page does not promise a general incremental or parallel-build contract.
The available modes and their invalidation or resource behavior require
separate qualification for the compiler revision and workload.

## Cross-Compilation

`vex compile --target <triple> main.vx` selects a target triple. Parsing a
triple or emitting code is not proof that the corresponding runtime support,
linker, system libraries and native execution are available. Consult the
[platform support matrix](../platform-support.md), then build and run a
representative artifact on the exact target before relying on it.

## Debug Symbols (DWARF)

The CLI accepts `-g` / `--debug-info` for additional debug information. The
current native linking path also preserves line-table information for crash
symbolication, but debugger features and available symbols must be checked on
the actual target and build.

## Sanitizer Integration

The compile CLI accepts `--sanitize` values including `address`, `memory`,
`thread` and `undefined`; the linker path passes the corresponding sanitizer
flag through a Clang driver. Toolchain and target support differ, and passing a
linker flag alone does not establish complete instrumentation or detection
coverage. Qualify each sanitizer with a known failing test on the intended
compiler and target before using it as a CI safety gate.

## Best Practices

1. Start with `vex lint`, then run or compile the example on the intended host.
2. Record the compiler revision, optimization level, target and toolchain.
3. Use `--help` from that binary for its supported flags; avoid copying
   release-profile, sanitizer or cross-target recipes from a different revision.
4. Measure performance and validate debugging and sanitizer behavior separately.
