3

I'm following the link Parsing and Modifying LLVM IR code to read in IR file and trying to parse it. But I found no matter what input file I wrote in argument(.ll or .bc), it just won't parse file and save it into the variable.

Here's my code,

#include <iostream>
#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/Function.h"
#include "llvm/Support/CFG.h"
#include "llvm/Support/IRReader.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"

using namespace llvm;

int main(int argc, char** argv)
{
    if (argc < 2) {
        errs() << "Expected an argument - IR file name\n";
        exit(1);
    }

    LLVMContext &Context = getGlobalContext();
    SMDiagnostic Err;
    Module *Mod = ParseIRFile(argv[1], Err, Context);

    if (Mod) {
        std::cout << "Mod is not null" << std::endl;
    }
    else {
        std::cout << "Mod is null" << std::endl;
    }
    return 0;
}

By running this code with either ll or bc file it always shows Mod is null.

Can someone give me some hints on how to reslove this issue?

Here is my IR code

; ModuleID = 'bubble.bc'
target datalayout = "e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.9.0"

@Sort = global [8 x i32] [i32 1, i32 4, i32 2, i32 5, i32 7, i32 3, i32 6, i32 3],  align 16
@.str = private unnamed_addr constant [4 x i8] c"%d \00", align 1
@.str1 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1

; Function Attrs: nounwind ssp uwtable
define void @bubble() #0 {
  %tmp = alloca i32, align 4
  %i = alloca i32, align 4
  %j = alloca i32, align 4
  store i32 0, i32* %i, align 4
  br label %1

; <label>:1                                       ; preds = %41, %0
  %2 = load i32* %i, align 4
  %3 = icmp slt i32 %2, 8
  br i1 %3, label %4, label %44

; <label>:4                                       ; preds = %1
  %5 = load i32* %i, align 4
  %6 = add nsw i32 %5, 1
  store i32 %6, i32* %j, align 4
  br label %7

; <label>:7                                       ; preds = %37, %4
  %8 = load i32* %j, align 4
  %9 = icmp slt i32 %8, 8
  br i1 %9, label %10, label %40

; <label>:10                                      ; preds = %7
  %11 = load i32* %i, align 4
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [8 x i32]* @Sort, i32 0, i64 %12
  %14 = load i32* %13, align 4
  %15 = load i32* %j, align 4
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [8 x i32]* @Sort, i32 0, i64 %16
  %18 = load i32* %17, align 4
  %19 = icmp sge i32 %14, %18
  br i1 %19, label %20, label %36

//some similar stuff

!0 = metadata !{metadata !"Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)"}

Error meg is ./IRparser: /home/mingaoIrparser/testcase/bubble.ll:10:23: error: expected '{' in function body define void @bubble() #0 { ^

One more thing is that this IR is compiled from LLVM3.4 the library I'm using is 2.9 for now. Will it matter?

Community
  • 1
  • 1
Min Gao
  • 383
  • 4
  • 16
  • 1
    Can you print Err to http://llvm.org/docs/doxygen/html/classllvm_1_1raw__ostream.html and show the output? – Marco A. May 28 '14 at 15:37
  • ./IRparser: /home/mingaoIrparser/testcase/bubble.ll:10:23: error: expected '{' in function body define void @bubble() #0 { – Min Gao May 28 '14 at 18:01
  • 1
    that's it: there's an error – Marco A. May 28 '14 at 18:06
  • The thing I'm feeling strange is this IR is generated by llvm automatically, how can it get error while reading it again? I'm now want to know if version matters. I got this file from a 3.4 version llvm and the library i'm using is 2.9 – Min Gao May 28 '14 at 18:10
  • I tried compiling using 2.9 as well and it worked. Thanks anyway for reminding me print out error msg. – Min Gao May 28 '14 at 18:26

1 Answers1

3

This repository has a bunch of up-to-date samples of using LLVM and Clang as libraries. For example, this sample has what you need (as well as other samples):

int main(int argc, char **argv) {
  if (argc < 2) {
    errs() << "Usage: " << argv[0] << " <IR file>\n";
    return 1;
  }

  // Parse the input LLVM IR file into a module.
  SMDiagnostic Err;
  Module *Mod = ParseIRFile(argv[1], Err, getGlobalContext());
  if (!Mod) {
    Err.print(argv[0], errs());
    return 1;
  }

  // ... use module
}
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • Turned out it seems like to be a version compatible problem as my IR is made from version 3.4 and I'm doing it on another machine with 2.9 library. Thanks for pointing out a very good resource I can refer to. – Min Gao May 28 '14 at 18:28