From 1ae61a612633331ba2bc6a13fc9703de5590fa29 Mon Sep 17 00:00:00 2001 From: whitequark Date: Wed, 11 Apr 2018 22:40:42 +0000 Subject: [LLVM-C] Add LLVMGetHostCPU{Name,Features}. Without these functions it's hard to create a TargetMachine for Orc JIT that creates efficient native code. It's not sufficient to just expose LLVMGetHostCPUName(), because for some CPUs there's fewer features actually available than the CPU name indicates (e.g. AVX might be missing on some CPUs identified as Skylake). Differential Revision: https://reviews.llvm.org/D44861 llvm-svn: 329856 --- llvm/include/llvm-c/TargetMachine.h | 8 ++++++++ llvm/lib/Target/TargetMachineC.cpp | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) (limited to 'llvm') diff --git a/llvm/include/llvm-c/TargetMachine.h b/llvm/include/llvm-c/TargetMachine.h index f4f7f7698c4..fb0862c226a 100644 --- a/llvm/include/llvm-c/TargetMachine.h +++ b/llvm/include/llvm-c/TargetMachine.h @@ -137,6 +137,14 @@ LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T, LLVMModuleR disposed with LLVMDisposeMessage. */ char* LLVMGetDefaultTargetTriple(void); +/** Get the host CPU as a string. The result needs to be disposed with + LLVMDisposeMessage. */ +char* LLVMGetHostCPUName(void); + +/** Get the host CPU's features as a string. The result needs to be disposed + with LLVMDisposeMessage. */ +char* LLVMGetHostCPUFeatures(void); + /** Adds the target-specific analysis passes to the pass manager. */ void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM); diff --git a/llvm/lib/Target/TargetMachineC.cpp b/llvm/lib/Target/TargetMachineC.cpp index d6016efa246..d43124d7123 100644 --- a/llvm/lib/Target/TargetMachineC.cpp +++ b/llvm/lib/Target/TargetMachineC.cpp @@ -18,6 +18,7 @@ #include "llvm/IR/DataLayout.h" #include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Module.h" +#include "llvm/MC/SubtargetFeature.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/FormattedStream.h" #include "llvm/Support/Host.h" @@ -237,6 +238,21 @@ char *LLVMGetDefaultTargetTriple(void) { return strdup(sys::getDefaultTargetTriple().c_str()); } +char *LLVMGetHostCPUName(void) { + return strdup(sys::getHostCPUName().data()); +} + +char *LLVMGetHostCPUFeatures(void) { + SubtargetFeatures Features; + StringMap HostFeatures; + + if (sys::getHostCPUFeatures(HostFeatures)) + for (auto &F : HostFeatures) + Features.AddFeature(F.first(), F.second); + + return strdup(Features.getString().c_str()); +} + void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM) { unwrap(PM)->add( createTargetTransformInfoWrapperPass(unwrap(T)->getTargetIRAnalysis())); -- cgit v1.2.3