-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
llvm_module.cc
437 lines (400 loc) · 14.6 KB
/
llvm_module.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
* 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 llvm_module.cc
* \brief LLVM runtime module for TVM
*/
#ifdef TVM_LLVM_VERSION
#include <tvm/runtime/packed_func.h>
#include <tvm/runtime/registry.h>
#include <tvm/ir/module.h>
#include <tvm/target/codegen.h>
#include <mutex>
#include "llvm_common.h"
#include "codegen_llvm.h"
#include "codegen_blob.h"
#include "../../runtime/file_util.h"
#include "../../runtime/library_module.h"
namespace tvm {
namespace codegen {
using runtime::TVMArgs;
using runtime::TVMRetValue;
using runtime::PackedFunc;
class LLVMModuleNode final : public runtime::ModuleNode {
public:
~LLVMModuleNode() {
module_.reset();
if (ee_ != nullptr) {
ee_->runStaticConstructorsDestructors(true);
delete ee_;
}
}
const char* type_key() const {
return "llvm";
}
PackedFunc GetFunction(
const std::string& name,
const ObjectPtr<Object>& sptr_to_self) final {
if (name == "__tvm_is_system_module") {
bool flag =
(mptr_->getFunction("__tvm_module_startup") != nullptr);
return PackedFunc([flag](TVMArgs args, TVMRetValue *rv) {
* rv = flag;
});
} else if (name == "_get_target_triple") {
std::string target_triple = tm_->getTargetTriple().str();
return PackedFunc([target_triple](TVMArgs args, TVMRetValue *rv) {
* rv = target_triple;
});
}
if (ee_ == nullptr) LazyInitJIT();
std::lock_guard<std::mutex> lock(mutex_);
const std::string& fname = (name == runtime::symbol::tvm_module_main ?
entry_func_ : name);
TVMBackendPackedCFunc faddr =
reinterpret_cast<TVMBackendPackedCFunc>(GetFunctionAddr(fname));
if (faddr == nullptr) return PackedFunc();
return WrapPackedFunc(faddr, sptr_to_self);
}
void SaveToFile(const std::string& file_name,
const std::string& format) final {
std::string fmt = runtime::GetFileFormat(file_name, format);
std::error_code ecode;
llvm::raw_fd_ostream dest(file_name, ecode, llvm::sys::fs::F_None);
CHECK_EQ(ecode.value(), 0) << "Cannot open file: " << file_name
<< " " << ecode.message();
if (fmt == "o" || fmt == "obj") {
#if TVM_LLVM_VERSION <= 60
std::unique_ptr<llvm::Module> m = llvm::CloneModule(mptr_);
#else
std::unique_ptr<llvm::Module> m = llvm::CloneModule(*mptr_);
#endif
llvm::legacy::PassManager pass;
CHECK(tm_);
#if TVM_LLVM_VERSION <= 60
CHECK(tm_->addPassesToEmitFile(
pass, dest, llvm::TargetMachine::CGFT_ObjectFile) == 0)
<< "Cannot emit target CGFT_ObjectFile";
#elif TVM_LLVM_VERSION <= 90
CHECK(tm_->addPassesToEmitFile(
pass, dest, nullptr, llvm::TargetMachine::CGFT_ObjectFile) == 0)
<< "Cannot emit target CGFT_ObjectFile";
#else
CHECK(tm_->addPassesToEmitFile(
pass, dest, nullptr, llvm::CGFT_ObjectFile) == 0)
<< "Cannot emit target CGFT_ObjectFile";
#endif
pass.run(*m);
} else if (fmt == "s" || fmt == "asm") {
#if TVM_LLVM_VERSION <= 60
std::unique_ptr<llvm::Module> m = llvm::CloneModule(mptr_);
#else
std::unique_ptr<llvm::Module> m = llvm::CloneModule(*mptr_);
#endif
llvm::legacy::PassManager pass;
CHECK(tm_);
#if TVM_LLVM_VERSION <= 60
CHECK(tm_->addPassesToEmitFile(
pass, dest, llvm::TargetMachine::CGFT_AssemblyFile) == 0)
<< "Cannot emit target CGFT_AssemblyFile";
#elif TVM_LLVM_VERSION <= 90
CHECK(tm_->addPassesToEmitFile(
pass, dest, nullptr, llvm::TargetMachine::CGFT_AssemblyFile) == 0)
<< "Cannot emit target CGFT_AssemblyFile";
#else
CHECK(tm_->addPassesToEmitFile(
pass, dest, nullptr, llvm::CGFT_AssemblyFile) == 0)
<< "Cannot emit target CGFT_AssemblyFile";
#endif
pass.run(*m);
} else if (fmt == "ll") {
mptr_->print(dest, nullptr);
} else if (fmt == "bc") {
#if TVM_LLVM_VERSION <= 60
llvm::WriteBitcodeToFile(mptr_, dest);
#else
llvm::WriteBitcodeToFile(*mptr_, dest);
#endif
} else {
LOG(FATAL) << "Do not know how to save file "
<< file_name << " with format=\'"<< format << "\'";
}
dest.close();
}
void SaveToBinary(dmlc::Stream* stream) final {
LOG(FATAL) << "LLVMModule: SaveToBinary not supported";
}
std::string GetSource(const std::string& format) final {
std::string fmt = runtime::GetFileFormat("", format);
std::string type_str;
llvm::SmallString<256> str;
llvm::raw_svector_ostream rso(str);
if (fmt == "s" || fmt == "asm") {
#if TVM_LLVM_VERSION <= 60
std::unique_ptr<llvm::Module> m = llvm::CloneModule(mptr_);
#else
std::unique_ptr<llvm::Module> m = llvm::CloneModule(*mptr_);
#endif
llvm::legacy::PassManager pass;
CHECK(tm_);
#if TVM_LLVM_VERSION <= 60
CHECK(tm_->addPassesToEmitFile(
pass, rso, llvm::TargetMachine::CGFT_AssemblyFile) == 0)
<< "Cannot emit target CGFT_AssemblyFile";
#elif TVM_LLVM_VERSION <= 90
CHECK(tm_->addPassesToEmitFile(
pass, rso, nullptr, llvm::TargetMachine::CGFT_AssemblyFile) == 0)
<< "Cannot emit target CGFT_AssemblyFile";
#else
CHECK(tm_->addPassesToEmitFile(
pass, rso, nullptr, llvm::CGFT_AssemblyFile) == 0)
<< "Cannot emit target CGFT_AssemblyFile";
#endif
pass.run(*m);
return rso.str().str();
} else if (fmt == "" || fmt == "ll") {
std::string type_str;
llvm::raw_string_ostream rso(type_str);
CHECK(mptr_ != nullptr);
mptr_->print(rso, nullptr);
return rso.str();
} else {
LOG(FATAL) << "Do not know how to get source code with format: "
<< format << "\'";
}
return "";
}
void Init(const IRModule& mod, std::string target) {
InitializeLLVM();
tm_ = GetLLVMTargetMachine(target);
bool system_lib = (target.find("-system-lib") != std::string::npos);
ctx_ = std::make_shared<llvm::LLVMContext>();
std::unique_ptr<CodeGenLLVM> cg = CodeGenLLVM::Create(tm_.get());
std::vector<PrimFunc> funcs;
for (auto kv : mod->functions) {
CHECK(kv.second->IsInstance<PrimFuncNode>())
<< "Can only lower IR Module with PrimFuncs";
auto f = Downcast<PrimFunc>(kv.second);
if (f->HasNonzeroAttr(tir::attr::kIsEntryFunc)) {
auto global_symbol = f->GetAttr<runtime::String>(tvm::attr::kGlobalSymbol);
CHECK(global_symbol.defined());
entry_func_ = global_symbol;
}
funcs.push_back(f);
}
CHECK_NE(funcs.size(), 0U);
// TODO(tqchen): remove the entry function behavior as it does not
// makes sense when we start to use multiple modules.
cg->Init("TVMMod", tm_.get(), ctx_.get(), system_lib, system_lib);
for (const auto& f : funcs) {
cg->AddFunction(f);
}
if (entry_func_.length() != 0) {
cg->AddMainFunction(entry_func_);
}
module_ = cg->Finish();
module_->addModuleFlag(llvm::Module::Warning, "tvm_target", llvm::MDString::get(*ctx_, target));
module_->addModuleFlag(llvm::Module::Override, "Debug Info Version",
llvm::DEBUG_METADATA_VERSION);
if (tm_->getTargetTriple().isOSDarwin()) {
module_->addModuleFlag(llvm::Module::Override, "Dwarf Version", 2);
}
std::string verify_errors_storage;
llvm::raw_string_ostream verify_errors(verify_errors_storage);
LOG_IF(FATAL, llvm::verifyModule(*module_, &verify_errors))
<< "LLVM module verification failed with the following errors: \n"
<< verify_errors.str();
target_ = target;
mptr_ = module_.get();
}
void Init(std::unique_ptr<llvm::Module> module,
std::shared_ptr<llvm::LLVMContext> ctx) {
InitializeLLVM();
ctx_ = ctx;
llvm::SMDiagnostic err;
module_ = std::move(module);
if (module_ == nullptr) {
std::string msg = std::string(err.getMessage());
LOG(FATAL) << "Fail to load module: " << msg;
}
std::string target_;
llvm::Metadata* mtarget = module_->getModuleFlag("tvm_target");
if (mtarget != nullptr) {
llvm::MDString* pstr = llvm::dyn_cast<llvm::MDString>(mtarget);
CHECK(pstr != nullptr);
target_ = pstr->getString().str();
} else {
std::ostringstream os;
os << "llvm -target " << module_->getTargetTriple();
target_ = os.str();
}
mptr_ = module_.get();
tm_ = GetLLVMTargetMachine(target_);
}
void LoadIR(const std::string& file_name) {
auto ctx = std::make_shared<llvm::LLVMContext>();
llvm::SMDiagnostic err;
auto module = llvm::parseIRFile(file_name, err, *ctx);
if (module == nullptr) {
std::string msg = std::string(err.getMessage());
LOG(FATAL) << "Fail to load ir file " << file_name << "\n"
<< "line " << err.getLineNo() << ":" << msg;
}
Init(std::move(module), ctx);
}
private:
void LazyInitJIT() {
std::lock_guard<std::mutex> lock(mutex_);
if (ee_) {
return;
}
llvm::EngineBuilder builder(std::move(module_));
std::string triple, mcpu, mattr;
llvm::TargetOptions opt;
ParseLLVMTargetOptions(target_, &triple, &mcpu, &mattr, &opt);
builder.setEngineKind(llvm::EngineKind::JIT);
builder.setOptLevel(llvm::CodeGenOpt::Aggressive);
if (mcpu.length() != 0) {
builder.setMCPU(mcpu);
}
if (mattr.length() != 0) {
std::vector<std::string> mattrs{mattr};
builder.setMAttrs(mattrs);
}
builder.setTargetOptions(opt);
auto tm = std::unique_ptr<llvm::TargetMachine>(builder.selectTarget());
std::unique_ptr<llvm::TargetMachine> tm_sys = GetLLVMTargetMachine("llvm");
if (tm_sys->getTargetTriple().getArch() != tm->getTargetTriple().getArch()) {
LOG(FATAL) << "Cannot run module, architecture mismatch "
<< " module=" << tm->getTargetTriple().str()
<< " system=" << tm_sys->getTargetTriple().str();
}
llvm::DataLayout layout(tm->createDataLayout());
CHECK(layout == mptr_->getDataLayout())
<< "Data layout mismatch between module("
<< mptr_->getDataLayout().getStringRepresentation() << ")"
<< " and ExecutionEngine ("
<< layout.getStringRepresentation() << ")";
ee_ = builder.create(tm.release());
CHECK(ee_ != nullptr)
<< "Failed to initialize jit engine for " << mptr_->getTargetTriple();
ee_->runStaticConstructorsDestructors(false);
// setup context address.
entry_func_ =
reinterpret_cast<const char*>(GetGlobalAddr(runtime::symbol::tvm_module_main));
if (void** ctx_addr = reinterpret_cast<void**>(
GetGlobalAddr(runtime::symbol::tvm_module_ctx))) {
*ctx_addr = this;
}
runtime::InitContextFunctions([this](const char *name) {
return reinterpret_cast<void*>(GetGlobalAddr(name));
});
}
// Get global address from execution engine.
uint64_t GetGlobalAddr(const std::string& name) {
// first verifies if GV exists.
if (mptr_->getGlobalVariable(name) != nullptr) {
return ee_->getGlobalValueAddress(name);
} else {
return 0;
}
}
uint64_t GetFunctionAddr(const std::string& name) {
// first verifies if GV exists.
if (mptr_->getFunction(name) != nullptr) {
return ee_->getFunctionAddress(name);
} else {
return 0;
}
}
// The target configuration string
std::string target_;
// Name of entry function.
std::string entry_func_;
// JIT lock
std::mutex mutex_;
// execution engine
llvm::ExecutionEngine *ee_{nullptr};
// The raw pointer to the module.
llvm::Module* mptr_{nullptr};
// The target machine
std::unique_ptr<llvm::TargetMachine> tm_{nullptr};
// The module, can be moved to ee if JIT is enabled.
std::unique_ptr<llvm::Module> module_;
// the context.
std::shared_ptr<llvm::LLVMContext> ctx_;
};
unsigned LookupLLVMIntrinsic(const std::string& name) {
return llvm::Function::lookupIntrinsicID(name);
}
TVM_REGISTER_GLOBAL("target.build.llvm")
.set_body_typed([](IRModule mod, std::string target) {
auto n = make_object<LLVMModuleNode>();
n->Init(mod, target);
return runtime::Module(n);
});
TVM_REGISTER_GLOBAL("codegen.LLVMModuleCreate")
.set_body([](TVMArgs args, TVMRetValue *rv) {
auto n = make_object<LLVMModuleNode>();
auto target = args[0].operator std::string();
auto module_name = args[1].operator std::string();
// Generate a LLVM module from an input target string
InitializeLLVM();
auto tm = GetLLVMTargetMachine(target);
auto ctx = std::make_shared<llvm::LLVMContext>();
std::unique_ptr<llvm::Module> module(new llvm::Module(module_name, *ctx));
// Use a default data layout and target triple
auto triple = tm->getTargetTriple();
module->setTargetTriple(triple.str());
module->setDataLayout(tm->createDataLayout());
n->Init(std::move(module), ctx);
*rv = runtime::Module(n);
});
TVM_REGISTER_GLOBAL("target.llvm_lookup_intrinsic_id")
.set_body([](TVMArgs args, TVMRetValue* rv) {
*rv = static_cast<int64_t>(LookupLLVMIntrinsic(args[0]));
});
TVM_REGISTER_GLOBAL("target.llvm_version_major")
.set_body([](TVMArgs args, TVMRetValue* rv) {
int major = TVM_LLVM_VERSION / 10;
*rv = major;
});
TVM_REGISTER_GLOBAL("runtime.module.loadfile_ll")
.set_body([](TVMArgs args, TVMRetValue* rv) {
auto n = make_object<LLVMModuleNode>();
n->LoadIR(args[0]);
*rv = runtime::Module(n);
});
TVM_REGISTER_GLOBAL("codegen.llvm_target_enabled")
.set_body([](TVMArgs args, TVMRetValue* rv) {
InitializeLLVM();
*rv = (GetLLVMTargetMachine(args[0], true) != nullptr);
});
TVM_REGISTER_GLOBAL("codegen.codegen_blob")
.set_body([](TVMArgs args, TVMRetValue* rv) {
auto n = make_object<LLVMModuleNode>();
auto p = CodeGenBlob(args[0].operator std::string(),
args[1].operator bool(),
args[2].operator std::string());
n->Init(std::move(p.first), p.second);
*rv = runtime::Module(n);
});
} // namespace codegen
} // namespace tvm
#endif // TVM_LLVM_VERSION