LLVM 18.0.0git
TargetMachineC.cpp
Go to the documentation of this file.
1//===-- TargetMachine.cpp -------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the LLVM-C part of TargetMachine.h
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm-c/Core.h"
16#include "llvm/IR/DataLayout.h"
18#include "llvm/IR/Module.h"
26#include <cstring>
27#include <optional>
28
29using namespace llvm;
30
32 return reinterpret_cast<TargetMachine *>(P);
33}
35 return reinterpret_cast<Target*>(P);
36}
38 return reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine *>(P));
39}
40static LLVMTargetRef wrap(const Target * P) {
41 return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
42}
43
45 if (TargetRegistry::targets().begin() == TargetRegistry::targets().end()) {
46 return nullptr;
47 }
48
49 const Target *target = &*TargetRegistry::targets().begin();
50 return wrap(target);
51}
53 return wrap(unwrap(T)->getNext());
54}
55
57 StringRef NameRef = Name;
59 [&](const Target &T) { return T.getName() == NameRef; });
60 return I != TargetRegistry::targets().end() ? wrap(&*I) : nullptr;
61}
62
64 char **ErrorMessage) {
65 std::string Error;
66
68
69 if (!*T) {
70 if (ErrorMessage)
71 *ErrorMessage = strdup(Error.c_str());
72
73 return 1;
74 }
75
76 return 0;
77}
78
80 return unwrap(T)->getName();
81}
82
84 return unwrap(T)->getShortDescription();
85}
86
88 return unwrap(T)->hasJIT();
89}
90
92 return unwrap(T)->hasTargetMachine();
93}
94
96 return unwrap(T)->hasMCAsmBackend();
97}
98
100 const char *Triple, const char *CPU, const char *Features,
102 LLVMCodeModel CodeModel) {
103 std::optional<Reloc::Model> RM;
104 switch (Reloc){
105 case LLVMRelocStatic:
106 RM = Reloc::Static;
107 break;
108 case LLVMRelocPIC:
109 RM = Reloc::PIC_;
110 break;
113 break;
114 case LLVMRelocROPI:
115 RM = Reloc::ROPI;
116 break;
117 case LLVMRelocRWPI:
118 RM = Reloc::RWPI;
119 break;
121 RM = Reloc::ROPI_RWPI;
122 break;
123 default:
124 break;
125 }
126
127 bool JIT;
128 std::optional<CodeModel::Model> CM = unwrap(CodeModel, JIT);
129
131 switch (Level) {
133 OL = CodeGenOptLevel::None;
134 break;
136 OL = CodeGenOptLevel::Less;
137 break;
139 OL = CodeGenOptLevel::Aggressive;
140 break;
141 default:
142 OL = CodeGenOptLevel::Default;
143 break;
144 }
145
147 return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM, CM,
148 OL, JIT));
149}
150
152
154 const Target* target = &(unwrap(T)->getTarget());
155 return wrap(target);
156}
157
159 std::string StringRep = unwrap(T)->getTargetTriple().str();
160 return strdup(StringRep.c_str());
161}
162
164 std::string StringRep = std::string(unwrap(T)->getTargetCPU());
165 return strdup(StringRep.c_str());
166}
167
169 std::string StringRep = std::string(unwrap(T)->getTargetFeatureString());
170 return strdup(StringRep.c_str());
171}
172
174 LLVMBool VerboseAsm) {
175 unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm;
176}
177
179 return wrap(new DataLayout(unwrap(T)->createDataLayout()));
180}
181
184 LLVMCodeGenFileType codegen,
185 char **ErrorMessage) {
187 Module* Mod = unwrap(M);
188
190
191 std::string error;
192
193 Mod->setDataLayout(TM->createDataLayout());
194
196 switch (codegen) {
197 case LLVMAssemblyFile:
198 ft = CodeGenFileType::AssemblyFile;
199 break;
200 default:
201 ft = CodeGenFileType::ObjectFile;
202 break;
203 }
204 if (TM->addPassesToEmitFile(pass, OS, nullptr, ft)) {
205 error = "TargetMachine can't emit a file of this type";
206 *ErrorMessage = strdup(error.c_str());
207 return true;
208 }
209
210 pass.run(*Mod);
211
212 OS.flush();
213 return false;
214}
215
217 const char *Filename,
218 LLVMCodeGenFileType codegen,
219 char **ErrorMessage) {
220 std::error_code EC;
221 raw_fd_ostream dest(Filename, EC, sys::fs::OF_None);
222 if (EC) {
223 *ErrorMessage = strdup(EC.message().c_str());
224 return true;
225 }
226 bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage);
227 dest.flush();
228 return Result;
229}
230
232 LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
233 LLVMMemoryBufferRef *OutMemBuf) {
234 SmallString<0> CodeString;
235 raw_svector_ostream OStream(CodeString);
236 bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage);
237
238 StringRef Data = OStream.str();
239 *OutMemBuf =
241 return Result;
242}
243
245 return strdup(sys::getDefaultTargetTriple().c_str());
246}
247
248char *LLVMNormalizeTargetTriple(const char* triple) {
249 return strdup(Triple::normalize(StringRef(triple)).c_str());
250}
251
253 return strdup(sys::getHostCPUName().data());
254}
255
257 SubtargetFeatures Features;
258 StringMap<bool> HostFeatures;
259
260 if (sys::getHostCPUFeatures(HostFeatures))
261 for (const auto &[Feature, IsEnabled] : HostFeatures)
262 Features.AddFeature(Feature, IsEnabled);
263
264 return strdup(Features.getString().c_str());
265}
266
268 unwrap(PM)->add(
269 createTargetTransformInfoWrapperPass(unwrap(T)->getTargetIRAnalysis()));
270}
arm prera ldst opt
std::string Name
#define I(x, y, z)
Definition: MD5.cpp:58
Module.h This file contains the declarations for the Module class.
modulo schedule Modulo Schedule test pass
static std::unique_ptr< TargetMachine > createTargetMachine(Function *F, CodeGenOptLevel OptLevel)
Create the TargetMachine object to query the backend for optimization preferences.
#define P(N)
Module * Mod
const char LLVMTargetMachineRef TM
raw_pwrite_stream & OS
#define error(X)
static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M, raw_pwrite_stream &OS, LLVMCodeGenFileType codegen, char **ErrorMessage)
static TargetMachine * unwrap(LLVMTargetMachineRef P)
static LLVMTargetMachineRef wrap(const TargetMachine *P)
This pass exposes codegen information to IR-level passes.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
void setDataLayout(StringRef Desc)
Set the data layout.
Definition: Module.cpp:392
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:112
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Manages the enabling and disabling of subtarget specific features.
std::string getString() const
Returns features as a string.
void AddFeature(StringRef String, bool Enable=true)
Adds Features.
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:78
const Triple & getTargetTriple() const
TargetOptions Options
const Target & getTarget() const
MCTargetOptions MCOptions
Machine level options.
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
std::string normalize() const
Return the normalized form of this triple's string.
Definition: Triple.h:348
const std::string & str() const
Definition: Triple.h:414
PassManager manages ModulePassManagers.
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:454
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:428
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:672
StringRef str() const
Return a StringRef for the vector contents.
Definition: raw_ostream.h:697
LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(const char *InputData, size_t InputDataLength, const char *BufferName)
Definition: Core.cpp:4126
int LLVMBool
Definition: Types.h:28
struct LLVMOpaquePassManager * LLVMPassManagerRef
Definition: Types.h:127
struct LLVMOpaqueMemoryBuffer * LLVMMemoryBufferRef
LLVM uses a polymorphic type hierarchy which C cannot represent, therefore parameters must be passed ...
Definition: Types.h:48
struct LLVMOpaqueModule * LLVMModuleRef
The top-level container for all other LLVM Intermediate Representation (IR) objects.
Definition: Types.h:61
LLVMCodeGenFileType
Definition: TargetMachine.h:64
char * LLVMGetHostCPUFeatures(void)
Get the host CPU's features as a string.
LLVMTargetRef LLVMGetFirstTarget()
Returns the first llvm::Target in the registered targets list.
struct LLVMTarget * LLVMTargetRef
Definition: TargetMachine.h:35
char * LLVMGetHostCPUName(void)
Get the host CPU as a string.
struct LLVMOpaqueTargetMachine * LLVMTargetMachineRef
Definition: TargetMachine.h:34
LLVMCodeModel
Definition: TargetMachine.h:54
char * LLVMGetTargetMachineCPU(LLVMTargetMachineRef T)
Returns the cpu used creating this target machine.
const char * LLVMGetTargetName(LLVMTargetRef T)
Returns the name of a target.
void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM)
Adds the target-specific analysis passes to the pass manager.
LLVMTargetRef LLVMGetTargetFromName(const char *Name)
Finds the target corresponding to the given name and stores it in T.
const char * LLVMGetTargetDescription(LLVMTargetRef T)
Returns the description of a target.
char * LLVMGetDefaultTargetTriple(void)
Get a triple for the host machine as a string.
void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T, LLVMBool VerboseAsm)
Set the target machine's ASM verbosity.
LLVMBool LLVMGetTargetFromTriple(const char *TripleStr, LLVMTargetRef *T, char **ErrorMessage)
Finds the target corresponding to the given triple and stores it in T.
char * LLVMNormalizeTargetTriple(const char *triple)
Normalize a target triple.
LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T)
Returns if the target as an ASM backend (required for emitting output)
LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M, const char *Filename, LLVMCodeGenFileType codegen, char **ErrorMessage)
Emits an asm or object file for the given module to the filename.
char * LLVMGetTargetMachineTriple(LLVMTargetMachineRef T)
Returns the triple used creating this target machine.
LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T, const char *Triple, const char *CPU, const char *Features, LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc, LLVMCodeModel CodeModel)
Creates a new llvm::TargetMachine.
LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T)
Returns the next llvm::Target given a previous one (or null if there's none)
LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T, LLVMModuleRef M, LLVMCodeGenFileType codegen, char **ErrorMessage, LLVMMemoryBufferRef *OutMemBuf)
Compile the LLVM IR stored in M and store the result in OutMemBuf.
LLVMTargetDataRef LLVMCreateTargetDataLayout(LLVMTargetMachineRef T)
Create a DataLayout based on the targetMachine.
LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T)
Returns if the target has a TargetMachine associated.
LLVMRelocMode
Definition: TargetMachine.h:44
LLVMCodeGenOptLevel
Definition: TargetMachine.h:37
struct LLVMOpaqueTargetData * LLVMTargetDataRef
Definition: Target.h:37
LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T)
Returns the Target used in a TargetMachine.
void LLVMDisposeTargetMachine(LLVMTargetMachineRef T)
Dispose the LLVMTargetMachineRef instance generated by LLVMCreateTargetMachine.
LLVMBool LLVMTargetHasJIT(LLVMTargetRef T)
Returns if the target has a JIT.
char * LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T)
Returns the feature string used creating this target machine.
@ LLVMAssemblyFile
Definition: TargetMachine.h:65
@ LLVMRelocPIC
Definition: TargetMachine.h:47
@ LLVMRelocDynamicNoPic
Definition: TargetMachine.h:48
@ LLVMRelocStatic
Definition: TargetMachine.h:46
@ LLVMRelocRWPI
Definition: TargetMachine.h:50
@ LLVMRelocROPI
Definition: TargetMachine.h:49
@ LLVMRelocROPI_RWPI
Definition: TargetMachine.h:51
@ LLVMCodeGenLevelAggressive
Definition: TargetMachine.h:41
@ LLVMCodeGenLevelNone
Definition: TargetMachine.h:38
@ LLVMCodeGenLevelLess
Definition: TargetMachine.h:39
@ DynamicNoPIC
Definition: CodeGen.h:25
@ ROPI_RWPI
Definition: CodeGen.h:25
StringRef getHostCPUName()
getHostCPUName - Get the LLVM name for the host CPU.
Definition: Host.cpp:1616
bool getHostCPUFeatures(StringMap< bool, MallocAllocator > &Features)
getHostCPUFeatures - Get the LLVM names for the host CPU features.
std::string getDefaultTargetTriple()
getDefaultTargetTriple() - Return the default target triple the compiler has been configured to produ...
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
CodeGenFileType
These enums are meant to be passed into addPassesToEmitFile to indicate what type of file to emit,...
Definition: CodeGen.h:83
CodeGenOptLevel
Code generation optimization level.
Definition: CodeGen.h:54
ImmutablePass * createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA)
Create an analysis pass wrapper around a TTI object.
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1754
static const Target * lookupTarget(StringRef Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
static iterator_range< iterator > targets()