函数的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的方法readAnnotation
在Utils.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,拼接为一个字符串并返回。更多请参考这里