What Is a GCC Flag? How Compiler Flags Work in AI Workload Builds

What does flag do in GCC? Which flag classes change machine code and AI build throughput, which only warn, and how to verify a flag's effect.

What Is a GCC Flag? How Compiler Flags Work in AI Workload Builds
Written by TechnoLynx Published on 11 Jul 2026

A GCC flag is a command-line option that changes what the compiler does at one of its stages: preprocessing, compilation, assembly or linking. Flags fall into two kinds. Some change the machine code GCC emits: optimisation levels, architecture targeting, floating-point relaxation, hardening and instrumentation. Others, such as the -W warning family, only change what the compiler tells you about your source. Only the first kind can change the throughput of an AI build. If you are already past the definition and want to know which GCC flags matter when porting AI workloads, that page has the porting decision. This one explains how flags work and how to sort a flag string into the parts worth measuring and the parts that cannot matter.

What a Flag Does in GCC, Mechanically

Many GCC options have long names starting with -f or -W, and most of these come in a positive and a negative form, so -ffoo is switched off by -fno-foo. The prefix tells you something useful. Options of the form -f are machine-independent, while -m options belong to a particular target machine and control things like the processor variant, the ABI or optimisations specific to that machine.

An optimisation flag is basically a switch on one program transformation, either on or off. There are a lot of these GCC optimisation switches: one compiler auto-tuning study counts 106 optimisations in GCC 8.3.0, which makes an optimisation space of 2^106 combinations. The same study notes that turning on an optimisation does not always improve the compiled program, and that one flag can affect different programs in different ways.

The -O levels are bundles of these switches. -O1 turns on a fixed list that includes dead code elimination, dead store elimination and -fomit-frame-pointer. -O2 adds another long list, including global common subexpression elimination and strict aliasing. So writing “-O2” really means writing several dozen individual flags. That is why two builds that look identical at the level of -O can still differ when one of them adds or removes a single -f option.

Flag Classes, and Which Ones Change the Binary

The table below sorts flags by what they do to the output. Use it to triage a flag string you have inherited from a build script.

Class Examples Changes emitted code? What to expect
Optimisation level and individual passes -O1, -O2, -O3, -Os, -funroll-loops, -ftree-vectorize Yes Higher levels do more transformation at the cost of compile time, in the hope of lower execution time
Architecture targeting -march, -mtune, -mcpu, -mavx2 -march changes the instructions allowed; -mtune changes scheduling only -march output may not run on other processors
Floating-point relaxation -ffast-math, -Ofast Yes Faster code for some programs; incorrect output for programs needing exact IEEE or ISO maths
Diagnostics -Wall, -Wformat, -Werror No Warnings or errors only, with no runtime trade-off
Hardening -D_FORTIFY_SOURCE=2, -fstack-protector-strong, PIE Yes Overhead depends on context and is usually small; testing is needed
Instrumentation -pg, -fprofile-generate, -fsanitize=address Yes Adds profiling or checking code; sanitizers average a 2× to 4× slowdown
Profile-guided and link-time -fprofile-use, -flto Yes Code is reordered, inlined or merged across modules using profile data or the whole program
Driver and build stage -c, -E, -o No Decide which stages run and where output goes

Some cases sit at the edges. On AArch64, a few extension modifiers (the Statistical Profiling extension and the Speculation Barrier, for example) are documented as enabling the extension only at the assembler level and do not affect code generation. Some ABI-affecting flags such as -fshort-enums and -fpack-struct change the binary in ways that make it incompatible with code built without them.

Why -Wall Has No Effect on Runtime Performance

Diagnostic flags leave the generated binary untouched. The OpenSSF hardening guide says plainly that the compile-time checks enabled by warning options do not affect the binary code and so carry no performance or runtime trade-off; they only issue warnings, or errors under -Werror. -Wall is itself a group of individual warning flags.

People get confused here because some warnings only fire when certain optimisations are active. -Wstrict-overflow, for instance, warns about cases where the compiler optimises on the assumption that signed overflow does not happen, so what it reports depends on the optimisation level. The dependency only goes one way: the warning reports what the optimiser did and does not change the binary. If adding -Wall to a build seems to change measured throughput, the cause is somewhere else, such as run-to-run noise or measurement bias. The measurement section below covers both.

How -march Changes Which Instructions the Compiler Emits

-march=cpu-type lets GCC generate instructions for that machine type, including code that may not run at all on other processors. -mtune=cpu-type only tunes the generated code for that processor. Under -mtune, GCC schedules for the named chip but never emits code that cannot run on the default machine type unless -march is also given. GCC documents that there is no -march=generic, because -march names an instruction set and no instruction set is generic to all processors.

On AArch64 the split works differently. -mcpu sets both the allowed instructions (as -march would) and the tuning target (as -mtune would). Arm’s SVE adds one more choice: GCC can emit vector-length-agnostic code that works with any vector register size, or vector-length-specific code that assumes a size for optimisation.

The -march=native Trap in Build Pipelines

-march=native enables every instruction subset the compiling machine supports, so the result might not run on a different machine. In CI, this makes the binary’s instruction set depend on whichever runner built it. A container image built with -march=native on a newer host might not run at all on an older fleet.

When one artefact has to serve a mixed fleet, there are alternatives to a single -march:

  • Function multiversioning. GCC compiles several versions of a function, each for a different set of architecture extensions, and generates a resolver that picks the right one at runtime. On x86 you declare them with the target or target_clones attributes.
  • Per-ISA builds with runtime dispatch. Intel Extension for PyTorch copies kernel source files into one folder per ISA level, builds each with its own flags, and chooses the fastest available kernel at runtime based on what cpuinfo reports. Its AVX512 variant, for example, is built with -O3 -mavx512f -mavx512bw -mavx512vl -mavx512dq -mfma.

The IPEX documentation also lists minimum GCC versions per ISA level: AVX512_BF16 needs GCC 10.3 or later, and AMX needs GCC 11.2 or later. GCC’s own x86 documentation adds a rule that applies to any hand-rolled dispatch: the file that contains the CPU detection code must be compiled without the extended-instruction flags.

-ffast-math and -Ofast Trade Precision for Speed

-ffast-math turns on a set of options including -funsafe-math-optimizations and -ffinite-math-only. No -O level enables it except -Ofast, because it can produce incorrect output for programs that depend on exact IEEE or ISO maths rules. -Ofast turns on all of -O3, plus -ffast-math and other optimisations that are not valid for every standard-compliant program. So a build script that moves from -O3 to -Ofast has changed numerical semantics, not just how hard the compiler optimises.

A study of numerical differences between NVIDIA and AMD GPUs counted, in its FP32 tests, 13,877 discrepancies under -O3 with fast-math against 45 at -O0. The authors note that single precision is prevalent in machine learning applications running on 32-bit systems. The same study reports that -O3 -use_fast_math with nvcc gave a 100% speedup over -O0, but with 39% larger error. That is a CUDA compiler, not GCC, but GCC documents the same speed-for-accuracy trade-off for -ffast-math. Fast-math also behaves differently across toolchains: the authors found it caused problems with NaNs and infinities under HIP that it did not cause under CUDA.

A fast-math build should not pass on throughput alone. It needs an accuracy check on the model’s actual outputs, compared against a strict-maths reference build.

Which Flag Classes Are Worth Profiling When Porting an AI Workload

Profile the classes that change emitted code and that interact with the new target: architecture targeting, the optimisation level and any passes added to it, floating-point relaxation, profile-guided and link-time optimisation, and OpenMP offload flags. Diagnostics and driver options can be reviewed for correctness and then left out of performance work.

Measurements from CPU benchmarks, kernel builds, HPC codes and GPU offload kernels show gains depend on the target, and sometimes reverse:

  • Flag selection beyond -O3. A tuning study built custom optimisation levels that ran 9% faster than -O3 on an Arm Cortex-M3, 15% faster on a Cortex-A8 and 3% faster on an 8-bit AVR. Across these three embedded cores, potential gains grew with hardware complexity, and the AVR flag set overlapped less with the two Arm sets.
  • -O3 against -O2. With LLVM’s optimisation levels, the authors found the effect of -O3 over -O2 indistinguishable from random noise. In a bandwidth study, moving from -O2 to -O3 sharply reduced measured performance. A kernel built with GCC 11 using -O3 -march=native came out slightly slower on the geometric mean of its benchmarks, mostly because of I/O regressions.
  • Microarchitecture levels. Canonical’s early x86-64-v3 benchmarking showed up to a 60% improvement on a Glibc Log2 benchmark, but also outliers that regressed.
  • Profile-guided optimisation. A 2025 PGO survey cites earlier work putting speedups on real applications often at 5% to 30%, yet in its own runs GROMACS slowed down under FDO on an ARM platform, and sampling-based AutoFDO regressed on the memory-bound mcf benchmark.
  • Offload flags on GPUs. An OpenMP target offload study saw a 10× gain in ATLAS FastCaloSim from Clang’s -fopenmp-cuda-mode on NVIDIA GPUs that did not appear on AMD GPUs. On an A6000, using no extra flags was the fastest option for every data type tested on the zaxpy kernel. Performance on one kernel also got worse from Clang 15 to Clang 16.

One boundary is worth stating plainly. GPU kernels compiled with nvcc, hipcc or Clang have their own optimisation flags. Those are covered under tuning nvcc, Clang and SYCL builds. For edge targets, see what each GCC flag does in an edge inference build.

How to Tell Whether a Flag Actually Changed Measured Throughput

These steps run in order.

  1. Confirm the flag changed the code at all. Diff the enabled optimisations between two builds with gcc -Q --help=optimizers plus each build’s flags. -fopt-info reports which optimisation passes, such as vectorisation or inlining, were actually applied. -S stops after compilation and writes assembly you can diff. If neither the enabled passes nor the assembly changed, the flag cannot explain a throughput change.
  2. Pin and record the toolchain. State the compiler, its version and the full flag set next to every number. A study of MPI benchmarking found that rebuilding the same library at -O1, -O2 or -O3 changed results, and concluded that results without the compiler and flags stated are not comparable. LTO bytecode cannot even be shared across GCC versions.
  3. Control for layout bias. Changing a seemingly harmless part of the setup can make a researcher draw the wrong conclusion. Mytkowicz et al. (ASPLOS 2009) found this measurement bias on every architecture, compiler and SPEC CPU2006 C program they tried. Because optimisations also change code layout, a single binary is only one sample of the possible layouts.
  4. Repeat and test statistically. Run enough iterations to estimate variance, and treat a difference inside the noise as no difference.
  5. Measure on the deployment target. Flag impact depends on the platform as well as the program. For subsystem-level measurement, see the HPCC benchmark.
  6. Check outputs, not only speed. For any floating-point flag, compare model outputs against a strict-maths reference before accepting the gain.

The Flags That Deserve Review Time

The useful outcome of all this is a smaller review list. Flag strings mix warnings, build-stage options and code-changing flags on one command line. Only the last group needs re-measuring when the target changes. Within it, architecture targeting and floating-point relaxation can break a build instead of just slowing it down. What stays uncertain for any given port is the direction of the effect: the sources show the same flag helping on one target and hurting on another. A porting plan should therefore schedule the measurement and not assume the result. For the per-class recommendations, see which GCC flags matter when porting AI workloads. For what a full performance and porting assessment covers before a migration, see that page.

FAQ

What Is the -c Flag in GCC?

-c tells GCC not to run the linker, so the output is object files produced by the assembler.

What Is the Purpose of the -g and -O Flags in GCC?

-O and its numbered levels turn on bundles of optimisation passes that change the generated code. -g asks the compiler to include debugging information in the output. Some optimisation passes can make debugging impossible because variables no longer stay in a fixed register.

How Does GCC Work?

Compilation can involve up to four stages, always in this order: preprocessing, compilation proper, assembly and linking. Linking combines the object files into an executable.