[LLVMdev] Having trouble with GEP

Dirkjan Ochtman dirkjan at ochtman.nl
Tue Jan 15 23:58:29 PST 2013


On Wed, Jan 16, 2013 at 8:22 AM, Tim Northover <t.p.northover at gmail.com> wrote:
> I get that error when the %str type is undefined (presumably because
> LLVM doesn't know its alignment requirements so can't lay out the
> array$str type properly). For example, the following complete program
> works correctly for me:
>
> %str = type {i8*, i64}
> %array$str = type { i64, [0 x %str] }
>
> declare i8* @malloc(i64)
>
> define i64 @foo(i64 %objsz) {
>   %array.raw = call i8* @malloc(i64 %objsz)
>   %array = bitcast i8* %array.raw to %array$str*
>   %array.data = getelementptr %array$str* %array, i32 0, i32 1
>   %ret = ptrtoint [0 x %str]* %array.data to i64
>   ret i64 %ret
> }
>
> But it fails if the first line is commented out. Could you give a
> similar complete example that has the error you're seeing?

Yes:

%array$str = type { i64, [0 x %str] }

declare i8* @malloc(i64)

define i64 @foo(i64 %objsz) {
    %array.raw = call i8* @malloc(i64 %objsz);
    %array = bitcast i8* %array.raw to %array$str*
    %array.data = getelementptr %array$str* %array, i32 0, i32 1
    %ret = ptrtoint [0 x %str]* %array.data to i64
    ret i64 %ret
}

%str = type {i8*, i64}

So you're right that it does have to do with the %str type not being defined.

What I'm confused about is why LLVM isn't able to generate a better
error message than this. Perhaps this is one of those cases that
hasn't been tested much because not many people write their IR without
making use of LLVM's API, which would error out sooner if I tried
this? Or is there a more fundamental reason why there's no better
error message? Is it useful to create a bug about this?

Cheers,

Dirkjan



More information about the llvm-dev mailing list