[LLVMdev] Dynamic Profiling - Instrumentation basic query

Alastair Murray alastairmurray42 at gmail.com
Sun Jan 20 13:31:35 PST 2013


Hi Silky,

Sorry for the slow reply.  You probably already fixed this, but just in 
case I'll reply anyway.

Comments inline below

On 15/01/13 19:38, SArora wrote:
> Hi Alastair,
> Thank you so much for the information on the tools. Actually, I need to
> analyze which sections of code are prone to misses and mis predicts, and
> would have to eventually instrument the code.
>
> I was able to instrument and call an external function, but faced an issue
> while passing an argument to the function. I am following EdgeProfiling.cpp
> but couldn't figure out the problem. Could you please see where I am going
> wrong here -
>
>   virtual bool runOnModule(Module &M)
>          {
>              Constant *hookFunc;
>              LLVMContext& context = M.getContext();
>              hookFunc =
> M.getOrInsertFunction("cacheCounter",Type::getVoidTy(M.getContext()),
>
> llvm::Type::getInt32Ty(M.getContext()),
>                                                              (Type*)0);
>              cacheCounter= cast<Function>(hookFunc);
>
>              for(Module::iterator F = M.begin(), E = M.end(); F!= E; ++F)
>              {
>
>                  for(Function::iterator BB = F->begin(), E = F->end(); BB !=
> E; ++BB)
>                  {
>                      cacheProf::runOnBasicBlock(BB, hookFunc, context);
>                  }
>              }
>
>              return false;
>          }
>          virtual bool runOnBasicBlock(Function::iterator &BB, Constant*
> hookFunc, LLVMContext& context)
>          {
>
>              for(BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI !=
> BE; ++BI)
>              {
>                  std::vector<Value*> Args(1);
>                  unsigned a =100;
>                  Args[0] =  ConstantInt::get(Type::getInt32Ty(context), a);
>                  if(isa<LoadInst>(&(*BI)) )
>                      {
>                          CallInst *newInst = CallInst::Create(hookFunc, Args,
> "",BI);

Did this line compile without warning?  Shouldn't 'BI' be '&(*BI)' or 
something?

>                      }
>
>              }
>              return true;
>          }
>
> The C code is as follows -
>
> extern "C" void cacheCounter(unsigned a){
>      std::cout<<a<<" Load instruction\n";
> }

I was kind of surprised that this worked, so I tried it out and it does! 
  In general libprofile_rt.so is written in C though, if you write it in 
C++ it will require a C++ library at run-time.

>
> Error:
> line 8: 18499 Segmentation fault      (core dumped) lli out.bc
>
> Also, the code works fine when I don't try to print out 'a'.
>
> Thanks for your help.
>
> Regards,
> Silky

You could look at the IR for out.bc before running it, if there is 
something wrong it will probably be pretty obvious there.

Regards,
Alastair.




More information about the llvm-dev mailing list