# How to run Relay/Relax-Generated LLVM IR Code?

**URL:** <https://discuss.tvm.apache.org/t/how-to-run-relay-relax-generated-llvm-ir-code/18101>\
**Category:** Questions\
**Created:** [February 19, 2025, 6:39am UTC](https://discuss.tvm.apache.org/t/how-to-run-relay-relax-generated-llvm-ir-code/18101 "2025-02-19T06:39:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![BruceZhao0728](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/brucezhao0728/32/5728_2.png) [@BruceZhao0728](https://discuss.tvm.apache.org/u/BruceZhao0728)\
**Post date:** [February 19, 2025, 6:39am UTC](https://discuss.tvm.apache.org/t/how-to-run-relay-relax-generated-llvm-ir-code/18101/1 "2025-02-19T06:39:11Z")

</div>

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:

```python
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:

```auto
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:

```python
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:

```python
---------------------------------------------------------------------------
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!
