Debian Codegraph @

tensilelite/rocisa/test/test_code.py

raw
1 ################################################################################
2 #
3 # Copyright (C) 2025 Advanced Micro Devices, Inc. All rights reserved.
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell cop-
9 # ies of the Software, and to permit persons to whom the Software is furnished
10 # to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in all
13 # copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM-
16 # PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNE-
20 # CTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 ################################################################################
22
23 from rocisa import code
24 from rocisa.container import sgpr
25 from rocisa.instruction import SMovB32
26 from copy import deepcopy
27 import pickle
28 import timeit
29
30 def fastdeepcopy(x):
31 # Note: Some object can't be pickled
32 return pickle.loads(pickle.dumps(x))
33
34 def test_code():
35 label = code.Label("label", comment="comment")
36 print(label)
37 tesxtblock = code.TextBlock("textblock")
38 print(tesxtblock)
39
40 #Test Module
41 module = code.Module("module")
42 module.add(SMovB32(dst=sgpr(1), src=sgpr(2)))
43 module2 = module.add(code.Module("module2"))
44 module2.add(SMovB32(dst=sgpr(3), src=sgpr(4)))
45 print(module)
46 print(module.prettyPrint(""))
47
48 for item in module.items():
49 item.name = "newname"
50 for item in module.items():
51 assert item.name == "newname"
52
53 flat = module.flatitems()
54 testpop = []
55 while flat:
56 item = flat.pop(0)
57 testpop.append(item)
58 print("Test pop", testpop)
59
60
61 srdUpperValue = code.SrdUpperValue((9,4,2))
62 print(srdUpperValue.desc())
63 assert srdUpperValue.getValue() == 131072
64
65 signature = code.SignatureBase(kernelName="123",
66 kernArgsVersion=1,
67 codeObjectVersion="4",
68 groupSegmentSize=256,
69 sgprWorkGroup=(1, 1, 100),
70 vgprWorkItem=1,
71 flatWorkGroupSize=(256),
72 preloadKernArgs=True)
73 print(signature)
74
75 vs = code.ValueSet("BufferLimit", 0xFFFFFFFF, format=1)
76 print(vs)
77
78 def timeit_decorator(func):
79 def wrapper(*args, **kwargs):
80 start_time = timeit.default_timer()
81 result = func(*args, **kwargs)
82 end_time = timeit.default_timer()
83 print(f"Function {func.__name__} took {end_time - start_time:.6f} seconds")
84 return result
85 return wrapper
86
87 def copyfunc(obj, noPickle=False):
88 obj2 = deepcopy(obj)
89 print("Copied item using deepcopy:", obj2)
90 if noPickle:
91 return
92 obj3 = fastdeepcopy(obj)
93 print("Copied item using pickle:", obj3)
94
95 def test_copy():
96 label = code.Label("label", comment="comment")
97 copyfunc(label)
98 tesxtblock = code.TextBlock("textblock")
99 copyfunc(tesxtblock)
100
101 module = code.Module("module")
102 module.add(SMovB32(dst=sgpr(1), src=sgpr(2)))
103 module2 = module.add(code.Module("module2"))
104 module2.add(SMovB32(dst=sgpr(3), src=sgpr(4)))
105 copyfunc(module, True)
106
107 valueset = code.ValueSet("Test", 1)
108 copyfunc(valueset)
109
110 test_code()
111 test_copy()