Global Variables and Classes - 2023.2 English

Vitis High-Level Synthesis User Guide (UG1399)

Document ID
Release Date
2023.2 English

AMD does not recommend using global variables in classes. They can prevent some optimizations from occurring. In the following code example, a class is used to create the component for a filter (class polyd_cell is used as a component that performs shift, multiply and accumulate operations).

typedef long long acc_t;
typedef int mult_t;
typedef char data_t;
typedef char coef_t;

#define TAPS 3
#define PHASES 4
#define DATA_SAMPLES 256
#define CELL_SAMPLES 12

// Use k on line 73 static int k;

template <typename T0, typename T1, typename T2, typename T3, int N>
class polyd_cell {
     T0 areg;
     T0 breg;
     T2 mreg;
     T1 preg;
 T0 shift[N];
     int k;   //line 73
 T0 shift_output;
     void exec(T1 *pcout, T0 *dataOut, T1 pcin, T3 coeff, T0 data, int col)

     if (col==0) {
   SHIFT:for (k = N-1; k >= 0; --k) {
      if (k > 0) 
        shift[k] = shift[k-1];
        shift[k] = data;
     *dataOut = shift_output;
     shift_output = shift[N-1];
     *pcout = (shift[4*col]* coeff) + pcin;


// Top-level function with class instantiated
void cpp_class_data (
 acc_t *dataOut,
 coef_t coeff1[PHASES][TAPS],
 coef_t  coeff2[PHASES][TAPS],
 data_t  dataIn[DATA_SAMPLES],
 int  row
) {

 acc_t pcin0 = 0;
 acc_t pcout0, pcout1;
 data_t dout0, dout1;
 int col;
 static acc_t accum=0;
 static int sample_count = 0;
 static polyd_cell<data_t, acc_t, mult_t, coef_t, CELL_SAMPLES> 
 static polyd_cell<data_t, acc_t, mult_t, coef_t, CELL_SAMPLES> 

 COL:for (col = 0; col <= TAPS-1; ++col) {




    if ((row==0) && (col==2)) {
        *dataOut = accum;
        accum = pcout1;
    } else {
        accum = pcout1 + accum;


Within class polyd_cell there is a loop SHIFT used to shift data. If the loop index k used in loop SHIFT was removed and replaced with the global index for k (shown earlier in the example, but commented static int k), Vitis HLS is unable to pipeline any loop or function in which class polyd_cell was used. Vitis HLS would issue the following message:

@W [XFORM-503] Cannot unroll loop 'SHIFT' in function 'polyd_cell<char, long long, 
int, char, 12>::exec' completely: variable loop bound.

Using local non-global variables for loop indexing ensures that Vitis HLS can perform all optimizations.