[llvm] r358150 - [RISCV] Put data smaller than eight bytes to small data section

Shiva Chen via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 10 21:59:13 PDT 2019


Author: shiva
Date: Wed Apr 10 21:59:13 2019
New Revision: 358150

URL: http://llvm.org/viewvc/llvm-project?rev=358150&view=rev
Log:
[RISCV] Put data smaller than eight bytes to small data section

Because of gp = sdata_start_address + 0x800, gp with signed twelve-bit offset
could covert most of the small data section. Linker relaxation could transfer
the multiple data accessing instructions to a gp base with signed twelve-bit
offset instruction.

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

Added:
    llvm/trunk/test/CodeGen/RISCV/sdata-limit-0.ll
    llvm/trunk/test/CodeGen/RISCV/sdata-limit-4.ll
    llvm/trunk/test/CodeGen/RISCV/sdata-limit-8.ll
    llvm/trunk/test/CodeGen/RISCV/sdata-local-sym.ll
Modified:
    llvm/trunk/include/llvm/Target/TargetLoweringObjectFile.h
    llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
    llvm/trunk/lib/Target/RISCV/RISCVTargetObjectFile.cpp
    llvm/trunk/lib/Target/RISCV/RISCVTargetObjectFile.h

Modified: llvm/trunk/include/llvm/Target/TargetLoweringObjectFile.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Target/TargetLoweringObjectFile.h?rev=358150&r1=358149&r2=358150&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Target/TargetLoweringObjectFile.h (original)
+++ llvm/trunk/include/llvm/Target/TargetLoweringObjectFile.h Wed Apr 10 21:59:13 2019
@@ -79,6 +79,9 @@ public:
   /// Emit the module-level metadata that the platform cares about.
   virtual void emitModuleMetadata(MCStreamer &Streamer, Module &M) const {}
 
+  /// Get the module-level metadata that the platform cares about.
+  virtual void getModuleMetadata(Module &M) {}
+
   /// Given a constant with the SectionKind, return a section that it should be
   /// placed in.
   virtual MCSection *getSectionForConstant(const DataLayout &DL,

Modified: llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp?rev=358150&r1=358149&r2=358150&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp (original)
+++ llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp Wed Apr 10 21:59:13 2019
@@ -259,6 +259,9 @@ bool AsmPrinter::doInitialization(Module
   const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
     .Initialize(OutContext, TM);
 
+  const_cast<TargetLoweringObjectFile &>(getObjFileLowering())
+      .getModuleMetadata(M);
+
   OutStreamer->InitSections(false);
 
   // Emit the version-min deployment target directive if needed.

Modified: llvm/trunk/lib/Target/RISCV/RISCVTargetObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/RISCV/RISCVTargetObjectFile.cpp?rev=358150&r1=358149&r2=358150&view=diff
==============================================================================
--- llvm/trunk/lib/Target/RISCV/RISCVTargetObjectFile.cpp (original)
+++ llvm/trunk/lib/Target/RISCV/RISCVTargetObjectFile.cpp Wed Apr 10 21:59:13 2019
@@ -8,6 +8,9 @@
 
 #include "RISCVTargetObjectFile.h"
 #include "RISCVTargetMachine.h"
+#include "llvm/BinaryFormat/ELF.h"
+#include "llvm/MC/MCContext.h"
+#include "llvm/MC/MCSectionELF.h"
 
 using namespace llvm;
 
@@ -15,4 +18,97 @@ void RISCVELFTargetObjectFile::Initializ
                                           const TargetMachine &TM) {
   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
   InitializeELF(TM.Options.UseInitArray);
+
+  SmallDataSection = getContext().getELFSection(
+      ".sdata", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
+  SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
+                                               ELF::SHF_WRITE | ELF::SHF_ALLOC);
+}
+
+// A address must be loaded from a small section if its size is less than the
+// small section size threshold. Data in this section could be addressed by
+// using gp_rel operator.
+bool RISCVELFTargetObjectFile::isInSmallSection(uint64_t Size) const {
+  // gcc has traditionally not treated zero-sized objects as small data, so this
+  // is effectively part of the ABI.
+  return Size > 0 && Size <= SSThreshold;
+}
+
+// Return true if this global address should be placed into small data/bss
+// section.
+bool RISCVELFTargetObjectFile::isGlobalInSmallSection(
+    const GlobalObject *GO, const TargetMachine &TM) const {
+  // Only global variables, not functions.
+  const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO);
+  if (!GVA)
+    return false;
+
+  // If the variable has an explicit section, it is placed in that section.
+  if (GVA->hasSection()) {
+    StringRef Section = GVA->getSection();
+
+    // Explicitly placing any variable in the small data section overrides
+    // the global -G value.
+    if (Section == ".sdata" || Section == ".sbss")
+      return true;
+
+    // Otherwise reject putting the variable to small section if it has an
+    // explicit section name.
+    return false;
+  }
+
+  if (((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
+       GVA->hasCommonLinkage()))
+    return false;
+
+  Type *Ty = GVA->getValueType();
+  // It is possible that the type of the global is unsized, i.e. a declaration
+  // of a extern struct. In this case don't presume it is in the small data
+  // section. This happens e.g. when building the FreeBSD kernel.
+  if (!Ty->isSized())
+    return false;
+
+  return isInSmallSection(
+      GVA->getParent()->getDataLayout().getTypeAllocSize(Ty));
+}
+
+MCSection *RISCVELFTargetObjectFile::SelectSectionForGlobal(
+    const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
+  // Handle Small Section classification here.
+  if (Kind.isBSS() && isGlobalInSmallSection(GO, TM))
+    return SmallBSSSection;
+  if (Kind.isData() && isGlobalInSmallSection(GO, TM))
+    return SmallDataSection;
+
+  // Otherwise, we work the same as ELF.
+  return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
+}
+
+void RISCVELFTargetObjectFile::getModuleMetadata(Module &M) {
+  SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
+  M.getModuleFlagsMetadata(ModuleFlags);
+
+  for (const auto &MFE : ModuleFlags) {
+    StringRef Key = MFE.Key->getString();
+    if (Key == "SmallDataLimit") {
+      SSThreshold = mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue();
+      break;
+    }
+  }
+}
+
+/// Return true if this constant should be placed into small data section.
+bool RISCVELFTargetObjectFile::isConstantInSmallSection(
+    const DataLayout &DL, const Constant *CN) const {
+  return isInSmallSection(DL.getTypeAllocSize(CN->getType()));
+}
+
+MCSection *RISCVELFTargetObjectFile::getSectionForConstant(
+    const DataLayout &DL, SectionKind Kind, const Constant *C,
+    unsigned &Align) const {
+  if (isConstantInSmallSection(DL, C))
+    return SmallDataSection;
+
+  // Otherwise, we work the same as ELF.
+  return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C, Align);
 }

Modified: llvm/trunk/lib/Target/RISCV/RISCVTargetObjectFile.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/RISCV/RISCVTargetObjectFile.h?rev=358150&r1=358149&r2=358150&view=diff
==============================================================================
--- llvm/trunk/lib/Target/RISCV/RISCVTargetObjectFile.h (original)
+++ llvm/trunk/lib/Target/RISCV/RISCVTargetObjectFile.h Wed Apr 10 21:59:13 2019
@@ -16,7 +16,31 @@ class RISCVTargetMachine;
 
 /// This implementation is used for RISCV ELF targets.
 class RISCVELFTargetObjectFile : public TargetLoweringObjectFileELF {
+  MCSection *SmallDataSection;
+  MCSection *SmallBSSSection;
+  unsigned SSThreshold = 8;
+
+public:
   void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
+
+  /// Return true if this global address should be placed into small data/bss
+  /// section.
+  bool isGlobalInSmallSection(const GlobalObject *GO,
+                              const TargetMachine &TM) const;
+
+  MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
+                                    const TargetMachine &TM) const override;
+
+  /// Return true if this constant should be placed into small data section.
+  bool isConstantInSmallSection(const DataLayout &DL, const Constant *CN) const;
+
+  MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
+                                   const Constant *C,
+                                   unsigned &Align) const override;
+
+  void getModuleMetadata(Module &M) override;
+
+  bool isInSmallSection(uint64_t Size) const;
 };
 
 } // end namespace llvm

Added: llvm/trunk/test/CodeGen/RISCV/sdata-limit-0.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/RISCV/sdata-limit-0.ll?rev=358150&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/RISCV/sdata-limit-0.ll (added)
+++ llvm/trunk/test/CodeGen/RISCV/sdata-limit-0.ll Wed Apr 10 21:59:13 2019
@@ -0,0 +1,14 @@
+; RUN: llc -mtriple=riscv32 < %s | FileCheck -check-prefix=RV32 %s
+; RUN: llc -mtriple=riscv64 < %s | FileCheck -check-prefix=RV64 %s
+
+ at v = dso_local global i32 0, align 4
+ at r = dso_local global i64 7, align 8
+
+; SmallDataLimit set to 0, so we expect no data will put in sbss and sdata.
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"SmallDataLimit", i32 0}
+
+; RV32-NOT:    .section        .sbss
+; RV32-NOT:    .section        .sdata
+; RV64-NOT:    .section        .sbss
+; RV64-NOT:    .section        .sdata

Added: llvm/trunk/test/CodeGen/RISCV/sdata-limit-4.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/RISCV/sdata-limit-4.ll?rev=358150&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/RISCV/sdata-limit-4.ll (added)
+++ llvm/trunk/test/CodeGen/RISCV/sdata-limit-4.ll Wed Apr 10 21:59:13 2019
@@ -0,0 +1,15 @@
+; RUN: llc -mtriple=riscv32 < %s | FileCheck -check-prefix=RV32 %s
+; RUN: llc -mtriple=riscv64 < %s | FileCheck -check-prefix=RV64 %s
+
+ at v = dso_local global i32 0, align 4
+ at r = dso_local global i64 7, align 8
+
+; SmallDataLimit set to 4, so we expect @v will be put in sbss,
+; but @r won't be put in sdata.
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"SmallDataLimit", i32 4}
+
+; RV32:    .section        .sbss
+; RV32-NOT:    .section        .sdata
+; RV64:    .section        .sbss
+; RV64-NOT:    .section        .sdata

Added: llvm/trunk/test/CodeGen/RISCV/sdata-limit-8.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/RISCV/sdata-limit-8.ll?rev=358150&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/RISCV/sdata-limit-8.ll (added)
+++ llvm/trunk/test/CodeGen/RISCV/sdata-limit-8.ll Wed Apr 10 21:59:13 2019
@@ -0,0 +1,15 @@
+; RUN: llc -mtriple=riscv32 < %s | FileCheck -check-prefix=RV32 %s
+; RUN: llc -mtriple=riscv64 < %s | FileCheck -check-prefix=RV64 %s
+
+ at v = dso_local global i32 0, align 4
+ at r = dso_local global i64 7, align 8
+
+; SmallDataLimit set to 8, so we expect @v will be put in sbss
+; and @r will be put in sdata.
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"SmallDataLimit", i32 8}
+
+; RV32:    .section        .sbss
+; RV32:    .section        .sdata
+; RV64:    .section        .sbss
+; RV64:    .section        .sdata

Added: llvm/trunk/test/CodeGen/RISCV/sdata-local-sym.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/RISCV/sdata-local-sym.ll?rev=358150&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/RISCV/sdata-local-sym.ll (added)
+++ llvm/trunk/test/CodeGen/RISCV/sdata-local-sym.ll Wed Apr 10 21:59:13 2019
@@ -0,0 +1,16 @@
+; RUN: llc -mtriple=riscv32 < %s | FileCheck -check-prefix=RV32 %s
+; RUN: llc -mtriple=riscv64 < %s | FileCheck -check-prefix=RV64 %s
+
+ at v = internal global i32 0, align 4
+ at r = internal global i64 7, align 8
+
+; @v and @r are local symbols.
+; SmallDataLimit set to 8, so we expect @v will be put in sbss
+; and @r will be put in sdata.
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"SmallDataLimit", i32 8}
+
+; RV32:    .section        .sbss
+; RV32:    .section        .sdata
+; RV64:    .section        .sbss
+; RV64:    .section        .sdata




More information about the llvm-commits mailing list