1 #ifndef CPPAD_CG_LLVM_BASE_MODEL_LIBRARY_PROCESSOR_IMPL_INCLUDED 2 #define CPPAD_CG_LLVM_BASE_MODEL_LIBRARY_PROCESSOR_IMPL_INCLUDED 19 #include <cppad/cg/model/llvm/llvm_base_model_library_processor.hpp> 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;
45 const std::string& version) :
56 _includePaths = includePaths;
70 std::unique_ptr<LlvmModelLibrary<Base>>
create() {
74 _linker.reset(
nullptr);
76 this->modelLibraryHelper_->startingJob(
"", JobTimer::JIT_MODEL_LIBRARY);
78 llvm::InitializeAllTargetMCs();
79 llvm::InitializeAllTargets();
80 llvm::InitializeAllAsmPrinters();
82 _context.reset(
new llvm::LLVMContext());
84 const std::map<std::string, ModelCSourceGen<Base>*>& models = this->modelLibraryHelper_->getModels();
85 for (
const auto& p : models) {
86 const std::map<std::string, std::string>& modelSources = this->getSources(*p.second);
87 createLlvmModules(modelSources);
90 const std::map<std::string, std::string>& sources = this->getLibrarySources();
91 createLlvmModules(sources);
93 const std::map<std::string, std::string>& customSource = this->modelLibraryHelper_->getCustomSources();
94 createLlvmModules(customSource);
96 llvm::InitializeNativeTarget();
100 this->modelLibraryHelper_->finishedJob();
113 using namespace llvm;
120 std::unique_ptr<LlvmModelLibrary<Base>> lib;
122 this->modelLibraryHelper_->startingJob(
"", JobTimer::JIT_MODEL_LIBRARY);
128 const std::set<std::string>& bcFiles = this->
createBitCode(clang, _version);
133 llvm::InitializeAllTargets();
134 llvm::InitializeAllAsmPrinters();
136 _context.reset(
new llvm::LLVMContext());
138 std::unique_ptr<Module> linkerModule;
140 for (
const std::string& itbc : bcFiles) {
143 ErrorOr<std::unique_ptr<MemoryBuffer>> buffer = MemoryBuffer::getFile(itbc);
144 if (buffer.get() ==
nullptr) {
149 Expected<std::unique_ptr<Module>> moduleOrError = llvm::parseBitcodeFile(buffer.get()->getMemBufferRef(), *_context.get());
150 if (!moduleOrError) {
151 std::ostringstream error;
153 handleAllErrors(moduleOrError.takeError(), [&](ErrorInfoBase& eib) {
154 if (nError > 0) error <<
"; ";
155 error << eib.message();
162 if (_linker.get() ==
nullptr) {
163 linkerModule = std::move(moduleOrError.get());
164 _linker.reset(
new llvm::Linker(*linkerModule));
166 if (_linker->linkInModule(std::move(moduleOrError.get()))) {
172 llvm::InitializeNativeTarget();
183 this->modelLibraryHelper_->finishedJob();
190 virtual void createLlvmModules(
const std::map<std::string, std::string>& sources) {
191 for (
const auto& p : sources) {
192 createLlvmModule(p.first, p.second);
196 virtual void createLlvmModule(
const std::string& filename,
197 const std::string& source) {
198 using namespace llvm;
199 using namespace clang;
201 ArrayRef<StringRef> paths;
202 llvm::sys::findProgramByName(
"clang", paths);
204 IntrusiveRefCntPtr<DiagnosticOptions> diagOpts =
new DiagnosticOptions();
205 TextDiagnosticPrinter* diagClient =
new TextDiagnosticPrinter(llvm::errs(), &*diagOpts);
206 IntrusiveRefCntPtr<DiagnosticIDs> diagID(
new DiagnosticIDs());
207 IntrusiveRefCntPtr<DiagnosticsEngine> diags(
new DiagnosticsEngine(diagID, &*diagOpts, diagClient));
209 ArrayRef<const char*> args {
"-Wall",
"-x",
"c",
"string-input"};
210 std::shared_ptr<CompilerInvocation> invocation(createInvocationFromCommandLine(args, diags));
211 if (invocation.get() ==
nullptr)
212 throw CGException(
"Failed to create compiler invocation");
216 CompilerInvocation::setLangDefaults(*invocation->getLangOpts(), InputKind::C,
217 llvm::Triple(invocation->TargetOpts->Triple),
218 invocation->getPreprocessorOpts(),
219 LangStandard::lang_unspecified);
220 invocation->getFrontendOpts().DisableFree =
false;
223 CompilerInstance compiler;
224 compiler.setInvocation(invocation);
228 compiler.createDiagnostics();
229 if (!compiler.hasDiagnostics())
233 std::unique_ptr<llvm::MemoryBuffer> buffer = llvm::MemoryBuffer::getMemBufferCopy(source,
"SIMPLE_BUFFER");
234 if (buffer.get() ==
nullptr)
235 throw CGException(
"Failed to create memory buffer");
238 PreprocessorOptions& po = compiler.getInvocation().getPreprocessorOpts();
239 po.addRemappedFile(
"string-input", buffer.release());
241 HeaderSearchOptions& hso = compiler.getInvocation().getHeaderSearchOpts();
242 std::string iClangHeaders = this->findInternalClangCHeaders(_version, hso.ResourceDir);
243 if(!iClangHeaders.empty()) {
244 hso.AddPath(llvm::StringRef(iClangHeaders), clang::frontend::Angled,
false,
false);
247 for (
size_t s = 0; s < _includePaths.size(); s++)
248 hso.AddPath(llvm::StringRef(_includePaths[s]), clang::frontend::Angled,
false,
false);
251 clang::EmitLLVMOnlyAction action(_context.get());
252 if (!compiler.ExecuteAction(action))
255 std::unique_ptr<llvm::Module> module = action.takeModule();
256 if (module.get() ==
nullptr)
259 if (_linker.get() ==
nullptr) {
260 _module.reset(module.release());
261 _linker.reset(
new llvm::Linker(*_module.get()));
263 if (_linker->linkInModule(std::move(module))) {
void setIncludePaths(const std::vector< std::string > &includePaths)
const std::vector< std::string > & getIncludePaths() const
LlvmBaseModelLibraryProcessorImpl(ModelLibraryCSourceGen< Base > &librarySourceGen, const std::string &version)
const std::set< std::string > & createBitCode(ClangCompiler< Base > &clang, const std::string &version)
std::unique_ptr< LlvmModelLibrary< Base > > create()
std::unique_ptr< LlvmModelLibrary< Base > > create(ClangCompiler< Base > &clang)