Suggestions for Vectorizing Source Code
Vectorization Suggestion |
Cause of Failure to Vectorize |
How to Modify |
Example |
||||
|---|---|---|---|---|---|---|---|
Extracting loop control variables |
The loop control variable of the for loop is a structure member. The compiler cannot determine the loop end condition. As a result, the loop cannot be automatically vectorized. |
Extract the loop control variable out of the loop. |
Example code:
Modify it as follows:
|
||||
Modifying the loop control condition |
Clang 15 supports automatic vectorization while earlier versions do not. |
Change the loop condition from <= to <, and the loop length from len to len+1. |
Example code:
Modify it as follows:
|
||||
Adding a compilation instruction for automatic vectorization |
After evaluating the benefits of vectorization, the compiler adopts a conservative policy and determines not to perform automatic vectorization. |
Add the pragma compilation instruction to force automatic code vectorization. |
Example code:
Modify it as follows:
|
||||
Specifying that the memory to which the pointer points is not referenced by other pointers |
It cannot be determined whether the memory to which the pointer points is referenced by any other pointers. The compiler will abandon automatic vectorization. |
Add the restrict keyword to label the pointer variable. |
Example code:
Modify it as follows:
|
||||
Keeping the consistent data type and length |
The variable type does not match. The compiler cannot perform automatic vectorization. |
Change the variable type from long to int. |
Example code:
Modify it as follows:
|
||||
Splitting the loop |
The l-value space of the loop operation is fixed and the loop dependency exists. Therefore, the compiler cannot perform vectorization. |
Split the loop. Store the l-value of each round of loop operation independently and then merge all the left values. |
Example code:
Modify it as follows:
|
||||
Simplifying the code logic in the conditional branch |
Complex operations exist in conditional branch statements. As a result, automatic vectorization is impossible. |
Extract the operation statements out of the conditional branch. |
Example code:
Modify it as follows:
|
||||
Changing the data type to unsigned |
The data types are inconsistent and the compiler cannot perform vectorization. |
Change the data type from signed to unsigned. |
Example code:
Modify it as follows:
|
||||
Reducing the calculation precision |
The calculation precision requirement is high. To ensure the calculation precision, the compiler does not perform automatic vectorization. |
Reduce the calculation precision. |
Example code:
Modify it as follows:
|
||||
Splitting the loop |
The loo has many statements. The compiler cannot determine the variable dependency and does not perform vectorization. |
Split the statements of the loop and add them to multiple loops. |
Example code:
Modify it as follows:
|
||||
Reducing function calls in the loop |
Function calls exist in the loop and the compiler cannot perform vectorization. |
Extract calculations related to function calls out of the loop. |
Example code:
Modify it as follows:
|
||||
Using Fortran keywords |
The Fortran language feature is not fully used. |
Use array assignment instead of the loop to implement operations on multiple data records. |
Example code:
Modify it as follows:
|
||||
Specifying that the memory to which the pointer points is not referenced by other pointers and adding compilation commands |
It cannot be determined whether the memory to which the pointer points is referenced by any other pointers. The compiler will abandon automatic vectorization. |
Add the restrict keyword to label the pointer variable. |
Example code:
Modify it as follows:
|