Hi, I’m trying to run the LLVM IR code generated by Relay/Relax models. As far as I know, TVM doesn’t support directly converting the LLVM IR model into IRModule, so I compiled the LLVM IR code into a shared object file (.so) by Clang and LLC. Then I use tvm.runtime.load_module
to load the shared object file and run it. However I kept stuck in getting the correct function. After comparing the shared object exported by export_library
function and the one I compiled, I found that the shared object file I compiled has something missing.
I wonder if there’s a method to export all I need to manually compile the LLVM IR code into a .so file? Or if it’s possible to load the LLVM IR code into TVM?
Here’s the detailed code. After building the Relay model I exported the LLVM IR code:
target = 'llvm'
with tvm.transform.PassContext(opt_level=3):
lib = relay.build(relay_model, target=target, mod_name="myfunc")
llvm_ir = lib.get_lib().get_source('ll')
with open('llvm_ir.ll','w') as f:
f.write(llvm_ir)
I compiled the LLVM IR code manually through the following commands:
llc -filetype=obj -o llvm_ir_to_obj.o llvm_ir.ll -relocation-model=pic
clang -shared -o obj_to_binary.so llvm_ir_to_obj.o -lm
Then I loaded the shared object file:
import numpy as np
import tvm
from tvm.contrib import graph_executor
obj_to_binary_lib = tvm.runtime.load_module('obj_to_binary.so')
obj_to_binary_module = graph_executor.GraphModule(obj_to_binary_lib['myfunc'](dev))
And here’s the error I got:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[2], line 6
3 from tvm.contrib import graph_executor
5 obj_to_binary_lib = tvm.runtime.load_module('obj_to_binary.so')
----> 6 obj_to_binary_module = graph_executor.GraphModule(obj_to_binary_lib['myfunc'](dev))
File ~/tvm/python/tvm/runtime/module.py:192, in Module.__getitem__(self, name)
190 if not isinstance(name, string_types):
191 raise ValueError("Can only take string as function name")
--> 192 return self.get_function(name)
File ~/tvm/python/tvm/runtime/module.py:176, in Module.get_function(self, name, query_imports)
170 check_call(
171 _LIB.TVMModGetFunction(
172 self.handle, c_str(name), ctypes.c_int(query_imports), ctypes.byref(ret_handle)
173 )
174 )
175 if not ret_handle.value:
--> 176 raise AttributeError(f"Module has no function '{name}'")
177 return PackedFunc(ret_handle, False)
AttributeError: Module has no function 'myfunc'
Thank you very much!