-1

I had read this page , http://llvm.org/docs/WritingAnLLVMPass.html

And i can do the example of the Hello.so completely.

Now i just want to make a .so file that can be called by opt and read my IR file name as input argument. And after i commit it , it will output the name of the file.

I had tried several methods before , but i still don't know how to do it....

I hope i can do it like this.

opt -load ../Debug+Asserts/lib/xxxx.so -flag < llvm.ll > /dev/null

when i press ENTER , it will output the name of the file -> "llvm.ll"

Can anyone help me write this simple program , i am going to optimize the llvm IR as my semester project , and now i stuck here ... help me , thanks ~


Can you tell me the code in detail , this doesn't work for me

using namespace llvm;

namespace {
  struct Hello : public ModulePass {
    static char ID;
    Hello() : ModulePass(ID) {}

   virtual  bool runOnModule(Module &M) {
       dbgs() << M.getModuleIdentifier() << "\n";
      return false;
     }
  };
}

char Hello::ID = 0;
static RegisterPass<Hello> X("hello", "Hello World Pass", false, false);
~
Ian Tsai
  • 21
  • 3

1 Answers1

2

Your question could really be simplified to "how can I access the name of the current .ll file from within an LLVM pass". You don't need to "parse LLVM IR" or anything like that - when an LLVM pass is being ran it is already way past the parsing phase.

In any case, I'm not aware of any surefire way to get the filename from an LLVM module, but you can encode that information when you prepare the .ll file. For example, set the module id to be the filename via ; ModuleID = 'llvm.ll', then retrieve it by writing a module pass and invoking getModuleIdentifier to get the string. Then you could just print it out, e.g.

bool runOnModule(Module& M) {
  dbgs() << M.getModuleIdentifier() << "\n";
  return false;
}

Alternatively, use metadata.

Oak
  • 26,231
  • 8
  • 93
  • 152
  • I have another question , there are functionPass and ModulePass , does these two supprot the "ll" type of llvm IR , or only support the "bc" type. – Ian Tsai May 27 '13 at 17:02
  • @IanTsai did you set the module id to be some string, and then still got ""? – Oak May 27 '13 at 18:41
  • @IanTsai and regarding .bc and .ll, it's the same thing: see [this](http://stackoverflow.com/questions/14107743/llvm-and-compiler-nomenclature/14108628#14108628) and [this](http://stackoverflow.com/questions/12929670/what-is-llvm-intermediate-representation/12954198#12954198) related answers of mine – Oak May 28 '13 at 07:12