[llvm-dev] Parse LLVM IR (original) (raw)

Alex Denisov via llvm-dev llvm-dev at lists.llvm.org
Mon Dec 10 02:23:55 PST 2018


At a first glance your usage looks correct. Though, it seems like you are misusing the sizeof: I assume you want to see the number of functions in the module, while sizeof returns size of the iterator itself. Then, it seems there are no functions in the module, therefore you are dereferencing an iterator that points to invalid location, then your program terminates.

One reason the module can be empty:

Module *m = parseIRFile("t.ll", error, context).get();

In this line, parseIRFile returns std::unique_ptr, but the result is not stored anywhere, the instance of unique_ptr deallocated right after the call to .get() at the end of the line. When you use the Module *m later on it already points to a deallocated memory.

I hope it helps.

Cheers, Alex.

On 10. Dec 2018, at 09:36, div code via llvm-dev <llvm-dev at lists.llvm.org> wrote:

Any suggestions on this? I am sorry for the trouble this has caused, but the inconsistent APIs cause me a bunch of confusions.. Best, Irene On Sun, Dec 9, 2018 at 4:40 PM div code <divsubmission at gmail.com> wrote: Hello, I am a newbie to LLVM and right now I am on the hook to parse some IR code and do some instrumentations. However, my problem is that no matter how I tweak my parsing code, it simply cannot print out anything. So here is my C code: int yourfun(int arg2) { int x = arg2; return x+2; }

And here is my parsing code: #include <llvm/IR/Module.h> #include <llvm/IRReader/IRReader.h> #include <llvm/IR/LLVMContext.h> #include <llvm/Support/SourceMgr.h> #include #include <llvm/Support/rawostream.h> using namespace llvm; int main() { LLVMContext context; SMDiagnostic error; Module *m = parseIRFile("t.ll", error, context).get(); if(!m) { return 0; } std::cout << error.getMessage().str() << std::endl;_ _std::cout << sizeof(m->getFunctionList()) << std::endl;_ _auto iter1 = m->getFunctionList().begin(); std::cout << " Function: " << (*iter1).getName().str() << std::endl;_ _for (auto iter1 = m->getFunctionList().begin(); iter1 != m->getFunctionList().end(); iter1++) { Function &f = *iter1; std::cout << " Function: " << std::endl;_ _std::cout << " Function: " << f.getName().str() << std::endl;_ _for (auto iter2 = f.getBasicBlockList().begin();_ _iter2 != f.getBasicBlockList().end(); iter2++) {_ _BasicBlock &bb = *iter2;_ _std::cout << " BasicBlock: " << bb.getName().str() << std::endl;_ _for (auto iter3 = bb.begin(); iter3 != bb.end(); iter3++) {_ _Instruction &inst = *iter3;_ _std::cout << " Instruction " << &inst << " : " << inst.getOpcodeName();_ _unsigned int i = 0;_ _unsigned int opntcnt = inst.getNumOperands();_ _for(; i < opntcnt; ++i)_ _{_ _Value *opnd = inst.getOperand(i);_ _std::string o;_ _if (opnd->hasName()) { o = opnd->getName(); std::cout << " " << o << "," ; } else { std::cout << " ptr" << opnd << ","; } } std:: cout << std::endl; } } } return 1; } And this is my output: ➜ test git:(develop) ✗ clang -S -emit-llvm t.c ➜ test git:(develop) ✗ clang++ parse.cpp -o reader llvm-config --cxxflags --libs --ldflags --system-libs -g warning: unknown warning option '-Wno-maybe-uninitialized'; did you mean '-Wno-uninitialized'? [-Wunknown-warning-option] 1 warning generated. ➜ test git:(develop) ✗ ./reader 16 Function: ➜ test git:(develop) ✗ clang --version clang version 6.0.0-1ubuntu2 (tags/RELEASE600/final) Target: x8664-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin As you can see, I basically cannot get anything meaningful here. Could anyone shed some light on this? Thanks a lot. Irene


LLVM Developers mailing list llvm-dev at lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev

-- AlexDenisov Software Engineer, https://lowlevelbits.org

-------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 488 bytes Desc: Message signed with OpenPGP URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20181210/7daaffce/attachment.sig>



More information about the llvm-dev mailing list