Skip to content

Commit

Permalink
Add back r201608, r201622, r201624 and r201625
Browse files Browse the repository at this point in the history
r201608 made llvm corretly handle private globals with MachO. r201622 fixed
a bug in it and r201624 and r201625 were changes for using private linkage,
assuming that llvm would do the right thing.

They all got reverted because r201608 introduced a crash in LTO. This patch
includes a fix for that. The issue was that TargetLoweringObjectFile now has
to be initialized before we can mangle names of private globals. This is
trivially true during the normal codegen pipeline (the asm printer does it),
but LTO has to do it manually.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@201700 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
espindola committed Feb 19, 2014
1 parent cf42174 commit 737c9f6
Show file tree
Hide file tree
Showing 35 changed files with 377 additions and 173 deletions.
4 changes: 2 additions & 2 deletions examples/ExceptionDemo/ExceptionDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ void generateStringPrint(llvm::LLVMContext &context,
new llvm::GlobalVariable(module,
stringConstant->getType(),
true,
llvm::GlobalValue::LinkerPrivateLinkage,
llvm::GlobalValue::PrivateLinkage,
stringConstant,
"");
}
Expand Down Expand Up @@ -959,7 +959,7 @@ void generateIntegerPrint(llvm::LLVMContext &context,
new llvm::GlobalVariable(module,
stringConstant->getType(),
true,
llvm::GlobalValue::LinkerPrivateLinkage,
llvm::GlobalValue::PrivateLinkage,
stringConstant,
"");
}
Expand Down
3 changes: 3 additions & 0 deletions include/llvm/CodeGen/AsmPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ namespace llvm {
/// getCurrentSection() - Return the current section we are emitting to.
const MCSection *getCurrentSection() const;

void getNameWithPrefix(SmallVectorImpl<char> &Name,
const GlobalValue *GV) const;

MCSymbol *getSymbol(const GlobalValue *GV) const;

//===------------------------------------------------------------------===//
Expand Down
25 changes: 15 additions & 10 deletions include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ class TargetLoweringObjectFileELF : public TargetLoweringObjectFile {

/// Return an MCExpr to use for a reference to the specified type info global
/// variable from exception handling information.
const MCExpr *getTTypeGlobalReference(const GlobalValue *GV,
unsigned Encoding, Mangler &Mang,
MachineModuleInfo *MMI,
MCStreamer &Streamer) const
const MCExpr *
getTTypeGlobalReference(const GlobalValue *GV, unsigned Encoding,
Mangler &Mang, const TargetMachine &TM,
MachineModuleInfo *MMI, MCStreamer &Streamer) const
LLVM_OVERRIDE;

// The symbol that gets passed to .cfi_personality.
MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV, Mangler &Mang,
const TargetMachine &TM,
MachineModuleInfo *MMI) const LLVM_OVERRIDE;

void InitializeELF(bool UseInitArray_);
Expand All @@ -90,6 +91,9 @@ class TargetLoweringObjectFileMachO : public TargetLoweringObjectFile {
Mangler &Mang, const TargetMachine &TM) const
LLVM_OVERRIDE;

bool isSectionAtomizableBySymbols(const MCSection &Section) const
LLVM_OVERRIDE;

const MCSection *SelectSectionForGlobal(const GlobalValue *GV,
SectionKind Kind, Mangler &Mang,
const TargetMachine &TM) const
Expand All @@ -105,18 +109,19 @@ class TargetLoweringObjectFileMachO : public TargetLoweringObjectFile {
/// This hook allows targets to selectively decide not to emit the
/// UsedDirective for some symbols in llvm.used.
/// FIXME: REMOVE this (rdar://7071300)
bool shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler &Mang) const
LLVM_OVERRIDE;
bool shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler &Mang,
TargetMachine &TM) const LLVM_OVERRIDE;

/// The mach-o version of this method defaults to returning a stub reference.
const MCExpr *getTTypeGlobalReference(const GlobalValue *GV,
unsigned Encoding, Mangler &Mang,
MachineModuleInfo *MMI,
MCStreamer &Streamer) const
const MCExpr *
getTTypeGlobalReference(const GlobalValue *GV, unsigned Encoding,
Mangler &Mang, const TargetMachine &TM,
MachineModuleInfo *MMI, MCStreamer &Streamer) const
LLVM_OVERRIDE;

// The symbol that gets passed to .cfi_personality.
MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV, Mangler &Mang,
const TargetMachine &TM,
MachineModuleInfo *MMI) const LLVM_OVERRIDE;
};

Expand Down
7 changes: 4 additions & 3 deletions include/llvm/IR/Mangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ class Mangler {
/// Print the appropriate prefix and the specified global variable's name.
/// If the global variable doesn't have a name, this fills in a unique name
/// for the global.
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV) const;
void getNameWithPrefix(SmallVectorImpl<char> &OutName,
const GlobalValue *GV) const;
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
bool CannotUsePrivateLabel) const;
void getNameWithPrefix(SmallVectorImpl<char> &OutName, const GlobalValue *GV,
bool CannotUsePrivateLabel) const;

/// Print the appropriate prefix and the specified name as the global variable
/// name. GVName must not be empty.
Expand Down
6 changes: 6 additions & 0 deletions include/llvm/Target/TargetLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ namespace llvm {
class MachineFunction;
class MachineInstr;
class MachineJumpTableInfo;
class Mangler;
class MCContext;
class MCExpr;
class MCSymbol;
template<typename T> class SmallVectorImpl;
class DataLayout;
class TargetRegisterClass;
Expand Down Expand Up @@ -1343,6 +1345,10 @@ class TargetLoweringBase {
return LibcallCallingConvs[Call];
}

void getNameWithPrefix(SmallVectorImpl<char> &Name, const GlobalValue *GV,
Mangler &Mang, bool MayAlwaysUsePrivate = false) const;
MCSymbol *getSymbol(const GlobalValue *GV, Mangler &Mang) const;

private:
const TargetMachine &TM;
const DataLayout *DL;
Expand Down
36 changes: 20 additions & 16 deletions include/llvm/Target/TargetLoweringObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ class TargetLoweringObjectFile : public MCObjectFileInfo {
/// This hook allows targets to selectively decide not to emit the
/// UsedDirective for some symbols in llvm.used.
/// FIXME: REMOVE this (rdar://7071300)
virtual bool shouldEmitUsedDirectiveFor(const GlobalValue *GV,
Mangler &Mang) const {
virtual bool shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler &Mang,
TargetMachine &TM) const {
return GV != 0;
}

Expand Down Expand Up @@ -117,25 +117,22 @@ class TargetLoweringObjectFile : public MCObjectFileInfo {

/// Return an MCExpr to use for a reference to the specified global variable
/// from exception handling information.
virtual const MCExpr *getTTypeGlobalReference(const GlobalValue *GV,
unsigned Encoding,
Mangler &Mang,
MachineModuleInfo *MMI,
MCStreamer &Streamer) const;

/// Return the MCSymbol for the specified global value. This symbol is the
/// main label that is the address of the global
MCSymbol *getSymbol(const GlobalValue *GV, Mangler &M) const;
virtual const MCExpr *
getTTypeGlobalReference(const GlobalValue *GV, unsigned Encoding,
Mangler &Mang, const TargetMachine &TM,
MachineModuleInfo *MMI, MCStreamer &Streamer) const;

/// Return the MCSymbol for a private symbol with global value name as its
/// base, with the specified suffix.
MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV,
StringRef Suffix, Mangler &M) const;
StringRef Suffix, Mangler &Mang,
const TargetMachine &TM) const;

// The symbol that gets passed to .cfi_personality.
virtual MCSymbol *
getCFIPersonalitySymbol(const GlobalValue *GV, Mangler &Mang,
MachineModuleInfo *MMI) const;
virtual MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV,
Mangler &Mang,
const TargetMachine &TM,
MachineModuleInfo *MMI) const;

const MCExpr *
getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding,
Expand All @@ -157,10 +154,17 @@ class TargetLoweringObjectFile : public MCObjectFileInfo {
virtual const MCExpr *getDebugThreadLocalSymbol(const MCSymbol *Sym) const;

virtual const MCExpr *
getExecutableRelativeSymbol(const ConstantExpr *CE, Mangler &Mang) const {
getExecutableRelativeSymbol(const ConstantExpr *CE, Mangler &Mang,
const TargetMachine &TM) const {
return 0;
}

/// \brief True if the section is atomized using the symbols in it.
/// This is false if the section is not atomized at all (most ELF sections) or
/// if it is atomized based on its contents (MachO' __TEXT,__cstring for
/// example).
virtual bool isSectionAtomizableBySymbols(const MCSection &Section) const;

protected:
virtual const MCSection *
SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Expand Down
15 changes: 11 additions & 4 deletions lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,13 @@ void AsmPrinter::EmitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const {
llvm_unreachable("Unknown linkage type!");
}

void AsmPrinter::getNameWithPrefix(SmallVectorImpl<char> &Name,
const GlobalValue *GV) const {
TM.getTargetLowering()->getNameWithPrefix(Name, GV, *Mang);
}

MCSymbol *AsmPrinter::getSymbol(const GlobalValue *GV) const {
return getObjFileLowering().getSymbol(GV, *Mang);
return TM.getTargetLowering()->getSymbol(GV, *Mang);
}

/// EmitGlobalVariable - Emit the specified global variable to the .s file.
Expand Down Expand Up @@ -1369,7 +1374,7 @@ void AsmPrinter::EmitLLVMUsedList(const ConstantArray *InitList) {
for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
const GlobalValue *GV =
dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, *Mang))
if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, *Mang, TM))
OutStreamer.EmitSymbolAttribute(getSymbol(GV), MCSA_NoDeadStrip);
}
}
Expand Down Expand Up @@ -1574,7 +1579,8 @@ static const MCExpr *lowerConstant(const Constant *CV, AsmPrinter &AP) {
}

if (const MCExpr *RelocExpr =
AP.getObjFileLowering().getExecutableRelativeSymbol(CE, *AP.Mang))
AP.getObjFileLowering().getExecutableRelativeSymbol(CE, *AP.Mang,
AP.TM))
return RelocExpr;

switch (CE->getOpcode()) {
Expand Down Expand Up @@ -2103,7 +2109,8 @@ MCSymbol *AsmPrinter::GetJTSetSymbol(unsigned UID, unsigned MBBID) const {

MCSymbol *AsmPrinter::getSymbolWithGlobalValueBase(const GlobalValue *GV,
StringRef Suffix) const {
return getObjFileLowering().getSymbolWithGlobalValueBase(GV, Suffix, *Mang);
return getObjFileLowering().getSymbolWithGlobalValueBase(GV, Suffix, *Mang,
TM);
}

/// GetExternalSymbolSymbol - Return the MCSymbol for the specified
Expand Down
2 changes: 1 addition & 1 deletion lib/CodeGen/AsmPrinter/AsmPrinterDwarf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void AsmPrinter::EmitTTypeReference(const GlobalValue *GV,
const TargetLoweringObjectFile &TLOF = getObjFileLowering();

const MCExpr *Exp =
TLOF.getTTypeGlobalReference(GV, Encoding, *Mang, MMI, OutStreamer);
TLOF.getTTypeGlobalReference(GV, Encoding, *Mang, TM, MMI, OutStreamer);
OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding));
} else
OutStreamer.EmitIntValue(0, GetSizeOfEncodedValue(Encoding));
Expand Down
3 changes: 2 additions & 1 deletion lib/CodeGen/AsmPrinter/DwarfCFIException.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ void DwarfCFIException::beginFunction(const MachineFunction *MF) {
if (!shouldEmitPersonality)
return;

const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(Per, *Asm->Mang, MMI);
const MCSymbol *Sym =
TLOF.getCFIPersonalitySymbol(Per, *Asm->Mang, Asm->TM, MMI);
Asm->OutStreamer.EmitCFIPersonality(Sym, PerEncoding);

Asm->OutStreamer.EmitDebugLabel
Expand Down
3 changes: 2 additions & 1 deletion lib/CodeGen/AsmPrinter/Win64Exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ void Win64Exception::endFunction(const MachineFunction *) {
if (shouldEmitPersonality) {
const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
const Function *Per = MMI->getPersonalities()[MMI->getPersonalityIndex()];
const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(Per, *Asm->Mang, MMI);
const MCSymbol *Sym =
TLOF.getCFIPersonalitySymbol(Per, *Asm->Mang, Asm->TM, MMI);

Asm->OutStreamer.PushSection();
Asm->OutStreamer.EmitWin64EHHandlerData();
Expand Down
29 changes: 29 additions & 0 deletions lib/CodeGen/TargetLoweringBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/Mangler.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorHandling.h"
Expand Down Expand Up @@ -1426,3 +1428,30 @@ bool TargetLoweringBase::isLegalAddressingMode(const AddrMode &AM,

return true;
}

void TargetLoweringBase::getNameWithPrefix(SmallVectorImpl<char> &Name,
const GlobalValue *GV,
Mangler &Mang,
bool MayAlwaysUsePrivate) const {
if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
// Simple case: If GV is not private, it is not important to find out if
// private labels are legal in this case or not.
Mang.getNameWithPrefix(Name, GV, false);
return;
}
SectionKind GVKind =
TargetLoweringObjectFile::getKindForGlobal(GV, getTargetMachine());
const TargetLoweringObjectFile &TLOF = getObjFileLowering();
const MCSection *TheSection =
TLOF.SectionForGlobal(GV, GVKind, Mang, getTargetMachine());
bool CannotUsePrivateLabel = TLOF.isSectionAtomizableBySymbols(*TheSection);
Mang.getNameWithPrefix(Name, GV, CannotUsePrivateLabel);
}

MCSymbol *TargetLoweringBase::getSymbol(const GlobalValue *GV,
Mangler &Mang) const {
SmallString<60> NameStr;
getNameWithPrefix(NameStr, GV, Mang);
const TargetLoweringObjectFile &TLOF = getObjFileLowering();
return TLOF.getContext().GetOrCreateSymbol(NameStr.str());
}
Loading

0 comments on commit 737c9f6

Please sign in to comment.