Llvm IR segmentation fault:(Core dumped)

Hi,
I tried an example to run through the whole compilation flow.
I took the add.pbtxt in the tensorflow project (tensorflow/add.pbtxt at master · tensorflow/tensorflow · GitHub)
My steps:

  1. Use tf-mlir-translate to convert .pbtxt file to MLIR file.
  2. tf-opt -tf-executor-to-functional-conversion (tf_executor Dialect → tf Dialect)
  3. tf-opt --tf-to-hlo-pipeline (tf Dialect → hlo Dialect)
  4. mlir-hlo-opt -hlo-legalize-to-lhlo (hlo Dialect → lhlo Dialect)
  5. mlir-hlo-opt -lhlo-legalize-to-linalg (lhlo Dialect → linalg Dialect)
  6. mlir-opt -convert-linalg-to-loops (linalg Dialect → Scf Dialect)
  7. mlir-opt -convert-scf-to-std (Scf Dialect → Std Dialect)
  8. mlir-opt -convert-std-to-llvm (Std Dialect → LLVM Dialect)
  9. mlir-translate -mlir-to-llvmir (LLVM Dialect → LLVM IR file)
  10. Use lli to run the .ll file

Here is the result I got.

How could I solve this error?

Thanks a lot!

You probably need to share other final IR to get a helpful answer.

I’m guessing that you did not define a “main” function in this case though. And even if you did, I’m not sure how running most Tensorflow programs through lli would work, considering you need to pass input buffers for the code to run on.

Here is the code from the file I got.


I got this file from the conversion I mentioned before.
There is not main function in it.
I wonder that does that mean the conversion failed or not.

Please prefer posting code as text, not images.

Here is the code from the file.
Sorry for unreadable code post

; ModuleID = 'LLVMDialectModule'
source_filename = "LLVMDialectModule"

declare i8* @malloc(i64)

declare void @free(i8*)

define { i32*, i32*, i64, [1 x i64], [1 x i64] } @main(i32* %0, i32* %1, i64 %2, i64 %3, i64 %4, i32* %5, i32* %6, i64 %7, i64 %8, i64 %9) !dbg !3 {
  %11 = insertvalue { i32*, i32*, i64, [1 x i64], [1 x i64] } undef, i32* %0, 0, !dbg !7
  %12 = insertvalue { i32*, i32*, i64, [1 x i64], [1 x i64] } %11, i32* %1, 1, !dbg !9
  %13 = insertvalue { i32*, i32*, i64, [1 x i64], [1 x i64] } %12, i64 %2, 2, !dbg !10
  %14 = insertvalue { i32*, i32*, i64, [1 x i64], [1 x i64] } %13, i64 %3, 3, 0, !dbg !11
  %15 = insertvalue { i32*, i32*, i64, [1 x i64], [1 x i64] } %14, i64 %4, 4, 0, !dbg !12
  %16 = insertvalue { i32*, i32*, i64, [1 x i64], [1 x i64] } undef, i32* %5, 0, !dbg !13
  %17 = insertvalue { i32*, i32*, i64, [1 x i64], [1 x i64] } %16, i32* %6, 1, !dbg !14
  %18 = insertvalue { i32*, i32*, i64, [1 x i64], [1 x i64] } %17, i64 %7, 2, !dbg !15
  %19 = insertvalue { i32*, i32*, i64, [1 x i64], [1 x i64] } %18, i64 %8, 3, 0, !dbg !16
  %20 = insertvalue { i32*, i32*, i64, [1 x i64], [1 x i64] } %19, i64 %9, 4, 0, !dbg !17
  %21 = call i8* @malloc(i64 mul (i64 ptrtoint (i32* getelementptr (i32, i32* null, i32 1) to i64), i64 10)), !dbg !18
  %22 = bitcast i8* %21 to i32*, !dbg !19
  %23 = insertvalue { i32*, i32*, i64, [1 x i64], [1 x i64] } undef, i32* %22, 0, !dbg !20
  %24 = insertvalue { i32*, i32*, i64, [1 x i64], [1 x i64] } %23, i32* %22, 1, !dbg !21
  %25 = insertvalue { i32*, i32*, i64, [1 x i64], [1 x i64] } %24, i64 0, 2, !dbg !22
  %26 = insertvalue { i32*, i32*, i64, [1 x i64], [1 x i64] } %25, i64 10, 3, 0, !dbg !23
  %27 = insertvalue { i32*, i32*, i64, [1 x i64], [1 x i64] } %26, i64 1, 4, 0, !dbg !24
  br label %28, !dbg !25

28:                                               ; preds = %31, %10
  %29 = phi i64 [ %41, %31 ], [ 0, %10 ]
  %30 = icmp slt i64 %29, 10, !dbg !26
  br i1 %30, label %31, label %42, !dbg !27

31:                                               ; preds = %28
  %32 = extractvalue { i32*, i32*, i64, [1 x i64], [1 x i64] } %15, 1, !dbg !28
  %33 = getelementptr i32, i32* %32, i64 %29, !dbg !29
  %34 = load i32, i32* %33, align 4, !dbg !30
  %35 = extractvalue { i32*, i32*, i64, [1 x i64], [1 x i64] } %20, 1, !dbg !31
  %36 = getelementptr i32, i32* %35, i64 %29, !dbg !32
  %37 = load i32, i32* %36, align 4, !dbg !33
  %38 = add i32 %34, %37, !dbg !34
  %39 = extractvalue { i32*, i32*, i64, [1 x i64], [1 x i64] } %27, 1, !dbg !35
  %40 = getelementptr i32, i32* %39, i64 %29, !dbg !36
  store i32 %38, i32* %40, align 4, !dbg !37
  %41 = add i64 %29, 1, !dbg !38
  br label %28, !dbg !39

42:                                               ; preds = %28
  ret { i32*, i32*, i64, [1 x i64], [1 x i64] } %27, !dbg !40
}

 !llvm.dbg.cu = !{!0}
 !llvm.module.flags = !{!2}

!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "mlir", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
!1 = !DIFile(filename: "LLVMDialectModule", directory: "/")
!2 = !{i32 2, !"Debug Info Version", i32 3}
!3 = distinct !DISubprogram(name: "main", linkageName: "main", scope: null, file: !4, line: 3, type: !5, scopeLine: 3, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !6)
!4 = !DIFile(filename: "add-llvm.mlir", directory: "/users/student/mr108/cwlu19/llvm_tensorflow/llvm-project/add-example")
!5 = !DISubroutineType(types: !6)
!6 = !{}
!7 = !DILocation(line: 5, column: 10, scope: !8)
!8 = !DILexicalBlockFile(scope: !3, file: !4, discriminator: 0)
!9 = !DILocation(line: 6, column: 10, scope: !8)
!10 = !DILocation(line: 7, column: 10, scope: !8)
!11 = !DILocation(line: 8, column: 10, scope: !8)
!12 = !DILocation(line: 9, column: 10, scope: !8)
!13 = !DILocation(line: 11, column: 10, scope: !8)
!14 = !DILocation(line: 12, column: 10, scope: !8)
!15 = !DILocation(line: 13, column: 10, scope: !8)
!16 = !DILocation(line: 14, column: 11, scope: !8)
!17 = !DILocation(line: 15, column: 11, scope: !8)
!18 = !DILocation(line: 24, column: 11, scope: !8)
!19 = !DILocation(line: 25, column: 11, scope: !8)
!20 = !DILocation(line: 27, column: 11, scope: !8)
!21 = !DILocation(line: 28, column: 11, scope: !8)
!22 = !DILocation(line: 30, column: 11, scope: !8)
!23 = !DILocation(line: 31, column: 11, scope: !8)
!24 = !DILocation(line: 32, column: 11, scope: !8)
!25 = !DILocation(line: 33, column: 5, scope: !8)
!26 = !DILocation(line: 35, column: 11, scope: !8)
!27 = !DILocation(line: 36, column: 5, scope: !8)
!28 = !DILocation(line: 38, column: 11, scope: !8)
!29 = !DILocation(line: 39, column: 11, scope: !8)
!30 = !DILocation(line: 40, column: 11, scope: !8)
!31 = !DILocation(line: 41, column: 11, scope: !8)
!32 = !DILocation(line: 42, column: 11, scope: !8)
!33 = !DILocation(line: 43, column: 11, scope: !8)
!34 = !DILocation(line: 44, column: 11, scope: !8)
!35 = !DILocation(line: 45, column: 11, scope: !8)
!36 = !DILocation(line: 46, column: 11, scope: !8)
!37 = !DILocation(line: 47, column: 5, scope: !8)
!38 = !DILocation(line: 48, column: 11, scope: !8)
!39 = !DILocation(line: 49, column: 5, scope: !8)
!40 = !DILocation(line: 51, column: 5, scope: !8)

The signature of main is not what the C ABI expects (int, char **) so it cannot just work. As @tpopp mentioned, TF kernels will need to get some input data and this is what the generated “main” expects. It shouldn’t really be “main” though.