Skip to content

Commit 9489de7

Browse files
committed
Use 'Expression.MakeStruct' rather than a sequence of cast operations to construct lists.
1 parent baf63b4 commit 9489de7

File tree

4 files changed

+39
-18
lines changed

4 files changed

+39
-18
lines changed

typed_python/compiler/native_ast_to_llvm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ def _convert(self, expr):
849849
for i in range(len(exprs)):
850850
value = self.builder.insert_value(value, exprs[i], i)
851851

852-
return TypedLLVMValue(value, native_ast.Type.Struct(names_and_types))
852+
return TypedLLVMValue(value, native_ast.Type.Struct(element_types=names_and_types))
853853

854854
if expr.matches.StructElementByIndex:
855855
val = self.convert(expr.left)

typed_python/compiler/tests/list_of_compilation_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ def checkFunction(self, f, argsToCheck):
4242
self.assertEqual(fastval, slowval)
4343
return t_py, t_fast
4444

45+
def test_compiled_empty_list(self):
46+
@Entrypoint
47+
def f():
48+
return ListOf(int)()
49+
50+
lst = f()
51+
52+
assert isinstance(lst, ListOf(int))
53+
assert lst.reserved() == 1
54+
4555
def test_list_of_list_refcounts(self):
4656
@Compiled
4757
def f(x: ListOf(ListOf(int)), z: bool):

typed_python/compiler/type_wrappers/list_of_wrapper.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,17 @@ def createEmptyList(self, context, out):
360360
out.expr.store(
361361
runtime_functions.malloc.call(28).cast(self.getNativeLayoutType())
362362
)
363-
>> out.nonref_expr.ElementPtrIntegers(0, 0).store(native_ast.const_int_expr(1)) # refcount
364-
>> out.nonref_expr.ElementPtrIntegers(0, 1).store(native_ast.const_int32_expr(-1)) # hash cache
365-
>> out.nonref_expr.ElementPtrIntegers(0, 2).store(native_ast.const_int32_expr(0)) # count
366-
>> out.nonref_expr.ElementPtrIntegers(0, 3).store(native_ast.const_int32_expr(1)) # reserved
367-
>> out.nonref_expr.ElementPtrIntegers(0, 4).store(
368-
runtime_functions.malloc.call(self.underlyingWrapperType.getBytecount())
369-
) # data
363+
>> out.nonref_expr.ElementPtrIntegers(0).store(
364+
native_ast.Expression.MakeStruct(
365+
args=(
366+
('refcount', native_ast.const_int_expr(1)),
367+
('hash_cache', native_ast.const_int32_expr(-1)),
368+
('count', native_ast.const_int32_expr(0)),
369+
('reserved', native_ast.const_int32_expr(1)),
370+
('data', runtime_functions.malloc.call(self.underlyingWrapperType.getBytecount())),
371+
)
372+
)
373+
)
370374
)
371375

372376
def convert_setitem(self, context, expr, index, item):

typed_python/compiler/type_wrappers/tuple_of_wrapper.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,23 @@ def initializeEmptyListExpr(self, out, length):
162162
runtime_functions.malloc.call(native_ast.const_int_expr(28))
163163
.cast(self.tupleTypeWrapper.getNativeLayoutType())
164164
) >>
165-
out.expr.load().ElementPtrIntegers(0, 4).store(
166-
runtime_functions.malloc.call(
167-
length.nonref_expr
168-
.mul(native_ast.const_int_expr(self.underlyingWrapperType.getBytecount()))
169-
).cast(native_ast.UInt8Ptr)
170-
) >>
171-
out.expr.load().ElementPtrIntegers(0, 0).store(native_ast.const_int_expr(1)) >>
172-
out.expr.load().ElementPtrIntegers(0, 1).store(native_ast.const_int32_expr(-1)) >>
173-
out.expr.load().ElementPtrIntegers(0, 2).store(native_ast.const_int32_expr(0)) >>
174-
out.expr.load().ElementPtrIntegers(0, 3).store(length.nonref_expr.cast(native_ast.Int32))
165+
out.expr.load().ElementPtrIntegers(0).store(
166+
native_ast.Expression.MakeStruct(
167+
args=(
168+
('refcount', native_ast.const_int_expr(1)),
169+
('hash_cache', native_ast.const_int32_expr(-1)),
170+
('count', native_ast.const_int32_expr(0)),
171+
('reserved', length.nonref_expr.cast(native_ast.Int32)),
172+
(
173+
'data',
174+
runtime_functions.malloc.call(
175+
length.nonref_expr
176+
.mul(native_ast.const_int_expr(self.underlyingWrapperType.getBytecount()))
177+
)
178+
),
179+
)
180+
)
181+
)
175182
)
176183

177184

0 commit comments

Comments
 (0)