-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
codegen_blob.cc
215 lines (183 loc) · 8.06 KB
/
codegen_blob.cc
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 limitations
* under the License.
*/
/*!
* \file codegen_blob.cc
*/
#ifdef TVM_LLVM_VERSION
#include <tvm/runtime/module.h>
#include <cstring>
#include <cstdint>
#include "codegen_blob.h"
namespace tvm {
namespace codegen {
namespace {
#if TVM_LLVM_VERSION < 50
inline unsigned HexDigitValue(char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10U;
if (c >= 'A' && c <= 'F') return c - 'A' + 10U;
return -1U;
}
inline uint8_t HexFromNibbles(char msb, char lsb) {
unsigned U1 = HexDigitValue(msb);
unsigned U2 = HexDigitValue(lsb);
CHECK(U1 != -1U && U2 != -1U);
return static_cast<uint8_t>((U1 << 4) | U2);
}
#endif
std::string TVMFromHex(const std::string& data) {
#if TVM_LLVM_VERSION >= 50
return llvm::fromHex(data);
#else
if (data.empty()) {
return std::string();
}
std::string output;
llvm::StringRef input = data;
output.reserve((input.size() + 1) / 2);
if (input.size() % 2 == 1) {
output.push_back(HexFromNibbles('0', input.front()));
input = input.drop_front();
}
CHECK_EQ(input.size() % 2, 0);
while (!input.empty()) {
uint8_t hex = HexFromNibbles(input[0], input[1]);
output.push_back(hex);
input = input.drop_front(2);
}
return output;
#endif
}
} // namespace
std::pair<std::unique_ptr<llvm::Module>,
std::shared_ptr<llvm::LLVMContext>> CodeGenBlob(const std::string& data,
bool system_lib,
const std::string& target) {
InitializeLLVM();
auto tm = GetLLVMTargetMachine(target);
auto target_triple = tm->getTargetTriple();
auto ctx = std::make_shared<llvm::LLVMContext>();
std::string module_name = "devc";
std::unique_ptr<llvm::Module> module(new llvm::Module(module_name, *ctx));
module->setTargetTriple(target_triple.str());
module->setDataLayout(tm->createDataLayout());
llvm::Constant* blob_value = llvm::ConstantDataArray::getString(*ctx, TVMFromHex(data), false);
auto* tvm_dev_mblob = new llvm::GlobalVariable(*module, blob_value->getType(), true,
llvm::GlobalValue::ExternalLinkage, blob_value,
runtime::symbol::tvm_dev_mblob, nullptr,
llvm::GlobalVariable::NotThreadLocal, 0);
#if TVM_LLVM_VERSION >= 100
tvm_dev_mblob->setAlignment(llvm::Align(1));
#else
tvm_dev_mblob->setAlignment(1);
#endif
if (target_triple.isOSWindows()) {
tvm_dev_mblob->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
}
if (system_lib) {
// LLVM type helper
auto void_ty = llvm::Type::getVoidTy(*ctx);
auto int32_ty = llvm::Type::getInt32Ty(*ctx);
auto int8_ty = llvm::Type::getInt8Ty(*ctx);
auto int8_ptr_ty = int8_ty->getPointerTo(0);
llvm::Constant* constant_zero = llvm::Constant::getNullValue(int32_ty);
auto* tvm_dev_mblob_reg =
new llvm::GlobalVariable(*module, int32_ty,
false, llvm::GlobalValue::InternalLinkage,
constant_zero,
std::string(runtime::symbol::tvm_dev_mblob) + "_reg_");
auto tvm_dev_mblob_reg_alignment = module->getDataLayout().getABITypeAlignment(int32_ty);
#if TVM_LLVM_VERSION >= 100
tvm_dev_mblob_reg->setAlignment(llvm::Align(tvm_dev_mblob_reg_alignment));
#else
tvm_dev_mblob_reg->setAlignment(tvm_dev_mblob_reg_alignment);
#endif
auto* tvm_dev_mblob_string_ty =
llvm::ArrayType::get(int8_ty, std::strlen(runtime::symbol::tvm_dev_mblob) + 1);
auto* tvm_dev_mblob_string_value =
llvm::ConstantDataArray::getString(*ctx, runtime::symbol::tvm_dev_mblob, true);
auto* tvm_dev_mblob_string =
new llvm::GlobalVariable(*module, tvm_dev_mblob_string_ty,
true, llvm::GlobalValue::PrivateLinkage,
tvm_dev_mblob_string_value,
std::string(runtime::symbol::tvm_dev_mblob) + ".str");
#if TVM_LLVM_VERSION >= 100
tvm_dev_mblob_string->setAlignment(llvm::Align(1));
#else
tvm_dev_mblob_string->setAlignment(1);
#endif
// Global init function
llvm::Function* init_fn = llvm::Function::Create(llvm::FunctionType::get(void_ty, false),
llvm::GlobalValue::InternalLinkage,
llvm::Twine("_GLOBAL__sub_I_", module_name),
module.get());
// Create variable initialization function.
llvm::Function* var_init_fn = llvm::Function::Create(llvm::FunctionType::get(void_ty, false),
llvm::GlobalValue::InternalLinkage,
llvm::Twine("__cxx_global_var_init"),
module.get());
// Create TVMBackendRegisterSystemLibSymbol function
llvm::Function* tvm_backend_fn =
llvm::Function::Create(llvm::FunctionType::get(int32_ty, {int8_ptr_ty, int8_ptr_ty}, false),
llvm::GlobalValue::ExternalLinkage,
llvm::Twine("TVMBackendRegisterSystemLibSymbol"),
module.get());
// Set necessary fn sections
auto get_static_init_section_specifier = [&target_triple]() -> std::string {
if (target_triple.isOSLinux()) {
return ".text.startup";
} else if (target_triple.isOSDarwin()) {
return "__TEXT,__StaticInit,regular,pure_instructions";
} else {
return "";
}
};
auto static_init_section_specifier = get_static_init_section_specifier();
if (!static_init_section_specifier.empty()) {
init_fn->setSection(static_init_section_specifier);
var_init_fn->setSection(static_init_section_specifier);
}
// The priority is 65535 for all platforms as clang do.
llvm::appendToGlobalCtors(*module, init_fn, 65535);
// Define init_fn body
llvm::IRBuilder<> ir_builder(*ctx);
llvm::BasicBlock* init_fn_bb = llvm::BasicBlock::Create(*ctx, "entry", init_fn);
ir_builder.SetInsertPoint(init_fn_bb);
ir_builder.CreateCall(var_init_fn);
ir_builder.CreateRetVoid();
// Define var_init_fn body
llvm::BasicBlock* var_init_fn_bb = llvm::BasicBlock::Create(*ctx, "entry", var_init_fn);
ir_builder.SetInsertPoint(var_init_fn_bb);
llvm::Constant* indices[] = {constant_zero, constant_zero};
llvm::SmallVector<llvm::Value*, 2> args;
args.push_back(llvm::ConstantExpr::getGetElementPtr(tvm_dev_mblob_string_ty,
tvm_dev_mblob_string,
indices));
args.push_back(llvm::ConstantExpr::getGetElementPtr(blob_value->getType(),
tvm_dev_mblob,
indices));
auto* tvm_backend_fn_ret_value = ir_builder.CreateCall(tvm_backend_fn, args);
ir_builder.CreateStore(tvm_backend_fn_ret_value, tvm_dev_mblob_reg);
ir_builder.CreateRetVoid();
}
return std::make_pair(std::move(module), ctx);
}
} // namespace codegen
} // namespace tvm
#endif // TVM_LLVM_VERSION