[llvm] f565444 - [libLTO] Set data-sections by default in libLTO.

Quinn Pham via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 27 06:34:46 PDT 2022


Author: Quinn Pham
Date: 2022-07-27T08:34:40-05:00
New Revision: f565444b486d49f84297c3a279ca24d785961ea8

URL: https://github.com/llvm/llvm-project/commit/f565444b486d49f84297c3a279ca24d785961ea8
DIFF: https://github.com/llvm/llvm-project/commit/f565444b486d49f84297c3a279ca24d785961ea8.diff

LOG: [libLTO] Set data-sections by default in libLTO.

This patch changes legacy LTO to set data-sections by default. The user can
explicitly unset data-sections. The reason for this patch is to match the
behaviour of lld and gold plugin. Both lld and gold plugin have data-sections on
by default.

This patch also fixes the forwarding of the clang options -fno-data-sections and
-fno-function-sections to libLTO. Now, when -fno-data/function-sections are
specified in clang, -data/function-sections=0 will be passed to libLTO to
explicitly unset data/function-sections.

Reviewed By: w2yehia, MaskRay

Differential Revision: https://reviews.llvm.org/D129401

Added: 
    llvm/test/LTO/PowerPC/data-sections-aix.ll
    llvm/test/LTO/PowerPC/data-sections-linux.ll

Modified: 
    clang/lib/Driver/ToolChains/CommonArgs.cpp
    clang/test/Driver/function-sections.c
    llvm/lib/LTO/LTOCodeGenerator.cpp

Removed: 
    clang/test/Driver/gold-lto-sections.c


################################################################################
diff  --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 1d2c085d683e1..05afa712a809c 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -567,14 +567,16 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
       isUseSeparateSections(ToolChain.getEffectiveTriple());
 
   if (Args.hasFlag(options::OPT_ffunction_sections,
-                   options::OPT_fno_function_sections, UseSeparateSections)) {
-    CmdArgs.push_back("-plugin-opt=-function-sections");
-  }
+                   options::OPT_fno_function_sections, UseSeparateSections))
+    CmdArgs.push_back("-plugin-opt=-function-sections=1");
+  else if (Args.hasArg(options::OPT_fno_function_sections))
+    CmdArgs.push_back("-plugin-opt=-function-sections=0");
 
   if (Args.hasFlag(options::OPT_fdata_sections, options::OPT_fno_data_sections,
-                   UseSeparateSections)) {
-    CmdArgs.push_back("-plugin-opt=-data-sections");
-  }
+                   UseSeparateSections))
+    CmdArgs.push_back("-plugin-opt=-data-sections=1");
+  else if (Args.hasArg(options::OPT_fno_data_sections))
+    CmdArgs.push_back("-plugin-opt=-data-sections=0");
 
   // Pass an option to enable split machine functions.
   if (auto *A = Args.getLastArg(options::OPT_fsplit_machine_functions,

diff  --git a/clang/test/Driver/function-sections.c b/clang/test/Driver/function-sections.c
index bfb6cc6920bd4..4d206ad150291 100644
--- a/clang/test/Driver/function-sections.c
+++ b/clang/test/Driver/function-sections.c
@@ -6,6 +6,12 @@
 // CHECK-NODS-NOT: -fdata-sections
 // CHECK-US-NOT: -fno-unique-section-names
 // CHECK-NOUS: -fno-unique-section-names
+// CHECK-PLUGIN-DEFAULT-NOT: "-plugin-opt=-function-sections
+// CHECK-PLUGIN-DEFAULT-NOT: "-plugin-opt=-data-sections
+// CHECK-PLUGIN-SECTIONS: "-plugin-opt=-function-sections=1"
+// CHECK-PLUGIN-SECTIONS: "-plugin-opt=-data-sections=1"
+// CHECK-PLUGIN-NO-SECTIONS: "-plugin-opt=-function-sections=0"
+// CHECK-PLUGIN-NO-SECTIONS: "-plugin-opt=-data-sections=0"
 
 // RUN: %clang -### %s -fsyntax-only 2>&1       \
 // RUN:     --target=i386-unknown-linux \
@@ -72,3 +78,18 @@
 // RUN:     --target=i386-unknown-linux \
 // RUN:     -fno-unique-section-names \
 // RUN:   | FileCheck --check-prefix=CHECK-NOUS %s
+
+
+// RUN: %clang -### %s -flto 2>&1                \
+// RUN:     --target=x86_64-unknown-linux \
+// RUN:   | FileCheck --check-prefix=CHECK-PLUGIN-DEFAULT %s
+
+// RUN: %clang -### %s -flto 2>&1                \
+// RUN:     --target=x86_64-unknown-linux \
+// RUN:     -ffunction-sections -fdata-sections \
+// RUN:   | FileCheck --check-prefix=CHECK-PLUGIN-SECTIONS %s
+
+// RUN: %clang -### %s -flto 2>&1                \
+// RUN:     --target=x86_64-unknown-linux \
+// RUN:     -fno-function-sections -fno-data-sections \
+// RUN:   | FileCheck --check-prefix=CHECK-PLUGIN-NO-SECTIONS %s

diff  --git a/clang/test/Driver/gold-lto-sections.c b/clang/test/Driver/gold-lto-sections.c
deleted file mode 100644
index 83d72cf7f97b8..0000000000000
--- a/clang/test/Driver/gold-lto-sections.c
+++ /dev/null
@@ -1,8 +0,0 @@
-// RUN: touch %t.o
-//
-// RUN: %clang -target x86_64-unknown-linux -### %t.o -flto 2>&1 \
-// RUN:     -Wl,-plugin-opt=foo -O3 \
-// RUN:     -ffunction-sections -fdata-sections \
-// RUN:     | FileCheck %s
-// CHECK: "-plugin-opt=-function-sections"
-// CHECK: "-plugin-opt=-data-sections"

diff  --git a/llvm/lib/LTO/LTOCodeGenerator.cpp b/llvm/lib/LTO/LTOCodeGenerator.cpp
index 2f7c485b9fc8f..8c374e0f2f858 100644
--- a/llvm/lib/LTO/LTOCodeGenerator.cpp
+++ b/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,11 @@ bool LTOCodeGenerator::determineTarget() {
       Config.CPU = "cyclone";
   }
 
+  // If data-sections is not explicitly set or unset, set data-sections by
+  // default to match the behaviour of lld and gold plugin.
+  if (!codegen::getExplicitDataSections())
+    Config.Options.DataSections = true;
+
   TargetMach = createTargetMachine();
   assert(TargetMach && "Unable to create target machine");
 

diff  --git a/llvm/test/LTO/PowerPC/data-sections-aix.ll b/llvm/test/LTO/PowerPC/data-sections-aix.ll
new file mode 100644
index 0000000000000..98d7e094dd4a2
--- /dev/null
+++ b/llvm/test/LTO/PowerPC/data-sections-aix.ll
@@ -0,0 +1,20 @@
+; 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=1 %t/bc.bc -o \
+; RUN:   %t/data-sections.o
+; RUN: llvm-lto -exported-symbol var -O0 --data-sections=0 %t/bc.bc -o \
+; RUN:   %t/no-data-sections.o
+; RUN: llvm-objdump -t %t/default.o | FileCheck %s
+; RUN: llvm-objdump -t %t/data-sections.o | FileCheck %s
+; RUN: llvm-objdump -t %t/no-data-sections.o | FileCheck --check-prefix \
+; RUN:   CHECK-NO-DATA-SECTIONS %s
+
+target triple = "powerpc-ibm-aix7.2.0.0"
+
+ at var = global i32 0
+
+; CHECK-NOT:              00000000 g O .data (csect: .data) [[#%x,]] var
+
+; CHECK-NO-DATA-SECTIONS: 00000000 g O .data (csect: .data) [[#%x,]] var

diff  --git a/llvm/test/LTO/PowerPC/data-sections-linux.ll b/llvm/test/LTO/PowerPC/data-sections-linux.ll
new file mode 100644
index 0000000000000..2ece3c3c90fae
--- /dev/null
+++ b/llvm/test/LTO/PowerPC/data-sections-linux.ll
@@ -0,0 +1,20 @@
+; 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=1 %t/bc.bc -o \
+; RUN:   %t/data-sections.o
+; RUN: llvm-lto -exported-symbol var -O0 --data-sections=0 %t/bc.bc -o \
+; RUN:   %t/no-data-sections.o
+; RUN: llvm-objdump -t %t/default.o | FileCheck %s
+; RUN: llvm-objdump -t %t/data-sections.o | FileCheck %s
+; RUN: llvm-objdump -t %t/no-data-sections.o | FileCheck --check-prefix \
+; RUN:   CHECK-NO-DATA-SECTIONS %s
+
+target triple = "powerpc64le-unknown-linux-gnu"
+
+ at var = global i32 0
+
+; CHECK:                  0000000000000000 g O .bss.var [[#%x,]] var
+
+; CHECK-NO-DATA-SECTIONS: 0000000000000000 g O .bss [[#%x,]] var


        


More information about the llvm-commits mailing list