[PATCH] D129401: [libLTO] Set data-sections by default in libLTO for ELF and XCOFF.

Quinn Pham via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 8 15:00:30 PDT 2022


quinnp created this revision.
Herald added subscribers: ormris, steven_wu, hiraditya, inglorion, nemanjai.
Herald added a project: All.
quinnp requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This patch changes libLTO to set data-sections by default for ELF and XCOFF.
The user can explicitly unset data-sections. The reason for this patch is to
match the behaviour of lld and gold plugin. Specifically, lld forces
data-sections to be set for ELF, and gold plugin has data-sections set by
default.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D129401

Files:
  llvm/lib/LTO/LTOCodeGenerator.cpp
  llvm/test/LTO/PowerPC/data-sections-aix.ll
  llvm/test/LTO/PowerPC/data-sections-linux.ll


Index: llvm/test/LTO/PowerPC/data-sections-linux.ll
===================================================================
--- /dev/null
+++ llvm/test/LTO/PowerPC/data-sections-linux.ll
@@ -0,0 +1,26 @@
+; RUN: rm -rf %t
+; RUN: mkdir %t
+; RUN: llvm-as %s -o %t/bc.bc
+; RUN: llvm-lto -exported-symbol var -O0 %t/bc.bc -o %t/default.o
+; RUN: llvm-lto -exported-symbol var -O0 --data-sections=true %t/bc.bc -o \
+; RUN:   %t/data-sections.o
+; RUN: llvm-lto -exported-symbol var -O0 --data-sections=false %t/bc.bc -o \
+; RUN:   %t/no-data-sections.o
+; RUN: obj2yaml %t/default.o | FileCheck --match-full-lines %s
+; RUN: obj2yaml %t/data-sections.o | FileCheck --match-full-lines %s
+; RUN: obj2yaml %t/no-data-sections.o | FileCheck --match-full-lines \
+; RUN:   --check-prefix CHECK-NO-DATA-SECTIONS %s
+
+target triple = "powerpc64le-unknown-linux-gnu"
+
+ at var = global i32 0
+
+; CHECK:                       Symbols:
+; CHECK:                         - Name:    var
+; CHECK-NEXT:                      Type:    STT_OBJECT
+; CHECK-NEXT:                      Section: .bss.var
+
+; CHECK-NO-DATA-SECTIONS:      Symbols:
+; CHECK-NO-DATA-SECTIONS:        - Name:    var
+; CHECK-NO-DATA-SECTIONS-NEXT:     Type:    STT_OBJECT
+; CHECK-NO-DATA-SECTIONS-NEXT:     Section: .bss
Index: llvm/test/LTO/PowerPC/data-sections-aix.ll
===================================================================
--- /dev/null
+++ llvm/test/LTO/PowerPC/data-sections-aix.ll
@@ -0,0 +1,22 @@
+; RUN: rm -rf %t
+; RUN: mkdir %t
+; RUN: llvm-as %s -o %t/bc.bc
+; RUN: llvm-lto -exported-symbol var -O0 %t/bc.bc -o %t/default.o
+; RUN: llvm-lto -exported-symbol var -O0 --data-sections=true %t/bc.bc -o \
+; RUN:   %t/data-sections.o
+; RUN: llvm-lto -exported-symbol var -O0 --data-sections=false %t/bc.bc -o \
+; RUN:   %t/no-data-sections.o
+; RUN: obj2yaml %t/default.o | FileCheck --match-full-lines %s
+; RUN: obj2yaml %t/data-sections.o | FileCheck --match-full-lines %s
+; RUN: obj2yaml %t/no-data-sections.o | FileCheck --match-full-lines \
+; RUN:   --check-prefix CHECK-NO-DATA-SECTIONS %s
+
+target triple = "powerpc-ibm-aix7.2.0.0"
+
+ at var = global i32 0
+
+; CHECK:                  Symbols:
+; CHECK-NOT:                - Name: .data
+
+; CHECK-NO-DATA-SECTIONS: Symbols:
+; CHECK-NO-DATA-SECTIONS:   - Name: .data
Index: llvm/lib/LTO/LTOCodeGenerator.cpp
===================================================================
--- llvm/lib/LTO/LTOCodeGenerator.cpp
+++ llvm/lib/LTO/LTOCodeGenerator.cpp
@@ -19,6 +19,7 @@
 #include "llvm/Analysis/TargetLibraryInfo.h"
 #include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/Bitcode/BitcodeWriter.h"
+#include "llvm/CodeGen/CommandFlags.h"
 #include "llvm/CodeGen/ParallelCG.h"
 #include "llvm/CodeGen/TargetSubtargetInfo.h"
 #include "llvm/Config/config.h"
@@ -344,6 +345,14 @@
       Config.CPU = "cyclone";
   }
 
+  // If data-sections is not explicitly set or unset, set data-sections by
+  // default for ELF and XCOFF to match the behaviour of lld and gold plugin.
+  llvm::Triple::ObjectFormatType ObjectFormat = Triple.getObjectFormat();
+  if (!codegen::getExplicitDataSections() &&
+      (ObjectFormat == llvm::Triple::ObjectFormatType::ELF ||
+       ObjectFormat == llvm::Triple::ObjectFormatType::XCOFF))
+    Config.Options.DataSections = true;
+
   TargetMach = createTargetMachine();
   assert(TargetMach && "Unable to create target machine");
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129401.443364.patch
Type: text/x-patch
Size: 3413 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220708/079d2324/attachment.bin>


More information about the llvm-commits mailing list