美文网首页
【LLVM】添加Annotation

【LLVM】添加Annotation

作者: 爱笑的云里看梦 | 来源:发表于2018-05-16 10:22 被阅读123次

函数的Annotation

LLVM通过在函数定义上使用annotation(注解),可以在编写源语言程序时控制LLVM在某个函数上的使用,或者禁止某些pass。这种方式为编程人员提供很大的灵活性。例如如下添加annotation的源代码片段:

int foo(int x) __attribute((__annotate__(("hello")))) {
    return x + 1;
}

clang 能够解析 annotation。LLVM IR 中的 llvm.global.annotations 含有 annotation 的信息,该变量是在 Module 中定义的。在 pass 的实现中可以先获取 annotation 内容并判断是否要进行处理。

对应到OLLVM中处理annotation的方法readAnnotationUtils.cpp文件中,该函数对annotation的处理方法如下:

std::string readAnnotate(Function *f) {
  std::string annotation = "";

  // Get annotation variable
  GlobalVariable *glob =
      f->getParent()->getGlobalVariable("llvm.global.annotations");

  if (glob != NULL) {
    // Get the array
    if (ConstantArray *ca = dyn_cast<ConstantArray>(glob->getInitializer())) {
      for (unsigned i = 0; i < ca->getNumOperands(); ++i) {
        // Get the struct
        if (ConstantStruct *structAn =
                dyn_cast<ConstantStruct>(ca->getOperand(i))) {
          if (ConstantExpr *expr =
                  dyn_cast<ConstantExpr>(structAn->getOperand(0))) {
            // If it's a bitcast we can check if the annotation is concerning
            // the current function
            if (expr->getOpcode() == Instruction::BitCast &&
                expr->getOperand(0) == f) {
              ConstantExpr *note = cast<ConstantExpr>(structAn->getOperand(1));
              // If it's a GetElementPtr, that means we found the variable
              // containing the annotations
              if (note->getOpcode() == Instruction::GetElementPtr) {
                if (GlobalVariable *annoteStr =
                        dyn_cast<GlobalVariable>(note->getOperand(0))) {
                  if (ConstantDataSequential *data =
                          dyn_cast<ConstantDataSequential>(
                              annoteStr->getInitializer())) {
                    if (data->isString()) {
                      annotation += data->getAsString().lower() + " ";
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  return annotation;
}

它实现的功能是获取与函数f相关的annotations,拼接为一个字符串并返回。更多请参考这里

相关文章

网友评论

      本文标题:【LLVM】添加Annotation

      本文链接:https://www.haomeiwen.com/subject/netpdftx.html