74 lines
1.9 KiB
Python
74 lines
1.9 KiB
Python
from llvm.core import *
|
|
import unittest
|
|
|
|
test_these_orderings = list(filter(bool, map(lambda s:s.strip(),
|
|
'''
|
|
unordered
|
|
monotonic
|
|
acquire
|
|
release
|
|
acq_rel
|
|
seq_cst
|
|
'''.splitlines())))
|
|
|
|
test_these_atomic_op = list(filter(bool, map(lambda s:s.strip(),
|
|
'''
|
|
xchg
|
|
add
|
|
sub
|
|
and
|
|
nand
|
|
or
|
|
xor
|
|
max
|
|
min
|
|
umax
|
|
umin
|
|
'''.splitlines())))
|
|
|
|
class TestAtomic(unittest.TestCase):
|
|
def test_atomic_cmpxchg(self):
|
|
mod = Module.new('mod')
|
|
functype = Type.function(Type.void(), [])
|
|
func = mod.add_function(functype, name='foo')
|
|
bb = func.append_basic_block('entry')
|
|
bldr = Builder.new(bb)
|
|
ptr = bldr.alloca(Type.int())
|
|
|
|
old = bldr.load(ptr)
|
|
new = Constant.int(Type.int(), 1234)
|
|
|
|
|
|
for ordering in test_these_orderings:
|
|
inst = bldr.atomic_cmpxchg(ptr, old, new, ordering)
|
|
self.assertEqual(ordering, str(inst).strip().split(' ')[-1])
|
|
|
|
|
|
inst = bldr.atomic_cmpxchg(ptr, old, new, ordering, crossthread=False)
|
|
self.assertEqual('singlethread', str(inst).strip().split(' ')[-2])
|
|
|
|
def test_atomic_rmw(self):
|
|
mod = Module.new('mod')
|
|
functype = Type.function(Type.void(), [])
|
|
func = mod.add_function(functype, name='foo')
|
|
bb = func.append_basic_block('entry')
|
|
bldr = Builder.new(bb)
|
|
ptr = bldr.alloca(Type.int())
|
|
|
|
old = bldr.load(ptr)
|
|
val = Constant.int(Type.int(), 1234)
|
|
|
|
for ordering in test_these_orderings:
|
|
inst = bldr.atomic_rmw('xchg', ptr, val, ordering)
|
|
self.assertEqual(ordering, str(inst).split(' ')[-1])
|
|
|
|
for op in test_these_atomic_op:
|
|
inst = bldr.atomic_rmw(op, ptr, val, ordering)
|
|
self.assertEqual(op, str(inst).strip().split(' ')[3])
|
|
|
|
inst = bldr.atomic_rmw('xchg', ptr, val, ordering, crossthread=False)
|
|
self.assertEqual('singlethread', str(inst).strip().split(' ')[-2])
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|