[llvm-dev] Auto-vectorization option

292 views
Skip to first unread message

Sudakshina Dutta via llvm-dev

unread,
May 13, 2021, 11:17:06 PM5/13/21
to LLVM Development List
Dear all,

Greetings. I want to generate the intermediate code (.ll file) after having LLVM auto-vectorization transformation on the source C file. I have the following code  (find_max.c) on which I want to apply auto-vectorization.

int main()
{
    int i, n, max, arr[50];
    for(i = 0; i < 35; i++)
    {
        printf("arr[%d] = ", i);
        scanf("%d", &arr[i]);
    }
    max = arr[0];
    for(i = 1; i < 35; i++)
    {
        if(arr[i] > max)
        {
            max = arr[i];
        }
    }

}

I have tried the following.

  1. clang  -S -emit-llvm find_max.c -o find_max.ll
  2. opt -loop-vectorize -force-vector-width=8 find_max.ll -o find_max_opt.ll
and
  1. clang -O3 -c find_max.c -Rpass=vector -Rpass-analysis=vector
  2. clang -O3 -c find_max.c -Rpass=vector -Rpass-analysis=vector -o find_max.ll

All the above options generate binary files. I request you to please help.

Thanks and regards,
Sudakshina

Sjoerd Meijer via llvm-dev

unread,
May 14, 2021, 4:08:17 AM5/14/21
to LLVM Development List, Sudakshina Dutta
Hi,

Not sure what you mean by only getting binaries: "clang -S -emit-llvm" should produce a .ll and so will opt. 

> clang  -S -emit-llvm find_max.c -o find_max.ll

Since no optimisation level is specified, this will result in very unoptimised code, probably resulting in vectorisation not triggering. I usually do "clang -O3 -S -emit-llvm -mllvm -print-before-all", or something equivalent, and then grab the IR just before vectorisation. 

> clang -O3 -c find_max.c -Rpass=vector -Rpass-analysis=vector

From a very quick look, this will probably not show anything because of your example: the second loop has no effect and is optimised away, only the first remains which is fully unrolled. Returning "max" will probably help.


From: llvm-dev <llvm-dev...@lists.llvm.org> on behalf of Sudakshina Dutta via llvm-dev <llvm...@lists.llvm.org>
Sent: 14 May 2021 04:16
To: LLVM Development List <llvm...@lists.llvm.org>
Subject: [llvm-dev] Auto-vectorization option
 

Sjoerd Meijer via llvm-dev

unread,
May 14, 2021, 4:12:37 AM5/14/21
to LLVM Development List, Sudakshina Dutta, Sjoerd Meijer
and also this:

-Rpass=vector

should be:

-Rpass=loop-vectorize

From: llvm-dev <llvm-dev...@lists.llvm.org> on behalf of Sjoerd Meijer via llvm-dev <llvm...@lists.llvm.org>
Sent: 14 May 2021 09:08
To: LLVM Development List <llvm...@lists.llvm.org>; Sudakshina Dutta <sudak...@iitgoa.ac.in>
Subject: Re: [llvm-dev] Auto-vectorization option
 

Nigel Perks via llvm-dev

unread,
May 14, 2021, 5:19:05 AM5/14/21
to Sudakshina Dutta, llvm...@lists.llvm.org

Try   opt -S   to get text IR instead of bitcode.

 

The second group of commands use   clang -c   so produce a compiled object file. For text IR you need   -S -emit-llvm   like in the first group.

 

Nigel

 

 

From: llvm-dev <llvm-dev...@lists.llvm.org> On Behalf Of Sudakshina Dutta via llvm-dev
Sent: 14 May 2021 04:17
To: LLVM Development List <llvm...@lists.llvm.org>
Subject: [llvm-dev] Auto-vectorization option

 

Dear all,

Sudakshina Dutta via llvm-dev

unread,
May 14, 2021, 7:31:15 AM5/14/21
to LLVM Development List
Dear all,

Thanks to all of you. I have executed the following commands on the code given above.

clang -O3 -S -c find_max.c -Rpass=vector -Rpass-analysis=vector -o find_max.ll

However, the generated code is an assembly code (attached). Is there any way to generate a vectorized IR (.ll) file ?

Thanks and regards,
Sudakshina
find_max.ll

Matt P. Dziubinski via llvm-dev

unread,
May 14, 2021, 8:23:32 AM5/14/21
to Sudakshina Dutta, LLVM Development List
On 5/14/2021 13:30, Sudakshina Dutta via llvm-dev wrote:
> Dear all,
>
> Thanks to all of you. I have executed the following commands on the code
> given above.
>
> clang -O3 -S -c find_max.c -Rpass=vector -Rpass-analysis=vector -o
> find_max.ll
>
> However, the generated code is an assembly code (attached). Is there any
> way to generate a vectorized IR (.ll) file ?

To get LLVM IR from the frontend (Clang) use -emit-llvm
-fno-discard-value-names, e.g., https://godbolt.org/z/aWz37qYdW

If you don't need debugger intrinsics (llvm.dbg.*) add -g0, e.g.,
https://godbolt.org/z/aWz37qYdW

As Sjoerd has mentioned, passing -mllvm -print-before-all to Clang is
usedful to get pre-vectorized LLVM IR (as well as observe the effects of
consecutive transformations); Example: https://godbolt.org/z/4za6h6fqo

You can then extract the unoptimized LLVM IR and play with it in "opt"
(the middle-end optimizer tool) to get the LLVM IR optimized by the
middle-end passes (including loop vectorizer); note that now you can
just pass -print-before-all directly: https://llvm.godbolt.org/z/P7E3PGE61

In particular, the LLVM IR displayed under "*** IR Dump Before
LoopVectorizePass on _Z1fPim ***" is a good baseline for comparisons.

Add "-mllvm -print-module-scope" to get the LLVM IR for the full module
(translation unit): https://godbolt.org/z/Go7zK8vsW

Then, pass this LLVM (right before LoopVectorizePass) to "opt" using
options "-loop-vectorize -debug-only=loop-vectorize" to observe the loop
vectorization pass in action:
https://llvm.godbolt.org/z/WMa1qosoq

Note that you need a binary built with assertions enabled to use -debug
options.

Last but not least you can give the optimized LLVM IR to "llc" (the
backend tool) to get the final assembly:
https://llvm.godbolt.org/z/hxevcqKEG

Best,
Matt
_______________________________________________
LLVM Developers mailing list
llvm...@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev

Sudakshina Dutta via llvm-dev

unread,
May 14, 2021, 9:20:48 AM5/14/21
to Matt P. Dziubinski, LLVM Development List
Dear Matt P. Dziubinski,

Thanks a lot for your reply. Although the vectorization is clearly visible in godbolt, I could not generate it by command line. Does it require some specific version of llvm/clang ?

Regards
Sudakshina

Matt P. Dziubinski via llvm-dev

unread,
May 14, 2021, 11:15:38 AM5/14/21
to Sudakshina Dutta, LLVM Development List
On 5/14/2021 15:20, Sudakshina Dutta wrote:
> Dear Matt P. Dziubinski,
>
> Thanks a lot for your reply. Although the vectorization is clearly
> visible in godbolt, I could not generate it by command line. Does it
> require some specific version of llvm/clang ?

No, only the -debug options require build with asserts; everything else
should be working with a regular release of Clang/LLVM. Chances are you
have to debug it (perhaps a missing side effect causing the entire loop
to be optimized away, etc.).

Best,
Matt

>
> Regards
> Sudakshina
>
> On Fri, May 14, 2021 at 5:53 PM Matt P. Dziubinski <mat...@gmail.com
> <mailto:mat...@gmail.com>> wrote:
>
> On 5/14/2021 13:30, Sudakshina Dutta via llvm-dev wrote:
> > Dear all,
> >
> > Thanks to all of you. I have executed the following commands on
> the code
> > given above.
> >
> > clang -O3 -S -c find_max.c -Rpass=vector -Rpass-analysis=vector -o
> > find_max.ll
> >
> > However, the generated code is an assembly code (attached). Is
> there any
> > way to generate a vectorized IR (.ll) file ?
>
> To get LLVM IR from the frontend (Clang) use -emit-llvm
> -fno-discard-value-names, e.g., https://godbolt.org/z/aWz37qYdW
> <https://godbolt.org/z/aWz37qYdW>
>
> If you don't need debugger intrinsics (llvm.dbg.*) add -g0, e.g.,

> https://godbolt.org/z/aWz37qYdW <https://godbolt.org/z/aWz37qYdW>


>
> As Sjoerd has mentioned, passing -mllvm -print-before-all to Clang is
> usedful to get pre-vectorized LLVM IR (as well as observe the
> effects of
> consecutive transformations); Example:

> https://godbolt.org/z/4za6h6fqo <https://godbolt.org/z/4za6h6fqo>


>
> You can then extract the unoptimized LLVM IR and play with it in "opt"
> (the middle-end optimizer tool) to get the LLVM IR optimized by the
> middle-end passes (including loop vectorizer); note that now you can
> just pass -print-before-all directly:
> https://llvm.godbolt.org/z/P7E3PGE61
> <https://llvm.godbolt.org/z/P7E3PGE61>
>
> In particular, the LLVM IR displayed under "*** IR Dump Before
> LoopVectorizePass on _Z1fPim ***" is a good baseline for comparisons.
>
> Add "-mllvm -print-module-scope" to get the LLVM IR for the full module
> (translation unit): https://godbolt.org/z/Go7zK8vsW
> <https://godbolt.org/z/Go7zK8vsW>
>
> Then, pass this LLVM (right before LoopVectorizePass) to "opt" using
> options "-loop-vectorize -debug-only=loop-vectorize" to observe the
> loop
> vectorization pass in action:
> https://llvm.godbolt.org/z/WMa1qosoq
> <https://llvm.godbolt.org/z/WMa1qosoq>
>
> Note that you need a binary built with assertions enabled to use -debug
> options.
>
> Last but not least you can give the optimized LLVM IR to "llc" (the
> backend tool) to get the final assembly:
> https://llvm.godbolt.org/z/hxevcqKEG

Reply all
Reply to author
Forward
0 new messages