1 #ifndef CPPAD_CG_LLVM_MODEL_LIBRARY_PROCESSOR_INCLUDED 2 #define CPPAD_CG_LLVM_MODEL_LIBRARY_PROCESSOR_INCLUDED 18 #include <cppad/cg/model/llvm/llvm_base_model_library_processor.hpp> 31 std::vector<std::string> _includePaths;
32 std::unique_ptr<llvm::Linker> _linker;
33 std::unique_ptr<llvm::LLVMContext> _context;
47 inline void setIncludePaths(
const std::vector<std::string>& includePaths) {
48 _includePaths = includePaths;
51 inline const std::vector<std::string>& getIncludePaths()
const {
55 std::unique_ptr<LlvmModelLibrary<Base>> create() {
61 this->modelLibraryHelper_->startingJob(
"", JobTimer::JIT_MODEL_LIBRARY);
63 llvm::InitializeAllTargets();
64 llvm::InitializeAllAsmPrinters();
66 _context.reset(
new llvm::LLVMContext());
68 const std::map<std::string, ModelCSourceGen<Base>*>& models = this->modelLibraryHelper_->getModels();
69 for (
const auto& p : models) {
70 const std::map<std::string, std::string>& modelSources = this->getSources(*p.second);
71 createLlvmModules(modelSources);
74 const std::map<std::string, std::string>& sources = this->getLibrarySources();
75 createLlvmModules(sources);
77 const std::map<std::string, std::string>& customSource = this->modelLibraryHelper_->getCustomSources();
78 createLlvmModules(customSource);
80 llvm::InitializeNativeTarget();
82 std::unique_ptr<LlvmModelLibrary<Base>> lib (
new LlvmModelLibrary3_2<Base>(_linker->releaseModule(), _context.release()));
84 this->modelLibraryHelper_->finishedJob();
96 virtual void createLlvmModules(
const std::map<std::string, std::string>& sources) {
97 for (
const auto& p : sources) {
98 createLlvmModule(p.first, p.second);
102 virtual void createLlvmModule(
const std::string& filename,
103 const std::string& source) {
104 using namespace llvm;
105 using namespace clang;
107 static const char* argv [] = {
"program",
"-Wall",
"-x",
"c",
"string-input"};
108 static const int argc =
sizeof (argv) /
sizeof (argv[0]);
110 IntrusiveRefCntPtr<DiagnosticOptions> diagOpts =
new DiagnosticOptions();
111 TextDiagnosticPrinter *diagClient =
new TextDiagnosticPrinter(llvm::errs(), &*diagOpts);
112 IntrusiveRefCntPtr<DiagnosticIDs> diagID(
new DiagnosticIDs());
113 IntrusiveRefCntPtr<DiagnosticsEngine> diags(
new DiagnosticsEngine(diagID, &*diagOpts, diagClient));
115 ArrayRef<const char *> args(argv + 1,
117 std::unique_ptr<CompilerInvocation> invocation(createInvocationFromCommandLine(args, diags));
118 if (invocation.get() ==
nullptr)
119 throw CGException(
"Failed to create compiler invocation");
120 CompilerInvocation::setLangDefaults(*invocation->getLangOpts(), IK_C,
121 LangStandard::lang_unspecified);
122 invocation->getFrontendOpts().DisableFree =
false;
125 CompilerInstance compiler;
126 compiler.setInvocation(invocation.release());
129 compiler.createDiagnostics(argc, const_cast<char**> (argv));
130 if (!compiler.hasDiagnostics())
134 llvm::MemoryBuffer * buffer = llvm::MemoryBuffer::getMemBufferCopy(source,
"SIMPLE_BUFFER");
135 if (buffer ==
nullptr)
136 throw CGException(
"Failed to create memory buffer");
139 PreprocessorOptions& po = compiler.getInvocation().getPreprocessorOpts();
140 po.addRemappedFile(
"string-input", buffer);
142 HeaderSearchOptions& hso = compiler.getInvocation().getHeaderSearchOpts();
143 for (
size_t s = 0; s < _includePaths.size(); s++)
144 hso.AddPath(llvm::StringRef(_includePaths[s]), clang::frontend::Angled,
true,
false,
false);
147 OwningPtr<CodeGenAction> action(
new clang::EmitLLVMOnlyAction(_context.get()));
148 if (!compiler.ExecuteAction(*action))
151 llvm::Module* module = action->takeModule();
152 if (module ==
nullptr)
155 if (_linker.get() ==
nullptr) {
156 _linker.reset(
new llvm::Linker(std::string(
"MyLinker"), module));
158 std::string errorMsg;
159 if (_linker->LinkInModule(module, &errorMsg)) {
169 inline llvm::Module* mergeModules(
const std::vector<llvm::Module*>& modules) {
173 std::string progName(
"MyLinker");
174 std::unique_ptr<llvm::Linker> ld(
new llvm::Linker(progName, modules[0]));
176 for (
size_t m = 1; m < modules.size(); m++) {
177 std::string errorMsg;
178 if (ld->LinkInModule(modules[m], &errorMsg)) {
183 return ld->releaseModule();
LlvmModelLibraryProcessor(ModelLibraryCSourceGen< Base > &modelLibraryHelper)