Skip to content

Commit 1ae61a6

Browse files
committedApr 11, 2018
[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
1 parent a557852 commit 1ae61a6

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed
 

‎llvm/include/llvm-c/TargetMachine.h

+8
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T, LLVMModuleR
137137
disposed with LLVMDisposeMessage. */
138138
char* LLVMGetDefaultTargetTriple(void);
139139

140+
/** Get the host CPU as a string. The result needs to be disposed with
141+
LLVMDisposeMessage. */
142+
char* LLVMGetHostCPUName(void);
143+
144+
/** Get the host CPU's features as a string. The result needs to be disposed
145+
with LLVMDisposeMessage. */
146+
char* LLVMGetHostCPUFeatures(void);
147+
140148
/** Adds the target-specific analysis passes to the pass manager. */
141149
void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM);
142150

‎llvm/lib/Target/TargetMachineC.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/IR/DataLayout.h"
1919
#include "llvm/IR/LegacyPassManager.h"
2020
#include "llvm/IR/Module.h"
21+
#include "llvm/MC/SubtargetFeature.h"
2122
#include "llvm/Support/FileSystem.h"
2223
#include "llvm/Support/FormattedStream.h"
2324
#include "llvm/Support/Host.h"
@@ -237,6 +238,21 @@ char *LLVMGetDefaultTargetTriple(void) {
237238
return strdup(sys::getDefaultTargetTriple().c_str());
238239
}
239240

241+
char *LLVMGetHostCPUName(void) {
242+
return strdup(sys::getHostCPUName().data());
243+
}
244+
245+
char *LLVMGetHostCPUFeatures(void) {
246+
SubtargetFeatures Features;
247+
StringMap<bool> HostFeatures;
248+
249+
if (sys::getHostCPUFeatures(HostFeatures))
250+
for (auto &F : HostFeatures)
251+
Features.AddFeature(F.first(), F.second);
252+
253+
return strdup(Features.getString().c_str());
254+
}
255+
240256
void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM) {
241257
unwrap(PM)->add(
242258
createTargetTransformInfoWrapperPass(unwrap(T)->getTargetIRAnalysis()));

0 commit comments

Comments
 (0)
Please sign in to comment.