I have a toolchain of two passes. First one, is a transformation pass that should add metadata to some structures (instructions/variables) and the second pass is an analyzing pass which needs to access the added metadata. The problem is with my adding metadata transformation pass. There might be two problems(or both):
First, maybe I don’t add correctly metadata.
LLVMContext& C = myInstruction->getContext();
MDNode* N = MDNode::get(C, MDString::get(C, "add info"));
myInstruction->setMetadata("important", N);
errs()<<"\n"<<cast<MDString>(myInstruction->getMetadata("important")->getOperand(0))->getString();
However, “add info” is printed after running the pass.
Second, it seems that the transformations are not applied on the .bc of the target program.
The Test1.bc (clean) and Test2.bc (transformation applied) are the same. I just have
using namespace llvm;
namespace {
struct metadata : public FunctionPass {
const Function *F;
static char ID; // Pass identifcation, replacement for typeid
metadata() : FunctionPass(ID) {
//initializeMemDepPrinterPass(*PassRegistry::getPassRegistry());
}
virtual bool runOnFunction(Function &F);
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
}
// virtual void releaseMemory() {
// F = 0;
// }
};
}
char metadata::ID = 0;
static RegisterPass<metadata> X("my-metadata", "Adding metadata", false, true);
at the beginning of my transformation pass. Please tell me how can I add metadata persistently.
Thank you for your answers !
The issue of interactions between passes (as raised by Oak’s comments) nonwithstanding, it’s not hard to write a pass that actually modifies the module by adding metadata. Here’s a (basic-block, for easier writing) pass that adds the same metadata to each instruction it encounters. If you dump the module before and after running this pass, you will see that the module is indeed modified:
Note that the
run***method returnstrueto signal to the pass manager that the basic block was indeed modified.