Skip to content

Instantly share code, notes, and snippets.

@dtzWill

dtzWill/README Secret

Last active April 21, 2017 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtzWill/df84b64a73001532e3fcfe73a2cffbb9 to your computer and use it in GitHub Desktop.
Save dtzWill/df84b64a73001532e3fcfe73a2cffbb9 to your computer and use it in GitHub Desktop.
Testing JIT for cfe-dev@
http://lists.llvm.org/pipermail/cfe-dev/2017-April/053617.html
with import <nixpkgs> {};
stdenv.mkDerivation {
name = "jit_test";
buildInputs = [ clang_4 llvm_4 ];
src = lib.cleanSource ./.;
buildPhase = ":| ./test.sh";
installPhase = ''
mv test.log $out
'';
}
#include <llvm/ADT/STLExtras.h>
#include <llvm/ADT/SmallString.h>
#include <llvm/ADT/Twine.h>
#include <llvm/Bitcode/BitcodeReader.h>
#include <llvm/Bitcode/BitcodeWriter.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ExecutionEngine/JITSymbol.h>
#include <llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h>
#include <llvm/ExecutionEngine/Orc/CompileUtils.h>
#include <llvm/ExecutionEngine/Orc/IRCompileLayer.h>
#include <llvm/ExecutionEngine/Orc/IRTransformLayer.h>
#include <llvm/ExecutionEngine/Orc/IndirectionUtils.h>
#include <llvm/ExecutionEngine/Orc/LambdaResolver.h>
#include <llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h>
#include <llvm/ExecutionEngine/Orc/OrcABISupport.h>
#include <llvm/ExecutionEngine/RuntimeDyld.h>
#include <llvm/ExecutionEngine/SectionMemoryManager.h>
#include <llvm/IR/DataLayout.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/LegacyPassManager.h>
#include <llvm/IR/Mangler.h>
#include <llvm/IR/Module.h>
#include <llvm/IRReader/IRReader.h>
#include <llvm/Support/CommandLine.h>
#include <llvm/Support/DynamicLibrary.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/Path.h>
#include <llvm/Support/PrettyStackTrace.h>
#include <llvm/Support/Signals.h>
#include <llvm/Support/SourceMgr.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Support/ToolOutputFile.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Target/TargetMachine.h>
#include <llvm/Transforms/Scalar.h>
#include <llvm/Transforms/Scalar/GVN.h>
#include <algorithm>
#include <memory>
#include <set>
#include <string>
#include <vector>
using namespace llvm;
using namespace llvm::orc;
class Jitter
{
private:
std::unique_ptr<TargetMachine> TM;
const DataLayout DL;
ObjectLinkingLayer<> ObjectLayer;
IRCompileLayer<decltype(ObjectLayer)> CompileLayer;
public:
typedef decltype(CompileLayer)::ModuleSetHandleT ModuleHandle;
Jitter() : TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
CompileLayer(ObjectLayer, SimpleCompiler(*TM))
{printf("!");
llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
}
TargetMachine &getTargetMachine() { return *TM; }
ModuleHandle addModule(std::unique_ptr<Module> &&M) {
// Build our symbol resolver:
// Lambda 1: Look back into the JIT itself to find symbols that are part of
// the same "logical dylib".
// Lambda 2: Search for external symbols in the host process.
auto Resolver = createLambdaResolver(
[&](const std::string &Name)
{
printf("FLUSH :0\n");
if (auto Sym = CompileLayer.findSymbol(Name, false))
return Sym;
return JITSymbol(nullptr);
},
[](const std::string &S)
{
printf("PLUSH :0\n");
if (auto SymAddr =
RTDyldMemoryManager::getSymbolAddressInProcess(S))
return JITSymbol(SymAddr, JITSymbolFlags::Exported);
return JITSymbol(nullptr);
});
// Build a singleton module set to hold our module.
std::vector<std::unique_ptr<Module>> Ms;
Ms.push_back(std::move(M));
// Add the set to the JIT with the resolver we created above and a newly
// created SectionMemoryManager.
return CompileLayer.addModuleSet(std::move(Ms),
make_unique<SectionMemoryManager>(),
std::move(Resolver));
}
JITSymbol findSymbol(const std::string Name) {
std::string MangledName;
raw_string_ostream MangledNameStream(MangledName);
Mangler::getNameWithPrefix(MangledNameStream, Name, DL);
printf("Tzearch for: %s\n\n", MangledNameStream.str().c_str());
return CompileLayer.findSymbol(MangledNameStream.str(), false);
}
void removeModule(ModuleHandle H) {
CompileLayer.removeModuleSet(H);
}
};
//////////////////////////////////////////////////////////////////
int main()
{
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmPrinter();
llvm::InitializeNativeTargetAsmParser();
llvm::LLVMContext context;
llvm::SMDiagnostic dia;
std::unique_ptr<llvm::Module> M = llvm::parseIRFile("./jit_main.ll", dia, context);
Jitter jit;
printf("Wuff?");
Jitter::ModuleHandle h = jit.addModule(std::move(M));
printf("KNUFF!\n");
auto MainAddr = (void*)jit.findSymbol("main").getAddress();
printf("Kuchen! 0x%p\n", MainAddr);
printf("For good measure:\n");
printf("\tjit_main: %p\n", MainAddr);
printf("\tour main: %p\n", (void *)&main); // non-standard but let's just make sure
system("read"); // PAUSE
return 0;
}
#include <stdio.h>
int main() {
printf("Hello, world!\n");
return 0;
}
+ clang++ --version
clang version 4.0.0 (tags/RELEASE_400/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /nix/store/1ai08a59jp19cjj8d3n22mg3rs4kfk0w-clang-4.0.0/bin
++ llvm-config --cxxflags --ldflags --libs
+ clang++ -I/nix/store/iv9wal4li1dl6h4ia8id8xyz54c412cr-llvm-4.0.0/include -fPIC -fvisibility-inlines-hidden -Wall -W -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wno-maybe-uninitialized -Wdelete-non-virtual-dtor -Wno-comment -Werror=date-time -std=c++11 -ffunction-sections -fdata-sections -O3 -DNDEBUG -fno-exceptions -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -L/nix/store/h73q0038lp0ic6930mrd10p9yh02icwr-llvm-4.0.0-lib/lib -lLLVM-4.0 jit.cpp -o jit
warning: unknown warning option '-Wno-maybe-uninitialized'; did you mean '-Wno-uninitialized'? [-Wunknown-warning-option]
jit.cpp:139:45: warning: ISO C++ does not allow 'main' to be used by a program [-Wmain]
printf("\tour main: %p\n", (void *)&main); // non-standard but let's just make sure
^
jit.cpp:141:9: warning: ignoring return value of function declared with 'warn_unused_result' attribute [-Wunused-result]
system("read"); // PAUSE
^~~~~~ ~~~~~~
jit.cpp:132:30: warning: unused variable 'h' [-Wunused-variable]
Jitter::ModuleHandle h = jit.addModule(std::move(M));
^
4 warnings generated.
+ clang main.c -o jit_main.ll -emit-llvm -S
+ stdbuf -oL -eL ./jit
!Wuff?KNUFF!
Tzearch for: main
FLUSH :0
PLUSH :0
Kuchen! 0x0x7ffff7fe8000
For good measure:
jit_main: 0x7ffff7fe8000
our main: 0x448ac0
#!/bin/sh
set -o pipefail
set -ex
(
# Build jit
clang++ --version
clang++ $(llvm-config --cxxflags --ldflags --libs) jit.cpp -o jit
# Build IR to run
clang main.c -o jit_main.ll -emit-llvm -S
stdbuf -oL -eL \
./jit
) |& tee test.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment