How to link the right libraries?

Hello, llvmers. Could someone explain me a little about the opt tool? I am
having problems to load a MachineFunctionPass using opt. I have this pass:

#include "llvm/Pass.h"
#include "llvm/Function.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include <iostream>
using namespace llvm;

namespace {
  struct MacFoo : public MachineFunctionPass {
    virtual bool runOnMachineFunction(MachineFunction &Fn) {
      std::cerr << " calling run machine function.\n";
      return false;
    }
    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
      AU.setPreservesAll();
    }
    virtual const char *getPassName() const {
      return "run on machine function";
    }
  };
  RegisterOpt<MacFoo> Z("allocpty", "Register Allocation Prototype Pass");

  struct FuncFoo : public FunctionPass {
    virtual bool runOnFunction(Function &F) {
      std::cerr << "Function name: " << F.getName() << "\n";
      return false;
    }
    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
      AU.setPreservesAll();
    }
  };
  RegisterOpt<FuncFoo> X("funcPass", "Hello World Pass");
}

When I call:

$> opt -load ../../../../llvm/Debug/lib/LLVMTest.so -funcPass < hello.bc >
/dev/null

It works fine; when I try the machineFunctionPass class, it goes like
this:

$> opt -load ../../../../llvm/Debug/lib/LLVMTest.so -allocpty < hello.bc >
/dev/null

"opt: error while loading shared libraries:
../../../../llvm/Debug/lib/LLVMTest.so: undefined symbol:
_ZN4llvm15MachineFunction3getEPKNS_8FunctionE"

I would really appreciate if someone could tell me which files do I have
to link to make the 'allocpty' to work. My Makefile is this:

LEVEL = ../../.. # points to $HOME/llvm
LIBRARYNAME = LLVMTest
SHARED_LIBRARY = 1
LOADABLE_MODULE = 1

include $(LEVEL)/Makefile.common

warm regards,

Fernando

Hello, llvmers. Could someone explain me a little about the opt tool? I am
having problems to load a MachineFunctionPass using opt. I have this pass:

MachineFunctionPass's cannot be used as part of opt, they can only be used as part of LLC. Further, you have to explicitly modify the target you are interested in to add it to the pass manager for that target.

What are you trying to do that requires a machine function pass?

-Chris

#include "llvm/Pass.h"
#include "llvm/Function.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include <iostream>
using namespace llvm;

namespace {
struct MacFoo : public MachineFunctionPass {
   virtual bool runOnMachineFunction(MachineFunction &Fn) {
     std::cerr << " calling run machine function.\n";
     return false;
   }
   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
     AU.setPreservesAll();
   }
   virtual const char *getPassName() const {
     return "run on machine function";
   }
};
RegisterOpt<MacFoo> Z("allocpty", "Register Allocation Prototype Pass");

struct FuncFoo : public FunctionPass {
   virtual bool runOnFunction(Function &F) {
     std::cerr << "Function name: " << F.getName() << "\n";
     return false;
   }
   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
     AU.setPreservesAll();
   }
};
RegisterOpt<FuncFoo> X("funcPass", "Hello World Pass");
}

When I call:

$> opt -load ../../../../llvm/Debug/lib/LLVMTest.so -funcPass < hello.bc >
/dev/null

It works fine; when I try the machineFunctionPass class, it goes like
this:

$> opt -load ../../../../llvm/Debug/lib/LLVMTest.so -allocpty < hello.bc >
/dev/null

"opt: error while loading shared libraries:
../../../../llvm/Debug/lib/LLVMTest.so: undefined symbol:
_ZN4llvm15MachineFunction3getEPKNS_8FunctionE"

I would really appreciate if someone could tell me which files do I have
to link to make the 'allocpty' to work. My Makefile is this:

LEVEL = ../../.. # points to $HOME/llvm
LIBRARYNAME = LLVMTest
SHARED_LIBRARY = 1
LOADABLE_MODULE = 1

include $(LEVEL)/Makefile.common

warm regards,

Fernando

_______________________________________________
LLVM Developers mailing list
LLVMdev@cs.uiuc.edu http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

-Chris

> Hello, llvmers. Could someone explain me a little about the opt tool? I am
> having problems to load a MachineFunctionPass using opt. I have this pass:

MachineFunctionPass's cannot be used as part of opt, they can only be used
as part of LLC. Further, you have to explicitly modify the target you are
interested in to add it to the pass manager for that target.

What are you trying to do that requires a machine function pass?

I see. Thank you, Chris. Actually, what I was trying to do was to
compile a little example with each pass, as the hello world pass
in http://llvm.org/docs/WritingAnLLVMPass.html. My intention is to
get more acquainted with LLVM, and C++

So, if I want to add machine dependent passes, for example, for the
X86, I have to add them in the X86TargetMachine.cpp file, is it right?
I am a little confused about where to register passes, though. For
instance, the passes "phi-node-elimination", and
"TwoAddressInstructionPass" are registered in
TwoAddressInstructionPass.cpp
and PHIElimination.cpp, but what does determine that the PHI elimination
pass is executed first? Also, why the register allocation pass is added
directly to the pass manager in X86TargetMachine, and the same does
not happen to the PHI elimination pass?

Thanks a lot,

Fernando

MachineFunctionPass's cannot be used as part of opt, they can only be used
as part of LLC. Further, you have to explicitly modify the target you are
interested in to add it to the pass manager for that target.

What are you trying to do that requires a machine function pass?

I see. Thank you, Chris. Actually, what I was trying to do was to
compile a little example with each pass, as the hello world pass
in http://llvm.org/docs/WritingAnLLVMPass.html. My intention is to
get more acquainted with LLVM, and C++

Ok.

So, if I want to add machine dependent passes, for example, for the
X86, I have to add them in the X86TargetMachine.cpp file, is it right?

Yes.

I am a little confused about where to register passes, though. For
instance, the passes "phi-node-elimination", and
"TwoAddressInstructionPass" are registered in
TwoAddressInstructionPass.cpp
and PHIElimination.cpp, but what does determine that the PHI elimination
pass is executed first? Also, why the register allocation pass is added
directly to the pass manager in X86TargetMachine, and the same does
not happen to the PHI elimination pass?

PHI elimination is a detail of particular register allocator algorithms: in particular, some arbitrary register allocator could choose to eliminate PHI's in a way that is different than the other regallocs. As such, phi elimination is handled with the pass dependency mechanism, the specific register allocators that want it declare that they need phi's to be eliminated.

-Chris