|
19 | 19 |
|
20 | 20 | /*! |
21 | 21 | * \file src/relax/backend/vm/codegen_vm.cc |
22 | | - * \brief A compiler to compile an IRModule to VM executable. |
| 22 | + * \brief A codegen to generate VM executable from an IRModule with relax functions. |
23 | 23 | */ |
24 | 24 |
|
25 | 25 | #include "codegen_vm.h" |
@@ -64,7 +64,7 @@ class CodeGenVM : public ExprFunctor<Instruction::Arg(const Expr&)> { |
64 | 64 | // TODO(@yuchen): handle local functions that capture local vars outside the func |
65 | 65 | // TODO(@yuchen): a renaming pass to resolve name conflicts, e.g. the input module has a |
66 | 66 | // function named "local_funcN" |
67 | | - // lift the local func to a global func and compile it normally |
| 67 | + // lift the local func to a global func and process it normally |
68 | 68 | builder_->EmitFunction("local_func" + std::to_string(local_func_counter_++), |
69 | 69 | func_node->params.size()); |
70 | 70 | } |
@@ -287,49 +287,27 @@ class CodeGenVM : public ExprFunctor<Instruction::Arg(const Expr&)> { |
287 | 287 | const Op& load_shape_op_ = Op::Get("relax.vm.builtin.load_shape"); |
288 | 288 | }; |
289 | 289 |
|
290 | | -void VMCompiler::Compile(IRModule mod, Target target, Target target_host) { |
| 290 | +void VMCodeGen::CodeGen(IRModule rx_mod) { |
291 | 291 | builder_ = relax::ExecBuilderNode::Create(); |
292 | | - |
293 | | - IRModule tir_mod; |
294 | | - IRModule rx_mod; |
295 | | - for (auto& p : mod->functions) { |
296 | | - auto gvar = p.first; |
297 | | - |
298 | | - BaseFunc func = p.second; |
299 | | - if (func.as<tir::PrimFuncNode>()) { |
300 | | - tir_mod->Add(gvar, func); |
301 | | - } else if (func.as<FunctionNode>()) { |
302 | | - rx_mod->Add(gvar, func); |
303 | | - } else { |
304 | | - LOG(FATAL) << "Cannot handle such function node now:\n" << func; |
305 | | - } |
306 | | - } |
307 | | - lib_ = tvm::build(tir_mod, target, target_host); |
308 | | - |
309 | | - CodeGenVM compiler(builder_.operator->()); |
| 292 | + CodeGenVM codegen(builder_.operator->()); |
310 | 293 | for (auto& p : rx_mod->functions) { |
311 | | - compiler.VisitExpr(p.second); |
| 294 | + codegen.VisitExpr(p.second); |
312 | 295 | } |
313 | 296 | } |
314 | 297 |
|
315 | | -Executable VMCompiler::GetExec() { |
| 298 | +Executable VMCodeGen::GetExec() { |
316 | 299 | return builder_->Get(); |
317 | 300 | } |
318 | 301 |
|
319 | | -runtime::Module VMCompiler::GetLib() { |
320 | | - return lib_; |
321 | | -} |
322 | | - |
323 | | -Array<ObjectRef> Build(IRModule mod, Target target, Target target_host) { |
324 | | - auto compiler = make_object<VMCompiler>(); |
325 | | - compiler->Compile(mod, target, target_host); |
326 | | - Executable exec = compiler->GetExec(); |
327 | | - Module lib = compiler->GetLib(); |
328 | | - return Array<ObjectRef>({exec, lib}); |
| 302 | +Executable CodeGen(IRModule mod) { |
| 303 | + auto codegen = make_object<VMCodeGen>(); |
| 304 | + codegen->CodeGen(mod); |
| 305 | + Executable exec = codegen->GetExec(); |
| 306 | + return exec; |
329 | 307 | } |
330 | 308 |
|
331 | | -TVM_REGISTER_GLOBAL("relax.VMBuild") |
332 | | -.set_body_typed(Build); |
| 309 | +TVM_REGISTER_GLOBAL("relax.VMCodeGen") |
| 310 | +.set_body_typed(CodeGen); |
333 | 311 |
|
334 | 312 | } // namespace relax_vm |
335 | 313 | } // namespace relax |
|
0 commit comments