From 3f7e7c061c9ef9c3452c25da2ac7ea1bb4079509 Mon Sep 17 00:00:00 2001 From: Bob Wilson Date: Fri, 12 Oct 2012 17:39:25 +0000 Subject: [PATCH] Set default CPU for Darwin targets with LTO. This is a temporary hack until Bill's project to record command line options in the LLVM IR is ready. Clang currently sets a default CPU but that isn't recorded anywhere and it doesn't get used in the final LTO compilation. llvm-svn: 165809 --- llvm/tools/lto/LTOCodeGenerator.cpp | 20 ++++++++++++++------ llvm/tools/lto/LTOModule.cpp | 20 ++++++++++++++------ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/llvm/tools/lto/LTOCodeGenerator.cpp b/llvm/tools/lto/LTOCodeGenerator.cpp index f1814ab..dd74ddd 100644 --- a/llvm/tools/lto/LTOCodeGenerator.cpp +++ b/llvm/tools/lto/LTOCodeGenerator.cpp @@ -218,12 +218,13 @@ bool LTOCodeGenerator::determineTarget(std::string& errMsg) { if (_target != NULL) return false; - std::string Triple = _linker.getModule()->getTargetTriple(); - if (Triple.empty()) - Triple = sys::getDefaultTargetTriple(); + std::string TripleStr = _linker.getModule()->getTargetTriple(); + if (TripleStr.empty()) + TripleStr = sys::getDefaultTargetTriple(); + llvm::Triple Triple(TripleStr); // create target machine from info for merged modules - const Target *march = TargetRegistry::lookupTarget(Triple, errMsg); + const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg); if (march == NULL) return true; @@ -244,11 +245,18 @@ bool LTOCodeGenerator::determineTarget(std::string& errMsg) { // construct LTOModule, hand over ownership of module and target SubtargetFeatures Features; - Features.getDefaultSubtargetFeatures(llvm::Triple(Triple)); + Features.getDefaultSubtargetFeatures(Triple); std::string FeatureStr = Features.getString(); + // Set a default CPU for Darwin triples. + if (_mCpu.empty() && Triple.isOSDarwin()) { + if (Triple.getArch() == llvm::Triple::x86_64) + _mCpu = "core2"; + else if (Triple.getArch() == llvm::Triple::x86) + _mCpu = "yonah"; + } TargetOptions Options; LTOModule::getTargetOptions(Options); - _target = march->createTargetMachine(Triple, _mCpu, FeatureStr, Options, + _target = march->createTargetMachine(TripleStr, _mCpu, FeatureStr, Options, RelocModel, CodeModel::Default, CodeGenOpt::Aggressive); return false; diff --git a/llvm/tools/lto/LTOModule.cpp b/llvm/tools/lto/LTOModule.cpp index 3c3701a..ffdcbe6 100644 --- a/llvm/tools/lto/LTOModule.cpp +++ b/llvm/tools/lto/LTOModule.cpp @@ -278,23 +278,31 @@ LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer, return NULL; } - std::string Triple = m->getTargetTriple(); - if (Triple.empty()) - Triple = sys::getDefaultTargetTriple(); + std::string TripleStr = m->getTargetTriple(); + if (TripleStr.empty()) + TripleStr = sys::getDefaultTargetTriple(); + llvm::Triple Triple(TripleStr); // find machine architecture for this module - const Target *march = TargetRegistry::lookupTarget(Triple, errMsg); + const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg); if (!march) return NULL; // construct LTOModule, hand over ownership of module and target SubtargetFeatures Features; - Features.getDefaultSubtargetFeatures(llvm::Triple(Triple)); + Features.getDefaultSubtargetFeatures(Triple); std::string FeatureStr = Features.getString(); + // Set a default CPU for Darwin triples. std::string CPU; + if (Triple.isOSDarwin()) { + if (Triple.getArch() == llvm::Triple::x86_64) + CPU = "core2"; + else if (Triple.getArch() == llvm::Triple::x86) + CPU = "yonah"; + } TargetOptions Options; getTargetOptions(Options); - TargetMachine *target = march->createTargetMachine(Triple, CPU, FeatureStr, + TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr, Options); LTOModule *Ret = new LTOModule(m.take(), target); if (Ret->parseSymbols(errMsg)) { -- 2.7.4