具有运行时值的 LLVM GEP

LLVM GEP with runtime value

我有一个定义为 %Colour = type { { i32, i32, i32 }, %RED*, %GREEN*, %BLUE*, %RGB* } 的类型,我使用第一个字段来存储 RTTI,我在 Colour*.0.0.2 加载字段并将其保存在变量 "arity" 中,但是当我尝试再次对对象执行 GEP 以获得 RGB*builder.CreateInBoundsGEP(valType, val, {builder.getInt32(0), arity}) 我得到 "Invalid GetElementPtrInst indices for type!"。转储 arity 类型,我得到了 i32,我做错了什么?

我不能在 GEP 中使用运行时值吗?我问是因为,在我 运行 之后,我尝试将其更改为 builder.CreateInBoundsGEP(valType, val, {builder.getInt32(0), builder.getInt32(4)}),这会产生 %2 = getelementptr inbounds %Colour, %Colour* %0, i32 0, i32 4 的预期结果,因此我可以加载它 %3 = load %RGB*, %RGB** %2

Can I not use a runtime value in a GEP?

在对该类型的结构组件进行索引时,您必须使用常量索引。否则不可能静态地知道 GEP 指令的类型

换句话说,因为 getelementptr inbounds %Colour, %Colour* %0, i32 0, <strong>i32 4</strong>%RGB**%y = getelementptr inbounds %Colour, %Colour* %0, i32 0, <strong>i32 3</strong>%BLUE**, 和GEP 的类型必须是静态已知的,最后一个索引必须是常量。

数组中的 GEP 索引不必是常量。

这是全部written explicitly in the langref:

The type of each index argument depends on the type it is indexing into. When indexing into a (optionally packed) structure, only i32 integer constants are allowed (when using a vector of indices they must all be the same i32 integer constant). When indexing into an array, pointer or vector, integers of any width are allowed, and they are not required to be constant. These integers are treated as signed values where relevant.

(强调我的)