[llvm] r369869 - [PowerPC][AIX] Adds support for writing the .data section in assembly files

Xing Xue via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 25 08:17:25 PDT 2019


Author: xingxue
Date: Sun Aug 25 08:17:25 2019
New Revision: 369869

URL: http://llvm.org/viewvc/llvm-project?rev=369869&view=rev
Log:
[PowerPC][AIX] Adds support for writing the .data section in assembly files

Summary:
Adds support for generating the .data section in assembly files for global variables with a non-zero initialization. The support for writing the .data section in XCOFF object files will be added in a follow-on patch. Any relocations are not included in this patch.

Reviewers: hubert.reinterpretcast, sfertile, jasonliu, daltenty, Xiangling_L

Reviewed by: hubert.reinterpretcast

Subscribers: nemanjai, hiraditya, kbarton, MaskRay, jsji, wuzish, shchenz, DiggerLin, llvm-commits

Tags: #llvm

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

Added:
    llvm/trunk/test/CodeGen/PowerPC/aix-xcoff-data.ll
Modified:
    llvm/trunk/include/llvm/CodeGen/AsmPrinter.h
    llvm/trunk/include/llvm/MC/MCAsmInfo.h
    llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
    llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
    llvm/trunk/lib/MC/MCAsmInfoXCOFF.cpp
    llvm/trunk/lib/MC/MCAsmStreamer.cpp
    llvm/trunk/lib/MC/MCObjectFileInfo.cpp
    llvm/trunk/lib/MC/MCSectionXCOFF.cpp
    llvm/trunk/lib/Target/PowerPC/PPCAsmPrinter.cpp

Modified: llvm/trunk/include/llvm/CodeGen/AsmPrinter.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/AsmPrinter.h?rev=369869&r1=369868&r2=369869&view=diff
==============================================================================
--- llvm/trunk/include/llvm/CodeGen/AsmPrinter.h (original)
+++ llvm/trunk/include/llvm/CodeGen/AsmPrinter.h Sun Aug 25 08:17:25 2019
@@ -635,6 +635,11 @@ public:
   /// supported by the target.
   void EmitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const;
 
+  /// Return the alignment in log2 form for the specified \p GV.
+  static unsigned getGVAlignmentLog2(const GlobalValue *GV,
+                                     const DataLayout &DL,
+                                     unsigned InBits = 0);
+
 private:
   /// Private state for PrintSpecial()
   // Assign a unique ID to this machine instruction.

Modified: llvm/trunk/include/llvm/MC/MCAsmInfo.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/MC/MCAsmInfo.h?rev=369869&r1=369868&r2=369869&view=diff
==============================================================================
--- llvm/trunk/include/llvm/MC/MCAsmInfo.h (original)
+++ llvm/trunk/include/llvm/MC/MCAsmInfo.h Sun Aug 25 08:17:25 2019
@@ -165,6 +165,10 @@ protected:
   /// instead.
   bool UseDataRegionDirectives = false;
 
+  /// True if .align is to be used for alignment. Only power-of-two
+  /// alignment is supported.
+  bool UseDotAlignForAlignment = false;
+
   //===--- Data Emission Directives -------------------------------------===//
 
   /// This should be set to the directive used to get some number of zero bytes
@@ -520,6 +524,10 @@ public:
     return UseDataRegionDirectives;
   }
 
+  bool useDotAlignForAlignment() const {
+    return UseDotAlignForAlignment;
+  }
+
   const char *getZeroDirective() const { return ZeroDirective; }
   const char *getAsciiDirective() const { return AsciiDirective; }
   const char *getAscizDirective() const { return AscizDirective; }

Modified: llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp?rev=369869&r1=369868&r2=369869&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp (original)
+++ llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp Sun Aug 25 08:17:25 2019
@@ -162,8 +162,9 @@ static gcp_map_type &getGCMap(void *&P)
 /// getGVAlignmentLog2 - Return the alignment to use for the specified global
 /// value in log2 form.  This rounds up to the preferred alignment if possible
 /// and legal.
-static unsigned getGVAlignmentLog2(const GlobalValue *GV, const DataLayout &DL,
-                                   unsigned InBits = 0) {
+unsigned AsmPrinter::getGVAlignmentLog2(const GlobalValue *GV,
+                                        const DataLayout &DL,
+                                        unsigned InBits) {
   unsigned NumBits = 0;
   if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
     NumBits = DL.getPreferredAlignmentLog(GVar);

Modified: llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp?rev=369869&r1=369868&r2=369869&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp (original)
+++ llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp Sun Aug 25 08:17:25 2019
@@ -1849,6 +1849,9 @@ MCSection *TargetLoweringObjectFileXCOFF
   if (Kind.isText())
     return TextSection;
 
+  if (Kind.isData())
+    return DataSection;
+
   report_fatal_error("XCOFF other section types not yet implemented.");
 }
 

Modified: llvm/trunk/lib/MC/MCAsmInfoXCOFF.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCAsmInfoXCOFF.cpp?rev=369869&r1=369868&r2=369869&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCAsmInfoXCOFF.cpp (original)
+++ llvm/trunk/lib/MC/MCAsmInfoXCOFF.cpp Sun Aug 25 08:17:25 2019
@@ -17,4 +17,8 @@ MCAsmInfoXCOFF::MCAsmInfoXCOFF() {
   HasDotTypeDotSizeDirective = false;
   COMMDirectiveAlignmentIsInBytes = false;
   LCOMMDirectiveAlignmentType = LCOMM::Log2Alignment;
+  UseDotAlignForAlignment = true;
+  AsciiDirective = nullptr; // not supported
+  AscizDirective = nullptr; // not supported
+  Data64bitsDirective = "\t.llong\t";
 }

Modified: llvm/trunk/lib/MC/MCAsmStreamer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCAsmStreamer.cpp?rev=369869&r1=369868&r2=369869&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCAsmStreamer.cpp (original)
+++ llvm/trunk/lib/MC/MCAsmStreamer.cpp Sun Aug 25 08:17:25 2019
@@ -1119,6 +1119,16 @@ void MCAsmStreamer::emitFill(const MCExp
 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
                                          unsigned ValueSize,
                                          unsigned MaxBytesToEmit) {
+  if (MAI->useDotAlignForAlignment()) {
+    if (!isPowerOf2_32(ByteAlignment))
+      report_fatal_error("Only power-of-two alignments are supported "
+                         "with .align.");
+    OS << "\t.align\t";
+    OS << Log2_32(ByteAlignment);
+    EmitEOL();
+    return;
+  }
+
   // Some assemblers don't support non-power of two alignments, so we always
   // emit alignments as a power of two if possible.
   if (isPowerOf2_32(ByteAlignment)) {

Modified: llvm/trunk/lib/MC/MCObjectFileInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCObjectFileInfo.cpp?rev=369869&r1=369868&r2=369869&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCObjectFileInfo.cpp (original)
+++ llvm/trunk/lib/MC/MCObjectFileInfo.cpp Sun Aug 25 08:17:25 2019
@@ -770,6 +770,10 @@ void MCObjectFileInfo::initXCOFFMCObject
   TextSection = Ctx->getXCOFFSection(
       ".text", XCOFF::StorageMappingClass::XMC_PR, XCOFF::XTY_SD,
       XCOFF::C_HIDEXT, SectionKind::getText());
+
+  DataSection = Ctx->getXCOFFSection(
+      ".data", XCOFF::StorageMappingClass::XMC_RW, XCOFF::XTY_SD,
+      XCOFF::C_HIDEXT, SectionKind::getData());
 }
 
 void MCObjectFileInfo::InitMCObjectFileInfo(const Triple &TheTriple, bool PIC,

Modified: llvm/trunk/lib/MC/MCSectionXCOFF.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCSectionXCOFF.cpp?rev=369869&r1=369868&r2=369869&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCSectionXCOFF.cpp (original)
+++ llvm/trunk/lib/MC/MCSectionXCOFF.cpp Sun Aug 25 08:17:25 2019
@@ -28,6 +28,16 @@ void MCSectionXCOFF::PrintSwitchToSectio
     return;
   }
 
+  if (getKind().isData()) {
+    assert(getMappingClass() == XCOFF::XMC_RW &&
+           "Unhandled storage-mapping class for data section.");
+
+    OS << "\t.csect " << getSectionName() << "["
+       << "RW"
+       << "]" << '\n';
+    return;
+  }
+
   if (getKind().isBSSLocal() || getKind().isCommon()) {
     assert((getMappingClass() == XCOFF::XMC_RW ||
             getMappingClass() == XCOFF::XMC_BS) &&

Modified: llvm/trunk/lib/Target/PowerPC/PPCAsmPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/PowerPC/PPCAsmPrinter.cpp?rev=369869&r1=369868&r2=369869&view=diff
==============================================================================
--- llvm/trunk/lib/Target/PowerPC/PPCAsmPrinter.cpp (original)
+++ llvm/trunk/lib/Target/PowerPC/PPCAsmPrinter.cpp Sun Aug 25 08:17:25 2019
@@ -1659,8 +1659,9 @@ void PPCAIXAsmPrinter::EmitGlobalVariabl
     report_fatal_error("COMDAT not yet supported by AIX.");
 
   SectionKind GVKind = getObjFileLowering().getKindForGlobal(GV, TM);
-  if (!GVKind.isCommon() && !GVKind.isBSSLocal())
-    report_fatal_error("Only common variables are supported on AIX for now.");
+  if (!GVKind.isCommon() && !GVKind.isBSSLocal() && !GVKind.isData())
+    report_fatal_error("Encountered a global variable kind that is "
+                       "not supported yet.");
 
   // Create the containing csect and switch to it.
   MCSectionXCOFF *CSect = cast<MCSectionXCOFF>(
@@ -1668,20 +1669,34 @@ void PPCAIXAsmPrinter::EmitGlobalVariabl
   OutStreamer->SwitchSection(CSect);
 
   // Create the symbol, set its storage class, and emit it.
-  MCSymbolXCOFF *XSym = cast<MCSymbolXCOFF>(getSymbol(GV));
-  XSym->setStorageClass(
+  MCSymbolXCOFF *GVSym = cast<MCSymbolXCOFF>(getSymbol(GV));
+  GVSym->setStorageClass(
       TargetLoweringObjectFileXCOFF::getStorageClassForGlobal(GV));
-  XSym->setContainingCsect(CSect);
+  GVSym->setContainingCsect(CSect);
 
   const DataLayout &DL = GV->getParent()->getDataLayout();
-  unsigned Align =
+
+  // Handle common symbols.
+  if (GVKind.isCommon() || GVKind.isBSSLocal()) {
+    unsigned Align =
       GV->getAlignment() ? GV->getAlignment() : DL.getPreferredAlignment(GV);
-  uint64_t Size = DL.getTypeAllocSize(GV->getType()->getElementType());
+    uint64_t Size = DL.getTypeAllocSize(GV->getType()->getElementType());
 
-  if (GVKind.isBSSLocal())
-    OutStreamer->EmitXCOFFLocalCommonSymbol(XSym, Size, Align);
-  else
-    OutStreamer->EmitCommonSymbol(XSym, Size, Align);
+    if (GVKind.isBSSLocal())
+      OutStreamer->EmitXCOFFLocalCommonSymbol(GVSym, Size, Align);
+    else
+      OutStreamer->EmitCommonSymbol(GVSym, Size, Align);
+    return;
+  }
+
+  // Get the alignment in the log2 form.
+  const unsigned AlignLog = getGVAlignmentLog2(GV, DL);
+
+  MCSymbol *EmittedInitSym = GVSym;
+  EmitLinkage(GV, EmittedInitSym);
+  EmitAlignment(AlignLog, GV);
+  OutStreamer->EmitLabel(EmittedInitSym);
+  EmitGlobalConstant(GV->getParent()->getDataLayout(), GV->getInitializer());
 }
 
 /// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code

Added: llvm/trunk/test/CodeGen/PowerPC/aix-xcoff-data.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/PowerPC/aix-xcoff-data.ll?rev=369869&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/PowerPC/aix-xcoff-data.ll (added)
+++ llvm/trunk/test/CodeGen/PowerPC/aix-xcoff-data.ll Sun Aug 25 08:17:25 2019
@@ -0,0 +1,57 @@
+; RUN: llc -mtriple powerpc-ibm-aix-xcoff < %s | FileCheck %s
+; RUN: llc -mtriple powerpc64-ibm-aix-xcoff < %s | FileCheck %s
+
+ at ivar = local_unnamed_addr global i32 35, align 4
+ at llvar = local_unnamed_addr global i64 36, align 8
+ at svar = local_unnamed_addr global i16 37, align 2
+ at fvar = local_unnamed_addr global float 8.000000e+02, align 4
+ at dvar = local_unnamed_addr global double 9.000000e+02, align 8
+ at over_aligned = local_unnamed_addr global double 9.000000e+02, align 32
+ at charr = local_unnamed_addr global [4 x i8] c"abcd", align 1
+ at dblarr = local_unnamed_addr global [4 x double] [double 1.000000e+00, double 2.000000e+00, double 3.000000e+00, double 4.000000e+00], align 8
+
+; CHECK:      .csect .data[RW]
+; CHECK-NEXT: .globl  ivar
+; CHECK-NEXT: .align  2
+; CHECK-NEXT: ivar:
+; CHECK-NEXT: .long   35
+
+; CHECK:      .globl  llvar
+; CHECK-NEXT: .align  3
+; CHECK-NEXT: llvar:
+; CHECK-NEXT: .llong  36
+
+; CHECK:      .globl  svar
+; CHECK-NEXT: .align  1
+; CHECK-NEXT: svar:
+; CHECK-NEXT: .short  37
+
+; CHECK:      .globl  fvar
+; CHECK-NEXT: .align  2
+; CHECK-NEXT: fvar:
+; CHECK-NEXT: .long   1145569280
+
+; CHECK:      .globl  dvar
+; CHECK-NEXT: .align  3
+; CHECK-NEXT: dvar:
+; CHECK-NEXT: .llong  4651127699538968576
+
+; CHECK:      .globl  over_aligned
+; CHECK-NEXT: .align  5
+; CHECK-NEXT: over_aligned:
+; CHECK-NEXT: .llong  4651127699538968576
+
+; CHECK:      .globl  charr
+; CHECK-NEXT: charr:
+; CHECK-NEXT: .byte   97
+; CHECK-NEXT: .byte   98
+; CHECK-NEXT: .byte   99
+; CHECK-NEXT: .byte   100
+
+; CHECK:      .globl  dblarr
+; CHECK-NEXT: .align  3
+; CHECK-NEXT: dblarr:
+; CHECK-NEXT: .llong  4607182418800017408
+; CHECK-NEXT: .llong  4611686018427387904
+; CHECK-NEXT: .llong  4613937818241073152
+; CHECK-NEXT: .llong  4616189618054758400




More information about the llvm-commits mailing list