如何获取LLVM中结构体成员的值?

问题描述 投票:0回答:3

所以我用这个创建了一个结构类型:

llvm::StructType* llvm_struct = llvm::StructType::create(llvm_context, struct_name);
std::vector<llvm::Type*> members;

for(size_t j = 0; j != struct_data.members.size(); j++){
    llvm::Type* member_type = /*get member type*/;
    members.push_back(member_type);
}

llvm_struct->setBody(members)

我想知道如何访问结构中的成员。

到目前为止,我尝试使用 getelementptr 但没有运气:

llvm::Value* member_index = llvm::ConstantInt::get(llvm_context, llvm::APInt(32, /*structure member index*/, true));
llvm::Value* indices[2] = {llvm::ConstantInt::get(member_index->getType(), 0), member_index};
llvm::Value* data = /*expression value*/;

return irbuilder.CreateInBoundsGEP(data, llvm::ArrayRef<llvm::Value*>(indices, 2), "membtmp");

感谢您的任何反馈!

编辑:

好吧,

llvm::Value* data
的类型是从堆栈上的指针加载的
%a_struct
。从文档看来,
irbuilder.CreateInBoundsGEP(llvm::Value*, llvm::ArrayRef<llvm::Value*>, llvm::Twine)
要求第一个参数是指向结构的指针,而不是结构本身的值。

将结构体的值复制到堆栈上的变量时,抛出此错误:

Expression: getOperand(0)->getType() == cast<PointerType>(getOperand(1)->getType())->getElementType(‌​‌​) && "Ptr must be a pointer to Val type!"
。引发此错误时粘贴到
irbuidler.CreateInBoundsGEP(...)
的指针是一个
llvm::AllocaInst*
,它是在堆栈上新分配的,并且包含复制到其中的
llvm::Value* data
的值(
%a_struct
的类型)。

在调用

irbuilder.CreateInBoundsGEP(...)
之前生成的 IR,并将值复制到堆栈上的变量:

define i32 @main() {
entry:
  %calltmp = call %a_struct @new_a_struct()
  %a_var = alloca %a_struct
  store %a_struct %calltmp, %a_struct* %a_var
  %a_var1 = load %a_struct, %a_struct* %a_var
  %memballoctmp = alloca %a_struct
  store %a_struct %a_var1, %a_struct* %memballoctmp
}

此外,应该有一种更好的方法来访问

%a_var
的成员而不重复它(同时仍然支持语言中的
a_struct_var1.member + a_struct_var2.member
等表达方式)。

c++ llvm llvm-ir
3个回答
4
投票

理解这个概念需要一些时间,但投入时间是非常值得的。查看 llvm 语言参考中的 getelementpointer 文档。它解释了会员访问的工作原理。

struct RT {
  char A;
  int B[10][20];
  char C;
};
struct ST {
  int X;
  double Y;
  struct RT Z;
};

int *foo(struct ST *s) {
  return &s[1].Z.B[5][13];
}

可以直接读取结构体的成员 B[5][13]:

%arrayidx = getelementptr inbounds %struct.ST, %struct.ST* %s, i64 1, i32 2, i32 1, i64 5, i64 13

或间接:

%t1 = getelementptr %struct.ST, %struct.ST* %s, i32 1
%t2 = getelementptr %struct.ST, %struct.ST* %t1, i32 0, i32 2
%t3 = getelementptr %struct.RT, %struct.RT* %t2, i32 0, i32 1
%t4 = getelementptr [10 x [20 x i32]], [10 x [20 x i32]]* %t3, i32 0, i32 5
%t5 = getelementptr [20 x i32], [20 x i32]* %t4, i32 0, i32 13

我会告诉你直接的方法。让我们首先创建结构体

StructType* createStruct(Module &M)
{
    Type* intTy = Type::getInt32Ty(M.getContext());
    Type* charTy = Type::getInt8Ty(M.getContext());
    Type* doubleTy = Type::getDoubleTy(M.getContext());

    auto* _B = ArrayType::get(intTy, 20);
    auto*  B = ArrayType::get(_B, 10);
    auto* RT = StructType::create("struct.RT", charTy, B, charTy);
    auto* ST = StructType::create("struct.ST", intTy, doubleTy, RT);

    RT->dump();
    ST->dump();
    return ST;
}

现在我们可以使用 gep 来访问该结构,但首先我们需要一个 Values* 向量来存储索引以访问特定的 gep 地址

template <size_t N>
std::vector<Value*> getIndex(Module &M, int (&dims)[N])
{
    std::vector<Value*> idx;
    for (auto i : dims)
    {
        idx.push_back(ConstantInt::get(M.getContext(), APInt(32, i, true)));
    }
    for (auto i : idx)
    {
        i->dump();
    }
    return idx;
}

void doGEP(Module &M)
{
    auto* structInst = createStruct(M);
    auto* structGlobVar = new GlobalVariable(M, structInst, true, GlobalVariable::ExternalLinkage, UndefValue::get(structInst), "_structGV", nullptr, GlobalVariable::ThreadLocalMode::NotThreadLocal, 0, true);
    structGlobVar->dump();
    int dims[] = {1, 2, 1, 5, 13};
    std::vector<Value*> indx = getIndex(M, dims);
    auto* gepInst = builder.CreateGEP(structGlobVar, indx);
    gepInst->dump();
}

将生成输出:

%struct.RT = type { i8, [10 x [20 x i32]], i8 }
%struct.ST = type { i32, double, %struct.RT }
i32 1
i32 2
i32 1
i32 5
i32 13
@_structGV = externally_initialized constant %struct.ST undef
i32* getelementptr (%struct.ST, %struct.ST* @_structGV, i32 1, i32 2, i32 1, i32 5, i32 13)

2
投票

我找到了解决方案。我认为我错误地传递了索引或其他东西。

注意:我还没有对具有不同数据类型的成员进行测试,但它似乎有效

llvm::Value* member_index = llvm::ConstantInt::get(llvm_context, llvm::APInt(32, index /*The index of the member*/, true));
llvm::Value* data = /*A structure value*/;

llvm::AllocaInst* alloc = irbuilder.CreateAlloca(struct_type, 0, "alloctmp");
irbuilder.CreateStore(data, alloc);

std::vector<llvm::Value*> indices(2);
indices[0] = llvm::ConstantInt::get(llvm_context, llvm::APInt(32, 0, true));
indices[1] = member_index;

llvm::Value* member_ptr = irbuilder.CreateGEP(struct_type, alloc, indices, "memberptr");
llvm::Value* loaded_member = irbuilder.CreateLoad(member_ptr, "loadtmp");

0
投票

这个问题有点老了,但仍然有很多观点,所以这里有一个更简单、更有效的方法来使用 CreateExtractValue 方法获取结构成员的值。它避免使用 CreateStore 将整个结构存储在内存中,然后重新加载所需的成员。你上面的代码看起来像这样:

std::vector<llvm::Value*> indices(1);
indices[0] = index;
llvm::Value* memberValue = irbuilder.CreateExtractValue(data, indices);

希望这有帮助

© www.soinside.com 2019 - 2024. All rights reserved.