[llvm] r236671 - Factor out a function which determines the cpu and feature strings based on
Akira Hatanaka
ahatanaka at apple.com
Wed May 6 16:49:24 PDT 2015
Author: ahatanak
Date: Wed May 6 18:49:24 2015
New Revision: 236671
URL: http://llvm.org/viewvc/llvm-project?rev=236671&view=rev
Log:
Factor out a function which determines the cpu and feature strings based on
command line options -mcpu and -mattr. NFC.
Modified:
llvm/trunk/include/llvm/CodeGen/CommandFlags.h
llvm/trunk/tools/llc/llc.cpp
llvm/trunk/tools/opt/opt.cpp
Modified: llvm/trunk/include/llvm/CodeGen/CommandFlags.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/CommandFlags.h?rev=236671&r1=236670&r2=236671&view=diff
==============================================================================
--- llvm/trunk/include/llvm/CodeGen/CommandFlags.h (original)
+++ llvm/trunk/include/llvm/CodeGen/CommandFlags.h Wed May 6 18:49:24 2015
@@ -17,8 +17,10 @@
#define LLVM_CODEGEN_COMMANDFLAGS_H
#include "llvm/MC/MCTargetOptionsCommandFlags.h"
+#include "llvm//MC/SubtargetFeature.h"
#include "llvm/Support/CodeGen.h"
#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/Host.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"
#include <string>
@@ -260,4 +262,33 @@ static inline TargetOptions InitTargetOp
return Options;
}
+static inline std::string getCPUStr() {
+ // If user asked for the 'native' CPU, autodetect here. If autodection fails,
+ // this will set the CPU to an empty string which tells the target to
+ // pick a basic default.
+ if (MCPU == "native")
+ return sys::getHostCPUName();
+
+ return MCPU;
+}
+
+static inline std::string getFeaturesStr() {
+ SubtargetFeatures Features;
+
+ // If user asked for the 'native' CPU, we need to autodetect features.
+ // This is necessary for x86 where the CPU might not support all the
+ // features the autodetected CPU name lists in the target. For example,
+ if (MCPU == "native") {
+ StringMap<bool> HostFeatures;
+ if (sys::getHostCPUFeatures(HostFeatures))
+ for (auto &F : HostFeatures)
+ Features.AddFeature(F.first(), F.second);
+ }
+
+ for (unsigned i = 0; i != MAttrs.size(); ++i)
+ Features.AddFeature(MAttrs[i]);
+
+ return Features.getString();
+}
+
#endif
Modified: llvm/trunk/tools/llc/llc.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llc/llc.cpp?rev=236671&r1=236670&r2=236671&view=diff
==============================================================================
--- llvm/trunk/tools/llc/llc.cpp (original)
+++ llvm/trunk/tools/llc/llc.cpp Wed May 6 18:49:24 2015
@@ -248,32 +248,7 @@ static int compileModule(char **argv, LL
return 1;
}
- // Package up features to be passed to target/subtarget
- std::string FeaturesStr;
- if (!MAttrs.empty() || MCPU == "native") {
- SubtargetFeatures Features;
-
- // If user asked for the 'native' CPU, we need to autodetect features.
- // This is necessary for x86 where the CPU might not support all the
- // features the autodetected CPU name lists in the target. For example,
- // not all Sandybridge processors support AVX.
- if (MCPU == "native") {
- StringMap<bool> HostFeatures;
- if (sys::getHostCPUFeatures(HostFeatures))
- for (auto &F : HostFeatures)
- Features.AddFeature(F.first(), F.second);
- }
-
- for (unsigned i = 0; i != MAttrs.size(); ++i)
- Features.AddFeature(MAttrs[i]);
- FeaturesStr = Features.getString();
- }
-
- // If user asked for the 'native' CPU, autodetect here. If autodection fails,
- // this will set the CPU to an empty string which tells the target to
- // pick a basic default.
- if (MCPU == "native")
- MCPU = sys::getHostCPUName();
+ std::string CPUStr = getCPUStr(), FeaturesStr = getFeaturesStr();
CodeGenOpt::Level OLvl = CodeGenOpt::Default;
switch (OptLevel) {
@@ -294,8 +269,9 @@ static int compileModule(char **argv, LL
Options.MCOptions.AsmVerbose = AsmVerbose;
std::unique_ptr<TargetMachine> Target(
- TheTarget->createTargetMachine(TheTriple.getTriple(), MCPU, FeaturesStr,
+ TheTarget->createTargetMachine(TheTriple.getTriple(), CPUStr, FeaturesStr,
Options, RelocModel, CMModel, OLvl));
+
assert(Target && "Could not allocate target machine!");
// If we don't have a module then just exit now. We do this down
Modified: llvm/trunk/tools/opt/opt.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/opt/opt.cpp?rev=236671&r1=236670&r2=236671&view=diff
==============================================================================
--- llvm/trunk/tools/opt/opt.cpp (original)
+++ llvm/trunk/tools/opt/opt.cpp Wed May 6 18:49:24 2015
@@ -264,7 +264,8 @@ static CodeGenOpt::Level GetCodeGenOptLe
}
// Returns the TargetMachine instance or zero if no triple is provided.
-static TargetMachine* GetTargetMachine(Triple TheTriple) {
+static TargetMachine* GetTargetMachine(Triple TheTriple, StringRef CPUStr,
+ StringRef FeaturesStr) {
std::string Error;
const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
Error);
@@ -273,32 +274,8 @@ static TargetMachine* GetTargetMachine(T
return nullptr;
}
- // Package up features to be passed to target/subtarget
- std::string FeaturesStr;
- if (MAttrs.size() || MCPU == "native") {
- SubtargetFeatures Features;
-
- // If user asked for the 'native' CPU, we need to autodetect features.
- // This is necessary for x86 where the CPU might not support all the
- // features the autodetected CPU name lists in the target. For example,
- // not all Sandybridge processors support AVX.
- if (MCPU == "native") {
- StringMap<bool> HostFeatures;
- if (sys::getHostCPUFeatures(HostFeatures))
- for (auto &F : HostFeatures)
- Features.AddFeature(F.first(), F.second);
- }
-
- for (unsigned i = 0; i != MAttrs.size(); ++i)
- Features.AddFeature(MAttrs[i]);
- FeaturesStr = Features.getString();
- }
-
- if (MCPU == "native")
- MCPU = sys::getHostCPUName();
-
return TheTarget->createTargetMachine(TheTriple.getTriple(),
- MCPU, FeaturesStr,
+ CPUStr, FeaturesStr,
InitTargetOptionsFromCodeGenFlags(),
RelocModel, CMModel,
GetCodeGenOptLevel());
@@ -407,9 +384,14 @@ int main(int argc, char **argv) {
}
Triple ModuleTriple(M->getTargetTriple());
+ std::string CPUStr, FeaturesStr;
TargetMachine *Machine = nullptr;
- if (ModuleTriple.getArch())
- Machine = GetTargetMachine(ModuleTriple);
+ if (ModuleTriple.getArch()) {
+ CPUStr = getCPUStr();
+ FeaturesStr = getFeaturesStr();
+ Machine = GetTargetMachine(ModuleTriple, CPUStr, FeaturesStr);
+ }
+
std::unique_ptr<TargetMachine> TM(Machine);
// If the output is set to be emitted to standard out, and standard out is a
More information about the llvm-commits
mailing list