Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
N
ngraph
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
ngraph
Commits
d185b48c
Commit
d185b48c
authored
Oct 06, 2017
by
Jaikrishnan Menon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Codegen: Add license header and switch line endings - CRLF -> LF
parent
da80abe6
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
275 additions
and
247 deletions
+275
-247
compiler.cpp
src/ngraph/codegen/compiler.cpp
+200
-186
compiler.hpp
src/ngraph/codegen/compiler.hpp
+75
-61
No files found.
src/ngraph/codegen/compiler.cpp
View file @
d185b48c
#include <iostream>
#include <clang/CodeGen/ObjectFilePCHContainerOperations.h>
#include <clang/Driver/DriverDiagnostic.h>
#include <clang/Driver/Options.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/CompilerInvocation.h>
#include <clang/Frontend/FrontendDiagnostic.h>
#include <clang/Frontend/TextDiagnosticBuffer.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <clang/Frontend/Utils.h>
#include <clang/FrontendTool/Utils.h>
#include <clang/Lex/Preprocessor.h>
#include <clang/Lex/PreprocessorOptions.h>
#include <llvm/ADT/Statistic.h>
#include <llvm/LinkAllPasses.h>
#include <llvm/Option/Arg.h>
#include <llvm/Option/ArgList.h>
#include <llvm/Option/OptTable.h>
#include <llvm/Support/ErrorHandling.h>
#include <llvm/Support/ManagedStatic.h>
#include <llvm/Support/Signals.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Support/Timer.h>
#include <llvm/Support/raw_ostream.h>
#include <clang/Basic/DiagnosticOptions.h>
#include <clang/Basic/TargetInfo.h>
#include <clang/CodeGen/CodeGenAction.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ExecutionEngine/SectionMemoryManager.h>
#include "compiler.hpp"
using
namespace
clang
;
using
namespace
llvm
;
using
namespace
llvm
::
opt
;
using
namespace
std
;
using
namespace
nervana
::
cpu
;
std
::
string
GetExecutablePath
(
const
char
*
Argv0
)
{
// This just needs to be some symbol in the binary; C++ doesn't
// allow taking the address of ::main however.
void
*
MainAddr
=
(
void
*
)(
intptr_t
)
GetExecutablePath
;
return
llvm
::
sys
::
fs
::
getMainExecutable
(
Argv0
,
MainAddr
);
}
execution_state
::
execution_state
()
:
m_execution_engine
{
nullptr
}
{
}
execution_state
::~
execution_state
()
{
// /// Take the LLVM context used by this action.
// llvm::LLVMContext *takeLLVMContext();
// if (m_execution_engine)
// {
// m_execution_engine->runStaticConstructorsDestructors(true);
// }
}
std
::
unique_ptr
<
llvm
::
Module
>
execution_state
::
compile
(
const
string
&
source
,
const
string
&
name
)
{
llvm
::
InitializeAllTargets
();
llvm
::
InitializeAllTargetMCs
();
llvm
::
InitializeAllAsmPrinters
();
llvm
::
InitializeAllAsmParsers
();
// Prepare compilation arguments
vector
<
const
char
*>
args
;
args
.
push_back
(
name
.
c_str
());
// Prepare DiagnosticEngine
DiagnosticOptions
DiagOpts
;
TextDiagnosticPrinter
*
textDiagPrinter
=
new
clang
::
TextDiagnosticPrinter
(
errs
(),
&
DiagOpts
);
IntrusiveRefCntPtr
<
clang
::
DiagnosticIDs
>
pDiagIDs
;
DiagnosticsEngine
*
pDiagnosticsEngine
=
new
DiagnosticsEngine
(
pDiagIDs
,
&
DiagOpts
,
textDiagPrinter
);
// Create and initialize CompilerInstance
std
::
unique_ptr
<
CompilerInstance
>
Clang
(
new
CompilerInstance
());
Clang
->
createDiagnostics
();
// Initialize CompilerInvocation
CompilerInvocation
::
CreateFromArgs
(
Clang
->
getInvocation
(),
&
args
[
0
],
&
args
[
0
]
+
args
.
size
(),
*
pDiagnosticsEngine
);
// Infer the builtin include path if unspecified.
if
(
Clang
->
getHeaderSearchOpts
().
UseBuiltinIncludes
&&
Clang
->
getHeaderSearchOpts
().
ResourceDir
.
empty
())
{
void
*
MainAddr
=
(
void
*
)(
intptr_t
)
GetExecutablePath
;
auto
path
=
CompilerInvocation
::
GetResourcesPath
(
args
[
0
],
MainAddr
);
Clang
->
getHeaderSearchOpts
().
ResourceDir
=
path
;
}
auto
&
HSO
=
Clang
->
getInvocation
().
getHeaderSearchOpts
();
// Add base toolchain-supplied header paths
// Ideally one would use the Linux toolchain definition in clang/lib/Driver/ToolChains.h
// But that's a private header and isn't part of the public libclang API
// Instead of re-implementing all of that functionality in a custom toolchain
// just hardcode the paths relevant to frequently used build/test machines for now
HSO
.
AddPath
(
"/usr/include/x86_64-linux-gnu"
,
clang
::
frontend
::
System
,
false
,
false
);
HSO
.
AddPath
(
"/usr/include"
,
clang
::
frontend
::
System
,
false
,
false
);
// Add C++ standard library headers
// Debian-like + GCC 4.8 libstdc++
HSO
.
AddPath
(
"/usr/include/x86_64-linux-gnu/c++/4.8"
,
clang
::
frontend
::
System
,
false
,
false
);
HSO
.
AddPath
(
"/usr/include/c++/4.8"
,
clang
::
frontend
::
System
,
false
,
false
);
// Debian-like + GCC 5 libstdc++
HSO
.
AddPath
(
"/usr/include/x86_64-linux-gnu/c++/5"
,
clang
::
frontend
::
System
,
false
,
false
);
HSO
.
AddPath
(
"/usr/include/c++/5"
,
clang
::
frontend
::
System
,
false
,
false
);
// Map code filename to a memoryBuffer
StringRef
source_ref
(
source
);
unique_ptr
<
MemoryBuffer
>
buffer
=
MemoryBuffer
::
getMemBufferCopy
(
source_ref
);
Clang
->
getInvocation
().
getPreprocessorOpts
().
addRemappedFile
(
name
,
buffer
.
get
());
// Create and execute action
// CodeGenAction *compilerAction = new EmitLLVMOnlyAction();
// CodeGenAction* compilerAction = new EmitAssemblyAction();
CodeGenAction
*
compilerAction
=
new
EmitCodeGenOnlyAction
();
Clang
->
ExecuteAction
(
*
compilerAction
);
buffer
.
release
();
return
compilerAction
->
takeModule
();
}
bool
execution_state
::
add_module
(
std
::
unique_ptr
<
llvm
::
Module
>&
module
)
{
bool
rc
=
false
;
if
(
module
)
{
rc
=
true
;
if
(
!
m_execution_engine
)
{
std
::
string
jit_error_string
;
// auto mm = unique_ptr<RTDyldMemoryManager>(new method_resolver(this));
m_execution_engine
=
llvm
::
EngineBuilder
(
move
(
module
))
.
setEngineKind
(
llvm
::
EngineKind
::
JIT
)
.
setOptLevel
(
llvm
::
CodeGenOpt
::
Aggressive
)
.
setErrorStr
(
&
jit_error_string
)
// .setUseMCJIT(true)
// .setMCJITMemoryManager(std::move(mm))
.
create
();
if
(
m_execution_engine
)
{
}
else
{
cout
<<
"nullptr engine
\n
"
;
cout
<<
jit_error_string
<<
endl
;
rc
=
false
;
}
}
}
else
{
cout
<<
"nullptr module
\n
"
;
rc
=
false
;
}
return
rc
;
}
void
execution_state
::
finalize
()
{
if
(
m_execution_engine
)
{
m_execution_engine
->
finalizeObject
();
m_execution_engine
->
runStaticConstructorsDestructors
(
false
);
}
else
{
throw
std
::
runtime_error
(
"must add_module before finalize"
);
}
}
// ----------------------------------------------------------------------------
// Copyright 2017 Nervana Systems Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// ----------------------------------------------------------------------------
#include <iostream>
#include <clang/CodeGen/ObjectFilePCHContainerOperations.h>
#include <clang/Driver/DriverDiagnostic.h>
#include <clang/Driver/Options.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/CompilerInvocation.h>
#include <clang/Frontend/FrontendDiagnostic.h>
#include <clang/Frontend/TextDiagnosticBuffer.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <clang/Frontend/Utils.h>
#include <clang/FrontendTool/Utils.h>
#include <clang/Lex/Preprocessor.h>
#include <clang/Lex/PreprocessorOptions.h>
#include <llvm/ADT/Statistic.h>
#include <llvm/LinkAllPasses.h>
#include <llvm/Option/Arg.h>
#include <llvm/Option/ArgList.h>
#include <llvm/Option/OptTable.h>
#include <llvm/Support/ErrorHandling.h>
#include <llvm/Support/ManagedStatic.h>
#include <llvm/Support/Signals.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Support/Timer.h>
#include <llvm/Support/raw_ostream.h>
#include <clang/Basic/DiagnosticOptions.h>
#include <clang/Basic/TargetInfo.h>
#include <clang/CodeGen/CodeGenAction.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ExecutionEngine/SectionMemoryManager.h>
#include "compiler.hpp"
using
namespace
clang
;
using
namespace
llvm
;
using
namespace
llvm
::
opt
;
using
namespace
std
;
using
namespace
nervana
::
cpu
;
std
::
string
GetExecutablePath
(
const
char
*
Argv0
)
{
// This just needs to be some symbol in the binary; C++ doesn't
// allow taking the address of ::main however.
void
*
MainAddr
=
(
void
*
)(
intptr_t
)
GetExecutablePath
;
return
llvm
::
sys
::
fs
::
getMainExecutable
(
Argv0
,
MainAddr
);
}
execution_state
::
execution_state
()
:
m_execution_engine
{
nullptr
}
{
}
execution_state
::~
execution_state
()
{
// /// Take the LLVM context used by this action.
// llvm::LLVMContext *takeLLVMContext();
// if (m_execution_engine)
// {
// m_execution_engine->runStaticConstructorsDestructors(true);
// }
}
std
::
unique_ptr
<
llvm
::
Module
>
execution_state
::
compile
(
const
string
&
source
,
const
string
&
name
)
{
llvm
::
InitializeAllTargets
();
llvm
::
InitializeAllTargetMCs
();
llvm
::
InitializeAllAsmPrinters
();
llvm
::
InitializeAllAsmParsers
();
// Prepare compilation arguments
vector
<
const
char
*>
args
;
args
.
push_back
(
name
.
c_str
());
// Prepare DiagnosticEngine
DiagnosticOptions
DiagOpts
;
TextDiagnosticPrinter
*
textDiagPrinter
=
new
clang
::
TextDiagnosticPrinter
(
errs
(),
&
DiagOpts
);
IntrusiveRefCntPtr
<
clang
::
DiagnosticIDs
>
pDiagIDs
;
DiagnosticsEngine
*
pDiagnosticsEngine
=
new
DiagnosticsEngine
(
pDiagIDs
,
&
DiagOpts
,
textDiagPrinter
);
// Create and initialize CompilerInstance
std
::
unique_ptr
<
CompilerInstance
>
Clang
(
new
CompilerInstance
());
Clang
->
createDiagnostics
();
// Initialize CompilerInvocation
CompilerInvocation
::
CreateFromArgs
(
Clang
->
getInvocation
(),
&
args
[
0
],
&
args
[
0
]
+
args
.
size
(),
*
pDiagnosticsEngine
);
// Infer the builtin include path if unspecified.
if
(
Clang
->
getHeaderSearchOpts
().
UseBuiltinIncludes
&&
Clang
->
getHeaderSearchOpts
().
ResourceDir
.
empty
())
{
void
*
MainAddr
=
(
void
*
)(
intptr_t
)
GetExecutablePath
;
auto
path
=
CompilerInvocation
::
GetResourcesPath
(
args
[
0
],
MainAddr
);
Clang
->
getHeaderSearchOpts
().
ResourceDir
=
path
;
}
auto
&
HSO
=
Clang
->
getInvocation
().
getHeaderSearchOpts
();
// Add base toolchain-supplied header paths
// Ideally one would use the Linux toolchain definition in clang/lib/Driver/ToolChains.h
// But that's a private header and isn't part of the public libclang API
// Instead of re-implementing all of that functionality in a custom toolchain
// just hardcode the paths relevant to frequently used build/test machines for now
HSO
.
AddPath
(
"/usr/include/x86_64-linux-gnu"
,
clang
::
frontend
::
System
,
false
,
false
);
HSO
.
AddPath
(
"/usr/include"
,
clang
::
frontend
::
System
,
false
,
false
);
// Add C++ standard library headers
// Debian-like + GCC 4.8 libstdc++
HSO
.
AddPath
(
"/usr/include/x86_64-linux-gnu/c++/4.8"
,
clang
::
frontend
::
System
,
false
,
false
);
HSO
.
AddPath
(
"/usr/include/c++/4.8"
,
clang
::
frontend
::
System
,
false
,
false
);
// Debian-like + GCC 5 libstdc++
HSO
.
AddPath
(
"/usr/include/x86_64-linux-gnu/c++/5"
,
clang
::
frontend
::
System
,
false
,
false
);
HSO
.
AddPath
(
"/usr/include/c++/5"
,
clang
::
frontend
::
System
,
false
,
false
);
// Map code filename to a memoryBuffer
StringRef
source_ref
(
source
);
unique_ptr
<
MemoryBuffer
>
buffer
=
MemoryBuffer
::
getMemBufferCopy
(
source_ref
);
Clang
->
getInvocation
().
getPreprocessorOpts
().
addRemappedFile
(
name
,
buffer
.
get
());
// Create and execute action
// CodeGenAction *compilerAction = new EmitLLVMOnlyAction();
// CodeGenAction* compilerAction = new EmitAssemblyAction();
CodeGenAction
*
compilerAction
=
new
EmitCodeGenOnlyAction
();
Clang
->
ExecuteAction
(
*
compilerAction
);
buffer
.
release
();
return
compilerAction
->
takeModule
();
}
bool
execution_state
::
add_module
(
std
::
unique_ptr
<
llvm
::
Module
>&
module
)
{
bool
rc
=
false
;
if
(
module
)
{
rc
=
true
;
if
(
!
m_execution_engine
)
{
std
::
string
jit_error_string
;
// auto mm = unique_ptr<RTDyldMemoryManager>(new method_resolver(this));
m_execution_engine
=
llvm
::
EngineBuilder
(
move
(
module
))
.
setEngineKind
(
llvm
::
EngineKind
::
JIT
)
.
setOptLevel
(
llvm
::
CodeGenOpt
::
Aggressive
)
.
setErrorStr
(
&
jit_error_string
)
// .setUseMCJIT(true)
// .setMCJITMemoryManager(std::move(mm))
.
create
();
if
(
m_execution_engine
)
{
}
else
{
cout
<<
"nullptr engine
\n
"
;
cout
<<
jit_error_string
<<
endl
;
rc
=
false
;
}
}
}
else
{
cout
<<
"nullptr module
\n
"
;
rc
=
false
;
}
return
rc
;
}
void
execution_state
::
finalize
()
{
if
(
m_execution_engine
)
{
m_execution_engine
->
finalizeObject
();
m_execution_engine
->
runStaticConstructorsDestructors
(
false
);
}
else
{
throw
std
::
runtime_error
(
"must add_module before finalize"
);
}
}
src/ngraph/codegen/compiler.hpp
View file @
d185b48c
#pragma once
#include <llvm/ExecutionEngine/MCJIT.h> // forces JIT to link in
#include <llvm/ExecutionEngine/SectionMemoryManager.h>
#include <llvm/Option/Arg.h>
namespace
nervana
{
namespace
cpu
{
class
module
;
class
execution_state
;
}
}
class
nervana
::
cpu
::
module
{
public
:
private
:
std
::
unique_ptr
<
llvm
::
Module
>
m_module
;
};
class
nervana
::
cpu
::
execution_state
:
public
llvm
::
SectionMemoryManager
{
public
:
execution_state
();
~
execution_state
();
std
::
unique_ptr
<
llvm
::
Module
>
compile
(
const
std
::
string
&
source
,
const
std
::
string
&
name
=
""
);
bool
add_module
(
std
::
unique_ptr
<
llvm
::
Module
>&
);
void
finalize
();
template
<
typename
ftype
>
std
::
function
<
ftype
>
find_function
(
const
std
::
string
&
func_name
)
{
auto
f
=
m_execution_engine
->
getPointerToNamedFunction
(
func_name
);
return
f_cast
<
ftype
>
(
f
);
}
private
:
llvm
::
ExecutionEngine
*
m_execution_engine
;
template
<
typename
signature
>
std
::
function
<
signature
>
f_cast
(
void
*
f
)
{
return
static_cast
<
signature
*>
((
signature
*
)
f
);
}
// class method_resolver : public llvm::RTDyldMemoryManager
// {
// public:
// method_resolver(compiler* m);
// virtual uint64_t getSymbolAddress(const std::string &name) override;
// private:
// compiler* m_Compiler;
// };
};
// ----------------------------------------------------------------------------
// Copyright 2017 Nervana Systems Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// ----------------------------------------------------------------------------
#pragma once
#include <llvm/ExecutionEngine/MCJIT.h> // forces JIT to link in
#include <llvm/ExecutionEngine/SectionMemoryManager.h>
#include <llvm/Option/Arg.h>
namespace
nervana
{
namespace
cpu
{
class
module
;
class
execution_state
;
}
}
class
nervana
::
cpu
::
module
{
public
:
private
:
std
::
unique_ptr
<
llvm
::
Module
>
m_module
;
};
class
nervana
::
cpu
::
execution_state
:
public
llvm
::
SectionMemoryManager
{
public
:
execution_state
();
~
execution_state
();
std
::
unique_ptr
<
llvm
::
Module
>
compile
(
const
std
::
string
&
source
,
const
std
::
string
&
name
=
""
);
bool
add_module
(
std
::
unique_ptr
<
llvm
::
Module
>&
);
void
finalize
();
template
<
typename
ftype
>
std
::
function
<
ftype
>
find_function
(
const
std
::
string
&
func_name
)
{
auto
f
=
m_execution_engine
->
getPointerToNamedFunction
(
func_name
);
return
f_cast
<
ftype
>
(
f
);
}
private
:
llvm
::
ExecutionEngine
*
m_execution_engine
;
template
<
typename
signature
>
std
::
function
<
signature
>
f_cast
(
void
*
f
)
{
return
static_cast
<
signature
*>
((
signature
*
)
f
);
}
// class method_resolver : public llvm::RTDyldMemoryManager
// {
// public:
// method_resolver(compiler* m);
// virtual uint64_t getSymbolAddress(const std::string &name) override;
// private:
// compiler* m_Compiler;
// };
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment