[llvm] r366727 - Stubs out TLOF for AIX and add support for common vars in assembly output.

Sean Fertile via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 22 12:15:29 PDT 2019


Author: sfertile
Date: Mon Jul 22 12:15:29 2019
New Revision: 366727

URL: http://llvm.org/viewvc/llvm-project?rev=366727&view=rev
Log:
Stubs out TLOF for AIX and add support for common vars in assembly output.

Stubs out a TargetLoweringObjectFileXCOFF class, implementing only
SelectSectionForGlobal for common symbols. Also adds an override of
EmitGlobalVariable in PPCAIXAsmPrinter which adds a number of defensive errors
and adds support for emitting common globals.

Added:
    llvm/trunk/test/CodeGen/PowerPC/aix-xcoff-common.ll
Modified:
    llvm/trunk/include/llvm/BinaryFormat/XCOFF.h
    llvm/trunk/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
    llvm/trunk/include/llvm/MC/MCContext.h
    llvm/trunk/include/llvm/MC/MCSectionXCOFF.h
    llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
    llvm/trunk/lib/MC/MCAsmInfoXCOFF.cpp
    llvm/trunk/lib/MC/MCContext.cpp
    llvm/trunk/lib/MC/MCObjectFileInfo.cpp
    llvm/trunk/lib/MC/MCSectionXCOFF.cpp
    llvm/trunk/lib/Target/PowerPC/PPCAsmPrinter.cpp
    llvm/trunk/lib/Target/PowerPC/PPCTargetMachine.cpp

Modified: llvm/trunk/include/llvm/BinaryFormat/XCOFF.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/BinaryFormat/XCOFF.h?rev=366727&r1=366726&r2=366727&view=diff
==============================================================================
--- llvm/trunk/include/llvm/BinaryFormat/XCOFF.h (original)
+++ llvm/trunk/include/llvm/BinaryFormat/XCOFF.h Mon Jul 22 12:15:29 2019
@@ -139,6 +139,14 @@ enum StorageClass : uint8_t {
   C_TCSYM = 134 // Reserved
 };
 
+enum SymbolType {
+  XTY_ER = 0, ///< External reference.
+  XTY_SD = 1, ///< Csect definition for initialized storage.
+  XTY_LD = 2, ///< Label definition.
+              ///< Defines an entry point to an initialized csect.
+  XTY_CM = 3  ///< Common csect definition. For uninitialized storage.
+};
+
 } // end namespace XCOFF
 } // end namespace llvm
 

Modified: llvm/trunk/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h?rev=366727&r1=366726&r2=366727&view=diff
==============================================================================
--- llvm/trunk/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h (original)
+++ llvm/trunk/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h Mon Jul 22 12:15:29 2019
@@ -206,6 +206,32 @@ public:
                                        const TargetMachine &TM) const override;
 };
 
+class TargetLoweringObjectFileXCOFF : public TargetLoweringObjectFile {
+public:
+  TargetLoweringObjectFileXCOFF() = default;
+  ~TargetLoweringObjectFileXCOFF() override = default;
+
+  MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
+                                      const TargetMachine &TM) const override;
+
+  MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
+                                    const TargetMachine &TM) const override;
+
+  bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
+                                           const Function &F) const override;
+
+  void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
+
+  MCSection *getStaticCtorSection(unsigned Priority,
+                                  const MCSymbol *KeySym) const override;
+  MCSection *getStaticDtorSection(unsigned Priority,
+                                  const MCSymbol *KeySym) const override;
+
+  const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
+                                       const GlobalValue *RHS,
+                                       const TargetMachine &TM) const override;
+};
+
 } // end namespace llvm
 
 #endif // LLVM_CODEGEN_TARGETLOWERINGOBJECTFILEIMPL_H

Modified: llvm/trunk/include/llvm/MC/MCContext.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/MC/MCContext.h?rev=366727&r1=366726&r2=366727&view=diff
==============================================================================
--- llvm/trunk/include/llvm/MC/MCContext.h (original)
+++ llvm/trunk/include/llvm/MC/MCContext.h Mon Jul 22 12:15:29 2019
@@ -490,7 +490,7 @@ namespace llvm {
 
     MCSectionXCOFF *getXCOFFSection(StringRef Section,
                                     XCOFF::StorageMappingClass MappingClass,
-                                    SectionKind K,
+                                    XCOFF::SymbolType CSectType, SectionKind K,
                                     const char *BeginSymName = nullptr);
 
     // Create and save a copy of STI and return a reference to the copy.

Modified: llvm/trunk/include/llvm/MC/MCSectionXCOFF.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/MC/MCSectionXCOFF.h?rev=366727&r1=366726&r2=366727&view=diff
==============================================================================
--- llvm/trunk/include/llvm/MC/MCSectionXCOFF.h (original)
+++ llvm/trunk/include/llvm/MC/MCSectionXCOFF.h Mon Jul 22 12:15:29 2019
@@ -23,16 +23,25 @@ class MCSymbol;
 
 // This class represents an XCOFF `Control Section`, more commonly referred to
 // as a csect. A csect represents the smallest possible unit of data/code which
-// will be relocated as a single block.
+// will be relocated as a single block. A csect can either be:
+// 1) Initialized: The Type will be XTY_SD, and the symbols inside the csect
+//    will have a label definition representing their offset within the csect.
+// 2) Uninitialized: The Type will be XTY_CM, it will contain a single symbol,
+//    and may not contain label definitions.
 class MCSectionXCOFF final : public MCSection {
   friend class MCContext;
 
   StringRef Name;
   XCOFF::StorageMappingClass MappingClass;
+  XCOFF::SymbolType Type;
 
   MCSectionXCOFF(StringRef Section, XCOFF::StorageMappingClass SMC,
-                 SectionKind K, MCSymbol *Begin)
-      : MCSection(SV_XCOFF, K, Begin), Name(Section), MappingClass(SMC) {}
+                 XCOFF::SymbolType ST, SectionKind K, MCSymbol *Begin)
+      : MCSection(SV_XCOFF, K, Begin), Name(Section), MappingClass(SMC),
+        Type(ST) {
+    assert((ST == XCOFF::XTY_SD || ST == XCOFF::XTY_CM) &&
+           "Unexpected type for csect.");
+  }
 
 public:
   ~MCSectionXCOFF();
@@ -43,6 +52,7 @@ public:
 
   StringRef getSectionName() const { return Name; }
   XCOFF::StorageMappingClass getMappingClass() const { return MappingClass; }
+  XCOFF::SymbolType getCSectType() const { return Type; }
 
   void PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
                             raw_ostream &OS,

Modified: llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp?rev=366727&r1=366726&r2=366727&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp (original)
+++ llvm/trunk/lib/CodeGen/TargetLoweringObjectFileImpl.cpp Mon Jul 22 12:15:29 2019
@@ -43,6 +43,7 @@
 #include "llvm/MC/MCSectionELF.h"
 #include "llvm/MC/MCSectionMachO.h"
 #include "llvm/MC/MCSectionWasm.h"
+#include "llvm/MC/MCSectionXCOFF.h"
 #include "llvm/MC/MCStreamer.h"
 #include "llvm/MC/MCSymbol.h"
 #include "llvm/MC/MCSymbolELF.h"
@@ -1819,3 +1820,57 @@ MCSection *TargetLoweringObjectFileWasm:
   llvm_unreachable("@llvm.global_dtors should have been lowered already");
   return nullptr;
 }
+
+//===----------------------------------------------------------------------===//
+//                                  XCOFF
+//===----------------------------------------------------------------------===//
+MCSection *TargetLoweringObjectFileXCOFF::getExplicitSectionGlobal(
+    const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
+  llvm_unreachable("XCOFF explicit sections not yet implemented.");
+}
+
+MCSection *TargetLoweringObjectFileXCOFF::SelectSectionForGlobal(
+    const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
+  assert(!TM.getFunctionSections() && !TM.getDataSections() &&
+         "XCOFF unique sections not yet implemented.");
+
+  // Common symbols go into a csect with matching name which will get mapped
+  // into the .bss section.
+  if (Kind.isCommon()) {
+    SmallString<128> Name;
+    getNameWithPrefix(Name, GO, TM);
+    return getContext().getXCOFFSection(Name, XCOFF::XMC_RW, XCOFF::XTY_CM,
+                                        Kind, /* BeginSymbolName */ nullptr);
+  }
+
+  report_fatal_error("XCOFF other section types not yet implemented.");
+}
+
+bool TargetLoweringObjectFileXCOFF::shouldPutJumpTableInFunctionSection(
+    bool UsesLabelDifference, const Function &F) const {
+  llvm_unreachable("TLOF XCOFF not yet implemented.");
+}
+
+void TargetLoweringObjectFileXCOFF::Initialize(MCContext &Ctx,
+                                               const TargetMachine &TgtM) {
+  TargetLoweringObjectFile::Initialize(Ctx, TgtM);
+  TTypeEncoding = 0;
+  PersonalityEncoding = 0;
+  LSDAEncoding = 0;
+}
+
+MCSection *TargetLoweringObjectFileXCOFF::getStaticCtorSection(
+    unsigned Priority, const MCSymbol *KeySym) const {
+  llvm_unreachable("XCOFF ctor section not yet implemented.");
+}
+
+MCSection *TargetLoweringObjectFileXCOFF::getStaticDtorSection(
+    unsigned Priority, const MCSymbol *KeySym) const {
+  llvm_unreachable("XCOFF dtor section not yet implemented.");
+}
+
+const MCExpr *TargetLoweringObjectFileXCOFF::lowerRelativeReference(
+    const GlobalValue *LHS, const GlobalValue *RHS,
+    const TargetMachine &TM) const {
+  llvm_unreachable("XCOFF not yet implemented.");
+}

Modified: llvm/trunk/lib/MC/MCAsmInfoXCOFF.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCAsmInfoXCOFF.cpp?rev=366727&r1=366726&r2=366727&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCAsmInfoXCOFF.cpp (original)
+++ llvm/trunk/lib/MC/MCAsmInfoXCOFF.cpp Mon Jul 22 12:15:29 2019
@@ -15,4 +15,5 @@ void MCAsmInfoXCOFF::anchor() {}
 MCAsmInfoXCOFF::MCAsmInfoXCOFF() {
   IsLittleEndian = false;
   HasDotTypeDotSizeDirective = false;
+  COMMDirectiveAlignmentIsInBytes = false;
 }

Modified: llvm/trunk/lib/MC/MCContext.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCContext.cpp?rev=366727&r1=366726&r2=366727&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCContext.cpp (original)
+++ llvm/trunk/lib/MC/MCContext.cpp Mon Jul 22 12:15:29 2019
@@ -531,6 +531,7 @@ MCSectionWasm *MCContext::getWasmSection
 
 MCSectionXCOFF *MCContext::getXCOFFSection(StringRef Section,
                                            XCOFF::StorageMappingClass SMC,
+                                           XCOFF::SymbolType Type,
                                            SectionKind Kind,
                                            const char *BeginSymName) {
   // Do the lookup. If we have a hit, return it.
@@ -548,7 +549,7 @@ MCSectionXCOFF *MCContext::getXCOFFSecti
     Begin = createTempSymbol(BeginSymName, false);
 
   MCSectionXCOFF *Result = new (XCOFFAllocator.Allocate())
-      MCSectionXCOFF(CachedName, SMC, Kind, Begin);
+      MCSectionXCOFF(CachedName, SMC, Type, Kind, Begin);
   Entry.second = Result;
 
   auto *F = new MCDataFragment();

Modified: llvm/trunk/lib/MC/MCObjectFileInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCObjectFileInfo.cpp?rev=366727&r1=366726&r2=366727&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCObjectFileInfo.cpp (original)
+++ llvm/trunk/lib/MC/MCObjectFileInfo.cpp Mon Jul 22 12:15:29 2019
@@ -767,8 +767,9 @@ void MCObjectFileInfo::initXCOFFMCObject
   // get placed into this csect. The choice of csect name is not a property of
   // the ABI or object file format. For example, the XL compiler uses an unnamed
   // csect for program code.
-  TextSection = Ctx->getXCOFFSection(
-      ".text", XCOFF::StorageMappingClass::XMC_PR, SectionKind::getText());
+  TextSection =
+      Ctx->getXCOFFSection(".text", XCOFF::StorageMappingClass::XMC_PR,
+                           XCOFF::XTY_SD, SectionKind::getText());
 }
 
 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=366727&r1=366726&r2=366727&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCSectionXCOFF.cpp (original)
+++ llvm/trunk/lib/MC/MCSectionXCOFF.cpp Mon Jul 22 12:15:29 2019
@@ -19,15 +19,29 @@ void MCSectionXCOFF::PrintSwitchToSectio
                                           raw_ostream &OS,
                                           const MCExpr *Subsection) const {
   if (getKind().isText()) {
+    if (getMappingClass() != XCOFF::XMC_PR)
+      llvm_unreachable("Unsupported storage-mapping class for .text csect");
+
     OS << "\t.csect " << getSectionName() << "["
        << "PR"
        << "]" << '\n';
     return;
   }
 
+  if (getKind().isCommon()) {
+    if (getMappingClass() != XCOFF::XMC_RW)
+      llvm_unreachable("Unsupported storage-mapping class for common csect");
+    if (getCSectType() != XCOFF::XTY_CM)
+      llvm_unreachable("wrong csect type for common csect");
+    // Don't have to print a directive for switching to section for commons.
+    // '.comm' and '.lcomm' directives for the variable will create the needed
+    // csect.
+    return;
+  }
+
   report_fatal_error("Printing for this SectionKind is unimplemented.");
 }
 
 bool MCSectionXCOFF::UseCodeAlign() const { return getKind().isText(); }
 
-bool MCSectionXCOFF::isVirtualSection() const { return !getKind().isCommon(); }
+bool MCSectionXCOFF::isVirtualSection() const { return XCOFF::XTY_CM == Type; }

Modified: llvm/trunk/lib/Target/PowerPC/PPCAsmPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/PowerPC/PPCAsmPrinter.cpp?rev=366727&r1=366726&r2=366727&view=diff
==============================================================================
--- llvm/trunk/lib/Target/PowerPC/PPCAsmPrinter.cpp (original)
+++ llvm/trunk/lib/Target/PowerPC/PPCAsmPrinter.cpp Mon Jul 22 12:15:29 2019
@@ -51,9 +51,11 @@
 #include "llvm/MC/MCInstBuilder.h"
 #include "llvm/MC/MCSectionELF.h"
 #include "llvm/MC/MCSectionMachO.h"
+#include "llvm/MC/MCSectionXCOFF.h"
 #include "llvm/MC/MCStreamer.h"
 #include "llvm/MC/MCSymbol.h"
 #include "llvm/MC/MCSymbolELF.h"
+#include "llvm/MC/MCSymbolXCOFF.h"
 #include "llvm/MC/SectionKind.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/CodeGen.h"
@@ -164,6 +166,8 @@ public:
       : PPCAsmPrinter(TM, std::move(Streamer)) {}
 
   StringRef getPassName() const override { return "AIX PPC Assembly Printer"; }
+
+  void EmitGlobalVariable(const GlobalVariable *GV) override;
 };
 
 } // end anonymous namespace
@@ -1643,6 +1647,35 @@ bool PPCDarwinAsmPrinter::doFinalization
   return AsmPrinter::doFinalization(M);
 }
 
+void PPCAIXAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
+  // Early error checking limiting what is supported.
+  if (GV->isThreadLocal())
+    report_fatal_error("Thread local not yet supported on AIX.");
+
+  if (GV->hasSection())
+    report_fatal_error("Custom section for Data not yet supported.");
+
+  if (GV->hasComdat())
+    report_fatal_error("COMDAT not yet supported on AIX.");
+
+  SectionKind GVKind = getObjFileLowering().getKindForGlobal(GV, TM);
+  if (!GVKind.isCommon())
+    report_fatal_error("Only common variables are supported on AIX.");
+
+  // Create the containing csect and switch to it.
+  MCSectionXCOFF *CSect = dyn_cast<MCSectionXCOFF>(
+      getObjFileLowering().SectionForGlobal(GV, GVKind, TM));
+  OutStreamer->SwitchSection(CSect);
+
+  // Create the symbol and emit it.
+  MCSymbolXCOFF *XSym = dyn_cast<MCSymbolXCOFF>(getSymbol(GV));
+  auto DL = GV->getParent()->getDataLayout();
+  unsigned Align =
+      GV->getAlignment() ? GV->getAlignment() : DL.getPreferredAlignment(GV);
+  uint64_t Size = DL.getTypeAllocSize(GV->getType()->getElementType());
+  OutStreamer->EmitCommonSymbol(XSym, Size, Align);
+}
+
 /// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
 /// for a MachineFunction to the given output stream, in a format that the
 /// Darwin assembler can deal with.

Modified: llvm/trunk/lib/Target/PowerPC/PPCTargetMachine.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/PowerPC/PPCTargetMachine.cpp?rev=366727&r1=366726&r2=366727&view=diff
==============================================================================
--- llvm/trunk/lib/Target/PowerPC/PPCTargetMachine.cpp (original)
+++ llvm/trunk/lib/Target/PowerPC/PPCTargetMachine.cpp Mon Jul 22 12:15:29 2019
@@ -185,11 +185,12 @@ static std::string computeFSAdditions(St
 }
 
 static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
-  // If it isn't a Mach-O file then it's going to be a linux ELF
-  // object file.
   if (TT.isOSDarwin())
     return llvm::make_unique<TargetLoweringObjectFileMachO>();
 
+  if (TT.isOSAIX())
+    return llvm::make_unique<TargetLoweringObjectFileXCOFF>();
+
   return llvm::make_unique<PPC64LinuxTargetObjectFile>();
 }
 

Added: llvm/trunk/test/CodeGen/PowerPC/aix-xcoff-common.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/PowerPC/aix-xcoff-common.ll?rev=366727&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/PowerPC/aix-xcoff-common.ll (added)
+++ llvm/trunk/test/CodeGen/PowerPC/aix-xcoff-common.ll Mon Jul 22 12:15:29 2019
@@ -0,0 +1,24 @@
+; RUN: llc -mtriple powerpc-ibm-aix-xcoff < %s | FileCheck %s
+
+ at a = common global i32 0, align 4
+ at b = common global i64 0, align 8
+ at c = common global i16 0, align 2
+
+ at d = common local_unnamed_addr global double 0.000000e+00, align 8
+ at f = common local_unnamed_addr global float 0.000000e+00, align 4
+ at comm = common local_unnamed_addr global double 0.000000e+00, align 8
+
+ at over_aligned = common local_unnamed_addr global double 0.000000e+00, align 32
+
+ at array = common local_unnamed_addr global [32 x i8] zeroinitializer, align 1
+
+; CHECK:      .csect .text[PR]
+; CHECK-NEXT:  .file
+; CHECK-NEXT: .comm   a,4,2
+; CHECK-NEXT: .comm   b,8,3
+; CHECK-NEXT: .comm   c,2,1
+; CHECK-NEXT: .comm   d,8,3
+; CHECK-NEXT: .comm   f,4,2
+; CHECK-NEXT: .comm   comm,8,3
+; CHECK-NEXT: .comm   over_aligned,8,5
+; CHECK-NEXT: .comm   array,32,0




More information about the llvm-commits mailing list