LLVMTargetMachine with optimization level passed from clang.

I see the BackendUtil.cpp of Clang creates the TargetMachine with the optimization level based on below mentioned logic

CodeGenOpt::Level OptLevel = CodeGenOpt::Default;

switch (CodeGenOpts.OptimizationLevel) {

default: break;

case 0: OptLevel = CodeGenOpt::None; break;

case 3: OptLevel = CodeGenOpt::Aggressive; break;

}

As per my understanding, the correspondence between optimization level and CodeGenOpt is as below

O0 - none

O1 - less

O2 - default

O3 - aggressive

My goal is to turn on a target dependent codegen optimization at –O1(less). The TargetMachine is constructed with “CodeGenOpt::Default”. Is there a way I could do this in LLVM ? What is the standard way of doing this ? Also, the code in BackendUtil.cpp seem somewhat offbeat to me.

–Sumanth G

If I understand you correctly, you should look at putting your pass in the pass manager at the opt level u want.

Ryan

Apparently “less” is not used by clang, do you want your optimization to be turned on for O1 *and above* or just for O1 specifically?

Also what kind of optimization is this? A SelectionDAG combine or a specific pass? If it is a pass, is it IR or MIR?

My mistake, u want a code gen opt, not IR, so a backend pass?

Yes, it is a target independent back-end pass.

I want the optimization to be turned on at -O1 and above.
In my case, it is a target independent back-end pass. (Eg: MachinePipeliner)

That usually happens in TargetPassConfig. Have you looked at TargetPassConfig::addMachinePasses()?

Most of the stuff you’re looking for lives in TargetPassConfig.cpp. There are a few examples of how you might do it that live in there too.

What it comes down to is finding the place you want your pass to live and sticking in some logic like this in there:

if (getOptLevel() != CodeGenOpt::None)
    addPass(&MyPassID);

A lot of passes are added in just TargetPassConfig::addMachinePasses(), but some live in more specialized areas like, for the sake of example, TargetPassConfig::addMachineLateOptimization(). You can add it wherever seems best for your specific pass.

Hope that helps!

Jessica Paquette

getOptLevel() gets the level from TargetMachine which is created by the Backendutil in clang with either
"Default", "None" or "Aggressive". Threre is no correspondence for "Less".
This boils down to , if I pass "-O1", the Target Machine is created with CodeGenOpt::Default.

I am available on IRC @ sgundapa.

I don’t see why is it a problem? That’s why I asked earlier if you want it on for O1 *and above* or only O1.
You just have to enable it when CodeGenOpt is Default or above (or disabling it when CodeGenOpt::None), or did I miss something?

Here is a problem scenario.

I want to enable a backend pass at -O2 or above.
if (TM->getOptLevel() >= CodeGenOpt::Default)
    addPass(&xxxxx);

This pass will be run at -O1 too since clang is creating the TargetMachine with CodeGenOpt::Default for -O1.

--Sumanth G

Right, you can’t.
(You wrote yesterday "I want the optimization to be turned on at -O1 and above.”).

Apologies for the confusion. I was trying to understand why passes were run at -O1 when they are intended for -O2 and above.
Is there a way I could avoid running optimizations at -O1 that are meant to run at -O2 and above?

--Sumanth G

From: llvm-dev [mailto:llvm-dev-bounces@lists.llvm.org] On Behalf Of Mehdi
Amini via llvm-dev
Sent: Friday, January 06, 2017 11:10 AM
To: Sumanth Gundapaneni
Cc: LLVM Developers
Subject: Re: [llvm-dev] LLVMTargetMachine with optimization level passed
from clang.

>
> Here is a problem scenario.
>
> I want to enable a backend pass at -O2 or above.
> if (TM->getOptLevel() >= CodeGenOpt::Default)
> addPass(&xxxxx);
>
> This pass will be run at -O1 too since clang is creating the
TargetMachine with CodeGenOpt::Default for -O1.

Right, you can’t.

Can somebody explain why it's not a bug that -O1 and -O2 are identical?
Thanks,
--paulr

This is only a CodegenOpts, I don’t think it *has to* be a bug that this option is the same between O1 and O2. There are many other diff between O1/O2

However I clearly don’t like that `llc -O1` and `clang -O1` are not using the same settings here: https://reviews.llvm.org/D28409

From: mehdi.amini@apple.com [mailto:mehdi.amini@apple.com]
Sent: Friday, January 06, 2017 12:49 PM
To: Robinson, Paul
Cc: Sumanth Gundapaneni; llvm-dev@lists.llvm.org
Subject: Re: [llvm-dev] LLVMTargetMachine with optimization level passed
from clang.

>
>
>
>> From: llvm-dev [mailto:llvm-dev-bounces@lists.llvm.org] On Behalf Of
Mehdi
>> Amini via llvm-dev
>> Sent: Friday, January 06, 2017 11:10 AM
>> To: Sumanth Gundapaneni
>> Cc: LLVM Developers
>> Subject: Re: [llvm-dev] LLVMTargetMachine with optimization level
passed
>> from clang.
>>
>>
>>>
>>> Here is a problem scenario.
>>>
>>> I want to enable a backend pass at -O2 or above.
>>> if (TM->getOptLevel() >= CodeGenOpt::Default)
>>> addPass(&xxxxx);
>>>
>>> This pass will be run at -O1 too since clang is creating the
>> TargetMachine with CodeGenOpt::Default for -O1.
>>
>> Right, you can’t.
>
> Can somebody explain why it's not a bug that -O1 and -O2 are identical?

This is only a CodegenOpts, I don’t think it *has to* be a bug that this
option is the same between O1 and O2. There are many other diff between
O1/O2

However I clearly don’t like that `llc -O1` and `clang -O1` are not using
the same settings here: https://reviews.llvm.org/D28409

Awesome, thanks!
--paulr

Thanks for fixing it.

--Sumanth G