Using Profile Information

Hi,

How can I use profile information into my pass for performing some analysis.

I tried something like:

      PassManager PassMgr = PassManager();
      cl::opt<std::string> ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
                  cl::Optional, cl::init("llvmprof.out"));
      PassMgr.add(createProfileLoaderPass(ProfileDataFile));
      PassMgr.run(M);
      ProfileInfo *PI;
     PI = &getAnalysis<ProfileInfo>();

But this dosent seem to work.
I want to perform profile guided analysis and thus want to use profile info.

regards,
Ambika

Hi!

How can I use profile information into my pass for performing some analysis.

I tried something like:

     PassManager PassMgr = PassManager();
     cl::opt<std::string> ProfileDataFile(cl::Positional,
cl::desc("<llvmprof.out file>"),
                 cl::Optional, cl::init("llvmprof.out"));
     PassMgr.add(createProfileLoaderPass(ProfileDataFile));
     PassMgr.run(M);

This does look good to me, I assume you have a debug build? Then use -debug-pass=Details to see what is going on in the passmanager.

     ProfileInfo *PI;
    PI = &getAnalysis<ProfileInfo>();

This is from the top of my head, but I guess you have to use this inside a pass, do you call this in the same module as the previous code? Or do you use this inside your profile-guided analysis pass?

Andi

Ah BTW...

    ProfileInfo *PI;
    PI = &getAnalysis<ProfileInfo>();

If this _in_ your pass, then you have to register the usage of this in the getAnalysisUsage() method:

void getAnalysisUsage(AnalysisUsage &AU) const {
  AU.addRequired<ProfileInfo>();
}

Also you have to ensure that the pass you are using is executed right after the ProfileInfoLoader, currently not all passes are preserving the profile information.

If you need intermediate passes I already have a patch that preserves (most) of the information, I'm waiting for after 2.7 to put this in...

Andi

I have not made a separate pass. I have done it in single pass ie added ProfileLoaderPass , got information in PI and now I am planning to perform the analysis here itself.

I was trying to print information using ProfileInfoPrinter pass but was unable to do it.

namespace {
class MyAna : public ModulePass {
   ProfileInfo *PI;
public:
   static char ID; // Class identification, replacement for typeinfo
   MyAna() : ModulePass(&ID) {}

   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
     AU.setPreservesAll();
     AU.addRequired<ProfileInfo>();
   }

   bool runOnModule(Module &M) {
     PassManager PassMgr = PassManager();
     cl::opt<std::string> ProfileDataFile(cl::Positional, cl::desc("<llvmprof.out file>"),
                 cl::Optional, cl::init("llvmprof.out"));
     PassMgr.add(createProfileLoaderPass(ProfileDataFile));
         PI = &getAnalysis<ProfileInfo>();

   }
};
}

char MyAna::ID = 0;
static RegisterPass<MyAna> X("my-ana", "Testing prof info");

What I basically want is to use block profile information in this pass to perform my analysis. Here I created llvmprof.out separately and then i am using it. Is there a way so that it is generated itself for the program?

Andreas Neustifter wrote:

Hi!

I was trying to print information using ProfileInfoPrinter pass but was
unable to do it.

How did you try that? The ProfileInfoPrinter is local to the llvm-prof tool, so you can not just use this pass, please try the llvm-prof tool on your generated llvmprof.out and see if that output is fine.

If this output is not what you expect, please tell me the exact steps on how you generate the profile...

namespace {
class MyAna : public ModulePass {
ProfileInfo *PI;
public:
static char ID; // Class identification, replacement for typeinfo
MyAna() : ModulePass(&ID) {}

virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
AU.addRequired<ProfileInfo>();
}

This looks fine to me, haven't tried to compile it tough...

bool runOnModule(Module &M) {
PassManager PassMgr = PassManager();
cl::opt<std::string> ProfileDataFile(cl::Positional,
cl::desc("<llvmprof.out file>"),
cl::Optional, cl::init("llvmprof.out"));
PassMgr.add(createProfileLoaderPass(ProfileDataFile));

You do not need to generate a PassManager inside your pass. This is all handled by LLVM. You just request the analysis as above and fetch the results with "getAnalysis<ProfileInfo>()".

> PI = &getAnalysis<ProfileInfo>();

Yes, exactly so. But to fully understand this please read carefully the information provided in
http://llvm.org/docs/WritingAnLLVMPass.html#interaction

What I basically want is to use block profile information in this pass
to perform my analysis. Here I created llvmprof.out separately and then
i am using it. Is there a way so that it is generated itself for the
program?

I'm not sure if I understand you correctly, I have written quite a detailed post on how to do profiling, maybe you can work trough this: http://lists.cs.uiuc.edu/pipermail/llvmdev/2009-November/026931.html

Attached in this message is also a profile.pl that does this all automatically.

Andi