r/Compilers • u/BrainThinkerMan • 11d ago
Compilers add or remove perfomace?
isnt it that compilers change the code, as in not only transferring it into machine language, but before that changing the structure of the code. so how would one attempt to make performant code if the compiler changes sed code that i tried to make performant?
3
u/serge-mv 11d ago
Compiler's main job is to preserve the semantics of what you've written during the translation process. They do not change the fundamental structure of your code. If you wrote an efficient algorithm, your algorithm will stay efficient. And if you wrote bad algorithm, it won't suddenly improve just because it was compiled with -O3.
The kind of optimization that can be influenced by compilers is stuff like loop unrolling, which might or might not happen depending on your compiler config. It's more of a localized optimization, but still can have a major impact on performance. You can write many of these kinds of things yourself in high level language without involving the assembly and the compiler won't change it.
1
u/BrainThinkerMan 10d ago
now the questiion is how to write good code? that is fast and takes use of all the cpu clock cycles and dont waste a single core,
2
u/serge-mv 10d ago
These principals are understood very well and you can read a book about it. For example, iterate in the correct direction of your memory layout, use loop counters based on cache line size, etc. Sometimes if you are not efficient, compilers will fix it for you on a small scale. But most of the time optimization is your job.
1
u/AustinVelonaut 11d ago
And if you wrote bad algorithm, it won't suddenly improve just because it was compiled with -O3.
Except in those cases where a pattern recognizer replaces the code wholesale with an equivalent, but more efficient algorithm, or even a compile-time constant!
3
u/RealisticDuck1957 11d ago
But never a behavior it is good to count on. Write a good algorithm and you're more likely to get good compiled code, across different compilers, target platforms, and settings.
2
u/AustinVelonaut 11d ago
In general I agree. But I can see an argument for recognizing and canonicalizing a "standard", "simple" pattern, replacing it with the best equivalent, as it will be done in one place. But still, I wonder whether most of this is just to game benchmarks. For example, Clang, at any level of optimization except -O0, will compile this loop:
int foo () { int tally = 0; for (int i = 0; i < 10; ++ i) { tally += i; } return tally; }to:
_foo: ; @foo .cfi_startproc ; %bb.0: mov w0, #45 ; =0x2d ret1
1
u/Helpful-Educator-415 11d ago
Almost always, compilers will only make your code more efficient, not less. If you go through the work of creating a highly optimized algorithm, it'll probably leave it alone, but if you don't, it might recognize the pattern and then figure out a faster way. There's no incentive to make your code worse, so.
1
u/BrainThinkerMan 10d ago
basically i want to know, code that is the most FASTEST and most PERFORMANT what does that look like , ie instead of doing something like int x = 5 + 5, int name = x + 10, do 5 + 5 + 10, because that doesnt make the name variable wait for x's instruction to be done, which saves x amount of clock cycle
1
u/cokeisahelluvadrug 10d ago
I think you just described exactly what it looks like. In general you want to be “helping the compiler” by understanding what conditions must hold to allow it to do certain optimizations etc.
1
u/tiger-56 10d ago edited 10d ago
The compiler will probably figure out that x = 10 and name = 20 and eliminate the additions for you before generating any code. You just write it the way that's best for someone reading your code to understand your algorithm.
1
u/ComprehensiveBig6215 8d ago
Yes, those simple transformations and substitutions will be done by the compiler at most optimisation levels. They are free wins.
Beyond that though, there is no FASTEST. It's all tradeoffs.
If you have a function that has a load of code inlined into it, loops unrolled, the code size will grow. Re-ordering code to break dependency chains increases register pressure. Hot variables might get spilled onto the stack.
In isolation, the function will likely run faster but the code around it gets slower as it suffers more cache misses.
Fastest depends on the specific CPU too. Differences in things like branch predictors may mean aligning or organising code differently.
3
8
u/Big_Tadpole7174 11d ago
Most compilers perform compilation in these steps:
Lexical analyzing
Parsing to an AST
Semantic analysis
Optimization of the AST
Code generation
Possibly optimization after code generation
Step 4 is usually where the bulk of the optimization takes place. You can rearrange AST nodes, remove unused nodes, etc. So it's not so much that the machine code is optimized (though it can in step 6) but that the tree that is used to generate code is altered. Hope this helps.
18
u/kniy 11d ago
AST is not really suitable for optimizations. Compilers generally have additional intermediate representations specifically for optimizations.
So it's more like:
Lexical analyzing
Parsing to an AST
Semantic analysis
Code generation in intermediate language 1
Optimization of the intermediate code
Code generation in intermediate language 2
Optimization of the intermediate code
Code generation in machine code
Possibly optimizations on the machine code
6
u/AustinVelonaut 11d ago
a (desugared, core) AST is a great place to do optimizations in a pure functional language, though: tree transformations, inlining, dead code removal, control-flow and data flow analysis, etc. are all very natural, there.
4
u/EpochVanquisher 11d ago
Yeah, I came in here to say the exact same thing.
To add to your comment, the different IRs will often have different “levels” of information, like the higher-level IR will have more type information, and the lower-level IR will have more information about stuff like exact values and memory layout. Different optimizations at different levels (but the AST usually isn’t optimized)
1
u/Big_Tadpole7174 10d ago
Interesting, I didn't know that. What kind of intermediate languages are we talking about? And are these representations passed between optimization stages, or is each stage self-contained?
2
u/EggplantExtra4946 7d ago
What kind of intermediate languages are we talking about?
There are many variants but most of them have the Static Single Assignment (SSA) property. The most common is the SSA CFG IR.
And are these representations passed between optimization stages,
Yes, each optimization takes the IR as input and modifies it. Then an another optimization can optimize it more.
Some optimizations require the SSA IR to be shaped specially, for example some loop optimization work on the Loop Closed SSA IR:
https://llvm.org/docs/LoopTerminology.html
https://gcc.gnu.org/onlinedocs/gccint/LCSSA.html
Some high level optimizations work on an SSA IR but where the instructions are high level operations, check out the MLIR dialects. The compiler will do all the high level optimizations on that high level IR, then lower it to an IR with lower level instructions and perform low level optimizations.
https://mlir.llvm.org/docs/Dialects/
Some loop vectorizers transform loops bodies from a CFG IR representation into a predicated IR representation, to eliminate conditionals inside the loop.
1
2
u/fl00pz 11d ago
The compiler tries to optimize your code the best it can. The compiler is still constrained by the code you wrote. If you can write more efficient code then the compiler has an easier time optimizing.
For example, if you have an algorithm that is O(n2) you may get a compiler version that is an optimized program but is still O(n2). If you can solve the same problem with an algorithm that is O(n) or O(log n) and the compiler version is also O(n) or O(log n), then you just optimized your code regardless of the compiler optimization. The version you optimized is better than the compiler optimized O(n2) program.
1
u/mooglinux 11d ago
This is completely dependent on the capabilities of the specific compiler being used. Many compilers have multiple optimization passes to try and find a sequence of instructions which accomplishes the same result with fewer instructions. It also depends on whether the compiler is making use of instructions tailored for the hardware it will be executing on; different CPUs have different capabilities that can be leveraged or not.
1
u/Dan13l_N 4d ago
Without compiling, there is no performance. There's no "baseline" to compare against, so that you could say "the compiler removed performance".
4
u/flatfinger 11d ago
Some compilers are designed to transform a program into machine code while preserving the overall structure, while others are designed to figure out what a program is trying to do, and then use a built-in template to accomplish that in a way which might be faster than some ways the programmer could have specified the operations, but not necessarily all.
If, for example, clang is given a loop whose index starts at a multiple of four and counts down by 4 while greater than or equal to zero, clang may transform the loop so that it instead tests whether the index is greater than three, even if checking whether the index became negative would have been cheaper than testing whether it became 3 or less.