[llvm-dev] clang optimizations

144 views
Skip to first unread message

via llvm-dev

unread,
Jan 25, 2021, 6:48:47 AM1/25/21
to llvm...@lists.llvm.org

Hello,

 

I am trying to apply level 3 of optimizations to a simple program. If I run:

 

clang -S -O3 -emit-llvm test.cpp -o testo.ll

 

all seems OK, the result testo.ll is highly optimized.

 

However if I attempt the same using:

 

clang -S -emit-llvm test.cpp -o a.ll

              opt-10 -S --O3 a.ll -o test.ll

 

then there is no optimization performed.

 

The test program that I am using is:

 

#include <iostream>

 

__attribute__((always_inline)) void ADD(bool en, int x, int y, bool &ok, int &r)

{

              if(en)

              {

                             ok = true;

                             r = x + y;

              }

              else

              {

                             ok = false;

                             r = 0;

              }            

}

 

void test()

{

              bool en = true;

              int x = 123;

              int y = 321;

              int z;

              bool ok;

              ADD(en, x, y, ok, z);

              std::cout << z;

}

 

Best regards,

 

Viorel

Florian Hahn via llvm-dev

unread,
Jan 25, 2021, 6:58:42 AM1/25/21
to viorel.p...@gmail.com, llvm-dev
Hi,


On Jan 25, 2021, at 11:48, via llvm-dev <llvm...@lists.llvm.org> wrote:

Hello,
 
I am trying to apply level 3 of optimizations to a simple program. If I run:
 
clang -S -O3 -emit-llvm test.cpp -o testo.ll
 
all seems OK, the result testo.ll is highly optimized.
 
However if I attempt the same using:
 
clang -S -emit-llvm test.cpp -o a.ll
              opt-10 -S --O3 a.ll -o test.ll
 

Please take a look at the attributes added to the functions: https://godbolt.org/z/fs9f8n . Without optimizations (-O0), Clang will add `optnone` to the functions, effectively disabling optimizations when you run `opt`. If you don’t want clang to run optimizations, but not add `optnone`, you can use `clang -O3 -emit-llvm -S -mllvm -disable-llvm-optzns`.

Cheers,
Florian 

via llvm-dev

unread,
Jan 25, 2021, 7:12:54 AM1/25/21
to Florian Hahn, llvm-dev

Hi Florian,

 

Thank you for your quick response. This fixes the optimization.

 

Best regards,

 

Viorel

Reply all
Reply to author
Forward
0 new messages