[v2] meson: add support for clang LTO

Message ID 20220401045435.60172-1-mahdi.rakhshandehroo@gmail.com (mailing list archive)
State Changes Requested, archived
Delegated to: Thomas Monjalon
Headers
Series [v2] meson: add support for clang LTO |

Checks

Context Check Description
ci/checkpatch warning coding style issues
ci/iol-testing fail build patch failure
ci/Intel-compilation fail Compilation issues

Commit Message

Mahdi Rakhshandehroo April 1, 2022, 4:54 a.m. UTC
  Currently, building DPDK with clang + LTO is unsupported, which is a
showstopper for cross-language interprocedural optimizations. The root
cause is that pmdinfogen expects to scan through the section headers of
generated ELF object files, but clang's "-flto" flag generates LLVM
bitcode instead. This patch enables LTO builds with clang, provided that
the linker is set to lld.

The mechanism of action is clang's "-fembed-bitcode" flag, which acts
similarly to gcc's "-ffat-lto-objects" - the compiler outputs a normal
ELF object file that keeps pmdinfogen happy, but also embeds IR bitcode
in a special section of the ELF that lld can then use to construct the
final binary.

Signed-off-by: Mahdi Rakhshandehroo <mahdi.rakhshandehroo@gmail.com>
---
Forgot to remove get_linker_id() check from the previous patch - that
function is only available in Meson >= 0.54.

 config/meson.build | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
  

Comments

Stephen Hemminger July 6, 2023, 2:55 a.m. UTC | #1
On Thu, 31 Mar 2022 21:54:35 -0700
Mahdi Rakhshandehroo <mahdi.rakhshandehroo@gmail.com> wrote:

> Currently, building DPDK with clang + LTO is unsupported, which is a
> showstopper for cross-language interprocedural optimizations. The root
> cause is that pmdinfogen expects to scan through the section headers of
> generated ELF object files, but clang's "-flto" flag generates LLVM
> bitcode instead. This patch enables LTO builds with clang, provided that
> the linker is set to lld.
> 
> The mechanism of action is clang's "-fembed-bitcode" flag, which acts
> similarly to gcc's "-ffat-lto-objects" - the compiler outputs a normal
> ELF object file that keeps pmdinfogen happy, but also embeds IR bitcode
> in a special section of the ELF that lld can then use to construct the
> final binary.
> 
> Signed-off-by: Mahdi Rakhshandehroo <mahdi.rakhshandehroo@gmail.com>
> ---

This patch still has several build failures such as Intel compiler.
Please resolve and resubmit
  

Patch

diff --git a/config/meson.build b/config/meson.build
index 7134e80..5425181 100644
--- a/config/meson.build
+++ b/config/meson.build
@@ -406,16 +406,21 @@  if is_windows
 endif
 
 if get_option('b_lto')
-    if cc.has_argument('-ffat-lto-objects')
+    if cc.get_id() == 'gcc' and cc.has_argument('-ffat-lto-objects')
         add_project_arguments('-ffat-lto-objects', language: 'c')
+
+        # workaround for gcc bug 81440
+        if cc.version().version_compare('<8.0')
+            add_project_arguments('-Wno-lto-type-mismatch', language: 'c')
+            add_project_link_arguments('-Wno-lto-type-mismatch', language: 'c')
+        endif
+    elif cc.get_id() == 'clang' and cc.has_argument('-fembed-bitcode')
+        # TODO: enforce use of lld using cc.get_linker_id() with Meson >= 0.54
+        add_project_arguments('-fno-lto', '-fembed-bitcode', language: 'c')
+        endif
     else
         error('compiler does not support fat LTO objects - please turn LTO off')
     endif
-    # workaround for gcc bug 81440
-    if cc.get_id() == 'gcc' and cc.version().version_compare('<8.0')
-        add_project_arguments('-Wno-lto-type-mismatch', language: 'c')
-        add_project_link_arguments('-Wno-lto-type-mismatch', language: 'c')
-    endif
 endif
 
 if get_option('b_sanitize') == 'address' or get_option('b_sanitize') == 'address,undefined'