C++ errors when concatenating metadata in LLVM API

65 Views Asked by At

I'm writing an LLVM frontend and want to set alias.scope and noalias metadata to my memory operations. I've found some code in an pass (LoopVersioningLICM) to find an example of how this is done with the API:

void LoopVersioningLICM::setNoAliasToLoop(Loop *VerLoop) {
  // Get latch terminator instruction.
  Instruction *I = VerLoop->getLoopLatch()->getTerminator();
  // Create alias scope domain.
  MDBuilder MDB(I->getContext());
  MDNode *NewDomain = MDB.createAnonymousAliasScopeDomain("LVDomain");
  StringRef Name = "LVAliasScope";
  MDNode *NewScope = MDB.createAnonymousAliasScope(NewDomain, Name);
  SmallVector<Metadata *, 4> Scopes{NewScope}, NoAliases{NewScope};
  // Iterate over each instruction of loop.
  // set no-alias for all load & store instructions.
  for (auto *Block : CurLoop->getBlocks()) {
    for (auto &Inst : *Block) {
      // Only interested in instruction that may modify or read memory.
      if (!Inst.mayReadFromMemory() && !Inst.mayWriteToMemory())
        continue;
      // Set no-alias for current instruction.
      Inst.setMetadata(
          LLVMContext::MD_noalias,
          MDNode::concatenate(Inst.getMetadata(LLVMContext::MD_noalias),
                              MDNode::get(Inst.getContext(), NoAliases)));
      // set alias-scope for current instruction.
      Inst.setMetadata(
          LLVMContext::MD_alias_scope,
          MDNode::concatenate(Inst.getMetadata(LLVMContext::MD_alias_scope),
                              MDNode::get(Inst.getContext(), Scopes)));
    }
  }
}

So I've copied the line to set metadata in my own function (called with my own C wrapper):

void set_noalias(LLVMValueRef instruction, SmallVector<Metadata *, 4> Scope){
    Instruction *I = reinterpret_cast<Instruction *>(instruction);

    I->setMetadata(
        LLVMContext::MD_noalias,
        MDNode::concatenate(I->getMetadata(LLVMContext::MD_noalias),
                            MDNode::get(I->getContext(), Scope))
    );
}

But this causes errors for me. Firstly, on I->getcontext():

Cannot initialize object parameter of type 'const llvm::Value' with an expression of type 'Instruction'

And then if I hack this away by passing the required LLVMContext value directly as an argument, I then get on the MDNode::get() call:

Cannot initialize a parameter of type 'MDNode *' with an rvalue of type 'MDTuple *'

This is because MDNode::get() returns an MDTuple * but MDNode::concatenate() expects two MDNode * values. But the LLVM code passes an MDTuple * instead without any errors.

I'm unsure what the differences could be between my code and the LLVM code I'm looking at, is anyone able to explain what's going on and how to fix it?

Thanks.

0

There are 0 best solutions below