-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjit_mc.cpp
140 lines (119 loc) · 4.39 KB
/
jit_mc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// MIT License
//
// Copyright (c) 2020 sonson
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT W ARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <iostream>
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/ExecutionEngine/MCJIT.h"
// Context for LLVM
static llvm::LLVMContext context;
// ; ModuleID = 'originalModule'
// source_filename = "originalModule"
//
// define double @originalFunction(double %a, double %b) {
// entry:
// %addtmp = fadd double %a, %b
// ret double %addtmp
// }
int main() {
using llvm::Module;
using llvm::IRBuilder;
using llvm::Function;
using llvm::FunctionType;
using llvm::Type;
using llvm::Value;
using llvm::BasicBlock;
using llvm::ExecutionEngine;
using llvm::EngineBuilder;
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmPrinter();
// Create a new module
std::unique_ptr<Module> module(new Module("originalModule", context));
// LLVM IR builder
static IRBuilder<> builder(context);
// define function
// argument name list
auto functionName = "originalFunction";
std::vector<std::string> argNames{"a", "b"};
// argument type list
std::vector<Type *> Doubles(2, Type::getDoubleTy(context));
// create function type
FunctionType *functionType = FunctionType::get(Type::getDoubleTy(context), Doubles, false);
// create function in the module.
Function *function = Function::Create(functionType, Function::ExternalLinkage, functionName, module.get());
// Set names for all arguments.
// I'd like to use "zip" function, here.....
unsigned idx = 0;
for (auto &arg : function->args()) {
arg.setName(argNames[idx++]);
}
// Create argument table for LLVM::Value type.
static std::map<std::string, Value*> name2VariableMap;
for (auto &arg : function->args()) {
name2VariableMap[arg.getName().str()] = &arg;
}
// Create a new basic block to start insertion into.
BasicBlock *basicBlock = BasicBlock::Create(context, "entry", function);
builder.SetInsertPoint(basicBlock);
// calculate "add"
auto result = builder.CreateFAdd(name2VariableMap["a"], name2VariableMap["b"], "addtmp");
// set return
builder.CreateRet(result);
// varify LLVM IR
if (verifyFunction(*function)) {
std::cout << ": Error constructing function!\n" << std::endl;
return 1;
}
// confirm LLVM IR
module->print(llvm::outs(), nullptr);
// confirm the current module status
if (verifyModule(*module)) {
std::cout << ": Error module!\n" << std::endl;
return 1;
}
// Builder JIT
std::string errStr;
ExecutionEngine *engineBuilder = EngineBuilder(std::move(module))
.setEngineKind(llvm::EngineKind::JIT)
.setErrorStr(&errStr)
.create();
if (!engineBuilder) {
std::cout << "error: " << errStr << std::endl;
return 1;
}
// Get pointer to a function which is built by EngineBuilder.
auto f = reinterpret_cast<double(*)(double, double)>(
engineBuilder->getFunctionAddress(function->getName().str()));
if (f == NULL) {
std::cout << "error" << std::endl;
return 1;
}
// Execution
// a + b
std::cout << f(1.0, 2.0) << std::endl;
std::cout << f(2.0, 2.0) << std::endl;
return 0;
}