Modern compilers often apply auto-vectorization to loops to improve performance by utilizing SIMD instructions. This enables the CPU to execute multiple operations simultaneously using vector registers, significantly enhancing performance in computation-heavy code. This tutorial shows how to check which loops were vectorized using MSVC compiler.
Here's a simple function that divides an array of floats by a constant divisor:
#include <stdio.h>
void divide(float *data, const size_t n, const float divisor) {
for (size_t i = 0; i < n; ++i) {
data[i] /= divisor;
}
}
int main() {
const int n = 18;
float a[] = {
0, 14, 28, 42, 56, 70, 84, 98, 112, 126, 140, 154, 168, 182, 196, 210, 224, 255
};
divide(a, n, 255);
for (size_t i = 0; i < n; ++i) {
printf("%f ", a[i]);
}
return 0;
}
This loop is well-suited for vectorization, as it performs a straightforward operation on a contiguous block of data.
The /Qvec-report:2
option tells to output the report which loops were vectorized. To compile the code and produce the vectorization report, use the following command:
cl /nologo /O2 /Qvec-report:2 main.c > vec.log
/nologo
- hides MSVC startup banner./O2
- enables full optimizations (includes vectorization)./Qvec-report:2
- shows detailed vectorization reports (both success and failure).> vec.log
- redirects output to a file.
After compiling, open vec.log
. You might see a message like:
main.c
--- Analyzing function: divide
D:\main.c(4) : info C5001: loop vectorized
--- Analyzing function: main
D:\main.c(4) : info C5001: loop vectorized
D:\main.c(16) : info C5002: loop not vectorized due to reason '1200'
This means:
- The loop in
divide
was successfully vectorized. - The loop that prints the array was not vectorized. The reason code 1200 means that the loop has data dependencies between iterations that block vectorization.
Leave a Comment
Cancel reply