Unverified Commit ee1f6929 authored by Robert Kimball's avatar Robert Kimball Committed by GitHub

Merge pull request #251 from NervanaSystems/bob/pch

Bob/pch
parents 8ff5c586 d98771f8
......@@ -87,6 +87,11 @@ endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
# Set true if CPU backend is built by default
if (NOT DEFINED NGRAPH_CPU_ENABLE)
SET(NGRAPH_CPU_ENABLE TRUE)
endif()
#-----------------------------------------------------------------------------------------------
# External projects install directory
#-----------------------------------------------------------------------------------------------
......
......@@ -42,6 +42,7 @@
#include <clang/Basic/TargetInfo.h>
#include <clang/CodeGen/CodeGenAction.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/FrontendActions.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <llvm/Support/TargetSelect.h>
......@@ -73,6 +74,11 @@ Compiler::~Compiler()
{
}
void Compiler::set_precompiled_header_source(const std::string& source)
{
s_static_compiler.set_precompiled_header_source(source);
}
std::unique_ptr<llvm::Module> Compiler::compile(const std::string& source)
{
lock_guard<mutex> lock(m_mutex);
......@@ -88,14 +94,10 @@ static std::string GetExecutablePath(const char* Argv0)
}
StaticCompiler::StaticCompiler()
: m_precompiled_headers_enabled(false)
: m_precompiled_header_valid(false)
, m_debuginfo_enabled(false)
, m_source_name("code.cpp")
{
#if NGCPU_PCH
m_precompiled_headers_enabled = true;
#endif
#if NGCPU_DEBUGINFO
m_debuginfo_enabled = true;
#endif
......@@ -214,14 +216,6 @@ StaticCompiler::StaticCompiler()
CGO.setDebugInfo(codegenoptions::FullDebugInfo);
}
if (m_precompiled_headers_enabled)
{
// Preprocessor options
auto& PPO = m_compiler->getInvocation().getPreprocessorOpts();
PPO.ImplicitPCHInclude = "ngcpu.pch";
PPO.DisablePCHValidation = 1;
}
// Enable various target features
// Most of these are for Eigen
auto& TO = m_compiler->getInvocation().getTargetOpts();
......@@ -313,6 +307,18 @@ void StaticCompiler::use_cached_files()
std::unique_ptr<llvm::Module> StaticCompiler::compile(const string& source)
{
if (!m_precompiled_header_valid && m_precomiled_header_source.empty() == false)
{
generate_pch(m_precomiled_header_source);
}
if (m_precompiled_header_valid)
{
// Preprocessor options
auto& PPO = m_compiler->getInvocation().getPreprocessorOpts();
PPO.ImplicitPCHInclude = m_pch_path;
PPO.DisablePCHValidation = 0;
}
// Map code filename to a memoryBuffer
StringRef source_ref(source);
unique_ptr<MemoryBuffer> buffer = MemoryBuffer::getMemBufferCopy(source_ref);
......@@ -333,24 +339,24 @@ std::unique_ptr<llvm::Module> StaticCompiler::compile(const string& source)
return rc;
}
// std::unique_ptr<llvm::Module> StaticCompiler::generate_pch(const string& source)
// {
// // Map code filename to a memoryBuffer
// StringRef source_ref(source);
// unique_ptr<MemoryBuffer> buffer = MemoryBuffer::getMemBufferCopy(source_ref);
// m_compiler->getInvocation().getPreprocessorOpts().addRemappedFile(m_source_name, buffer.get());
void StaticCompiler::generate_pch(const string& source)
{
m_pch_path = file_util::path_join(file_util::get_temp_directory(), "ngraph.pch");
m_compiler->getFrontendOpts().OutputFile = m_pch_path;
// // Create and execute action
// CodeGenAction* compilerAction = new GeneratePCHAction();
// std::unique_ptr<llvm::Module> rc;
// if (m_compiler->ExecuteAction(*compilerAction) == true)
// {
// rc = compilerAction->takeModule();
// }
// Map code filename to a memoryBuffer
StringRef source_ref(source);
unique_ptr<MemoryBuffer> buffer = MemoryBuffer::getMemBufferCopy(source_ref);
m_compiler->getInvocation().getPreprocessorOpts().addRemappedFile(m_source_name, buffer.get());
// buffer.release();
// Create and execute action
clang::GeneratePCHAction* compilerAction = new clang::GeneratePCHAction();
if (m_compiler->ExecuteAction(*compilerAction) == true)
{
m_precompiled_header_valid = true;
}
// m_compiler->getInvocation().getPreprocessorOpts().clearRemappedFiles();
buffer.release();
// return rc;
// }
m_compiler->getInvocation().getPreprocessorOpts().clearRemappedFiles();
}
......@@ -51,6 +51,7 @@ class ngraph::codegen::Compiler
public:
Compiler();
~Compiler();
void set_precompiled_header_source(const std::string& source);
std::unique_ptr<llvm::Module> compile(const std::string& source);
private:
......@@ -62,19 +63,24 @@ public:
StaticCompiler();
~StaticCompiler();
void set_precompiled_headers_enabled(bool state) { m_precompiled_headers_enabled = state; }
bool is_precompiled_headers_enabled() { return m_precompiled_headers_enabled; }
void set_debuginfo_enabled(bool state) { m_debuginfo_enabled = state; }
bool is_debuginfo_enabled() { return m_debuginfo_enabled; }
void set_precompiled_header_source(const std::string& source)
{
m_precomiled_header_source = source;
}
void add_header_search_path(const std::string& path);
std::unique_ptr<llvm::Module> compile(const std::string& source);
void generate_pch(const std::string& source);
private:
std::unique_ptr<clang::CompilerInstance> m_compiler;
bool m_precompiled_headers_enabled;
bool m_precompiled_header_valid;
bool m_debuginfo_enabled;
std::string m_source_name;
std::vector<std::string> m_extra_search_path_list;
std::string m_pch_path;
std::string m_precomiled_header_source;
bool is_version_number(const std::string& path);
void use_cached_files();
......
......@@ -197,6 +197,7 @@ void ExternalFunction::compile()
using namespace ngraph::runtime::cpu::eigen;
)";
string pch_header_source = TU.get_code();
TU << "// Declare all functions\n";
for (shared_ptr<Function> f : pass_manager.get_state().get_functions())
......@@ -318,6 +319,8 @@ using namespace ngraph::runtime::cpu::eigen;
codegen::Compiler compiler;
codegen::ExecutionEngine execution_engine;
compiler.set_precompiled_header_source(pch_header_source);
auto llvm_module = compiler.compile(code);
if (llvm_module == nullptr)
{
......
......@@ -57,7 +57,7 @@ if(MKLDNN_INCLUDE_DIR)
set(SRC ${SRC} mkldnn.cpp)
endif()
if(LLVM_INCLUDE_DIR)
if(NGRAPH_CPU_ENABLE AND LLVM_INCLUDE_DIR)
include_directories(SYSTEM ${LLVM_INCLUDE_DIR})
link_directories(${LLVM_LIB_DIR})
set(SRC ${SRC} codegen.cpp)
......
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