How to test register pressure

Hi All,

I like to test the register pressure of the register allocator available in LLVM. I’m able to successfully print out the steps that the register allocator takes, but I’m having a challenge creating a test example that forces the register pressure to be high(lots of spills and reloads).

I created the sample below that calculate a random matrix vector calculation. It takes in an `UNROLL_FACTOR` based on how many times you like the loop to be unrolled. My assumption is that this would increase the register pressure, but it doesn’t seem to be spilling for any of the register allocators when I increase this value.

``````  1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #define AROW 32736
5 #define ACOL 32736
6 #define BCOL 32736
7
8 #ifndef UNROLL_FACTOR
9 #define UNROLL_FACTOR 0
10 #endif
11
12 void mat_vec(int** arrA, int* arrB, int* result )
13 {
14
15     for( int i = 0; i < AROW; i++ ) {
16
17         result[i] = 0;
18
19         #pragma clang loop unroll_count(UNROLL_FACTOR)
20         for( int k = 0; k < ACOL; k++ ) {
21
22             int t = arrA[i][k] * arrB[k];
23             result[i] += t;
24
25         }
26     }
27
28 };
29
30
31 int main()
32 {
33     /* declarations */
34     int** A      = new int* [ACOL];
35     int*  B      = new int  [BCOL];
36     int*  result = new int  [BCOL];
37
38     for( int i = 0; i < ACOL; i++ )
39     {
40         A[i] = new int[AROW];
41     }
42
43     srand(1337);
44
45     for( int i = 0; i < ACOL; i++ )
46     {
47         for( int j = 0; j < AROW; j++ )
48         {
49             A[i][j] = rand();
50         }
51     }
52
53     for( int i = 0; i < BCOL; i++)
54         B[i] = rand();
55
56
57     mat_vec( A, B, result );
58
59     for( int i = 0; i < BCOL; i++ )
60     {
61         result[i] = result[i];
62     }
63
64     /* clean up */
65     for( int i = 0; i < ACOL; i++ )
66     {
67         delete [] A[i];
68     }
70     delete [] A;
71     delete [] B;
72     delete [] result;
73     return 0;
``````

I wanted to know if this or any other examples or tests I could generate could help test register pressure. Any assistance or guidance would be greatly appreciated.

Thank you.

I don’t believe so, I’m not familiar with FPPPP.

After doing a quick search I see it’s related to the SPEC benchmarking suite. I have access to SPEC CPU 2017, but I was hoping to create a minimal example that would test register pressure.

If you want a simple test for register pressure, without needing it to look like real-world code, then this is my usual way to do it (in C++):

``````volatile int v;

template<int n>
[[always_inline]]
void pressure() {
int x = v;
if constexpr (n > 1)
pressure<n - 1>();
v = x;
}

void test() {
pressure<20>();
}
``````

I attempted to run your example and it compiles and runs fine, but I get an error saying the following when compiling it with my version of clang

``````clang++ reg_pressure.cpp -o reg_pressure
reg_pressure.cpp:11:3: warning: unknown attribute 'always_inline' ignored [-Wunknown-attributes]
[[always_inline]]
^~~~~~~~~~~~~
1 warning generated.
``````

Is this intended?

Thanks.

Ah, sorry, that should be `[[clang::always_inline]]` (or `__attribute((always_inline))`).