GCC arguments are the command-line switches that decide how hard the compiler optimises, which CPU instructions it may emit and how the final binary is linked. Those choices trade compile time against the speed and code size of the binary you ship. Four groups of arguments matter most here: the -O level, target-architecture flags such as -march, link-time optimisation with -flto, and section garbage collection at link time. None of them reaches a WebAssembly build directly, because Emscripten compiles C and C++ to WebAssembly through a Clang frontend and the LLVM WebAssembly backend, not GCC. A carefully set native GCC build is still worth having before the Pyodide fit decision, because it is the baseline the WASM path will be judged against.
How GCC Arguments Work, and What They Mean in Practice
The gcc driver runs up to four stages in a fixed order (preprocessing, compilation proper, assembly and linking), and arguments apply to particular stages. A few arguments, LTO above all, have to appear at both compile time and the final link.
An -O level is a bundle of individual flags. With the exception of -O0, each level switches on a set of individual -f flags, and the GCC manual lists which flags each level activates. You do not have to trust a blog post on what your compiler version enables: gcc -c -Q -O2 --help=optimizers prints the state of every optimiser flag, and diffing that output against the -O3 output shows exactly what the higher level adds. The same output is sensitive to earlier options on the command line, so the diff reflects the full flag string you actually pass.
All of this has a cost. Turning on optimisation makes GCC try to improve speed or code size at the expense of compile time and, possibly, debuggability. For the day-to-day edit-compile-debug loop, the manual recommends -Og, which keeps the -O1 optimisations that do not get in the way of debugging. In practice, a flag string copied from a forum answer is a set of decisions someone else made for a different workload, and reading it that way is the whole skill. For a flag-by-flag reference aimed at small devices, our guide to GCC compiler flags for edge inference covers the embedded side; this page stays on the native-versus-WASM comparison.
What -O0 Through -O3, -Os and -Ofast Change in an Inference Binary
-O2 is the sensible default for a release inference build, -O3 is something you test rather than assume, -Os or -Oz are for when footprint is the constraint, and -Ofast changes floating-point semantics, so it is a numerical decision as much as a speed one.
| Level | What it turns on | Size tendency | Speed tendency | Watch for |
|---|---|---|---|---|
-O0 |
Most performance optimisations off; GCC’s default | Large, unoptimised | Slow | In our judgement, not a latency baseline |
-O1 / -O |
Reduces size and execution time without passes that take a great deal of compile time | Smaller than -O0 | Moderate gain | Recommended by the manual for large machine-generated code |
-O2 |
Nearly all supported optimisations that do not involve a space-speed trade-off | Kept in check | Good general default | Since GCC 12, loop vectorisation runs here with a very cheap cost model |
-O3 |
Everything in -O2 plus loop interchange, unroll-and-jam, loop splitting, loop distribution and the dynamic vectoriser cost model | Can grow | Likely faster, not guaranteed | Problems are almost always undefined behaviour in the code |
-Os |
-O2 minus alignment and other options that often increase code size, tuned for size | Smaller | Usually below -O2 | Useful where cache or storage is tight |
-Oz |
Size first, even if more instructions execute | Optimised aggressively for size | Can be slower | Trades instruction count for bytes |
-Ofast |
Optimisations that may violate IEEE or ANSI rules and assume no NaNs or infinities | Not stated | Can be faster | Incorrect output for code that depends on exact IEEE behaviour |
The Gentoo wiki notes that compiling with -O3 is likely, though not guaranteed, to improve performance, and that problems at -O3 today are almost always undefined behaviour in the code rather than compiler bugs. For an inference kernel, the practical reading is that an -O3 build that produces different outputs from the -O2 build is telling you something about the source, and the fix belongs there. Phoronix’s GCC 11.1 sweep tested twelve flag combinations, from -O0 to -Ofast -march=native with and without LTO, across 43 benchmarks. A public sweep like that shows how wide the option space is; only your own model’s kernels settle which point in it wins.
-Ofast deserves more care than its name suggests. Beyond the IEEE relaxations, -Ofast, -ffast-math and -funsafe-math-optimizations normally link in startup code that sets the flush-to-zero and denormals-are-zero bits in the x86 MXCSR register, except when building a shared library with -shared. Python extension modules are shared libraries. So the same -Ofast flag can give a standalone test executable different denormal behaviour from the extension module you actually load, unless you set -mdaz-ftz or -mno-daz-ftz explicitly. If you benchmark the executable and ship the module, you have measured something you do not deploy.
Which GCC Arguments Control Target Architecture and SIMD, and Why Do They Matter for Compute-Bound Inference?
-march decides which instructions GCC may emit; -mtune only schedules code for a processor without using instructions the default machine lacks. The generic x86-64 target guarantees only MMX, SSE, SSE2 and FXSR, and without -march or extension flags GCC emits nothing the default machine cannot run. Individual extension flags go further: one enables MMX, the SSE levels through SSE4.2, AVX and FMA code generation together.
-march=native enables every instruction subset the build machine supports, so the result might not run on a different machine. The Gentoo wiki puts it bluntly: do not use it when the binary will run on another CPU. For an inference service built in CI and deployed to a fleet, that means naming the deployment microarchitecture explicitly, or compiling per-architecture variants with runtime dispatch; GCC’s documentation says applications doing run-time CPU detection must compile separate files per architecture, with the detection code itself built without those options.
This matters for compute-bound inference because of vector width. In Wang et al.’s browser study, compute-intensive kernels such as Einsum and FusedConv2D accounted for most latency, while SIMD had almost no effect on memory-intensive ones; see reading CPU GFLOPS for a given target. When compute-bound kernels move off the CPU entirely, our GPU performance engineering hub covers tuning accelerator inference.
Why Vectorisation May Not Fire Even with the Right -march
Setting -march makes wide instructions available; it does not make the vectoriser use them. GCC’s auto-vectoriser targets innermost countable loops whose operations work on same-sized types with consecutive memory accesses. Its documentation lists further conditions: a single basic block with no if-then-else, a loop count known before the loop starts, and aligned, stride-one array accesses. Vectorising has overhead (moving data in and out of vector registers, aligning accesses), so a cost model decides per loop whether it pays.
Version matters too. The Gentoo wiki reports that loop vectorisation was previously not enabled at -O2, and that GCC 12 enabled it there with the very-cheap cost model. We would trust the version-specific statement and confirm with -Q --help=optimizers on the compiler you use. The Gentoo wiki notes that -march=native at -O2 or lower on an AVX machine is likely to emit AVX instructions on the shorter SSE XMM registers rather than full YMM width. The project page documents a report flag, -ftree-vectorizer-verbose=n, that tells you which loops were vectorised and why others were not. Reading that report for your hot kernels answers more than any flag string does.
How Do GCC Linker and Link-Time Optimisation (-Flto) Arguments Affect Final Binary Footprint and Startup Cost?
LTO lets GCC optimise across translation units: object files carry GCC’s GIMPLE representation, and at link time all function bodies are treated as if they came from one unit, so the inliner can work across files. The rule that trips people up is that -flto and the optimisation options must be given at compile time and at the final link, ideally identical for every file in the link. Giving an -O level only at link time is generally ineffective, because unoptimised compilation skips passes that gather information LTO needs. Target -m flags are taken from the first object file that specifies them, so mixing -march settings across objects in one LTO link is a quiet way to lose the SIMD you thought you had.
Two more arguments shape the result. When the linker plugin is not available, -fwhole-program tells GCC no symbols need exporting, which allows more aggressive decisions. The plugin needs gold or GNU ld 2.21 or newer, and it is on by default when GCC is configured for such a linker. For libraries consumed by non-LTO builds, fat LTO objects carry both the intermediate representation and machine code.
LLVM data quoted by convolv.es shows basic LTO taking about 5x the non-LTO build time on the Clang 3.9 codebase for an average 2.86% runtime improvement, while parallel LTO reached 2.63% at 1.2x build time. Those are LLVM figures on a compiler codebase, not GCC on an inference kernel, so treat them as a sense of scale only. Memfault adds that LTO’s more aggressive inlining can raise stack usage.
Compiling with -ffunction-sections puts each function in its own linker section, and linking with --gc-sections removes sections nothing calls; -fdata-sections does the same for data. LTO also tends to produce smaller binaries, though not unconditionally: in one set of ARM embedded size benchmarks, -flto made a huge difference but produced a larger binary at -O0. That test targeted flash-limited microcontrollers, so the direction carries over to a desktop or server build more reliably than the magnitudes do.
On startup cost, our judgement is that a smaller binary has less to load, but time to first inference should be measured directly rather than inferred from binary size.
How GCC Argument Choices in a Native C++ Build Compare to What You Can and Cannot Control on a Pyodide/WASM Path
On the WASM path, you trade GCC’s native controls for Emscripten’s own options. A study of cross-compiling legacy C and C++ to WebAssembly found that native-specific flags such as -march and -Ofast are not available in Emscripten.
| Control | Native GCC build | Emscripten / Pyodide build |
|---|---|---|
| Optimisation level | -O0 to -Ofast |
emcc -O levels, similar in meaning but with WebAssembly-specific passes; no -Ofast |
| Target instruction set | -march, -mtune |
No -march; -msimd128 enables WebAssembly SIMD and LLVM autovectorisation |
| Vector width | Up to 256-bit AVX2 or 512-bit AVX-512 where the CPU has them | 128-bit; most 256-bit AVX and AVX2 operations are emulated as two 128-bit ones |
| Fused multiply-add | -mfma |
With -mrelaxed-simd, relaxed SIMD FMA; with -msimd128 only, separate multiply and add |
| Link-time optimisation | -flto, --gc-sections |
Binaryen runs at link time with general and whole-program optimisations |
| C++ exception catching | On by default | Off by default at -O1 and above |
| Default stack | 8MB on Linux, 1MB on Windows | 64KB, enlarged only at compile time with STACK_SIZE |
| Heap growth | Managed by the OS | Restricted unless built with ALLOW_MEMORY_GROWTH |
| Where flags enter | CFLAGS, CXXFLAGS, LDFLAGS |
Pyodide package builds read the same CFLAGS, CXXFLAGS and LDFLAGS variables |
One row needs a note on sources. The 2024 in-browser inference study by Wang et al. reports FMA as unavailable in Wasm, while Emscripten’s current documentation describes FMA through relaxed SIMD. We treat Emscripten’s documentation as the stronger source for what the toolchain offers today.
Programs that put a lot of data on the stack hit out-of-bounds memory errors under Emscripten defaults, and the size cannot be raised at runtime. Defaults also change behaviour: in that cross-compilation study, 4,687 of 4,691 tests passed as x86-64 binaries but only 4,316 passed in WebAssembly, with many failures resolvable by changing compiler settings.
Tuning that side is its own job, and the sweep of Emscripten and Pyodide flags for inference latency covers it. What matters here is the size of the gap a native build sets. Jangda et al. measured SPEC CPU code compiled to WebAssembly running on average 1.55x slower than native in Chrome and 1.45x slower in Firefox, with more loads, stores and branches than native code. In our judgement, a sloppy native build shrinks that apparent gap and makes the WASM path look better than it is.
Which GCC Arguments Give a Defensible Profiling Baseline for a Port-or-Not Decision?
A defensible baseline is a native build whose flags are fixed, recorded and chosen for the deployment target, measured on the axes the port decision cares about: binary size, time to first inference and steady-state per-inference latency. We hold that profiling, not best-practice lists, is what tells you which optimisations matter for a given workload, and the flag set is part of what you profile. This is the procedure we would follow:
- Fix one flag string for the baseline and record the effective optimiser set with
gcc -Q -O2 --help=optimizers, so anyone can reproduce it on the same compiler. - Start at
-O2with an explicit-marchfor the machine you deploy on; use-march=nativeonly if build and run hosts are the same. - Add
-O3, then-flto(at compile and link), then-ffunction-sections -fdata-sectionswith--gc-sections, one change at a time, re-measuring size, cold start and latency after each. - Keep frame pointers in profiling builds with
-fno-omit-frame-pointer, since they help when profiling a codebase. - Compare model outputs against a reference before keeping
-Ofast, because it can give incorrect results for code relying on exact IEEE behaviour. - Treat profile-guided optimisation as optional: build with
-fprofile-generate, run a representative workload, rebuild with-fprofile-use, and redo the profile whenever the code changes. - Build the WASM version from the same source with an Emscripten profiling build (
emcc -O2 --profiling).
A PGO build reflects whatever inputs you profiled with; Gentoo notes that the steps must be repeated on every update, and Android’s PGO guidance has Clang warn when a profile goes stale. If the comparison WASM build has no equivalent, report the PGO gain separately rather than folding it into the native number. For how these numbers feed the final call, see our analysis of when porting Python inference to C++ or WASM actually pays off.
The question the baseline leaves open is how far the WASM build can close the gap with its own flags. Wang et al. measured SIMD and multithreading together cutting in-browser prediction latency by 64.7% for TF.js and 63.4% for ORT.js, yet with both enabled the two frameworks still averaged a 16.9x CPU latency gap to native TF and ORT. That remaining distance, measured against a native build you can defend flag by flag, is the number the port decision should rest on.
FAQ
Is GCC for C or C++?
Both. GCC is a compiler collection that compiles C, C++ and other languages; gcc and g++ are drivers for the same toolchain.
What Does GCC Stand For?
GCC stands for the GNU Compiler Collection.
What Are the Stages of the Compilation Process?
GCC’s manual describes up to four stages, always in this order: preprocessing, compilation proper, assembly and linking. Arguments such as -E, -S and -c stop the process after a given stage.