Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow gep instructions to accept bare Python integers as indices. #710

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions llvmlite/ir/instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment on lines +501 to +502
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
i_type = types.IntType(bits)
i = i_type(i)
i = types.IntType(bits)(i)

indices_new.append(i)
lasttyp, typ = typ, typ.gep(i)
# inherit the addrspace from the last seen pointer
if isinstance(lasttyp, types.PointerType):
Expand All @@ -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):
Expand Down
11 changes: 6 additions & 5 deletions llvmlite/tests/test_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down