Debian Codegraph @

tensilelite/rocisa/test/test_container.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 import rocisa
24 from copy import deepcopy
25 import pickle
26 import os
27
28 isa = (9, 0, 10)
29 rocm_path = os.environ.get("ROCM_PATH", "/opt/rocm")
30 global_isa = rocisa.rocIsa.getInstance()
31 global_isa.init(isa, rocm_path + "/bin/amdclang++", False)
32 global_isa.setKernel(isa, 64)
33
34 def test_containers():
35 # Test Container
36 container = rocisa.container.Container()
37 assert isinstance(container, rocisa.container.Container)
38
39 # Test DSModifiers
40 ds_modifiers = rocisa.container.DSModifiers(1, 2, gds=True)
41 assert str(ds_modifiers) == " offset:2 gds"
42
43 # Test FLATModifiers
44 flat_modifiers = rocisa.container.FLATModifiers(8, True, False, True, False)
45 assert str(flat_modifiers) == " offset:8 glc lds"
46
47 # Test MUBUFModifiers
48 mubuf_modifiers = rocisa.container.MUBUFModifiers(True, 12, True, False, True, False, True)
49 assert str(mubuf_modifiers) == " offen offset:12, glc"
50
51 # Test SMEMModifiers
52 smem_modifiers = rocisa.container.SMEMModifiers(True, False, 8)
53 assert str(smem_modifiers) == " offset:8 glc"
54
55 # Test SDWAModifiers
56 sdwa_modifiers = rocisa.container.SDWAModifiers(
57 dst_sel=rocisa.enum.SelectBit.WORD_0,
58 src0_sel=rocisa.enum.SelectBit.WORD_0,
59 src1_sel=rocisa.enum.SelectBit.WORD_1
60 )
61 assert str(sdwa_modifiers) == " dst_sel:WORD_0 src0_sel:WORD_0 src1_sel:WORD_1"
62
63 # Test VOP3PModifiers
64 vop3p_modifiers = rocisa.container.VOP3PModifiers([0, 0], [0, 1], [0, 0])
65 assert str(vop3p_modifiers) == " op_sel:[0,0] op_sel_hi:[0,1] byte_sel:[0,0]"
66
67 # Test EXEC
68 exec_modifiers = rocisa.container.EXEC(True)
69 assert str(exec_modifiers) == "exec"
70
71 # Test VCC
72 vcc_modifiers = rocisa.container.VCC(True)
73 assert str(vcc_modifiers) == "vcc"
74
75 # Test HWRegContainer
76 hwreg_container = rocisa.container.HWRegContainer("reg", [1, 1])
77 assert str(hwreg_container) == "hwreg(reg,1,1)"
78
79 # Test RegName
80 reg_name = rocisa.container.RegName("GSUIndex", [4, 4])
81 assert reg_name.getTotalOffsets() == 8
82 assert str(reg_name) == "GSUIndex+4+4"
83 reg_name.addOffset(4)
84 assert reg_name.getTotalOffsets() == 12
85 reg_name.setOffset(0, 8)
86 assert reg_name.getTotalOffsets() == 16
87
88 # Test RegisterContainer
89 reg_container = rocisa.container.RegisterContainer("v", reg_name, 4, 2)
90 assert reg_container.getRegNameWithType() == "vgprGSUIndex"
91 reg_container.setAbs(True)
92 assert str(reg_container) == "abs(v[vgprGSUIndex+8+4+4:vgprGSUIndex+8+4+4+1])"
93 reg_container.setAbs(False)
94 assert str(reg_container) == "v[vgprGSUIndex+8+4+4:vgprGSUIndex+8+4+4+1]"
95 assert reg_container != None
96 reg1, reg2 = reg_container.splitRegContainer()
97 assert str(reg1) == "v[vgprGSUIndex+8+4+4]"
98 assert str(reg2) == "v[vgprGSUIndex+8+4+4+1]"
99
100 # Test HolderContainer
101 holder_container = rocisa.container.HolderContainer("v", 4, 2)
102 assert holder_container.getCopiedRC() == holder_container
103 assert str(holder_container) == "v[4:5]"
104
105 # Test Holder
106 from rocisa.container import vgpr, sgpr, accvgpr, mgpr, Holder
107 holder = Holder("holder");
108 assert holder.idx == -1
109 assert str(holder.name) == "holder"
110 assert isinstance(holder, Holder)
111
112 # Test xgpr
113 testGpr = vgpr("TestGpr", 2)
114 assert str(testGpr) == "v[vgprTestGpr:vgprTestGpr+1]"
115 testGpr.setAbs(True)
116 assert str(testGpr) == "abs(v[vgprTestGpr:vgprTestGpr+1])"
117 testGpr = sgpr(2, 2)
118 assert str(testGpr) == "s[2:3]"
119 testGpr = accvgpr(Holder(4))
120 assert str(testGpr) == "acc4"
121 testGpr = mgpr(holder)
122 assert str(testGpr) == "m[mgprholder]"
123
124 def test_containers_copy():
125 def copy_test(name, obj):
126 print("Copy test:", name)
127 obj2 = deepcopy(obj)
128 print(" Copied using deepcopy:", str(obj2))
129 obj2 = pickle.loads(pickle.dumps(obj))
130 print(" Copied using pickle:", str(obj2))
131
132 ds = rocisa.container.DSModifiers(gds=True)
133 copy_test("DSModifiers", ds)
134 flat = rocisa.container.FLATModifiers(lds=True)
135 copy_test("FLATModifiers", flat)
136 mubuf = rocisa.container.MUBUFModifiers(True, 12, True, False, True, False, True)
137 copy_test("MUBUFModifiers", mubuf)
138 smem = rocisa.container.SMEMModifiers(True, False, 8)
139 copy_test("SMEMModifiers", smem)
140 sdwa = rocisa.container.SDWAModifiers(
141 dst_sel=rocisa.enum.SelectBit.WORD_0,
142 src0_sel=rocisa.enum.SelectBit.WORD_0,
143 src1_sel=rocisa.enum.SelectBit.WORD_1
144 )
145 copy_test("SDWAModifiers", sdwa)
146 vop3p = rocisa.container.VOP3PModifiers([0, 0], [0, 1], [0, 0])
147 copy_test("VOP3PModifiers", vop3p)
148 exec = rocisa.container.EXEC(True)
149 copy_test("EXEC", exec)
150 vcc = rocisa.container.VCC(True)
151 copy_test("VCC", vcc)
152 hwreg = rocisa.container.HWRegContainer("reg", [1, 1])
153 copy_test("HWRegContainer", hwreg)
154 reg_name = rocisa.container.RegName("GSUIndex", [4, 4])
155 copy_test("RegName", reg_name)
156 reg = rocisa.container.RegisterContainer("v", reg_name, 4, 2)
157 copy_test("RegisterContainer", reg)
158 holder = rocisa.container.HolderContainer("v", 4, 2)
159 copy_test("HolderContainer", holder)
160
161 test_containers()
162 test_containers_copy()