Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
15 years ago

NIOS II GCC -funroll-all-loops

I have try a bench for see if the unrool loop flag work fine,

When the inital idx value is a constant know at compilation -> the unrool work fine.

But if the inital idx value is not know at compilation -> the unrool is not generated.

I use the wrong GCC flag ? ( -funroll-all-loops)

The next example show that

# ########################################################

idx value is not know at compilation

int main()

{

UINT32 idx;

while(idx!=0)

{

*((volatile UINT32* )(0x00000000)) = 0;

idx--;

}

while (1);

return 0;

}

traduction ASM

.file "hello_world_small.c"

.section .text

.align 2

.global main

.type main, @function

main:

beq r2, zero, .L2

mov r3, zero

.L3:

addi r2, r2, -1

stw zero, 0(r3) //NO UNROLL

bne r2, zero, .L3

.L2:

.L6:

br .L6

.size main, .-main

.ident "GCC: (Altera 10.1 Build 153) 4.1.2"

For example GCC could make that for unrool this loop ->

int main()

{

UINT32 idx;

while(idx>7)

{

*((volatile UINT32* )(0x00000000)) = 0;

*((volatile UINT32* )(0x00000000)) = 0;

*((volatile UINT32* )(0x00000000)) = 0;

*((volatile UINT32* )(0x00000000)) = 0;

*((volatile UINT32* )(0x00000000)) = 0;

*((volatile UINT32* )(0x00000000)) = 0;

*((volatile UINT32* )(0x00000000)) = 0;

*((volatile UINT32* )(0x00000000)) = 0;

idx-=8;

}

while(idx!=0)

{

*((volatile UINT32* )(0x00000000)) = 0;

idx--;

}

while (1);

return 0;

}

# ########################################################

idx value is a constant know at compilation

# define UINT32 unsigned int

int main()

{

UINT32 idx = 800;

while(idx!=0)

{

*((volatile UINT32* )(0x00000000)) = 0;

idx--;

}

while (1);

return 0;

}

.file "hello_world_small.c"

.section .text

.align 2

.global main

.type main, @function

main:

movi r3, 800

mov r2, zero

.L2:

stw zero, 0(r2) // UNROLL : D

stw zero, 0(r2)

stw zero, 0(r2)

stw zero, 0(r2)

stw zero, 0(r2)

stw zero, 0(r2)

stw zero, 0(r2)

addi r3, r3, -8

stw zero, 0(r2)

bne r3, zero, .L2

.L14:

br .L14

.size main, .-main

.ident "GCC: (Altera 10.1 Build 153) 4.1.2"

# ########################################################

I'm wrong ?

Thanks for answer : D

Have a nice day ;)

4 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    If the compiler doesn't know how many times the loop is going to iterate then I wouldn't expect it to unroll the loop for you. So by not specifying an initial value for 'idx' the compiler has no clue if it should unroll the loop 2, 3, 4, 5, ...100, etc.... times. Loop unrolling is a static optimization so all the unknowns need to be.... known for the optimization to occur.

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    If the compiler doesn't know how many times the loop is going to iterate then I wouldn't expect it to unroll the loop for you. So by not specifying an initial value for 'idx' the compiler has no clue if it should unroll the loop 2, 3, 4, 5, ...100, etc.... times. Loop unrolling is a static optimization so all the unknowns need to be.... known for the optimization to occur.

    --- Quote End ---

    Thanks for answer :)

    But,

    int main()

    {

    UINT32 idx;

    while(idx>7)

    {

    *((volatile UINT32* )(0x00000000)) = 0;

    *((volatile UINT32* )(0x00000000)) = 0;

    *((volatile UINT32* )(0x00000000)) = 0;

    *((volatile UINT32* )(0x00000000)) = 0;

    *((volatile UINT32* )(0x00000000)) = 0;

    *((volatile UINT32* )(0x00000000)) = 0;

    *((volatile UINT32* )(0x00000000)) = 0;

    *((volatile UINT32* )(0x00000000)) = 0;

    idx-=8;

    }

    while(idx!=0)

    {

    *((volatile UINT32* )(0x00000000)) = 0;

    idx--;

    }

    while (1);

    return 0;

    }

    That a way to unrool dynamic loop no ?

    The compilateur is afraid by that ?

    or i don't have understood your answer ?
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    if he don't know the more probable of the initial idx value he don't want take the risk to optimise ?

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Compilers typically do not attempt to try to guess what your intentions are by throwing optimizations at your code willy-nilly and potentially breaking it. For example the optimization you created by hand the compiler would not know to do since the statements in that loop should not be run if idx started off being 0 (which is commonly the initialization state of an uninitialized variable). So to enforce loop unrolling you should implement it manually; however, in the case of the loop you are talking about in the present state where idx starts unknown that doesn't look like a good idea to me.