# Clang LTO Support Merged For Linux 5.12

## alamahant

https://www.phoronix.com/scan.php?page=news_item&px=Linux-5.12-Clang-LTO-Merged

The essence:

After entering the kernel tree please run

```

make LLVM=1 LLVM_IAS=1 menuconfig   #.I suppose you can use any target you like..I just prefer a stock .config that i usually steal from Arch so i dont run this first step

scripts/config -e LTO_CLANG_THIN

make LLVM=1 LLVM_IAS=1

```

Before running "make LLVM=1 LLVM_IAS=1" please make sure that in your .config the initial stanza is something like

```

CONFIG_CC_VERSION_TEXT="clang version 12.0.0"

CONFIG_GCC_VERSION=0

CONFIG_CC_IS_CLANG=y

CONFIG_CLANG_VERSION=120000

CONFIG_LD_VERSION=0

CONFIG_LD_IS_LLD=y

CONFIG_LLD_VERSION=120000

CONFIG_CC_CAN_LINK=y

CONFIG_CC_HAS_ASM_GOTO=y

CONFIG_CC_HAS_ASM_GOTO_OUTPUT=y

CONFIG_TOOLS_SUPPORT_RELR=y

CONFIG_CC_HAS_ASM_INLINE=y

CONFIG_IRQ_WORK=y

CONFIG_BUILDTIME_TABLE_SORT=y

CONFIG_THREAD_INFO_IN_TASK=y

```

Then when you run 

make LLVM=1 LLVM_IAS=1

it will offer you among others a choice of Clang LTO thin or full.

I am making a kernel right now...

Lets see........

 :Smile: 

----------

## Torangan

I just tried to build gentoo-sources-5.12.9 with clang LTO but it doesn't work. It shows me clang and the setup variables but my LTO choices are only LTO=none. Neither full nor thin offered. What am I doing wrong?

EDIT: not having lld installed was kind of a showstopper here.   :Very Happy: 

----------

## alamahant

Did you run

```

make LLVM=1 LLVM_IAS=1 menuconfig

scripts/config -e LTO_CLANG_THIN

make LLVM=1 LLVM_IAS=1

```

from inside the kernel tree?

For me it worked just fine but it produced a slightly bigger kernel.

----------

## Naib

So I was able to build the kernel, but the nvidia drivers however... https://forums.gentoo.org/viewtopic-t-1136808-highlight-clang.html

meanwhile I will carry on trying to build the entire system with clang

----------

## alamahant

Yes for some obscure reason you cant build external modules.

For me both nvidia and zfs-kmod.

Maybe you need to use clang to build them but still i couldnt.

----------

## Naib

Well the toolchain's need to align and so does the linking methodology 

luckily Ionen pushed out an experimental update and it works well 

https://forums.gentoo.org/viewtopic-t-1136808-highlight-.html

----------

## Perfect Gentleman

```
fsck                 |    to "Linux version 5.12.10-gentoo (root@De-Humanizer) (clang version 12.0.1, LLD 12.0.1) #1 SMP PREEMPT Sat Jun 12 16:13:09 +07 2021"
```

cannot build NTFS3 module and virtualbox-modules

----------

## Ionen

 *Perfect Gentleman wrote:*   

> cannot build NTFS3 module and virtualbox-modules

 Yeah, every modules are likely to fail except nvidia-drivers without at least some workarounds.

Passing the right toolchain is something that could be added to linux-mod.eclass eventually but that the kernel enables thinlto cache by default is a problem for sandbox and there's not really a safe way to turn it off for every modules right now (for nvidia-drivers I know I can safetly set ldflags-y and LDFLAGS_MODULE though).

You could of course remove it from the kernel source itself, or use full lto instead. Not to say they may not fail for other reasons (looking into it, nvidia apparently been testing this).

----------

## Perfect Gentleman

It was built with FullLTO. There is patch for NTFS3 module, but I haven't tested it. And I have no clue how to build virtualbox-modules.

----------

## alamahant

I built it with flto=thin.

I will try to use Ionen's solution at least for nvidia-drivers.

Be back soon.

----------

## Naib

Between this nvidia tweak and the libunwind update, converting as much of mmy system is now a reality

```
bash test.sh

Build with GCC:951  Built with Clang:297  23% conversion
```

this is just an emerge -e @system with 200 packages to go  and only 5 needed adding to package.env explicitly calling gcc.   Once this is done ill filter down the remaining

----------

