1#ifndef CPPAD_CG_LLVM_MODEL_LIBRARY_PROCESSOR_INCLUDED
2#define CPPAD_CG_LLVM_MODEL_LIBRARY_PROCESSOR_INCLUDED
19#include <cppad/cg/model/llvm/llvm_base_model_library_processor.hpp>
30class LlvmModelLibraryProcessor :
public LlvmBaseModelLibraryProcessor<Base> {
32 const std::string _version;
33 std::vector<std::string> _includePaths;
34 std::shared_ptr<llvm::LLVMContext> _context;
35 std::unique_ptr<llvm::Linker> _linker;
36 std::unique_ptr<llvm::Module> _module;
77 std::unique_ptr<LlvmModelLibrary<Base>>
create() {
81 _linker.reset(
nullptr);
83 this->modelLibraryHelper_->startingJob(
"", JobTimer::JIT_MODEL_LIBRARY);
85 llvm::InitializeAllTargetMCs();
86 llvm::InitializeAllTargets();
87 llvm::InitializeAllAsmPrinters();
89 _context.reset(
new llvm::LLVMContext());
91 const std::map<std::string, ModelCSourceGen<Base>*>&
models = this->modelLibraryHelper_->getModels();
93 const std::map<std::string, std::string>&
modelSources = this->getSources(*
p.second);
97 const std::map<std::string, std::string>&
sources = this->getLibrarySources();
100 const std::map<std::string, std::string>&
customSource = this->modelLibraryHelper_->getCustomSources();
103 llvm::InitializeNativeTarget();
107 this->modelLibraryHelper_->finishedJob();
120 using namespace llvm;
127 std::unique_ptr<LlvmModelLibrary<Base>>
lib;
129 this->modelLibraryHelper_->startingJob(
"", JobTimer::JIT_MODEL_LIBRARY);
140 llvm::InitializeAllTargets();
141 llvm::InitializeAllAsmPrinters();
143 _context.reset(
new llvm::LLVMContext());
151 if (
buffer.get() ==
nullptr) {
158 std::ostringstream
error;
161 if (nError > 0) error <<
"; ";
162 error << eib.message();
169 if (_linker ==
nullptr) {
179 llvm::InitializeNativeTarget();
190 this->modelLibraryHelper_->finishedJob();
202 virtual void createLlvmModules(
const std::map<std::string, std::string>&
sources) {
204 createLlvmModule(
p.first,
p.second);
208 virtual void createLlvmModule(
const std::string&
filename,
209 const std::string&
source) {
210 using namespace llvm;
211 using namespace clang;
214 llvm::sys::findProgramByName(
"clang",
paths);
224 throw CGException(
"Failed to create compiler invocation");
228 CompilerInvocation::setLangDefaults(*
invocation->getLangOpts(),
IK_C,
231 LangStandard::lang_unspecified);
232 invocation->getFrontendOpts().DisableFree =
false;
242 throw CGException(
"No diagnostics");
245 std::unique_ptr<llvm::MemoryBuffer>
buffer = llvm::MemoryBuffer::getMemBufferCopy(
source,
"SIMPLE_BUFFER");
247 throw CGException(
"Failed to create memory buffer");
251 po.addRemappedFile(
"string-input",
buffer.release());
254 std::string
iClangHeaders = this->findInternalClangCHeaders(
"4.0",
hso.ResourceDir);
256 hso.AddPath(llvm::StringRef(
iClangHeaders), clang::frontend::Angled,
false,
false);
259 for (
size_t s = 0; s < _includePaths.size(); s++)
260 hso.AddPath(llvm::StringRef(_includePaths[s]), clang::frontend::Angled,
false,
false);
263 clang::EmitLLVMOnlyAction
action(_context.get());
265 throw CGException(
"Failed to emit LLVM bitcode");
267 std::unique_ptr<llvm::Module>
module = action.takeModule();
269 throw CGException(
"No module");
271 if (_linker ==
nullptr) {
272 _module.reset(
module.release());
273 _linker.reset(
new llvm::Linker(*_module));
275 if (_linker->linkInModule(std::move(
module))) {
276 throw CGException(
"LLVM failed to link module");
const std::set< std::string > & createBitCode(ClangCompiler< Base > &clang, const std::string &version)
std::unique_ptr< LlvmModelLibrary< Base > > create(ClangCompiler< Base > &clang)
const std::string & getVersion() const
void setIncludePaths(const std::vector< std::string > &includePaths)
std::unique_ptr< LlvmModelLibrary< Base > > create()
const std::vector< std::string > & getIncludePaths() const
LlvmModelLibraryProcessor(ModelLibraryCSourceGen< Base > &librarySourceGen)
bool GreaterThanZero(const cg::CG< Base > &x)