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> 30 class LlvmModelLibraryProcessor :
public LlvmBaseModelLibraryProcessor<Base> {
32 const std::string _version;
33 std::vector<std::string> _includePaths;
34 std::unique_ptr<llvm::Linker> _linker;
35 std::unique_ptr<llvm::LLVMContext> _context;
56 inline void setIncludePaths(
const std::vector<std::string>& includePaths) {
57 _includePaths = includePaths;
60 inline const std::vector<std::string>& getIncludePaths()
const {
64 std::unique_ptr<LlvmModelLibrary<Base>> create() {
77 std::unique_ptr<LlvmModelLibrary<Base>> lib;
79 this->modelLibraryHelper_->startingJob(
"", JobTimer::JIT_MODEL_LIBRARY);
85 const std::set<std::string>& bcFiles = this->
createBitCode(clang,
"3.4");
90 llvm::InitializeAllTargets();
91 llvm::InitializeAllAsmPrinters();
93 _context.reset(
new llvm::LLVMContext());
95 for (
const std::string& itbc : bcFiles) {
97 OwningPtr<MemoryBuffer> buffer;
99 error_code ec = MemoryBuffer::getFile(itbc, buffer);
100 if (buffer.get() ==
nullptr)
105 Module* module = llvm::ParseBitcodeFile(buffer.get(), *_context.get(), &errMsg);
106 if(module ==
nullptr)
107 throw CGException(
"Failed to create LLVM bitcode: ", errMsg);
110 if (_linker.get() ==
nullptr) {
111 _linker.reset(
new llvm::Linker(module));
113 if (_linker->linkInModule(module, &errMsg)) {
119 llvm::InitializeNativeTarget();
130 this->modelLibraryHelper_->finishedJob();
146 static void createnPrintModule() {
147 using namespace llvm;
148 using namespace clang;
156 static const char* argv [] = {
"program",
"-Wall",
"-x",
"c",
"string-input"};
157 static const int argc =
sizeof (argv) /
sizeof (argv[0]);
159 IntrusiveRefCntPtr<DiagnosticOptions> diagOpts =
new DiagnosticOptions();
160 TextDiagnosticPrinter *diagClient =
new TextDiagnosticPrinter(llvm::errs(), &*diagOpts);
161 IntrusiveRefCntPtr<DiagnosticIDs> diagID(
new DiagnosticIDs());
162 IntrusiveRefCntPtr<DiagnosticsEngine> diags(
new DiagnosticsEngine(diagID, &*diagOpts, diagClient));
164 ArrayRef<const char *> args(argv + 1,
166 std::unique_ptr<CompilerInvocation> invocation(createInvocationFromCommandLine(args, diags));
167 if (invocation.get() ==
nullptr)
168 throw CGException(
"Failed to create compiler invocation");
169 CompilerInvocation::setLangDefaults(*invocation->getLangOpts(), IK_C,
170 LangStandard::lang_unspecified);
171 invocation->getFrontendOpts().DisableFree =
false;
174 CompilerInstance compiler;
175 compiler.setInvocation(invocation.release());
178 compiler.createDiagnostics();
179 if (!compiler.hasDiagnostics())
183 llvm::MemoryBuffer * buffer = llvm::MemoryBuffer::getMemBufferCopy(source,
"SIMPLE_BUFFER");
184 if (buffer ==
nullptr)
185 throw CGException(
"Failed to create memory buffer");
188 PreprocessorOptions& po = compiler.getInvocation().getPreprocessorOpts();
189 po.addRemappedFile(
"string-input", buffer);
191 HeaderSearchOptions& hso = compiler.getInvocation().getHeaderSearchOpts();
192 for (
size_t s = 0; s < _includePaths.size(); s++)
193 hso.AddPath(llvm::StringRef(_includePaths[s]), clang::frontend::Angled,
true,
false);
196 OwningPtr<CodeGenAction> action(
new clang::EmitLLVMOnlyAction(_context.get()));
198 if (!compiler.ExecuteAction(*action))
201 llvm::Module* module = action->takeModule();
202 if (module ==
nullptr)
209 raw_fd_ostream os(STDOUT_FILENO,
true);
std::unique_ptr< LlvmModelLibrary< Base > > create(ClangCompiler< Base > &clang)
const std::set< std::string > & createBitCode(ClangCompiler< Base > &clang, const std::string &version)
LlvmModelLibraryProcessor(ModelLibraryCSourceGen< Base > &modelLibraryHelper)
const std::string & getVersion() const