Leonard Schütz
Leonard Schütz

Reputation: 526

LLVM IR getelementptr invalid indices

I'm currently in the phase of learning how to use LLVM. I'm trying to compile the following file via llc struct-method.ll -o struct-method.

struct-method.ll

; ModuleID = 'struct-method.ll'

@.formatstring = private unnamed_addr constant [13 x i8] c"%c\0A%ld\0A%lld\0A\00"

%box = type { i8, i32, i64 }

declare i32 @printf(i8* noalias nocapture, ...)

define i32 @set_prop_32(%box* %object, i32 %value) {
entry:
  %0 = getelementptr inbounds %box, %box* %object, i64 0, i64 1
  %1 = load i32, i32* %0
  ret i32 %1
}

define i32 @main() {
alloca:
  %mybox = alloca %box
  br label %entry

entry:
  %format = getelementptr [13 x i8], [13 x i8]* @.formatstring, i64 0, i64 0
  %0 = getelementptr inbounds %box, %box* %mybox, i64 0, i64 0
  %1 = getelementptr inbounds %box, %box* %mybox, i64 0, i64 1
  %2 = getelementptr inbounds %box, %box* %mybox, i64 0, i64 2

  store i8  65,       i8*  %0
  store i32 200,      i32* %1
  store i64 9999999,  i64* %2

  %f8 = load i8, i8* %0
  %f32 = load i32, i32* %1
  %f64 = load i64, i64* %2

  call i32 (i8*, ...) @printf(i8* %format, i8 %f8, i32 %f32, i64 %f64)

  call i32 (%box*, i32) @set_prop_32(%box* %mybox, i32 300)

  call i32 (i8*, ...) @printf(i8* %format, i8 %f8, i32 %f32, i64 %f64)

  ret i32 0
}

However I'm getting invalid getelementptr indices on line 11.

Does someone know why this could be and what I would write to solve this problem?

Edit: I'm on macOS Sierra 10.12 on a Macbook Pro late 2013.

Upvotes: 2

Views: 1967

Answers (1)

Chirag Patel
Chirag Patel

Reputation: 1161

as per http://llvm.org/docs/LangRef.html#getelementptr-instruction

"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"

in your case type { i8, i32, i64 } is struct type so try using i32 type indices.

instead of

%0 = getelementptr inbounds %box, %box* %object, i64 0, i64 1

try

%0 = getelementptr inbounds %box, %box* %object, i32 0, i32 1

Upvotes: 6

Related Questions