Repository navigation
Add AoCO 2025 Day 01 Study Notes - #24
Conversation
Summary of ChangesHello @gapry, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request adds comprehensive study notes that explore the compiler optimization technique involving the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
This PR adds comprehensive study notes on compiler optimizations, specifically focusing on the xor eax, eax optimization. The content is well-structured and educational, covering different optimization levels and their effects on assembly output.
Issues Found:
- Several typos throughout the document that need correction
- Missing newline at end of file
- Duplicate command line in one of the output blocks
Summary:
The technical content is accurate and provides valuable insights into compiler behavior. Once the typos are fixed, this will be a solid addition to the blog. All issues identified are minor formatting and spelling corrections that can be easily addressed with the provided suggestions.
You can now have the agent implement changes and create commits directly on your pull request's source branch. Simply comment with /q followed by your request in natural language to ask the agent to make changes.
| lldb version 18.1.8 | ||
| {% endhighlight %} | ||
|
|
||
| #### The different between `-O0`, `-O1`, and `-O2` |
There was a problem hiding this comment.
Fix typo in section heading. "different" should be "difference".
| #### The different between `-O0`, `-O1`, and `-O2` | |
| #### The difference between `-O0`, `-O1`, and `-O2` |
|
|
||
| It reduces the output from six instructions to three by removing the stack frame setup. | ||
|
|
||
| ###### Use `-O2` as optimzing level |
There was a problem hiding this comment.
Fix typo in section heading. "optimzing" should be "optimization".
| ###### Use `-O2` as optimzing level | |
| ###### Use `-O2` as optimization level |
| 87 0 0 87 57 main.o | ||
| {% endhighlight %} | ||
|
|
||
| As you can see, `-02` and `-O1` are both produce three instructions. |
There was a problem hiding this comment.
Fix typo: "-02" should be "-O2" (capital O, not zero).
| As you can see, `-02` and `-O1` are both produce three instructions. | |
| As you can see, `-O2` and `-O1` are both produce three instructions. |
|
|
||
| As you can see, `-02` and `-O1` are both produce three instructions. | ||
| The only differences is that `-O2` changes from `movl` to `xorl`. | ||
| The reason is the instructon size. `xorl %eax, %eax` only use two bytes, |
There was a problem hiding this comment.
Fix typo: "instructon" should be "instruction".
| The reason is the instructon size. `xorl %eax, %eax` only use two bytes, | |
| The reason is the instruction size. `xorl %eax, %eax` only use two bytes, |
| {% highlight bash %} | ||
| rm -f *.o; clang -O1 -c main.c; llvm-objdump -d --x86-asm-syntax=att main.o | ||
|
|
||
| main.o: file format elf64-x86-64 | ||
|
|
||
| Disassembly of section .text: | ||
|
|
||
| 0000000000000000 <main>: | ||
| 0: 31 c0 xorl %eax, %eax | ||
| 2: c3 retq | ||
| {% endhighlight %} |
There was a problem hiding this comment.
Remove duplicate command line from output. The command should only appear in the previous code block, not in the output.
| {% highlight bash %} | |
| rm -f *.o; clang -O1 -c main.c; llvm-objdump -d --x86-asm-syntax=att main.o | |
| main.o: file format elf64-x86-64 | |
| Disassembly of section .text: | |
| 0000000000000000 <main>: | |
| 0: 31 c0 xorl %eax, %eax | |
| 2: c3 retq | |
| {% endhighlight %} | |
| {% highlight bash %} | |
| main.o: file format elf64-x86-64 | |
| Disassembly of section .text: | |
| 0000000000000000 <main>: | |
| 0: 31 c0 xorl %eax, %eax | |
| 2: c3 retq | |
| {% endhighlight %} |
| 2: c3 retq | ||
| {% endhighlight %} | ||
|
|
||
| As we know, the x86-64 calling converstion requires the return value to be stored in the |
There was a problem hiding this comment.
Fix typo: "converstion" should be "convention".
| As we know, the x86-64 calling converstion requires the return value to be stored in the | |
| As we know, the x86-64 calling convention requires the return value to be stored in the |
| | 5th | `%r8` | `%r8d` | | ||
| | 6th | `%r9` | `%r9d` | | ||
|
|
||
| The `xorl` optimization does not only apper for return values, you will also see it frequently |
There was a problem hiding this comment.
Fix typo: "apper" should be "appear".
| The `xorl` optimization does not only apper for return values, you will also see it frequently | |
| The `xorl` optimization does not only appear for return values, you will also see it frequently |
| ## References | ||
| - [x64 architecture](https://learn.microsoft.com/en-us/windows-hardware/drivers/debugger/x64-architecture) No newline at end of file |
There was a problem hiding this comment.
Add missing newline at end of file to follow standard conventions.
| ## References | |
| - [x64 architecture](https://learn.microsoft.com/en-us/windows-hardware/drivers/debugger/x64-architecture) | |
| ## References | |
| - [x64 architecture]( |
There was a problem hiding this comment.
Code Review
This pull request adds detailed study notes about the xor eax, eax compiler optimization, which is a great addition. The notes are well-structured and provide clear examples with different compilers and optimization levels. I've found a few typos and grammatical errors in the text. Addressing these will improve the clarity and professionalism of the post. I've left specific suggestions for these minor corrections.
| lldb version 18.1.8 | ||
| {% endhighlight %} | ||
|
|
||
| #### The different between `-O0`, `-O1`, and `-O2` |
| '----------' '------------' '------------' | ||
| {% endhighlight %} | ||
|
|
||
| For now, we use `main.c` as input (shown below), apply different optimization level, |
There was a problem hiding this comment.
There's a minor grammatical error here. 'optimization level' should be plural, 'optimization levels', since you are applying different ones.
| For now, we use `main.c` as input (shown below), apply different optimization level, | |
| For now, we use `main.c` as input (shown below), apply different optimization levels, |
|
|
||
| It reduces the output from six instructions to three by removing the stack frame setup. | ||
|
|
||
| ###### Use `-O2` as optimzing level |
| As you can see, `-02` and `-O1` are both produce three instructions. | ||
| The only differences is that `-O2` changes from `movl` to `xorl`. | ||
| The reason is the instructon size. `xorl %eax, %eax` only use two bytes, | ||
| making it smaller than the five bytes `movl $0x0, %eax`. | ||
| Hence, you can see the total `.text` size reduces from 90 bytes to 87 bytes. |
There was a problem hiding this comment.
I've noticed a few typos and grammatical errors in this paragraph:
- In the first sentence,
-02should be-O2, and 'are both produce' should be 'both produce'. - In the second sentence, 'differences is' should be 'difference is'.
- In the third sentence, 'instructon' should be 'instruction', and 'use' should be 'uses'.
| As you can see, `-02` and `-O1` are both produce three instructions. | |
| The only differences is that `-O2` changes from `movl` to `xorl`. | |
| The reason is the instructon size. `xorl %eax, %eax` only use two bytes, | |
| making it smaller than the five bytes `movl $0x0, %eax`. | |
| Hence, you can see the total `.text` size reduces from 90 bytes to 87 bytes. | |
| As you can see, `-O2` and `-O1` both produce three instructions. | |
| The only difference is that `-O2` changes from `movl` to `xorl`. | |
| The reason is the instruction size. `xorl %eax, %eax` only uses two bytes, | |
| making it smaller than the five bytes `movl $0x0, %eax`. | |
| Hence, you can see the total `.text` size reduces from 90 bytes to 87 bytes. |
| 2: c3 retq | ||
| {% endhighlight %} | ||
|
|
||
| You will find that the Clang's `-O1` output already use `xorl`, making it similar to GCC's `-O2`. |
There was a problem hiding this comment.
| As we know, the x86-64 calling converstion requires the return value to be stored in the | ||
| 64-bit `rax` register. However, we see that the compiler use the 32-bit `eax` register for the `xorl` instruction. |
There was a problem hiding this comment.
There are a couple of minor corrections needed in this sentence:
- 'converstion' should be 'convention'.
- 'compiler use' should be 'compiler uses'.
| As we know, the x86-64 calling converstion requires the return value to be stored in the | |
| 64-bit `rax` register. However, we see that the compiler use the 32-bit `eax` register for the `xorl` instruction. | |
| As we know, the x86-64 calling convention requires the return value to be stored in the | |
| 64-bit `rax` register. However, we see that the compiler uses the 32-bit `eax` register for the `xorl` instruction. |
| {% endhighlight %} | ||
|
|
||
| According to the x86-64 System V ABI, the first six integer or pointer arguments are passed in | ||
| specific registers. To pass `0` to all of them, the compiler again use the `xorl` optimization to |
There was a problem hiding this comment.
| The `xorl` optimization does not only apper for return values, you will also see it frequently | ||
| when a caller prepares arguments for a callee, As with the previous example, zeroing the 32-bit | ||
| version of these registers automatically zero-extends to the full 64-bit register. |
There was a problem hiding this comment.
I found a typo and a punctuation issue here:
- 'apper' should be 'appear'.
- There should be a period after 'callee' to separate the two sentences.
| The `xorl` optimization does not only apper for return values, you will also see it frequently | |
| when a caller prepares arguments for a callee, As with the previous example, zeroing the 32-bit | |
| version of these registers automatically zero-extends to the full 64-bit register. | |
| The `xorl` optimization does not only appear for return values, you will also see it frequently | |
| when a caller prepares arguments for a callee. As with the previous example, zeroing the 32-bit | |
| version of these registers automatically zero-extends to the full 64-bit register. |
| version of these registers automatically zero-extends to the full 64-bit register. | ||
|
|
||
| ## References | ||
| - [x64 architecture](https://learn.microsoft.com/en-us/windows-hardware/drivers/debugger/x64-architecture) No newline at end of file |
There was a problem hiding this comment.
The linked Microsoft documentation is for the Windows x64 calling convention. Since all the examples in this post are on Linux (Ubuntu), which uses the System V ABI, it would be more accurate to link to the System V ABI documentation. For example, you could link to the Wikipedia page or the official ABI document.
There was a problem hiding this comment.
Pull request overview
This PR adds comprehensive study notes for Day 1 of the Advent of Compiler Optimisations 2025 series, exploring the "Why xor eax, eax?" compiler optimization technique. The notes provide practical examples demonstrating how different optimization levels affect assembly generation and includes hands-on verification using LLDB.
Key Changes:
- Added detailed technical documentation about x86-64 compiler optimizations with practical examples
- Demonstrated differences between
-O0,-O1, and-O2optimization levels in GCC and Clang - Explained zero-extension behavior in x86-64 architecture with debugging verification
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
_posts/2026-01-01-Advent-of-Compiler-Optimisations-Study-Notes-01.md |
New 309-line study notes document covering xor eax optimization, including development environment setup, optimization level comparisons, register behavior explanations, and calling convention examples |
_posts/2025-12-17-notes.md |
Removed draft notes file, likely replaced by the new comprehensive document |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| 87 0 0 87 57 main.o | ||
| {% endhighlight %} | ||
|
|
||
| As you can see, `-02` and `-O1` are both produce three instructions. |
There was a problem hiding this comment.
Grammatical error: "are both produce" should be "both produce".
| As you can see, `-02` and `-O1` are both produce three instructions. | |
| As you can see, `-02` and `-O1` both produce three instructions. |
| {% endhighlight %} | ||
|
|
||
| As you can see, `-02` and `-O1` are both produce three instructions. | ||
| The only differences is that `-O2` changes from `movl` to `xorl`. |
There was a problem hiding this comment.
Typo in "differences": "differences" should be singular "difference" in this context.
| The only differences is that `-O2` changes from `movl` to `xorl`. | |
| The only difference is that `-O2` changes from `movl` to `xorl`. |
|
|
||
| As you can see, `-02` and `-O1` are both produce three instructions. | ||
| The only differences is that `-O2` changes from `movl` to `xorl`. | ||
| The reason is the instructon size. `xorl %eax, %eax` only use two bytes, |
There was a problem hiding this comment.
Multiple issues on this line: "instructon" should be "instruction", and "use" should be "uses".
| The reason is the instructon size. `xorl %eax, %eax` only use two bytes, | |
| The reason is the instruction size. `xorl %eax, %eax` only uses two bytes, |
| 2: c3 retq | ||
| {% endhighlight %} | ||
|
|
||
| As we know, the x86-64 calling converstion requires the return value to be stored in the |
There was a problem hiding this comment.
Typo: "converstion" should be spelled "convention".
| As we know, the x86-64 calling converstion requires the return value to be stored in the | |
| As we know, the x86-64 calling convention requires the return value to be stored in the |
| | 5th | `%r8` | `%r8d` | | ||
| | 6th | `%r9` | `%r9d` | | ||
|
|
||
| The `xorl` optimization does not only apper for return values, you will also see it frequently |
There was a problem hiding this comment.
Typo: "apper" should be spelled "appear".
| The `xorl` optimization does not only apper for return values, you will also see it frequently | |
| The `xorl` optimization does not only appear for return values, you will also see it frequently |
| lldb version 18.1.8 | ||
| {% endhighlight %} | ||
|
|
||
| #### The different between `-O0`, `-O1`, and `-O2` |
There was a problem hiding this comment.
Typo in "different". The word "difference" should be used here instead of "different".
| #### The different between `-O0`, `-O1`, and `-O2` | |
| #### The difference between `-O0`, `-O1`, and `-O2` |
|
|
||
| It reduces the output from six instructions to three by removing the stack frame setup. | ||
|
|
||
| ###### Use `-O2` as optimzing level |
There was a problem hiding this comment.
Typo: "optimzing" should be spelled "optimization".
| ###### Use `-O2` as optimzing level | |
| ###### Use `-O2` as optimization level |
These notes are based on the post Why xor eax, eax?, which is Day 1 of the Advent of Compiler Optimisations 2025 Series by Matt Godbolt.