Skip to content

Commit 4fd3b59

Browse files
authored
Merge pull request #1158 from rj-jesus/rjj/opaque-pointer-fixes
Opaque pointer fixes
2 parents 2677283 + 667dac7 commit 4fd3b59

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

llvmlite/ir/types.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,17 @@ def _to_string(self):
135135
else:
136136
return "ptr"
137137

138+
def __eq__(self, other):
139+
return (isinstance(other, PointerType) and
140+
self.addrspace == other.addrspace)
141+
138142
def __hash__(self):
139143
return hash(PointerType)
140144

145+
@property
146+
def intrinsic_name(self):
147+
return 'p%d' % self.addrspace
148+
141149
@classmethod
142150
def from_llvm(cls, typeref, ir_ctx):
143151
"""
@@ -169,7 +177,8 @@ def __eq__(self, other):
169177
if isinstance(other, _TypedPointerType):
170178
return (self.pointee, self.addrspace) == (other.pointee,
171179
other.addrspace)
172-
return isinstance(other, PointerType)
180+
return (isinstance(other, PointerType) and
181+
self.addrspace == other.addrspace)
173182

174183
def __hash__(self):
175184
return hash(_TypedPointerType)

llvmlite/tests/test_ir.py

+66
Original file line numberDiff line numberDiff line change
@@ -2389,6 +2389,72 @@ def test_comparisons(self):
23892389
self.assertFalse(tp == other, (tp, other))
23902390
self.assertTrue(tp != other, (tp, other))
23912391

2392+
def test_ptr_comparisons(self):
2393+
# Create instances of:
2394+
# * Opaque pointers.
2395+
# * Typed pointers of i1's.
2396+
# * Typed pointers of i8's.
2397+
# The choice of types for the typed pointers are not consequential -
2398+
# they just need to differ. Each pointer class has two instances, one
2399+
# in address space 0, another in address space 1.
2400+
ptrs = {
2401+
'op_a0': ir.PointerType(),
2402+
'op_a1': ir.PointerType(addrspace=1),
2403+
'tp_i1_a0': ir.PointerType(int1),
2404+
'tp_i1_a1': ir.PointerType(int1, addrspace=1),
2405+
'tp_i8_a0': ir.PointerType(int8),
2406+
'tp_i8_a1': ir.PointerType(int8, addrspace=1),
2407+
}
2408+
2409+
def assert_eq(ptr1, ptr2):
2410+
self.assertTrue(ptr1 == ptr2, (ptr1, ptr2))
2411+
self.assertTrue(ptr2 == ptr1, (ptr2, ptr1))
2412+
2413+
self.assertFalse(ptr1 != ptr2, (ptr1, ptr2))
2414+
self.assertFalse(ptr2 != ptr1, (ptr2, ptr1))
2415+
2416+
def assert_ne(ptr1, ptr2):
2417+
self.assertFalse(ptr1 == ptr2, (ptr1, ptr2))
2418+
self.assertFalse(ptr2 == ptr1, (ptr2, ptr1))
2419+
2420+
self.assertTrue(ptr1 != ptr2, (ptr1, ptr2))
2421+
self.assertTrue(ptr2 != ptr1, (ptr2, ptr1))
2422+
2423+
for ptr in ptrs.values():
2424+
# Compare the pointers against any non-pointer type.
2425+
for other in self.assorted_types():
2426+
if not isinstance(other, ir.PointerType):
2427+
assert_ne(ptr, other)
2428+
# Compare the pointers against themselves.
2429+
assert_eq(ptr, ptr)
2430+
2431+
# Compare the pointers against each other.
2432+
# Opaque pointers are always equal, unless their address space differs.
2433+
# Typed pointers always differ, unless their pointee type and address
2434+
# space match.
2435+
assert_ne(ptrs['op_a0'], ptrs['op_a1'])
2436+
assert_eq(ptrs['op_a0'], ptrs['tp_i1_a0'])
2437+
assert_ne(ptrs['op_a0'], ptrs['tp_i1_a1'])
2438+
assert_eq(ptrs['op_a0'], ptrs['tp_i8_a0'])
2439+
assert_ne(ptrs['op_a0'], ptrs['tp_i8_a1'])
2440+
assert_ne(ptrs['op_a1'], ptrs['tp_i1_a0'])
2441+
assert_eq(ptrs['op_a1'], ptrs['tp_i1_a1'])
2442+
assert_ne(ptrs['op_a1'], ptrs['tp_i8_a0'])
2443+
assert_eq(ptrs['op_a1'], ptrs['tp_i8_a1'])
2444+
assert_ne(ptrs['tp_i1_a0'], ptrs['tp_i1_a1'])
2445+
assert_ne(ptrs['tp_i1_a0'], ptrs['tp_i8_a0'])
2446+
assert_ne(ptrs['tp_i1_a0'], ptrs['tp_i8_a1'])
2447+
assert_ne(ptrs['tp_i1_a1'], ptrs['tp_i8_a0'])
2448+
assert_ne(ptrs['tp_i1_a1'], ptrs['tp_i8_a1'])
2449+
assert_ne(ptrs['tp_i8_a0'], ptrs['tp_i8_a1'])
2450+
2451+
def test_ptr_intrinsic_name(self):
2452+
self.assertEqual(ir.PointerType().intrinsic_name, 'p0')
2453+
self.assertEqual(ir.PointerType(addrspace=1).intrinsic_name, 'p1')
2454+
# Note: Should this be adjusted based on the pointer mode?
2455+
self.assertEqual(ir.PointerType(int1).intrinsic_name, 'p0i1')
2456+
self.assertEqual(ir.PointerType(int1, 1).intrinsic_name, 'p1i1')
2457+
23922458
def test_str(self):
23932459
"""
23942460
Test the string representation of types.

0 commit comments

Comments
 (0)