Dump module to llvm ir

Is there a way to dump the Module to llvm ir into a .s file which can be later built to standalone executable by clang ?

where I am:

  • dump an output from python following toturial
model_name = 'resnet18'
model = getattr(torchvision.models, model_name)(pretrained=True)
model = model.eval()

scripted_model = torch.jit.trace(model, input_data).eval()
mod, params = relay.frontend.from_pytorch(scripted_model,shape_list)

with relay.build_config(opt_level=3):
    graph, lib, params = relay.build(mod,target='llvm',target_host='llvm',params=params)

with open('/home/chao/tvm-playground/tvm-project/a.s','w') as f:
    f.write(lib.get_source())
  • assembling and linking with llvm backend
llvm-as -o a.bc a.s
llc -o a.o a.bc
as a.o -o a.as
cp ~/tvm/build/libtvm*.so ./
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PWD
ld -L "./libtvm.so" "./libtvm_runtime.so" "./libtvm_topi.so" -o a.out a.as
  • result
chao@DESKTOP-B79IRTG:~/tvm-playground/tvm-project$ ld -L "./libtvm.so" "./libtvm_runtime.so" "./libtvm_topi.so" -o a.out a.as
ld: a.as: undefined reference to symbol 'memset@@GLIBC_2.2.5'
//lib/x86_64-linux-gnu/libc.so.6: error adding symbols: DSO missing from command line

The llvm Ir is fine, but your problem seems mainly about the way you links the code, which has nothing to do with tvm itself though(seems was due to lack of libc)

@tqchen

I change my way like below

llvm-as a.s -o a.bc
clang -c a.bc -o a.o
clang a.o libtvm.so  libtvm_runtime.so  libtvm_topi.so

then got

/usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
clang-11: error: linker command failed with exit code 1 (use -v to see invocation)

But I am not able to determine which symbol to be the entry

Or is it necessary to write a main.c to call this model?

symbols from llvm ir are below

chao@DESKTOP-B79IRTG:~/tvm-playground/tvm-project$ readelf  -s a.o

Symbol table '.symtab' contains 181 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS TVMMod
     2: 0000000000000120     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI11_0
     3: 0000000000000130     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI11_1
     4: 0000000000000140     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI11_2
     5: 0000000000000150     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI11_3
     6: 0000000000000160     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI12_0
     7: 0000000000000170     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI12_1
     8: 0000000000000180     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI14_0
     9: 0000000000000190     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI14_1
    10: 00000000000001a0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI14_2
    11: 00000000000001b0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI14_3
    12: 00000000000001c0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI14_4
    13: 00000000000001d0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI14_5
    14: 00000000000001e0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI15_0
    15: 00000000000001f0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI15_1
    16: 0000000000000200     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI18_0
    17: 0000000000000210     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI18_1
    18: 0000000000000220     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI20_0
    19: 0000000000000230     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI20_1
    20: 0000000000000240     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI20_2
    21: 0000000000000250     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI20_3
    22: 0000000000000260     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI20_4
    23: 0000000000000270     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI20_5
    24: 0000000000000280     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI21_0
    25: 0000000000000290     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI21_1
    26: 00000000000002a0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI24_0
    27: 00000000000002b0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI25_0
    28: 00000000000002c0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI25_1
    29: 00000000000002d0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI25_2
    30: 00000000000002e0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI25_3
    31: 00000000000002f0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI29_0
    32: 0000000000000300     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI29_1
    33: 0000000000000310     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI29_2
    34: 0000000000000320     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI29_3
    35: 0000000000000330     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI33_0
    36: 0000000000000340     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI33_1
    37: 0000000000000350     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI35_0
    38: 0000000000000360     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI35_1
    39: 0000000000000370     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI35_2
    40: 0000000000000380     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI35_3
    41: 0000000000000390     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI35_4
    42: 00000000000003a0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI35_5
    43: 00000000000003b0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI36_0
    44: 00000000000003c0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI36_1
    45: 00000000000003d0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI36_2
    46: 00000000000003e0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI36_3
    47: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI3_0
    48: 0000000000000010     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI3_1
    49: 00000000000003f0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI40_0
    50: 0000000000000400     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI40_1
    51: 0000000000000410     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI40_2
    52: 0000000000000420     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI40_3
    53: 0000000000000430     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI44_0
    54: 0000000000000440     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI44_1
    55: 0000000000000450     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI44_2
    56: 0000000000000460     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI44_3
    57: 0000000000000470     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI48_0
    58: 0000000000000480     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI48_1
    59: 0000000000000490     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI51_0
    60: 00000000000004a0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI51_1
    61: 00000000000004b0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI53_0
    62: 00000000000004c0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI53_1
    63: 00000000000004d0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI53_2
    64: 00000000000004e0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI53_3
    65: 00000000000004f0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI53_4
    66: 0000000000000500     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI53_5
    67: 0000000000000510     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI54_0
    68: 0000000000000520     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI54_1
    69: 0000000000000530     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI54_2
    70: 0000000000000540     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI54_3
    71: 0000000000000550     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI58_0
    72: 0000000000000560     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI58_1
    73: 0000000000000570     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI58_2
    74: 0000000000000580     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI58_3
    75: 0000000000000020     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI5_0
    76: 0000000000000030     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI5_1
    77: 0000000000000040     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI5_2
    78: 0000000000000050     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI5_3
    79: 0000000000000060     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI5_4
    80: 0000000000000070     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI5_5
    81: 0000000000000590     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI62_0
    82: 00000000000005a0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI62_1
    83: 00000000000005b0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI62_2
    84: 00000000000005c0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI62_3
    85: 00000000000005d0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI66_0
    86: 00000000000005e0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI66_1
    87: 00000000000005f0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI68_0
    88: 0000000000000600     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI68_1
    89: 0000000000000610     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI68_2
    90: 0000000000000620     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI68_3
    91: 0000000000000630     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI69_0
    92: 0000000000000640     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI69_1
    93: 0000000000000080     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI6_0
    94: 0000000000000090     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI6_1
    95: 0000000000000650     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI71_0
    96: 0000000000000660     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI71_1
    97: 0000000000000670     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI71_2
    98: 0000000000000680     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI71_3
    99: 0000000000000690     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI72_0
   100: 00000000000006a0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI72_1
   101: 00000000000006b0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI72_2
   102: 00000000000006c0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI72_3
   103: 00000000000006d0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI75_0
   104: 00000000000006e0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI75_1
   105: 00000000000006f0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI75_2
   106: 0000000000000700     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI75_3
   107: 0000000000000710     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI79_0
   108: 0000000000000720     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI79_1
   109: 0000000000000730     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI79_2
   110: 0000000000000740     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI79_3
   111: 0000000000000750     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI83_0
   112: 0000000000000760     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI83_1
   113: 0000000000000770     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI83_2
   114: 0000000000000780     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI83_3
   115: 0000000000000790     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI86_0
   116: 00000000000007a0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI88_0
   117: 00000000000007b0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI88_1
   118: 00000000000000a0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI8_0
   119: 00000000000000b0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI8_1
   120: 00000000000000c0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI8_2
   121: 00000000000000d0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI8_3
   122: 00000000000000e0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI8_4
   123: 00000000000000f0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI8_5
   124: 00000000000007c0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI90_0
   125: 00000000000007d0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI91_0
   126: 00000000000007e0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI91_1
   127: 00000000000007f0     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI91_2
   128: 0000000000000800     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI91_3
   129: 0000000000000810     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI94_0
   130: 0000000000000820     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI94_1
   131: 0000000000000830     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI94_2
   132: 0000000000000840     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI94_3
   133: 0000000000000850     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI98_0
   134: 0000000000000860     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI98_1
   135: 0000000000000870     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI98_2
   136: 0000000000000880     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI98_3
   137: 0000000000000100     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI9_0
   138: 0000000000000110     0 NOTYPE  LOCAL  DEFAULT    4 .LCPI9_1
   139: 0000000000000000     0 SECTION LOCAL  DEFAULT    2
   140: 0000000000000000     0 SECTION LOCAL  DEFAULT    6
   141: 0000000000000000     0 SECTION LOCAL  DEFAULT    7
   142: 0000000000000000     0 SECTION LOCAL  DEFAULT    8
   143: 0000000000000000     0 SECTION LOCAL  DEFAULT   10
   144: 0000000000000000     0 SECTION LOCAL  DEFAULT   18
   145: 0000000000000000     8 OBJECT  WEAK   DEFAULT    5 __TVMAPISetLastError
   146: 0000000000000010     8 OBJECT  WEAK   DEFAULT    5 __TVMBackendAllocWorkspac
   147: 0000000000000018     8 OBJECT  WEAK   DEFAULT    5 __TVMBackendFreeWorkspace
   148: 0000000000000008     8 OBJECT  WEAK   DEFAULT    5 __TVMBackendParallelLaunc
   149: 0000000000002c00  1606 FUNC    GLOBAL DEFAULT    2 fused_layout_transform_17
   150: 000000000006d350  1603 FUNC    GLOBAL DEFAULT    2 fused_layout_transform_18
   151: 0000000000046800  1606 FUNC    GLOBAL DEFAULT    2 fused_layout_transform_19
   152: 00000000000471d0  1606 FUNC    GLOBAL DEFAULT    2 fused_layout_transform_20
   153: 000000000006c940  1603 FUNC    GLOBAL DEFAULT    2 fused_layout_transform_21
   154: 0000000000005600  1606 FUNC    GLOBAL DEFAULT    2 fused_layout_transform_22
   155: 0000000000004240  1606 FUNC    GLOBAL DEFAULT    2 fused_layout_transform_23
   156: 0000000000001fd0  1603 FUNC    GLOBAL DEFAULT    2 fused_layout_transform_24
   157: 0000000000004e70  1549 FUNC    GLOBAL DEFAULT    2 fused_layout_transform_25
   158: 0000000000003830  1603 FUNC    GLOBAL DEFAULT    2 fused_layout_transform_26
   159: 000000000001c500  1603 FUNC    GLOBAL DEFAULT    2 fused_layout_transform_27
   160: 000000000008d1b0  1434 FUNC    GLOBAL DEFAULT    2 fused_layout_transform_nn
   161: 0000000000006230  1605 FUNC    GLOBAL DEFAULT    2 fused_nn_adaptive_avg_poo
   162: 000000000008eea0  3669 FUNC    GLOBAL DEFAULT    2 fused_nn_contrib_conv2d_N
   163: 000000000006dd60  3664 FUNC    GLOBAL DEFAULT    2 fused_nn_contrib_conv2d_N
   164: 00000000000896b0  3670 FUNC    GLOBAL DEFAULT    2 fused_nn_contrib_conv2d_N
   165: 0000000000007160  4635 FUNC    GLOBAL DEFAULT    2 fused_nn_contrib_conv2d_N
   166: 00000000000a3700  4635 FUNC    GLOBAL DEFAULT    2 fused_nn_contrib_conv2d_N
   167: 00000000000519f0  4635 FUNC    GLOBAL DEFAULT    2 fused_nn_contrib_conv2d_N
   168: 000000000001d170  4629 FUNC    GLOBAL DEFAULT    2 fused_nn_contrib_conv2d_N
   169: 00000000000943f0  3675 FUNC    GLOBAL DEFAULT    2 fused_nn_contrib_conv2d_N
   170: 0000000000080830  3669 FUNC    GLOBAL DEFAULT    2 fused_nn_contrib_conv2d_N
   171: 0000000000062b80  3675 FUNC    GLOBAL DEFAULT    2 fused_nn_contrib_conv2d_N
   172: 0000000000047e40  3669 FUNC    GLOBAL DEFAULT    2 fused_nn_contrib_conv2d_N
   173: 00000000000717c0  3675 FUNC    GLOBAL DEFAULT    2 fused_nn_contrib_conv2d_N
   174: 000000000002f210  3669 FUNC    GLOBAL DEFAULT    2 fused_nn_contrib_conv2d_N
   175: 0000000000026580  3669 FUNC    GLOBAL DEFAULT    2 fused_nn_contrib_conv2d_N
   176: 0000000000016d40  3675 FUNC    GLOBAL DEFAULT    2 fused_nn_contrib_conv2d_N
   177: 0000000000000000  2732 FUNC    GLOBAL DEFAULT    2 fused_nn_dense_add
   178: 000000000008d760  1603 FUNC    GLOBAL DEFAULT    2 fused_nn_max_pool2d
   179: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND memcpy
   180: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND memset

The relay generated module is what I call it operator functions. You need a side program to call them according to the Json graph generated alongside. You can take a look at the runtime code.

@jinchenglee Thx for your guidance !

Is param also needed to be load manully ?

BTW is there any doc on implementing such side program in C/C++?

See src/runtime/crt and app/bundle_deploy

Excuse me if I disturbed, May I ask how have you overcome such situation? I am encountered this too, are we going to write a program that calls the llvm ir and the json graph? Thanks alot