diff --git a/llvmlite/ir/instructions.py b/llvmlite/ir/instructions.py index 7e82ee032..54fc92e30 100644 --- a/llvmlite/ir/instructions.py +++ b/llvmlite/ir/instructions.py @@ -494,7 +494,13 @@ def __init__(self, parent, ptr, indices, inbounds, name): typ = ptr.type lasttyp = None lastaddrspace = 0 + indices_new = [] for i in indices: + if isinstance(i, int): + bits = max(i.bit_length(), 32) + i_type = types.IntType(bits) + i = i_type(i) + indices_new.append(i) lasttyp, typ = typ, typ.gep(i) # inherit the addrspace from the last seen pointer if isinstance(lasttyp, types.PointerType): @@ -507,9 +513,9 @@ def __init__(self, parent, ptr, indices, inbounds, name): typ = typ.as_pointer(lastaddrspace) super(GEPInstr, self).__init__(parent, typ, "getelementptr", - [ptr] + list(indices), name=name) + [ptr] + indices_new, name=name) self.pointer = ptr - self.indices = indices + self.indices = indices_new self.inbounds = inbounds def descr(self, buf): diff --git a/llvmlite/tests/test_ir.py b/llvmlite/tests/test_ir.py index e97e528ac..769143be4 100644 --- a/llvmlite/tests/test_ir.py +++ b/llvmlite/tests/test_ir.py @@ -839,15 +839,16 @@ def test_mem_ops(self): def test_gep(self): block = self.block(name='my_block') builder = ir.IRBuilder(block) + elem_type = ir.LiteralStructType([int32, int32]) a, b = builder.function.args[:2] - c = builder.alloca(ir.PointerType(int32), name='c') - d = builder.gep(c, [ir.Constant(int32, 5), a], name='d') + c = builder.alloca(ir.PointerType(elem_type), name='c') + d = builder.gep(c, [ir.Constant(int32, 5), a, 1], name='d') self.assertEqual(d.type, ir.PointerType(int32)) self.check_block(block, """\ my_block: - %"c" = alloca i32* - %"d" = getelementptr i32*, i32** %"c", i32 5, i32 %".1" - """) + %"c" = alloca {i32, i32}* + %"d" = getelementptr {i32, i32}*, {i32, i32}** %"c", i32 5, i32 %".1", i32 1 + """) # noqa E501 # XXX test with more complex types def test_gep_castinstr(self):