From 93003b9ee7df0929305d886a36ef97571e080a37 Mon Sep 17 00:00:00 2001 From: Mehdi Amini Date: Tue, 11 Oct 2016 18:22:41 +0000 Subject: [PATCH] Fix "static initialization order fiasco" for the XCore Target. I fixed all the other Targets in r283702, and interestingly the sanitizers are only now "sometimes" catching this bug on the only one I missed. llvm-svn: 283914 --- .../XCore/Disassembler/XCoreDisassembler.cpp | 4 ++-- .../XCore/MCTargetDesc/XCoreMCTargetDesc.cpp | 16 +++++++++------- .../XCore/MCTargetDesc/XCoreMCTargetDesc.h | 3 +-- lib/Target/XCore/TargetInfo/XCoreTargetInfo.cpp | 7 +++++-- lib/Target/XCore/XCoreAsmPrinter.cpp | 2 +- lib/Target/XCore/XCoreTargetMachine.cpp | 2 +- 6 files changed, 19 insertions(+), 15 deletions(-) diff --git a/lib/Target/XCore/Disassembler/XCoreDisassembler.cpp b/lib/Target/XCore/Disassembler/XCoreDisassembler.cpp index 2e8f762458a..059b75ef482 100644 --- a/lib/Target/XCore/Disassembler/XCoreDisassembler.cpp +++ b/lib/Target/XCore/Disassembler/XCoreDisassembler.cpp @@ -769,7 +769,7 @@ MCDisassembler::DecodeStatus XCoreDisassembler::getInstruction( } namespace llvm { - extern Target TheXCoreTarget; + Target &getTheXCoreTarget(); } static MCDisassembler *createXCoreDisassembler(const Target &T, @@ -780,6 +780,6 @@ static MCDisassembler *createXCoreDisassembler(const Target &T, extern "C" void LLVMInitializeXCoreDisassembler() { // Register the disassembler. - TargetRegistry::RegisterMCDisassembler(TheXCoreTarget, + TargetRegistry::RegisterMCDisassembler(getTheXCoreTarget(), createXCoreDisassembler); } diff --git a/lib/Target/XCore/MCTargetDesc/XCoreMCTargetDesc.cpp b/lib/Target/XCore/MCTargetDesc/XCoreMCTargetDesc.cpp index 63ca1e7d464..c5859b7786f 100644 --- a/lib/Target/XCore/MCTargetDesc/XCoreMCTargetDesc.cpp +++ b/lib/Target/XCore/MCTargetDesc/XCoreMCTargetDesc.cpp @@ -124,26 +124,28 @@ static MCTargetStreamer *createTargetAsmStreamer(MCStreamer &S, // Force static initialization. extern "C" void LLVMInitializeXCoreTargetMC() { // Register the MC asm info. - RegisterMCAsmInfoFn X(TheXCoreTarget, createXCoreMCAsmInfo); + RegisterMCAsmInfoFn X(getTheXCoreTarget(), createXCoreMCAsmInfo); // Register the MC codegen info. - TargetRegistry::registerMCAdjustCodeGenOpts(TheXCoreTarget, + TargetRegistry::registerMCAdjustCodeGenOpts(getTheXCoreTarget(), adjustCodeGenOpts); // Register the MC instruction info. - TargetRegistry::RegisterMCInstrInfo(TheXCoreTarget, createXCoreMCInstrInfo); + TargetRegistry::RegisterMCInstrInfo(getTheXCoreTarget(), + createXCoreMCInstrInfo); // Register the MC register info. - TargetRegistry::RegisterMCRegInfo(TheXCoreTarget, createXCoreMCRegisterInfo); + TargetRegistry::RegisterMCRegInfo(getTheXCoreTarget(), + createXCoreMCRegisterInfo); // Register the MC subtarget info. - TargetRegistry::RegisterMCSubtargetInfo(TheXCoreTarget, + TargetRegistry::RegisterMCSubtargetInfo(getTheXCoreTarget(), createXCoreMCSubtargetInfo); // Register the MCInstPrinter - TargetRegistry::RegisterMCInstPrinter(TheXCoreTarget, + TargetRegistry::RegisterMCInstPrinter(getTheXCoreTarget(), createXCoreMCInstPrinter); - TargetRegistry::RegisterAsmTargetStreamer(TheXCoreTarget, + TargetRegistry::RegisterAsmTargetStreamer(getTheXCoreTarget(), createTargetAsmStreamer); } diff --git a/lib/Target/XCore/MCTargetDesc/XCoreMCTargetDesc.h b/lib/Target/XCore/MCTargetDesc/XCoreMCTargetDesc.h index 28e0275c72d..ac0f3fefbae 100644 --- a/lib/Target/XCore/MCTargetDesc/XCoreMCTargetDesc.h +++ b/lib/Target/XCore/MCTargetDesc/XCoreMCTargetDesc.h @@ -18,8 +18,7 @@ namespace llvm { class Target; - -extern Target TheXCoreTarget; +Target &getTheXCoreTarget(); } // End llvm namespace diff --git a/lib/Target/XCore/TargetInfo/XCoreTargetInfo.cpp b/lib/Target/XCore/TargetInfo/XCoreTargetInfo.cpp index c78cde9ac8a..df5774c7e8e 100644 --- a/lib/Target/XCore/TargetInfo/XCoreTargetInfo.cpp +++ b/lib/Target/XCore/TargetInfo/XCoreTargetInfo.cpp @@ -12,8 +12,11 @@ #include "llvm/Support/TargetRegistry.h" using namespace llvm; -Target llvm::TheXCoreTarget; +Target &llvm::getTheXCoreTarget() { + static Target TheXCoreTarget; + return TheXCoreTarget; +} extern "C" void LLVMInitializeXCoreTargetInfo() { - RegisterTarget X(TheXCoreTarget, "xcore", "XCore"); + RegisterTarget X(getTheXCoreTarget(), "xcore", "XCore"); } diff --git a/lib/Target/XCore/XCoreAsmPrinter.cpp b/lib/Target/XCore/XCoreAsmPrinter.cpp index 62fafd6f79c..a4ce5af2278 100644 --- a/lib/Target/XCore/XCoreAsmPrinter.cpp +++ b/lib/Target/XCore/XCoreAsmPrinter.cpp @@ -297,5 +297,5 @@ void XCoreAsmPrinter::EmitInstruction(const MachineInstr *MI) { // Force static initialization. extern "C" void LLVMInitializeXCoreAsmPrinter() { - RegisterAsmPrinter X(TheXCoreTarget); + RegisterAsmPrinter X(getTheXCoreTarget()); } diff --git a/lib/Target/XCore/XCoreTargetMachine.cpp b/lib/Target/XCore/XCoreTargetMachine.cpp index c3eab802f81..bf3138f2164 100644 --- a/lib/Target/XCore/XCoreTargetMachine.cpp +++ b/lib/Target/XCore/XCoreTargetMachine.cpp @@ -89,7 +89,7 @@ void XCorePassConfig::addPreEmitPass() { // Force static initialization. extern "C" void LLVMInitializeXCoreTarget() { - RegisterTargetMachine X(TheXCoreTarget); + RegisterTargetMachine X(getTheXCoreTarget()); } TargetIRAnalysis XCoreTargetMachine::getTargetIRAnalysis() {