Definition
A compiler flag is an option on the command line of a compiler, the tool that translates source code into machine instructions. A flag changes how the same source code is translated: it can select the target processor, change which language rules apply, turn optimizations (transformations that make the program faster or smaller) on or off, add checks that run with the program, add information for debugging, or control warnings. The resulting program can differ in size, speed, memory use and sometimes behavior. Manuals also call flags options or switches.
The idea
One source file has many correct translations into machine instructions: a short one, a fast one, one that is easy to follow in a debugger (a tool for stopping and inspecting a running program), one that checks itself while it runs. The compiler cannot guess which one is wanted, so flags tell it.
The consequence is that a measurement or a test result does not belong to the source code alone. It belongs to the source code together with its flags. In the worked example, one function is 13 instructions long in one build and 3 instructions long in another build of the identical source. A flag can also reveal a bug in the source, either by adding checks, or by producing a different but permitted result from code that breaks a language rule.
How it works
Flags fall into kinds by what they change. The examples are flags of the widely used compiler gcc.
Target flags, such as
-mcpu=cortex-m4: which processor’s instructions may be used.Language flags, such as
-std=c11or-fwrapv: which language rules the compiler applies.-fwrapvmakes addition, subtraction and multiplication of signed integers, the whole-number types that can be negative, wrap around when a result is too large, a case that standard C leaves undefined. Division is not covered: the true quotient of the most negative value of the C typeintdivided by is one larger than the largestint, and that case stays undefined. When such a division runs on an x86 processor, it stops the program.Optimization flags. A compiler optimization §3.39 is a transformation that makes the program faster or smaller without changing what it is supposed to compute.
-O0, the default, asks for a direct translation that is easy to debug.-O1,-O2and-O3turn on successively longer lists of optimizations, and-Osoptimizes for small size.Instrumentation §14.24 flags: the compiler inserts extra instructions that observe or check the program while it runs. A tool built from such checks is called a sanitizer §10.21.
-fsanitize=addressturns on AddressSanitizer §10.22, which detects memory errors, and-fsanitize=undefinedturns on UndefinedBehaviorSanitizer §10.23.Debug flags, such as
-g: the compiler adds a description of source lines and variables for a debugger.Warning flags, such as
-Wall: which suspicious constructs the compiler reports.
A flag acts on one run of the compiler, and one run translates one source file together with the header files, files of shared declarations, that it includes.
The build system, the tool that runs the compiler for a project, puts together a command line for each source file, from settings for the whole project, for a directory and for single files. A flag affects only the files whose command line contains it.
The compiler reads the flags and translates the file under them. Where several optimization levels appear, the last one is the one in effect:
-O2 -O0means-O0.Some flags must be given again when the program is linked. A sanitizer flag, for example, makes the linker §12.4 add the sanitizer’s runtime library, the code that prints the error reports.
Worked example
The function below returns 1 when x + 1 is greater than x, and 0 otherwise.
int grows(int x) {
int next = x + 1;
return next > x;
}
The type int is 32 bits wide here, and its largest value is 2,147,483,647. A test program reads the two numbers 41 and 2147483647 from its command line, so that the compiler cannot work out the results in advance. It calls grows on each number and prints the two results. It is built four times with version 13.3 of gcc for 64-bit x86 Linux:
| flags | prints | instructions in grows |
|---|---|---|
-O0 |
1 and 0 | 13 |
-O2 |
1 and 1 | 3 |
-O2 -fwrapv |
1 and 0 | 5 |
-O2 -fsanitize=signed-integer-overflow |
a report, then 1 and 0 | 20 |
With -O0 the processor adds 1 to 2,147,483,647. The mathematical result, 2,147,483,648, does not fit in a signed 32-bit number, which is called overflow. The processor keeps the low 32 bits, and as a signed number they stand for . That number is not greater than x, so the function returns 0.
With -O2 the compiler reasons that x + 1 is greater than x whenever the addition does not overflow. Standard C does not define what happens when a signed integer overflows §4.15, so the compiler may assume that it never happens. It replaces the body of the function by “return 1”. With -fwrapv the overflow is defined, that reasoning is no longer allowed, and the function compares x with 2,147,483,647. With the sanitizer flag the compiler adds a check after the addition and a call to a reporting routine. The run prints signed integer overflow: 2147483647 + 1 cannot be represented in type 'int' and then continues.
The result means this. For the input 41, whose computation follows every language rule, all four builds print 1. The builds differ only for the input that breaks a rule. The optimization flag did not create the bug. It changed how the bug shows, and the sanitizer flag made the bug visible.
Stated precisely
The contract for an optimization flag comes from the language standard. The C standard (2011 edition, section 5.1.2.3) defines the observable behavior of a program as three things:
accesses to objects declared volatile §4.34, a C keyword for memory that hardware or other code may read or change;
the data that the program has written to files by the time it ends;
the interplay of input and output on interactive devices, for example that a prompt appears before the program waits for the reply.
A compiler may translate a program in any way that keeps this behavior as the source code specifies. Instruction count and order, the existence of a function as a separate block of code, execution time §9.32, stack use (the memory that function calls take for their local data) and code size are not observable behavior, and an optimization flag may change all of them.
This contract covers only executions that follow the language rules. An execution has undefined behavior §4.12 when it performs an operation for which the standard sets no requirement, such as overflow of a signed integer. For such an execution the standard allows any result, and builds with different flags may differ.
The contract for a sanitizer flag is narrower. The added checks run only when the instrumented code runs, and they report the kinds of error that the sanitizer was built to detect. A run without a report says nothing about inputs that were not tried, about code compiled without the flag, or about other kinds of error.
Limits and common mistakes
A flag in a build script does not prove that the flag reached every file. A setting for a single file can replace it, a later flag can cancel it, and libraries that arrive already compiled were built with their own flags. The evidence is the complete command line of each compiler run.
An optimization flag permits transformations. It does not promise any particular one. Whether the compiler copies the body of a function into its callers, which is called inlining, is decided by rules of thumb inside the compiler.
-O0 does not mean that nothing is simplified. Version 13.3 of gcc compiles the one-line form return x + 1 > x; to “return 1” even at -O0.
A measurement is valid for the flags it was made with. Code size, timing and stack use taken from a build made for debugging or from a sanitizer build do not describe the release build, the one that is delivered to users. The documentation of AddressSanitizer gives its typical slowdown as a factor of 2.
Flags are not portable. Every compiler defines its own, and a flag of one compiler may be unknown to another.
A sanitizer finds errors only on the paths that a run executes. It is a testing tool, not a proof that the program is free of errors.
Questions answered
What does an optimization or sanitizer flag change in the produced program?
An optimization flag changes how the program computes, not what a correct program computes. It may change the number and order of machine instructions, remove computations whose results are not used, and copy small functions into their callers so that they no longer exist as separate blocks of code. Code size, speed, stack use (the memory that function calls take) and what a debugger, a tool for inspecting the running program, can show change with it. The flag must leave the program’s observable behavior unchanged, meaning its input, its output and its accesses to memory that the source marks as volatile, which is memory that hardware or other code may read or change. That duty holds only for executions that follow the language rules. Where the source has undefined behavior, meaning an operation for which the language sets no requirement, such as overflow of a signed integer in C, an optimization flag can change the visible result.
A sanitizer flag adds code. A sanitizer is a set of checks that the compiler inserts around the operations it covers, together with a runtime library that prints a report when a check fails. The program becomes larger and slower. It also reacts differently to an error: it prints a report where the plain program would have continued without any sign of the error. By default AddressSanitizer, which checks memory accesses, then stops the program, and UndefinedBehaviorSanitizer, which checks for operations that the language leaves undefined, lets it continue. Options change either default. Runs without errors compute the same results as before.
In both cases the change applies only to the files that were compiled with the flag.
Related entries: Link-time optimization (LTO) (§12.10), Dead-code elimination (§12.11), Sanitizer (§10.21), Undefined behavior (UB) (§4.12), Compiler optimization (§3.39), UndefinedBehaviorSanitizer (§10.23), Signed integer overflow (§4.15). Further reading: [25, 26, 78].