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> 29 class LlvmModelLibraryProcessor :
public LlvmBaseModelLibraryProcessor<Base> {
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() {
68 std::unique_ptr<LlvmModelLibrary<Base>> lib;
70 this->modelLibraryHelper_->startingJob(
"", JobTimer::JIT_MODEL_LIBRARY);
76 const std::set<std::string>& bcFiles = this->
createBitCode(clang,
"3.4");
81 llvm::InitializeAllTargets();
82 llvm::InitializeAllAsmPrinters();
84 _context.reset(
new llvm::LLVMContext());
86 for (
const std::string& itbc : bcFiles) {
88 OwningPtr<MemoryBuffer> buffer;
90 error_code ec = MemoryBuffer::getFile(itbc, buffer);
91 if (buffer.get() ==
nullptr)
96 Module* module = llvm::ParseBitcodeFile(buffer.get(), *_context.get(), &errMsg);
98 throw CGException(
"Failed to create LLVM bitcode: ", errMsg);
101 if (_linker.get() ==
nullptr) {
102 _linker.reset(
new llvm::Linker(module));
104 if (_linker->linkInModule(module, &errMsg)) {
110 llvm::InitializeNativeTarget();
121 this->modelLibraryHelper_->finishedJob();
137 static void createnPrintModule() {
138 using namespace llvm;
139 using namespace clang;
147 static const char* argv [] = {
"program",
"-Wall",
"-x",
"c",
"string-input"};
148 static const int argc =
sizeof (argv) /
sizeof (argv[0]);
150 IntrusiveRefCntPtr<DiagnosticOptions> diagOpts =
new DiagnosticOptions();
151 TextDiagnosticPrinter *diagClient =
new TextDiagnosticPrinter(llvm::errs(), &*diagOpts);
152 IntrusiveRefCntPtr<DiagnosticIDs> diagID(
new DiagnosticIDs());
153 IntrusiveRefCntPtr<DiagnosticsEngine> diags(
new DiagnosticsEngine(diagID, &*diagOpts, diagClient));
155 ArrayRef<const char *> args(argv + 1,
157 std::unique_ptr<CompilerInvocation> invocation(createInvocationFromCommandLine(args, diags));
158 if (invocation.get() ==
nullptr)
159 throw CGException(
"Failed to create compiler invocation");
160 CompilerInvocation::setLangDefaults(*invocation->getLangOpts(), IK_C,
161 LangStandard::lang_unspecified);
162 invocation->getFrontendOpts().DisableFree =
false;
165 CompilerInstance compiler;
166 compiler.setInvocation(invocation.release());
169 compiler.createDiagnostics();
170 if (!compiler.hasDiagnostics())
174 llvm::MemoryBuffer * buffer = llvm::MemoryBuffer::getMemBufferCopy(source,
"SIMPLE_BUFFER");
175 if (buffer ==
nullptr)
176 throw CGException(
"Failed to create memory buffer");
179 PreprocessorOptions& po = compiler.getInvocation().getPreprocessorOpts();
180 po.addRemappedFile(
"string-input", buffer);
182 HeaderSearchOptions& hso = compiler.getInvocation().getHeaderSearchOpts();
183 for (
size_t s = 0; s < _includePaths.size(); s++)
184 hso.AddPath(llvm::StringRef(_includePaths[s]), clang::frontend::Angled,
true,
false);
187 OwningPtr<CodeGenAction> action(
new clang::EmitLLVMOnlyAction(_context.get()));
189 if (!compiler.ExecuteAction(*action))
192 llvm::Module* module = action->takeModule();
193 if (module ==
nullptr)
200 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)