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>
// ----------------------------------------------------------------------------
// Copyright 2017 Nervana Systems Inc.
#include <clang/CodeGen/ObjectFilePCHContainerOperations.h>
// Licensed under the Apache License, Version 2.0 (the "License");
#include <clang/Driver/DriverDiagnostic.h>
// you may not use this file except in compliance with the License.
#include <clang/Driver/Options.h>
// You may obtain a copy of the License at
#include <clang/Frontend/CompilerInstance.h>
//
#include <clang/Frontend/CompilerInvocation.h>
// http://www.apache.org/licenses/LICENSE-2.0
#include <clang/Frontend/FrontendDiagnostic.h>
//
#include <clang/Frontend/TextDiagnosticBuffer.h>
// Unless required by applicable law or agreed to in writing, software
#include <clang/Frontend/TextDiagnosticPrinter.h>
// distributed under the License is distributed on an "AS IS" BASIS,
#include <clang/Frontend/Utils.h>
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#include <clang/FrontendTool/Utils.h>
// See the License for the specific language governing permissions and
#include <clang/Lex/Preprocessor.h>
// ----------------------------------------------------------------------------
#include <clang/Lex/PreprocessorOptions.h>
#include <llvm/ADT/Statistic.h>
#include <iostream>
#include <llvm/LinkAllPasses.h>
#include <llvm/Option/Arg.h>
#include <clang/CodeGen/ObjectFilePCHContainerOperations.h>
#include <llvm/Option/ArgList.h>
#include <clang/Driver/DriverDiagnostic.h>
#include <llvm/Option/OptTable.h>
#include <clang/Driver/Options.h>
#include <llvm/Support/ErrorHandling.h>
#include <clang/Frontend/CompilerInstance.h>
#include <llvm/Support/ManagedStatic.h>
#include <clang/Frontend/CompilerInvocation.h>
#include <llvm/Support/Signals.h>
#include <clang/Frontend/FrontendDiagnostic.h>
#include <llvm/Support/TargetSelect.h>
#include <clang/Frontend/TextDiagnosticBuffer.h>
#include <llvm/Support/Timer.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <llvm/Support/raw_ostream.h>
#include <clang/Frontend/Utils.h>
#include <clang/FrontendTool/Utils.h>
#include <clang/Basic/DiagnosticOptions.h>
#include <clang/Lex/Preprocessor.h>
#include <clang/Basic/TargetInfo.h>
#include <clang/Lex/PreprocessorOptions.h>
#include <clang/CodeGen/CodeGenAction.h>
#include <llvm/ADT/Statistic.h>
#include <clang/Frontend/CompilerInstance.h>
#include <llvm/LinkAllPasses.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <llvm/Option/Arg.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Option/ArgList.h>
#include <llvm/Option/OptTable.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/Support/ErrorHandling.h>
#include <llvm/ExecutionEngine/SectionMemoryManager.h>
#include <llvm/Support/ManagedStatic.h>
#include <llvm/Support/Signals.h>
#include "compiler.hpp"
#include <llvm/Support/TargetSelect.h>
#include <llvm/Support/Timer.h>
using
namespace
clang
;
#include <llvm/Support/raw_ostream.h>
using
namespace
llvm
;
using
namespace
llvm
::
opt
;
#include <clang/Basic/DiagnosticOptions.h>
using
namespace
std
;
#include <clang/Basic/TargetInfo.h>
#include <clang/CodeGen/CodeGenAction.h>
using
namespace
nervana
::
cpu
;
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
std
::
string
GetExecutablePath
(
const
char
*
Argv0
)
#include <llvm/Support/TargetSelect.h>
{
// This just needs to be some symbol in the binary; C++ doesn't
#include <llvm/ExecutionEngine/ExecutionEngine.h>
// allow taking the address of ::main however.
#include <llvm/ExecutionEngine/SectionMemoryManager.h>
void
*
MainAddr
=
(
void
*
)(
intptr_t
)
GetExecutablePath
;
return
llvm
::
sys
::
fs
::
getMainExecutable
(
Argv0
,
MainAddr
);
#include "compiler.hpp"
}
using
namespace
clang
;
execution_state
::
execution_state
()
using
namespace
llvm
;
:
m_execution_engine
{
nullptr
}
using
namespace
llvm
::
opt
;
{
using
namespace
std
;
}
using
namespace
nervana
::
cpu
;
execution_state
::~
execution_state
()
{
std
::
string
GetExecutablePath
(
const
char
*
Argv0
)
// /// Take the LLVM context used by this action.
{
// llvm::LLVMContext *takeLLVMContext();
// This just needs to be some symbol in the binary; C++ doesn't
// allow taking the address of ::main however.
// if (m_execution_engine)
void
*
MainAddr
=
(
void
*
)(
intptr_t
)
GetExecutablePath
;
// {
return
llvm
::
sys
::
fs
::
getMainExecutable
(
Argv0
,
MainAddr
);
// m_execution_engine->runStaticConstructorsDestructors(true);
}
// }
}
execution_state
::
execution_state
()
:
m_execution_engine
{
nullptr
}
std
::
unique_ptr
<
llvm
::
Module
>
execution_state
::
compile
(
const
string
&
source
,
const
string
&
name
)
{
{
}
llvm
::
InitializeAllTargets
();
llvm
::
InitializeAllTargetMCs
();
execution_state
::~
execution_state
()
llvm
::
InitializeAllAsmPrinters
();
{
llvm
::
InitializeAllAsmParsers
();
// /// Take the LLVM context used by this action.
// llvm::LLVMContext *takeLLVMContext();
// Prepare compilation arguments
vector
<
const
char
*>
args
;
// if (m_execution_engine)
args
.
push_back
(
name
.
c_str
());
// {
// m_execution_engine->runStaticConstructorsDestructors(true);
// Prepare DiagnosticEngine
// }
DiagnosticOptions
DiagOpts
;
}
TextDiagnosticPrinter
*
textDiagPrinter
=
new
clang
::
TextDiagnosticPrinter
(
errs
(),
&
DiagOpts
);
IntrusiveRefCntPtr
<
clang
::
DiagnosticIDs
>
pDiagIDs
;
std
::
unique_ptr
<
llvm
::
Module
>
execution_state
::
compile
(
const
string
&
source
,
const
string
&
name
)
DiagnosticsEngine
*
pDiagnosticsEngine
=
{
new
DiagnosticsEngine
(
pDiagIDs
,
&
DiagOpts
,
textDiagPrinter
);
llvm
::
InitializeAllTargets
();
llvm
::
InitializeAllTargetMCs
();
// Create and initialize CompilerInstance
llvm
::
InitializeAllAsmPrinters
();
std
::
unique_ptr
<
CompilerInstance
>
Clang
(
new
CompilerInstance
());
llvm
::
InitializeAllAsmParsers
();
Clang
->
createDiagnostics
();
// Prepare compilation arguments
// Initialize CompilerInvocation
vector
<
const
char
*>
args
;
CompilerInvocation
::
CreateFromArgs
(
args
.
push_back
(
name
.
c_str
());
Clang
->
getInvocation
(),
&
args
[
0
],
&
args
[
0
]
+
args
.
size
(),
*
pDiagnosticsEngine
);
// Prepare DiagnosticEngine
// Infer the builtin include path if unspecified.
DiagnosticOptions
DiagOpts
;
if
(
Clang
->
getHeaderSearchOpts
().
UseBuiltinIncludes
&&
TextDiagnosticPrinter
*
textDiagPrinter
=
new
clang
::
TextDiagnosticPrinter
(
errs
(),
&
DiagOpts
);
Clang
->
getHeaderSearchOpts
().
ResourceDir
.
empty
())
IntrusiveRefCntPtr
<
clang
::
DiagnosticIDs
>
pDiagIDs
;
{
DiagnosticsEngine
*
pDiagnosticsEngine
=
void
*
MainAddr
=
(
void
*
)(
intptr_t
)
GetExecutablePath
;
new
DiagnosticsEngine
(
pDiagIDs
,
&
DiagOpts
,
textDiagPrinter
);
auto
path
=
CompilerInvocation
::
GetResourcesPath
(
args
[
0
],
MainAddr
);
Clang
->
getHeaderSearchOpts
().
ResourceDir
=
path
;
// Create and initialize CompilerInstance
}
std
::
unique_ptr
<
CompilerInstance
>
Clang
(
new
CompilerInstance
());
Clang
->
createDiagnostics
();
auto
&
HSO
=
Clang
->
getInvocation
().
getHeaderSearchOpts
();
// Add base toolchain-supplied header paths
// Initialize CompilerInvocation
// Ideally one would use the Linux toolchain definition in clang/lib/Driver/ToolChains.h
CompilerInvocation
::
CreateFromArgs
(
// But that's a private header and isn't part of the public libclang API
Clang
->
getInvocation
(),
&
args
[
0
],
&
args
[
0
]
+
args
.
size
(),
*
pDiagnosticsEngine
);
// 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
// Infer the builtin include path if unspecified.
HSO
.
AddPath
(
"/usr/include/x86_64-linux-gnu"
,
clang
::
frontend
::
System
,
false
,
false
);
if
(
Clang
->
getHeaderSearchOpts
().
UseBuiltinIncludes
&&
HSO
.
AddPath
(
"/usr/include"
,
clang
::
frontend
::
System
,
false
,
false
);
Clang
->
getHeaderSearchOpts
().
ResourceDir
.
empty
())
// Add C++ standard library headers
{
// Debian-like + GCC 4.8 libstdc++
void
*
MainAddr
=
(
void
*
)(
intptr_t
)
GetExecutablePath
;
HSO
.
AddPath
(
"/usr/include/x86_64-linux-gnu/c++/4.8"
,
clang
::
frontend
::
System
,
false
,
false
);
auto
path
=
CompilerInvocation
::
GetResourcesPath
(
args
[
0
],
MainAddr
);
HSO
.
AddPath
(
"/usr/include/c++/4.8"
,
clang
::
frontend
::
System
,
false
,
false
);
Clang
->
getHeaderSearchOpts
().
ResourceDir
=
path
;
// 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
);
auto
&
HSO
=
Clang
->
getInvocation
().
getHeaderSearchOpts
();
// Add base toolchain-supplied header paths
// Map code filename to a memoryBuffer
// Ideally one would use the Linux toolchain definition in clang/lib/Driver/ToolChains.h
StringRef
source_ref
(
source
);
// But that's a private header and isn't part of the public libclang API
unique_ptr
<
MemoryBuffer
>
buffer
=
MemoryBuffer
::
getMemBufferCopy
(
source_ref
);
// Instead of re-implementing all of that functionality in a custom toolchain
Clang
->
getInvocation
().
getPreprocessorOpts
().
addRemappedFile
(
name
,
buffer
.
get
());
// 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
);
// Create and execute action
HSO
.
AddPath
(
"/usr/include"
,
clang
::
frontend
::
System
,
false
,
false
);
// CodeGenAction *compilerAction = new EmitLLVMOnlyAction();
// Add C++ standard library headers
// CodeGenAction* compilerAction = new EmitAssemblyAction();
// Debian-like + GCC 4.8 libstdc++
CodeGenAction
*
compilerAction
=
new
EmitCodeGenOnlyAction
();
HSO
.
AddPath
(
"/usr/include/x86_64-linux-gnu/c++/4.8"
,
clang
::
frontend
::
System
,
false
,
false
);
Clang
->
ExecuteAction
(
*
compilerAction
);
HSO
.
AddPath
(
"/usr/include/c++/4.8"
,
clang
::
frontend
::
System
,
false
,
false
);
// Debian-like + GCC 5 libstdc++
buffer
.
release
();
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
);
return
compilerAction
->
takeModule
();
}
// Map code filename to a memoryBuffer
StringRef
source_ref
(
source
);
bool
execution_state
::
add_module
(
std
::
unique_ptr
<
llvm
::
Module
>&
module
)
unique_ptr
<
MemoryBuffer
>
buffer
=
MemoryBuffer
::
getMemBufferCopy
(
source_ref
);
{
Clang
->
getInvocation
().
getPreprocessorOpts
().
addRemappedFile
(
name
,
buffer
.
get
());
bool
rc
=
false
;
if
(
module
)
// Create and execute action
{
// CodeGenAction *compilerAction = new EmitLLVMOnlyAction();
rc
=
true
;
// CodeGenAction* compilerAction = new EmitAssemblyAction();
if
(
!
m_execution_engine
)
CodeGenAction
*
compilerAction
=
new
EmitCodeGenOnlyAction
();
{
Clang
->
ExecuteAction
(
*
compilerAction
);
std
::
string
jit_error_string
;
// auto mm = unique_ptr<RTDyldMemoryManager>(new method_resolver(this));
buffer
.
release
();
m_execution_engine
=
llvm
::
EngineBuilder
(
move
(
module
))
.
setEngineKind
(
llvm
::
EngineKind
::
JIT
)
return
compilerAction
->
takeModule
();
.
setOptLevel
(
llvm
::
CodeGenOpt
::
Aggressive
)
}
.
setErrorStr
(
&
jit_error_string
)
// .setUseMCJIT(true)
bool
execution_state
::
add_module
(
std
::
unique_ptr
<
llvm
::
Module
>&
module
)
// .setMCJITMemoryManager(std::move(mm))
{
.
create
();
bool
rc
=
false
;
if
(
module
)
if
(
m_execution_engine
)
{
{
rc
=
true
;
}
if
(
!
m_execution_engine
)
else
{
{
std
::
string
jit_error_string
;
cout
<<
"nullptr engine
\n
"
;
// auto mm = unique_ptr<RTDyldMemoryManager>(new method_resolver(this));
cout
<<
jit_error_string
<<
endl
;
m_execution_engine
=
llvm
::
EngineBuilder
(
move
(
module
))
rc
=
false
;
.
setEngineKind
(
llvm
::
EngineKind
::
JIT
)
}
.
setOptLevel
(
llvm
::
CodeGenOpt
::
Aggressive
)
}
.
setErrorStr
(
&
jit_error_string
)
}
// .setUseMCJIT(true)
else
// .setMCJITMemoryManager(std::move(mm))
{
.
create
();
cout
<<
"nullptr module
\n
"
;
rc
=
false
;
if
(
m_execution_engine
)
}
{
}
return
rc
;
else
}
{
cout
<<
"nullptr engine
\n
"
;
void
execution_state
::
finalize
()
cout
<<
jit_error_string
<<
endl
;
{
rc
=
false
;
if
(
m_execution_engine
)
}
{
}
m_execution_engine
->
finalizeObject
();
}
m_execution_engine
->
runStaticConstructorsDestructors
(
false
);
else
}
{
else
cout
<<
"nullptr module
\n
"
;
{
rc
=
false
;
throw
std
::
runtime_error
(
"must add_module before finalize"
);
}
}
}
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
// ----------------------------------------------------------------------------
// Copyright 2017 Nervana Systems Inc.
#include <llvm/ExecutionEngine/MCJIT.h> // forces JIT to link in
// Licensed under the Apache License, Version 2.0 (the "License");
#include <llvm/ExecutionEngine/SectionMemoryManager.h>
// you may not use this file except in compliance with the License.
#include <llvm/Option/Arg.h>
// You may obtain a copy of the License at
//
namespace
nervana
// http://www.apache.org/licenses/LICENSE-2.0
{
//
namespace
cpu
// Unless required by applicable law or agreed to in writing, software
{
// distributed under the License is distributed on an "AS IS" BASIS,
class
module
;
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
class
execution_state
;
// See the License for the specific language governing permissions and
}
// ----------------------------------------------------------------------------
}
#pragma once
class
nervana
::
cpu
::
module
{
#include <llvm/ExecutionEngine/MCJIT.h> // forces JIT to link in
public
:
#include <llvm/ExecutionEngine/SectionMemoryManager.h>
private
:
#include <llvm/Option/Arg.h>
std
::
unique_ptr
<
llvm
::
Module
>
m_module
;
};
namespace
nervana
{
class
nervana
::
cpu
::
execution_state
:
public
llvm
::
SectionMemoryManager
namespace
cpu
{
{
public
:
class
module
;
execution_state
();
class
execution_state
;
~
execution_state
();
}
}
std
::
unique_ptr
<
llvm
::
Module
>
compile
(
const
std
::
string
&
source
,
const
std
::
string
&
name
=
""
);
class
nervana
::
cpu
::
module
bool
add_module
(
std
::
unique_ptr
<
llvm
::
Module
>&
);
{
public
:
void
finalize
();
private
:
std
::
unique_ptr
<
llvm
::
Module
>
m_module
;
template
<
typename
ftype
>
};
std
::
function
<
ftype
>
find_function
(
const
std
::
string
&
func_name
)
{
class
nervana
::
cpu
::
execution_state
:
public
llvm
::
SectionMemoryManager
auto
f
=
m_execution_engine
->
getPointerToNamedFunction
(
func_name
);
{
public
:
return
f_cast
<
ftype
>
(
f
);
execution_state
();
}
~
execution_state
();
private
:
std
::
unique_ptr
<
llvm
::
Module
>
compile
(
const
std
::
string
&
source
,
const
std
::
string
&
name
=
""
);
llvm
::
ExecutionEngine
*
m_execution_engine
;
bool
add_module
(
std
::
unique_ptr
<
llvm
::
Module
>&
);
template
<
typename
signature
>
std
::
function
<
signature
>
f_cast
(
void
*
f
)
void
finalize
();
{
return
static_cast
<
signature
*>
((
signature
*
)
f
);
template
<
typename
ftype
>
}
std
::
function
<
ftype
>
find_function
(
const
std
::
string
&
func_name
)
{
// class method_resolver : public llvm::RTDyldMemoryManager
auto
f
=
m_execution_engine
->
getPointerToNamedFunction
(
func_name
);
// {
// public:
return
f_cast
<
ftype
>
(
f
);
// method_resolver(compiler* m);
}
// virtual uint64_t getSymbolAddress(const std::string &name) override;
private
:
// private:
llvm
::
ExecutionEngine
*
m_execution_engine
;
// compiler* m_Compiler;
// };
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