Unverified Commit 7c59ca2e authored by Yixing Lao's avatar Yixing Lao Committed by GitHub

Remove LLVM/Clang dependency in headers (#341)

* remove llvm/clang dependency in headers

* copy elision
parent 1c5abc19
...@@ -31,6 +31,8 @@ ...@@ -31,6 +31,8 @@
#include <clang/Lex/Preprocessor.h> #include <clang/Lex/Preprocessor.h>
#include <clang/Lex/PreprocessorOptions.h> #include <clang/Lex/PreprocessorOptions.h>
#include <llvm/ADT/Statistic.h> #include <llvm/ADT/Statistic.h>
#include <llvm/ExecutionEngine/MCJIT.h> // forces JIT to link in
#include <llvm/IR/Module.h>
#include <llvm/LinkAllPasses.h> #include <llvm/LinkAllPasses.h>
#include <llvm/Option/Arg.h> #include <llvm/Option/Arg.h>
#include <llvm/Option/ArgList.h> #include <llvm/Option/ArgList.h>
...@@ -72,6 +74,20 @@ using namespace ngraph::codegen; ...@@ -72,6 +74,20 @@ using namespace ngraph::codegen;
static StaticCompiler s_static_compiler; static StaticCompiler s_static_compiler;
static std::mutex m_mutex; static std::mutex m_mutex;
ngraph::codegen::Module::Module(std::unique_ptr<llvm::Module> module)
: m_module(move(module))
{
}
ngraph::codegen::Module::~Module()
{
}
std::unique_ptr<llvm::Module> ngraph::codegen::Module::take_module()
{
return move(m_module);
}
Compiler::Compiler() Compiler::Compiler()
{ {
} }
...@@ -90,7 +106,7 @@ void Compiler::add_header_search_path(const std::string& path) ...@@ -90,7 +106,7 @@ void Compiler::add_header_search_path(const std::string& path)
s_static_compiler.add_header_search_path(path); s_static_compiler.add_header_search_path(path);
} }
std::unique_ptr<llvm::Module> Compiler::compile(const std::string& source) std::unique_ptr<ngraph::codegen::Module> Compiler::compile(const std::string& source)
{ {
lock_guard<mutex> lock(m_mutex); lock_guard<mutex> lock(m_mutex);
return s_static_compiler.compile(compiler_action, source); return s_static_compiler.compile(compiler_action, source);
...@@ -231,7 +247,7 @@ void StaticCompiler::add_header_search_path(const string& path) ...@@ -231,7 +247,7 @@ void StaticCompiler::add_header_search_path(const string& path)
} }
} }
std::unique_ptr<llvm::Module> std::unique_ptr<ngraph::codegen::Module>
StaticCompiler::compile(std::unique_ptr<clang::CodeGenAction>& compiler_action, StaticCompiler::compile(std::unique_ptr<clang::CodeGenAction>& compiler_action,
const string& source) const string& source)
{ {
...@@ -264,7 +280,7 @@ std::unique_ptr<llvm::Module> ...@@ -264,7 +280,7 @@ std::unique_ptr<llvm::Module>
m_compiler->getInvocation().getPreprocessorOpts().clearRemappedFiles(); m_compiler->getInvocation().getPreprocessorOpts().clearRemappedFiles();
return rc; return unique_ptr<ngraph::codegen::Module>(new ngraph::codegen::Module(move(rc)));
} }
void StaticCompiler::generate_pch(const string& source) void StaticCompiler::generate_pch(const string& source)
......
...@@ -17,18 +17,13 @@ ...@@ -17,18 +17,13 @@
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector>
#include <clang/CodeGen/CodeGenAction.h>
#include <llvm/ExecutionEngine/MCJIT.h> // forces JIT to link in
#include <llvm/ExecutionEngine/SectionMemoryManager.h>
#include <llvm/Option/Arg.h>
namespace ngraph namespace ngraph
{ {
namespace codegen namespace codegen
{ {
class module; class Module;
class Compiler; class Compiler;
class StaticCompiler; class StaticCompiler;
class HeaderCache; class HeaderCache;
...@@ -39,11 +34,21 @@ namespace clang ...@@ -39,11 +34,21 @@ namespace clang
{ {
class HeaderSearchOptions; class HeaderSearchOptions;
class CompilerInstance; class CompilerInstance;
class CodeGenAction;
}
namespace llvm
{
class Module;
} }
class ngraph::codegen::module class ngraph::codegen::Module
{ {
public: public:
Module(std::unique_ptr<llvm::Module> module);
~Module();
std::unique_ptr<llvm::Module> take_module();
private: private:
std::unique_ptr<llvm::Module> m_module; std::unique_ptr<llvm::Module> m_module;
}; };
...@@ -55,13 +60,13 @@ public: ...@@ -55,13 +60,13 @@ public:
~Compiler(); ~Compiler();
void set_precompiled_header_source(const std::string& source); void set_precompiled_header_source(const std::string& source);
void add_header_search_path(const std::string& path); void add_header_search_path(const std::string& path);
std::unique_ptr<llvm::Module> compile(const std::string& source); std::unique_ptr<ngraph::codegen::Module> compile(const std::string& source);
std::unique_ptr<clang::CodeGenAction>& get_compiler_action() { return compiler_action; } std::unique_ptr<clang::CodeGenAction>& get_compiler_action() { return compiler_action; }
private: private:
std::unique_ptr<clang::CodeGenAction> compiler_action; std::unique_ptr<clang::CodeGenAction> compiler_action;
}; };
class ngraph::codegen::StaticCompiler : public llvm::SectionMemoryManager class ngraph::codegen::StaticCompiler
{ {
public: public:
StaticCompiler(); StaticCompiler();
...@@ -75,8 +80,8 @@ public: ...@@ -75,8 +80,8 @@ public:
} }
void add_header_search_path(const std::string& path); void add_header_search_path(const std::string& path);
std::unique_ptr<llvm::Module> compile(std::unique_ptr<clang::CodeGenAction>& compiler_action, std::unique_ptr<ngraph::codegen::Module>
const std::string& source); compile(std::unique_ptr<clang::CodeGenAction>& compiler_action, const std::string& source);
void generate_pch(const std::string& source); void generate_pch(const std::string& source);
private: private:
......
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
#include <llvm/ExecutionEngine/ExecutionEngine.h> #include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ExecutionEngine/SectionMemoryManager.h>
#include "ngraph/codegen/execution_engine.hpp" #include "ngraph/codegen/execution_engine.hpp"
...@@ -32,13 +31,13 @@ codegen::ExecutionEngine::~ExecutionEngine() ...@@ -32,13 +31,13 @@ codegen::ExecutionEngine::~ExecutionEngine()
} }
} }
bool codegen::ExecutionEngine::add_module(std::unique_ptr<llvm::Module>& module) bool codegen::ExecutionEngine::add_module(std::unique_ptr<ngraph::codegen::Module>& module)
{ {
if (module) if (module)
{ {
if (!m_execution_engine) if (!m_execution_engine)
{ {
m_execution_engine.reset(llvm::EngineBuilder(move(module)) m_execution_engine.reset(llvm::EngineBuilder(module->take_module())
.setEngineKind(llvm::EngineKind::JIT) .setEngineKind(llvm::EngineKind::JIT)
.setOptLevel(llvm::CodeGenOpt::Aggressive) .setOptLevel(llvm::CodeGenOpt::Aggressive)
.setMCPU(llvm::sys::getHostCPUName()) .setMCPU(llvm::sys::getHostCPUName())
...@@ -74,3 +73,8 @@ void codegen::ExecutionEngine::finalize() ...@@ -74,3 +73,8 @@ void codegen::ExecutionEngine::finalize()
(m_jit_error.empty() ? "Could not create an execution engine" : m_jit_error)); (m_jit_error.empty() ? "Could not create an execution engine" : m_jit_error));
} }
} }
void* codegen::ExecutionEngine::get_pointer_to_named_function(const std::string& func_name)
{
return m_execution_engine->getPointerToNamedFunction(func_name);
}
...@@ -16,9 +16,7 @@ ...@@ -16,9 +16,7 @@
#include <memory> #include <memory>
#include <llvm/ExecutionEngine/MCJIT.h> // forces JIT to link in #include "ngraph/codegen/compiler.hpp"
#include <llvm/ExecutionEngine/SectionMemoryManager.h>
#include <llvm/Option/Arg.h>
namespace ngraph namespace ngraph
{ {
...@@ -28,27 +26,32 @@ namespace ngraph ...@@ -28,27 +26,32 @@ namespace ngraph
} }
} }
namespace llvm
{
class Module;
class ExecutionEngine;
}
class ngraph::codegen::ExecutionEngine class ngraph::codegen::ExecutionEngine
{ {
public: public:
ExecutionEngine(); ExecutionEngine();
~ExecutionEngine(); ~ExecutionEngine();
bool add_module(std::unique_ptr<llvm::Module>& module); bool add_module(std::unique_ptr<ngraph::codegen::Module>& module);
void finalize(); void finalize();
template <typename ftype> template <typename ftype>
std::function<ftype> find_function(const std::string& func_name) std::function<ftype> find_function(const std::string& func_name)
{ {
auto f = m_execution_engine->getPointerToNamedFunction(func_name); return f_cast<ftype>(get_pointer_to_named_function(func_name));
return f_cast<ftype>(f);
} }
private: private:
std::unique_ptr<llvm::ExecutionEngine> m_execution_engine; std::unique_ptr<llvm::ExecutionEngine> m_execution_engine;
std::string m_jit_error; std::string m_jit_error;
void* get_pointer_to_named_function(const std::string& func_name);
template <typename signature> template <typename signature>
std::function<signature> f_cast(void* f) std::function<signature> f_cast(void* f)
{ {
......
...@@ -76,7 +76,6 @@ ...@@ -76,7 +76,6 @@
#include "ngraph/ops/tan.hpp" #include "ngraph/ops/tan.hpp"
#include "ngraph/ops/tanh.hpp" #include "ngraph/ops/tanh.hpp"
#include "ngraph/ops/xla_get_tuple_element.hpp" #include "ngraph/ops/xla_get_tuple_element.hpp"
#include "ngraph/ops/xla_get_tuple_element.hpp"
#include "ngraph/ops/xla_tuple.hpp" #include "ngraph/ops/xla_tuple.hpp"
#include "ngraph/pass/assign_layout.hpp" #include "ngraph/pass/assign_layout.hpp"
#include "ngraph/pass/dump_sorted.hpp" #include "ngraph/pass/dump_sorted.hpp"
...@@ -571,13 +570,13 @@ using namespace ngraph::runtime; ...@@ -571,13 +570,13 @@ using namespace ngraph::runtime;
m_compiler->set_precompiled_header_source(pch_header_source); m_compiler->set_precompiled_header_source(pch_header_source);
auto llvm_module = m_compiler->compile(code); auto codegen_module = m_compiler->compile(code);
if (llvm_module == nullptr) if (codegen_module == nullptr)
{ {
throw runtime_error("function failed to compile"); throw runtime_error("function failed to compile");
} }
m_execution_engine->add_module(llvm_module); m_execution_engine->add_module(codegen_module);
m_execution_engine->finalize(); m_execution_engine->finalize();
m_compiled_function = m_execution_engine->find_function<EntryPoint_t>(function_name); m_compiled_function = m_execution_engine->find_function<EntryPoint_t>(function_name);
assert(m_compiled_function); assert(m_compiled_function);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment