Disable optimization

Hello together,

although i use the “-O0” flag as follow, the llvm perform dead code elimination.
How can i disable optimizing completely?

KR
Raad

llvm-gcc -O0 -emit-llvm main.cpp -S -o main.s

Sorry, it's not possible; the gcc front-end does some optimizations
which the LLVM backend can't do anything about, and dead code
elimination can arise naturally out of the approach LLVM uses for code
generation. Note that -O0 really means "generate code as quickly as
possible, and don't do any optimizations which reduce the quality of
debug symbols".

Why exactly do you want to disable dead code elimination anyway? I
can't see any reason why you'd want dead code in the generated
program, unless you're abusing asm statements or something like that.

-Eli

Hi Eli,

I want to disable optimizations because, i’m going to implement a framework in JAVA for educational purposes.

I have planned to do followings:

  1. Create LLVM-IR and export it as Assembly (without optimizing the source-code)
  2. Transform the LLVM-Assembly to a data-structure, similar to LLVM data structure (Module, Function, BB …) but implemented in Java
  3. Optimizing the code (should be performed by students :slight_smile: )
  4. Transform my data-structure to LLVM-Assembly.
  5. Generate machine code using “llvm-as”

Could you list me the other optimizations, which are performed by front-end?

Raad

Hi Eli,

I want to disable optimizations because, i'm going to implement a framework
in JAVA for educational purposes.

I have planned to do followings:

    1) Create LLVM-IR and export it as Assembly (without optimizing the
source-code)
    2) Transform the LLVM-Assembly to a data-structure, similar to LLVM data
structure (Module, Function, BB ...) but implemented in Java
    3) Optimizing the code (should be performed by students :slight_smile: )
    4) Transform my data-structure to LLVM-Assembly.
    5) Generate machine code using "llvm-as"

Ah, so roughly, Java bindings to LLVM? Interesting. Although, IMO,
for a topic as advanced as compilers, I don't think it would be a huge
burden to require using C++.

Could you list me the other optimizations, which are performed by front-end?

I don't know the gcc front-end too well, so this is a rough list. The
obvious optimizations are constant folding and dead code elimination.
The front-end will also do local optimizations on expressions, like
optimizing "a+b-b" to "a". There might also be some other small stuff
I'm not remembering, but there aren't any other big optimizations
happening at -O0.

If you really want the dumbest possible conversion into bitcode, you
can probably hack up clang (http://clang.llvm.org) to disable all
optimizations... clang generally does less optimization, and if
there's some particular optimization you want to disable, finding it
should be easier. That said, it's probably not worth bothering; if
the point is to teach the students to write optimizers, it doesn't
really make a difference whether the code starts off slightly
optimized.

-Eli

It matters if the optimisations the students have to implement are the
optimisations done implicitly by LLVM.

It matters if the optimisations the students have to implement are the
optimisations done implicitly by LLVM.

They are not really done by LLVM, because no optimizations are done on the
LLVM IR. They are done by llvm-gcc, a front-end to LLVM:
(1) gcc does constant folding all over the place (nothing can be done about
this)
(2) the llvm-gcc to LLVM converter also does constant folding (this can
be turned off easily enough)
(3) it seems that unused code can also be eliminated during the conversion
to LLVM IR. Presumably the code is simply never converted to IR in the
first place. I'm not sure how this happens. Perhaps it is possible to
ensure that everything gets converted.

Ciao,

Duncan.

Wouldn't Clang be a good option to generating unoptimized LLVM IR?

Dan

Daniel M Gessel dixit:

Wouldn't Clang be a good option to generating unoptimized LLVM IR?

More like broken :wink: sometimes at least.

I've had good luck with Clang, but I'll keep an eye out.

I still think it would be a good option in an educational environment.

Heck, fixing a bug or two in Clang would't be a bad learning experience.

:slight_smile:

Dan