After publishing my previous entry on the cost of loop alignment for Pentium III-M processors, I decided to look at what the results would be for different architectures. Since I did all of my experiments using a model system that consisted of a bunch of inline assembler compiled using Visual C++, I could run the benchmark application unmodified on all of the different computers that I have lying around my house. This spreadsheet summarizes all of the results for the following processors:
- 166MHz Pentium MMX
- 150MHz Pentium Pro
- 400MHz Celeron
- 600MHz Celeron
- 1130MHz Pentium III-M
- 1200MHz Athlon
- 1600MHz Pentium 4
All of the Intel P6 architecture processors (processors 2-5 on my list) share the following characteristics:
- No penalty if the target of a branch instruction is not aligned on a 16-byte boundary
- A one cycle penalty for the first additional 16-byte paragraph that needs to be fetched
- A one cycle penalty for the second additional 16-byte paragraph that needs to be fetched
The Intel P5 architecture processor (processor 1 on my list) has the following characteristics:
- No penalty if the target of a branch instruction is not aligned on a 16-byte boundary
- A half-cycle penalty for the first additional 16-byte paragraph that needs to be fetched
- No penalty for the second additional 16-byte paragraph that needs to be fetched
The Athlon architecture processor (processor 6 on my list) has the following characteristics:
- No penalty if the target of a branch instruction is not aligned on a 16-byte boundary
- A one cycle penalty for the first additional 16-byte paragraph that needs to be fetched
- No penalty for the second additional 16-byte paragraph that needs to be fetched
The Intel P7 architecture processor (processor 7 on my list) has the following characteristics:
- No penalty if the target of a branch instruction is not aligned on a 16-byte boundary
- No penalty for the first additional 16-byte paragraph that needs to be fetched
- No penalty for the second additional 16-byte paragraph that needs to be fetched
Clearly, all of the P6 architecture processors could benefit greatly from compiler optimizations that minimize the number of 16-byte paragraphs that a loop resides in. The P7 architecture processors are not sensitive to 16-byte alignment effects due to its new NetBurst architecture. All of the instructions in our loops are executed directly from the Pentium 4’s Trace Cache. The P5 and Athlon processors are somewhat less sensitive to 16-byte alignment effects since the 1 cycle penalty is paid on only the first additional 16 byte paragraph that is fetched from memory.
To run the benchmark software on your computer, click here to download the source code. Note that you must do a debug build, otherwise you will mess up the code alignment of the inline assembly instructions.
Currently, the JIT in the CLR does not seem to optimize for the number of 16 byte paragraphs that a loop resides in. I have seen examples of non-debug JIT’ed x86 code that straddle a 16 byte paragraph boundary unnecessarily. This would be a very useful JIT optimization in future versions of the CLR since the P6 architecture processors are clearly the mainstream processors of today.
I should also point out that these are extremely contrived benchmarks. I am measuring very simple model systems in an effort to understand exactly how the CPU executes specific instruction sequences. The simple counter loop that I was using in these systems executes in two cycles. The padded counter loop executes in six cycles. A one cycle penalty is very large (50%, 18% respectively) in these systems. Your mileage will definitely vary in real-world systems.