[llvm-dev] Forcing loop optimization

791 views
Skip to first unread message

Sudakshina Dutta via llvm-dev

unread,
Jun 14, 2021, 9:23:49 AM6/14/21
to LLVM Development List
Dear all,

Good day to you. Is it possible to have forced application of loop unrolling or any other loop optimization ? I have tried applying loop unrolling on the following code. However, the optimization has not been applied by Clang.

Code (code.c)
int main()
{
    int i1, i2, N;
    int In[50], A[50], D[50];

    N = 25;
    A[0] = In[0] + 5;
    #pragma clang loop unroll (enable)
    for(i1 = 1; i1 <= 25; i1++)
    {
            A[i1] = In[i1];
            D[i1] = A[i1];
    }
    return D[N];
}

The applied commands
  1. clang -S -emit-llvm code.c  -o code.ll -Xclang -disable-O0-optnon
  2. opt -loop-unroll -S code.ll -o code-opt.ll -opt-bisect-limit=300 -unroll-count=3 -print-after=loop-unroll
Regards,
Sudakshina

Johannes Doerfert via llvm-dev

unread,
Jun 14, 2021, 10:37:21 AM6/14/21
to Sudakshina Dutta, LLVM Development List
Hi Sudakshina,

Add -sroa (or mem2reg) to your second command to simplify the IR in a
way loop unroll can deal with:

https://godbolt.org/z/refxnMcf3

I removed options that are not needed in the link above.

Hope this helps,
  Johannes


On 6/14/21 8:23 AM, Sudakshina Dutta via llvm-dev wrote:
> Dear all,
>
> Good day to you. Is it possible to have forced application of loop
> unrolling or any other loop optimization ? I have tried applying loop
> unrolling on the following code. However, the optimization has not been
> applied by Clang.
>

> *Code (code.c)*


> int main()
> {
> int i1, i2, N;
> int In[50], A[50], D[50];
>
> N = 25;
> A[0] = In[0] + 5;
> #pragma clang loop unroll (enable)
> for(i1 = 1; i1 <= 25; i1++)
> {
> A[i1] = In[i1];
> D[i1] = A[i1];
> }
> return D[N];
> }
>

> *The applied commands*
>
> 1. clang -S -emit-llvm code.c -o code.ll -Xclang -disable-O0-optnon
> 2. opt -loop-unroll -S code.ll -o code-opt.ll -opt-bisect-limit=300
> -unroll-count=3 -print-after=loop-unroll
>
> Regards,
> Sudakshina
>
>
> _______________________________________________
> LLVM Developers mailing list
> llvm...@lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
_______________________________________________
LLVM Developers mailing list
llvm...@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev

Sudakshina Dutta via llvm-dev

unread,
Jun 14, 2021, 11:31:48 PM6/14/21
to Johannes Doerfert, LLVM Development List
Thanks a lot Mr. Johannes Doerfert.

Sudakshina Dutta via llvm-dev

unread,
Jun 15, 2021, 6:37:10 AM6/15/21
to Johannes Doerfert, LLVM Development List
Dear Prof. Johannes Doerfert,

Sorry to disturb you again. The code LLVM IR that you have shared is not only the result of applying x86-64 clang 12.0.0 applied with -emit-llvm -Xclang -disable-O0-optnone options. You might have applied some other options. The reason I am asking is that the LLVM IR code which is shared has a header and footer which generally do not appear if a code is subjected to godbolt explorer. If the LLVM IR code generated by applying x86-64 clang 12.0.0 applied with -emit-llvm -Xclang -disable-O0-optnone options is subjected to opt with -sroa -loop-unroll -unroll-count=3 options, some error is shown. I am very new to compiler explorer. I shall be grateful if you kindly reply.

Thanks,
Sudakshina

Johannes Doerfert via llvm-dev

unread,
Jun 15, 2021, 10:39:04 AM6/15/21
to Sudakshina Dutta, LLVM Development List

On 6/15/21 5:36 AM, Sudakshina Dutta wrote:
> Dear Prof. Johannes Doerfert,
>
Just Johannes.


> Sorry to disturb you again. The code LLVM IR that you have shared is not
> only the result of applying x86-64 clang 12.0.0 applied with -emit-llvm
> -Xclang -disable-O0-optnone options. You might have applied some other
> options. The reason I am asking is that the LLVM IR code which is shared
> has a header and footer which generally do not appear if a code is
> subjected to godbolt explorer. If the LLVM IR code generated by applying
> x86-64 clang 12.0.0 applied with -emit-llvm -Xclang -disable-O0-optnone
> options is subjected to opt with -sroa -loop-unroll -unroll-count=3
> options, some error is shown. I am very new to compiler explorer. I shall
> be grateful if you kindly reply.

Godbold has a "filter" function. Look for "Filter" in the clang tab
and disable what you don't want. You should be able to do all of
this locally as well. There are multiple good tutorials on how to
compile and use the tools like opt on youtube.

~ Johannes

Michael Kruse via llvm-dev

unread,
Jun 15, 2021, 1:03:26 PM6/15/21
to Sudakshina Dutta, LLVM Development List
For forcing transformations via source code annotations, see [1].

However, there may be other reasons for an optimization to be not implied, such as the loop not having been normalized. See [2]


Reply all
Reply to author
Forward
0 new messages