The modern equivalent of LLVM AnnotationManager?

Now that the LLVM AnnotationManager is gone (it disappeared in version 2.6, I think?), How can I get annotations for certain functions, globals and instructions?

(For example, I have a bitcode compiled from C void myFunction(__attribute__((annotate("foo"))) int var)--- given the Argument *reference to this argument int var, how can I determine which attributes are annotatebound to it?)

+3
source share
2 answers

To get annotations for a specific function, go to the element’s BasicBlock element to find its calls in @llvm.var.annotation, as shown below:

Module *module;

[...]

std::string getGlobalVariableString(std::string name)
{
    // assumption: the zeroth operand of a Value::GlobalVariableVal is the actual Value
    Value *v = module->getNamedValue(name)->getOperand(0);
    if(v->getValueID() == Value::ConstantArrayVal)
    {
        ConstantArray *ca = (ConstantArray *)v;
        return ca->getAsString();
    }

    return "";
}

void dumpFunctionArgAnnotations(std::string funcName)
{
    std::map<Value *,Argument*> mapValueToArgument;

    Function *func = module->getFunction(funcName);
    if(!func)
    {
        std::cout << "no function by that name.\n";
        return;
    }

    std::cout << funcName << "() ====================\n";


    // assumption: @llvm.var.annotation calls are always in the function entry block.
    BasicBlock *b = &func->getEntryBlock();


    // run through entry block first to build map of pointers to arguments
    for(BasicBlock::iterator it = b->begin();it!=b->end();++it)
    {
        Instruction *inst = it;
        if(inst->getOpcode()!=Instruction::Store)
            continue;

        // `store` operands: http://llvm.org/docs/LangRef.html#i_store
        mapValueToArgument[inst->getOperand(1)] = (Argument *)inst->getOperand(0);
    }


    // run through entry block a second time, to associate annotations with arguments
    for(BasicBlock::iterator it = b->begin();it!=b->end();++it)
    {
        Instruction *inst = it;
        if(inst->getOpcode()!=Instruction::Call)
            continue;

        // assumption: Instruction::Call operands are the function arguments, followed by the function name
        Value *calledFunction = inst->getOperand(inst->getNumOperands()-1);
        if(calledFunction->getName().str() != "llvm.var.annotation")
            continue;

        // `llvm.var.annotation` operands: http://llvm.org/docs/LangRef.html#int_var_annotation

        Value *annotatedValue = inst->getOperand(0);
        if(annotatedValue->getValueID() != Value::InstructionVal + Instruction::BitCast)
            continue;
        Argument *a = mapValueToArgument[annotatedValue->getUnderlyingObject()];
        if(!a)
            continue;

        Value *annotation = inst->getOperand(1);
        if(annotation->getValueID() != Value::ConstantExprVal)
            continue;
        ConstantExpr *ce = (ConstantExpr *)annotation;
        if(ce->getOpcode() != Instruction::GetElementPtr)
            continue;

        // `ConstantExpr` operands: http://llvm.org/docs/LangRef.html#constantexprs
        Value *gv = ce->getOperand(0);

        if(gv->getValueID() != Value::GlobalVariableVal)
            continue;

        std::cout << "    argument " << a->getType()->getDescription() << " " << a->getName().str()
            << " has annotation \"" << getGlobalVariableString(gv->getName().str()) << "\"\n";
    }
}
+4
source

AnnotationManager , ( ). llvm.global.annotations , , , .

IR, , C- IR .

+2

Source: https://habr.com/ru/post/1791634/


All Articles