[LLVMdev] adding a codegen pass into llvm

Jeff Kunkel jdkunk3 at gmail.com
Tue Jan 18 05:56:09 PST 2011


I have created a few codegen passes, and there are quite a few steps to
them. Here is how I would sum up the steps.

1. In the cpp file, put the initilize pass macros:

INITIALIZE_PASS_BEGIN(MyPassName, "a-short-space-free-description",
  "A long description", true, true)
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
INITIALIZE_PASS_END(MyPassName, "a-short-space-free-description",
  "A long description", true, true)

2. Create the class with the pass dependencies noted in the constructor.

class MyPassName: public MachineFunctionPass {
public:
  static char ID;
  MyPassName() {
    llvm::initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
  }
  virtual bool runOnMachineFunction(MachineFunction &mf);
  virtual void getAnalysisUsage(AnalysisUsage &AU) const;
  virtual void releaseMemory();
};
char CodegenPass ::ID = 0;

3. Create an initilization function for you pass in Passes.h, and please
note the concatenation of the pass name in the center of initialize and
Pass.
"void initializeMyPassNamePass(PassRegistry &Registry);"

4. If your class is not instantiated in a header, then place an "extern char
& MyPassNameID;" into passes.h so other passes can find it's dependencies.

5. I am not 100% sure this is necessary, but I do it anyway to be sure. In
CodeGen.cpp, place "initializeMyPassNamePass(Registry);" in the
"initializeCodeGen" function. It may be possible to use opt, but I have not
tired it.

6. The "LinkAllPasses.h" is more important if you are overriding the
Dag-Scheduler or Register allocator or some other important, ever present
pass. For a regular pass, other passes will call
the initializeMyPassNamePass, and other passes will use Analytical usage to
run it.

7. This is the hardest part. Find out where in this long chain of
CodeGen dependencies you can place your pass. If you want to do a pass
before register allocation, you may have to add some Analytical usage fields
to the first reg-alloc pass which is really SlotIndexes. Perhaps you can put
something in between other passes, but it's difficult. The dependencies are
such that I have found it difficult to place a simple analysis pass between
some steps.

- Writing a codegen pass by,
Jeff Kunkel

On Mon, Jan 17, 2011 at 11:44 PM, Qingan Li <ww345ww at gmail.com> wrote:

> Thanks for your last reply.
> Could I understand the way to adding a pass (built into the llvm rather
> than
> dynamic loadable) includes:
> 1. Declaring a creator function for this pass
> 2. Implementing the creator function for this pass
> 3. Instantiating this pass and get a object of it
> 3. Register this pass into the PassRegistry
>
> Then, for a built-into bytecode pass,
>         task 1(declaration of the creator) should be done in Scalar.h;
>         task 2(implementation of the creator) should be done the related
> mypass.cpp file;
>         task 3(instantiation of the pass class) should be done in
> LinkAllPasses.h;
>         task 4(registration of the pass into the PassRegistry) should be
> done by INITIALIZE_PASS
> class LiveVariables : public MachineFunctionPass is a case of point.
>
> For a built-into codegen/MachineCode pass,
>         task 1 should be done in Passes.h;
>         task 2 should be done in the related mypass.cpp file;
>         task 3 should be done in LLVMTargetMachine.cpp
>         task 4 should be done by INITIALIZE_PASS
>  class IntervalAnalylsis: public MachineFunctionPass is a case in point.
>
> I have implemented a new mypass (just for analysis, rather than
> transformation) as a subclass of MchineFunctionPass, and finished task 1,
> 2,
> 4. But I don't know how to finish task 3, since I failed to make clear how
> the class IntervalAnalysis did task 3. So I need your help.
>
> --
> Best regards,
>
> Li Qingan
>
>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20110118/352c16ef/attachment.html>


More information about the llvm-dev mailing list