Hi All, I am trying to write code for simple instrumentation. What I want to do is to insert a call to an external function for result of each conditional branch instruction. This external function simply print true or false based on the result of condition. The modified code is then written into new file. However when I try to link that file with another bitcode file (containing external function), it results "llvm-link: error loading file 'newfile.bc'" error. This error is inconsistent. It appears for some programs and not for some other. If it links with other file, during execution it produces following error - Assertion `Addr && "Code generation didn't add function to GlobalAddress table!"' failed Below is the code of my pass. It inherits ModulePass and does not have any other method except runOnModule. Am I missing something during call insertion or bitcode modification? I am using LLVM-2.6. The same code in LLVM-2.5 works correctly. (except Type::getInt32Ty, llvm-2.5 has Type::Int32Ty and no LLVMContext object) Thanks, Nehal bool ConditionProfile::runOnModule(Module &M) { // Iterates through all functions of the module for(Module::iterator mi = M.begin(), me = M.end(); mi!=me; mi++) { // Iterates through all basic blocks of the function for(Function::iterator fi = mi->begin(), fe = mi->end(); fi!=fe; fi++) { BasicBlock::iterator bi = fi->end(); bi--; // Getting the terminator/last instruction of BasicBlock if(isa<BranchInst>(bi)) { BranchInst *brInst = cast<BranchInst>(bi); // We are interested in conditional branch only if(brInst->isUnconditional()) continue; Value *condRes = brInst->getCondition(); errs()<<"Type:"<<condRes->getType()->getDescription()<<"\n"; // Looking for a function in Module Symbol table Constant *PrintFn M.getOrInsertFunction("_Z12PrintCondResb", Type::getInt32Ty(context), condRes->getType(), (Type *)0); if(!PrintFn) { errs()<<"GetOrInsertFailed\n"; continue; } std::vector<Value *> args(1); args[0] = condRes; // Creating a call instruction to above function. CallInst *callInst = CallInst::Create(PrintFn, args.begin(), args.end(), "", bi); callInst->setCallingConv(CallingConv::Fast); } } } // Writing modified module to new file. std::ostream *os = new std::ofstream("newfile.bc"); WriteBitcodeToFile(&M, *os); return true; } -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20100604/4152c602/attachment.html>
On Thu, Jun 3, 2010 at 9:43 PM, Nehal Gandhi <nbg2k7 at gmail.com> wrote:> Hi All, > > > > I am trying to write code for simple instrumentation. What I want to do is > to insert a call to an external function for result of each conditional > branch instruction. This external function simply print true or false based > on the result of condition. The modified code is then written into new file. > However when I try to link that file with another bitcode file (containing > external function), it results “llvm-link: error loading file 'newfile.bc'” > error. This error is inconsistent. It appears for some programs and not for > some other. If it links with other file, during execution it produces > following error - Assertion `Addr && "Code generation didn't add function > to GlobalAddress table!"' failed > > > > Below is the code of my pass. It inherits ModulePass and does not have any > other method except runOnModule. Am I missing something during call > insertion or bitcode modification? I am using LLVM-2.6. The same code in > LLVM-2.5 works correctly. (except Type::getInt32Ty, llvm-2.5 has > Type::Int32Ty and no LLVMContext object) > > > > Thanks, > > Nehal > > > > bool ConditionProfile::runOnModule(Module &M) > > { > > // Iterates through all functions of the module > > for(Module::iterator mi = M.begin(), me = M.end(); mi!=me; mi++) > > { > > // Iterates through all basic blocks of the function > > for(Function::iterator fi = mi->begin(), fe = mi->end(); fi!=fe; > fi++) > > { > > BasicBlock::iterator bi = fi->end(); > > bi--; // Getting the terminator/last > instruction of BasicBlock > > > > if(isa<BranchInst>(bi)) > > { > > BranchInst *brInst = cast<BranchInst>(bi); > > > > // We are interested in conditional branch only > > if(brInst->isUnconditional()) continue; > > > > Value *condRes = brInst->getCondition(); > > > errs()<<"Type:"<<condRes->getType()->getDescription()<<"\n"; > > > > // Looking for a function in Module Symbol > table > > Constant *PrintFn > M.getOrInsertFunction("_Z12PrintCondResb", Type::getInt32Ty(context), > condRes->getType(), (Type *)0); > > if(!PrintFn) { > > errs()<<"GetOrInsertFailed\n"; > > continue; > > } > > > > std::vector<Value *> args(1); > > args[0] = condRes; > > > > // Creating a call instruction to above function. > > CallInst *callInst > > CallInst::Create(PrintFn, args.begin(), > args.end(), "", bi); > > > > callInst->setCallingConv(CallingConv::Fast); > > } > > } > > } > > > > // Writing modified module to new file. > > std::ostream *os = new std::ofstream("newfile.bc"); > > WriteBitcodeToFile(&M, *os);Try putting "delete os" here? -Eli> return true; > > } > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > >
On 06/03/2010 11:08 PM, Eli Friedman wrote:>> // Writing modified module to new file. >> >> std::ostream *os = new std::ofstream("newfile.bc"); >> >> WriteBitcodeToFile(&M, *os); > > Try putting "delete os" here? > > -EliWhy does it even have to be dynamically allocated? It looks to me like an object on the stack will do fine. Sean
Hi Eli, Thanks for that. Rookie mistake on my side. It solves the linking issue. However, it was not the main problem. The problem is when I execute the linked file ( modified bitcode + file containing the function), I get an assertion error - Assertion `Addr && "Code generation didn't add function to GlobalAddress table!"' failed. So my main concern - is that a correct way to insert a call instruction in the bitcode the way I did? The same code for inserting a function call instruction works correctly with LLVM2.5 . Thanks, Nehal. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20100604/e8dba601/attachment.html>
Have you tried running the verifier after ConditionProfile runs? Just add it to the pass manager to run right afterwards and see what it picks up. Nick Nehal Gandhi wrote:> > > Hi All, > > I am trying to write code for simple instrumentation. What I want to do > is to insert a call to an external function for result of each > conditional branch instruction. This external function simply print true > or false based on the result of condition. The modified code is then > written into new file. However when I try to link that file with another > bitcode file (containing external function), it results “/llvm-link: > error loading file 'newfile.bc'/” error. This error is inconsistent. It > appears for some programs and not for some other. If it links with other > file, during execution it produces following error - /Assertion `Addr && > "Code generation didn't add function to GlobalAddress table!"' failed/ > > Below is the code of my pass. It inherits ModulePass and does not have > any other method except runOnModule. Am I missing something during call > insertion or bitcode modification? I am using LLVM-2.6. */The same code > in LLVM-2.5 works correctly/*. (except Type::getInt32Ty, llvm-2.5 has > Type::Int32Ty and no LLVMContext object) > > Thanks, > > Nehal > > bool ConditionProfile::runOnModule(Module &M) > > { > > // Iterates through all functions of the module > > for(Module::iterator mi = M.begin(), me = M.end(); mi!=me; mi++) > > { > > // Iterates through all basic blocks of the function > > for(Function::iterator fi = mi->begin(), fe = mi->end(); fi!=fe; fi++) > > { > > BasicBlock::iterator bi = fi->end(); > > bi--; // Getting the terminator/last instruction of BasicBlock > > if(isa<BranchInst>(bi)) > > { > > BranchInst *brInst = cast<BranchInst>(bi); > > // We are interested in conditional branch only > > if(brInst->isUnconditional()) continue; > > Value *condRes = brInst->getCondition(); > > errs()<<"Type:"<<condRes->getType()->getDescription()<<"\n"; > > // Looking for a function in Module Symbol table > > Constant *PrintFn = M.getOrInsertFunction("_Z12PrintCondResb", > Type::getInt32Ty(context), condRes->getType(), (Type *)0); > > if(!PrintFn) { > > errs()<<"GetOrInsertFailed\n"; > > continue; > > } > > std::vector<Value *> args(1); > > args[0] = condRes; > > // Creating a call instruction to above function. > > CallInst *callInst > > CallInst::Create(PrintFn, args.begin(), args.end(), "", bi); > > callInst->setCallingConv(CallingConv::Fast); > > } > > } > > } > > // Writing modified module to new file. > > std::ostream *os = new std::ofstream("newfile.bc"); > > WriteBitcodeToFile(&M, *os); > > return true; > > } > > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Hi Nick I am not quite sure what you meant by verifier. I checked online documents and found that there is a function verifyAnalysis() in Pass but it is empty. So do you suggest implementing that function? If it is the case, what should I look while implementing that function? Or something else altogether? Thanks, Nehal. -----Original Message----- From: Nick Lewycky [mailto:nicholas at mxc.ca] Sent: Friday, June 04, 2010 2:29 AM To: Nehal Gandhi Cc: llvmdev at cs.uiuc.edu Subject: Re: [LLVMdev] Inserting a function call into bitcode Have you tried running the verifier after ConditionProfile runs? Just add it to the pass manager to run right afterwards and see what it picks up. Nick