Debian Codegraph @

tensilelite/Tensile/Components/ComputeStoreVgprs.py

raw symbols SCIP
1 ################################################################################
2 #
3 # Copyright (C) 2022-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
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 # SOFTWARE.
22 #
23 ################################################################################
24
25 from rocisa.code import Module
26 from rocisa.container import ContinuousRegister, vgpr, sgpr
27 from rocisa.instruction import SMulI32, VAddLShiftLeftU32, VAddU32, VMovB32, VMulLOU32
28 from rocisa.functions import vectorStaticRemainder, vectorStaticDivideAndRemainder, vectorStaticDivide, vectorStaticMultiply
29
30 from ..Component import ComputeStoreVgprs
31 from ..Common import DataDirection, log2
32
33 class ComputeStoreVgprsVALU(ComputeStoreVgprs):
34 kernel = {"EnableMatrixInstruction": False,
35 "SourceSwap": False}
36
37 """
38 computeStoreVgprs
39 Compute workitem/TT offsets in VGPRS
40 and coord0/coord1
41 tid0Scale specifies the number of output elements in 0/coalesced dim
42 that should be written by each work-item in each batch element.
43 """
44 def __call__(self, writer, kernel, divisor, tid0Scale, tid1Scale):
45
46 module = Module("ComputeStoreVgprsVALU")
47
48 # tmpS0 = writer.getTmpSgpr(3).idx()
49 # tmpS1 = tmpS0+1
50 # wgMT1 = tmpS0+2
51
52 # if writer.prefetchAcrossPersistent:
53 # wg0="PrevWorkGroup0"
54 # wg1="PrevWorkGroup1"
55 # else:
56 wg0="WorkGroup0"
57 wg1="WorkGroup1"
58
59 # tid0, tid1: element offsets from the start of macroTile in 0 and 1 direction
60 # These will live for entire GlobalWrite loop - allocate before tmps
61 # to avoid fragmentation
62 tid0 = writer.vgprPool.checkOut(1, "tid0")
63 tid1 = writer.vgprPool.checkOut(1, "tid1")
64
65 packedC1 = kernel["PackedC1IndicesX"]
66
67 if kernel["BufferStore"]:
68 writer.vgprs.cinRowPtr = writer.vgprPool.checkOut(1, "cinRowPtr")
69 writer.vgprs.coutRowPtrD = writer.vgprPool.checkOut(1, "coutRowPtrD")
70
71 with writer.allocTmpSgpr(3) as tmpSgprInfo:
72 tmpS0 = tmpSgprInfo.idx
73 tmpS1 = tmpS0+1
74 wgMT1 = tmpS0+2
75 tmpVgpr = writer.vgprPool.checkOutAligned(2,2,"tmpVgpr")
76 tmpVgprRes = ContinuousRegister(tmpVgpr, 2)
77 # dot2: consecutive NumWaveSplitK threads compute the same element, divide it first before computing tile indices
78 if kernel["NumWaveSplitK"] > 1:
79 newSerial = writer.vgprPool.checkOut(1, "newSerial")
80 module.add(vectorStaticDivide(newSerial, "Serial", kernel["NumWaveSplitK"], tmpVgprRes, comment="Divided by NumWaveSplitK"))
81 module.add(vectorStaticDivideAndRemainder(tid1, tid0, newSerial, divisor, tmpVgprRes))
82 writer.vgprPool.checkIn(newSerial)
83 else:
84 module.add(vectorStaticDivideAndRemainder(tid1, tid0, "Serial", divisor, tmpVgprRes))
85
86 writer.vgprPool.checkIn(tmpVgpr)
87 tmpS1Res = ContinuousRegister(tmpS1, 1)
88 module.add(vectorStaticMultiply(vgpr(tid0), vgpr(tid0), tid0Scale, tmpS1Res))
89 if tid1Scale != 1:
90 module.add(vectorStaticMultiply(vgpr(tid1), vgpr(tid1), tid1Scale, tmpS1Res))
91
92 if kernel["BufferStore"]:
93 # compute rowStart- this is just tid1 scaled by appropriate stride.
94 # rowPtr is offset from the beginning of the tile/SRD not the tensor base
95 # when incremented, it moves in units of (col) Stride to get to a new row
96 # it is used for address computation, not element range detection.
97 # rowPtr is in the element space and must be scaled by bpe if bytes are required.
98 # Do this before code below which overwries the tid1:
99 # TODO-packed
100 # Eventually need to modify if supporting packed coord1, to start just assert if that case is detected
101 #--
102 strideC1 = "StrideC%s" % (writer.states.indexChars[packedC1[0]])
103 module.add(VMulLOU32(dst=vgpr(writer.vgprs.cinRowPtr), src0=vgpr(tid1), src1=sgpr(strideC1), comment="rowStart vgpr"))
104 strideD1 = "StrideD%s" % (writer.states.indexChars[packedC1[0]])
105 module.add(VMulLOU32(dst=vgpr(writer.vgprs.coutRowPtrD), src0=vgpr(tid1), src1=sgpr(strideD1), comment="rowStart vgpr"))
106 module.addSpaceLine()
107
108 # Compute coord0 and coord1
109 # These are element offsets from the beginning of the tensor.
110 # These are 'flattened' meaning they span packed tensor dims.
111 # They need to be preserved so can use in comparisons against
112 # product-of-packed sizes to determine OOB cases. (for Edge tiles only)
113 module.add(SMulI32(dst=sgpr(tmpS0), src0=hex(kernel["MacroTile0"]), src1=sgpr(wg0), comment="%s = wg0*MT0"%sgpr(tmpS0)))
114
115 # coord = tid*VW + workgroup offset
116 module.add(VAddU32(dst=vgpr(tid0), src0=sgpr(tmpS0), src1=vgpr(tid0), comment="coord0 = tid0*VW + wg0*MT0"))
117 module.add(SMulI32(dst=sgpr(wgMT1), src0=hex(kernel["MacroTile1"]), src1=sgpr(wg1), comment="<- wg1*MT1"))
118 module.add(VAddU32(dst=vgpr(tid1), src0=sgpr(wgMT1), src1=vgpr(tid1), comment="coord1 = tid1*VW + wg1*MT1"))
119
120 if len(packedC1) > 1:
121 module.add(writer.extractPackedCoord1ToRowStart(kernel, packedC1, tid1, 'D'))
122
123 writer.vgprs.coord0 = tid0
124 writer.vgprs.coord1 = tid1
125
126 return module
127
128 class ComputeStoreVgprsMFMA(ComputeStoreVgprs):
129 kernel = {"EnableMatrixInstruction": True,
130 "SourceSwap": False}
131
132 """
133 computeStoreVgprs
134 Compute workitem/TT offsets in VGPRS
135 and coord0/coord1
136 tid0Scale specifies the number of output elements in 0/coalesced dim
137 that should be written by each work-item in each batch element.
138 """
139 def __call__(self, writer, kernel):
140
141 # writer.coord0
142 # writer.coord1
143 # writer.vgprs.cinRowPtr : C buffer coulmn offset
144 # writer.vgprs.coutRowPtrD : D buffer coulmn offset
145
146 # alloc resources
147 tid0 = writer.vgprPool.checkOut(1, "coord0")
148 tid1 = writer.vgprPool.checkOut(1, "coord1")
149 if kernel["BufferStore"]:
150 writer.vgprs.cinRowPtr = writer.vgprPool.checkOut(1, "cinRowPtr")
151 writer.vgprs.coutRowPtrD = writer.vgprPool.checkOut(1, "coutRowPtrD")
152 if kernel["ProblemType"]["UseE"]:
153 writer.vgprs.coutRowPtrE = writer.vgprPool.checkOut(1, "coutRowPtrE")
154 if writer.states.useBias == DataDirection.WRITE and (not kernel["WorkGroupReduction"]) and kernel["ProblemType"]["BiasSrc"] == "D":
155 writer.vgprs.coutRowPtrBias = writer.vgprPool.checkOut(1, "coutRowPtrBias")
156 if kernel["LocalSplitU"] > 1:
157 writer.vgprs.coord0InMT = writer.vgprPool.checkOut(1, "coord0InMT")
158 writer.vgprs.coord1InMT = writer.vgprPool.checkOut(1, "coord1InMT")
159 else:
160 writer.vgprs.coord0InMT = tid0
161 writer.vgprs.coord1InMT = tid1
162 lsuTid0 = writer.vgprs.coord0InMT
163 lsuTid1 = writer.vgprs.coord1InMT
164
165 wave_id = writer.vgprPool.checkOut(1, "tmpWaveID")
166
167 tmpVgpr0 = writer.vgprPool.checkOut(1,"tmpVgpr0")
168 tmpVgpr1 = writer.vgprPool.checkOutAligned(2,2,"tmpVgpr1")
169 tmpVgpr0Res = ContinuousRegister(tmpVgpr0, 1)
170 tmpVgpr1Res = ContinuousRegister(tmpVgpr1, 2)
171 dummy = writer.vgprPool.checkOut(1,"dummy")
172
173 with writer.allocTmpSgpr(1) as tmpSgprInfo:
174 tmpSgpr = tmpSgprInfo.idx
175
176 # constant
177 MIBShape0 = kernel["MatrixInstM"] * kernel["MatrixInstBM"]
178 MIBShape1 = kernel["MatrixInstN"] * kernel["MatrixInstBN"]
179
180 matrixInstM = kernel["MatrixInstM"] * kernel["MatrixInstBM"] if (kernel["MatrixInstM"] == 4) else kernel["MatrixInstM"]
181 matrixInstN = kernel["MatrixInstN"] * kernel["MatrixInstBN"] if (kernel["MatrixInstN"] == 4) else kernel["MatrixInstN"]
182
183 module = Module("ComputeStoreVgprsMFMA")
184
185 # coord 1 : wave part
186 module.add(vectorStaticDivide(wave_id, "Serial", writer.states.kernel["WavefrontSize"], tmpVgpr1Res))
187 module.add(vectorStaticDivide(tmpVgpr0, wave_id, kernel["MIWaveGroup"][0], tmpVgpr1Res))
188 if kernel["LocalSplitU"] > 1:
189 module.add(vectorStaticRemainder(dummy, tmpVgpr0, tmpVgpr0, kernel["MIWaveGroup"][1], tmpVgpr1Res, tmpSgprInfo))
190 module.add(VMulLOU32(dst=vgpr(tid1), src0=hex(MIBShape1), src1=vgpr(tmpVgpr0), comment="wave coordination offset 1"))
191
192 # coord 1 : thread part
193 module.add(vectorStaticRemainder(dummy, tmpVgpr0, "Serial", matrixInstN, tmpVgpr1Res, tmpSgprInfo))
194 module.add(VAddLShiftLeftU32(dst=vgpr(lsuTid1), src0=vgpr(tmpVgpr0), src1=vgpr(tid1), shiftHex=log2(kernel["VectorWidthB"]), comment="coordination 1 = vwB *(wave_id1 + tid1)"))
195
196 # coord 1 : offset part
197 packedC1 = kernel["PackedC1IndicesX"]
198 strideC1 = "StrideC%s" % (writer.states.indexChars[packedC1[0]])
199 strideD1 = "StrideD%s" % (writer.states.indexChars[packedC1[0]])
200 module.add(VMulLOU32(dst=vgpr(writer.vgprs.cinRowPtr), src0=vgpr(lsuTid1), src1=sgpr(strideC1), comment=" offset 1"))
201 module.add(VMulLOU32(dst=vgpr(writer.vgprs.coutRowPtrD), src0=vgpr(lsuTid1), src1=sgpr(strideD1), comment=" offset 1"))
202 if kernel["ProblemType"]["UseE"] and (kernel["GlobalSplitU"] == 1 or kernel["GlobalSplitU"] == -1):
203 module.add(VMovB32(dst=vgpr(writer.vgprs.coutRowPtrE), src=vgpr(lsuTid1), comment=" save offset 1 for E"))
204 if writer.vgprs.coutRowPtrBias != -1:
205 index = packedC1[0] - 1
206 strideW1 = "Size%s" % "I" if index == 0 else ("J" if index == 1 else (writer.states.indexChars[index]))
207 module.add(VMulLOU32(dst=vgpr(writer.vgprs.coutRowPtrBias), src0=vgpr(lsuTid1), src1=sgpr(strideW1), comment=" offset 1"))
208
209 # coord 0 : wave part
210 module.add(vectorStaticRemainder(dummy, tmpVgpr0, wave_id, kernel["MIWaveGroup"][0], tmpVgpr1Res, tmpSgprInfo))
211 module.add(VMulLOU32(dst=vgpr(tmpVgpr0), src0=hex(MIBShape0), src1=vgpr(tmpVgpr0), comment="wave coordination offset 0"))
212
213 # coord 0 : thread part
214 module.add(vectorStaticRemainder(dummy, tid0, "Serial", writer.states.kernel["WavefrontSize"], tmpVgpr1Res, tmpSgprInfo))
215 module.add(vectorStaticDivide(tid0, tid0, matrixInstN, tmpVgpr1Res))
216 module.add(vectorStaticMultiply(vgpr(tid0), vgpr(tid0), kernel["MIOutputVectorWidth"], tmpSgprInfo, "thread0 * continuous_output"))
217 module.add(VAddLShiftLeftU32(dst=vgpr(lsuTid0), src0=vgpr(tmpVgpr0), src1=vgpr(tid0), shiftHex=log2(kernel["VectorWidthA"]), comment="coordination 0 = vwA *(wave_id0 + tid0)"))
218
219 wg0="WorkGroup0"
220 wg1="WorkGroup1"
221
222 # macro tile 0 part
223 module.add(SMulI32(dst=sgpr(tmpSgpr), src0=kernel["MacroTile0"], src1=sgpr(wg0), comment="wgp0 * MT0"))
224 module.add(VAddU32(dst=vgpr(tid0), src0=sgpr(tmpSgpr), src1=vgpr(lsuTid0), comment="coord 0 = (tid0/MI_m)*4 + waveG0*MIB_m + MT0*SG0"))
225
226 # macro tile 1 part
227 module.add(SMulI32(dst=sgpr(tmpSgpr), src0=kernel["MacroTile1"], src1=sgpr(wg1), comment="wgp1 * MT1"))
228 module.add(VAddU32(dst=vgpr(tid1), src0=sgpr(tmpSgpr), src1=vgpr(lsuTid1), comment="coord 1 = (tid0%MI_m) + waveG1*MIB_n + MT1*SG1"))
229
230 # release resource
231 writer.vgprPool.checkIn(dummy)
232 writer.vgprPool.checkIn(tmpVgpr1)
233 writer.vgprPool.checkIn(tmpVgpr0)
234 writer.vgprPool.checkIn(wave_id)
235
236 # StoreRemap: calculate
237 # 1. local read address
238 # 2. local write address
239 # 3. global write coord0 and coord1
240 if kernel["StoreRemapVectorWidth"]:
241 module.add(writer.storeRemapComputeStoreVgprs(kernel))
242
243 writer.vgprs.coord0 = tid0
244 writer.vgprs.coord1 = tid1
245
246 return module
247
248 class ComputeStoreVgprsMFMASwap(ComputeStoreVgprs):
249 kernel = {"EnableMatrixInstruction": True,
250 "SourceSwap": True}
251
252 """
253 computeStoreVgprs
254 Compute workitem/TT offsets in VGPRS
255 and coord0/coord1
256 tid0Scale specifies the number of output elements in 0/coalesced dim
257 that should be written by each work-item in each batch element.
258 """
259 def __call__(self, writer, kernel):
260
261 # writer.coord0
262 # writer.coord1
263 # writer.vgprs.cinRowPtr : C buffer coulmn offset
264 # writer.vgprs.coutRowPtrD : D buffer coulmn offset
265
266 # alloc resources
267 tid0 = writer.vgprPool.checkOut(1, "coord0")
268 tid1 = writer.vgprPool.checkOut(1, "coord1")
269 if kernel["BufferStore"]:
270 writer.vgprs.cinRowPtr = writer.vgprPool.checkOut(1, "cinRowPtr")
271 writer.vgprs.coutRowPtrD = writer.vgprPool.checkOut(1, "coutRowPtrD")
272 if kernel["ProblemType"]["UseE"]:
273 writer.vgprs.coutRowPtrE = writer.vgprPool.checkOut(1, "coutRowPtrE")
274 if writer.states.useBias == DataDirection.WRITE and (not kernel["WorkGroupReduction"]) and kernel["ProblemType"]["BiasSrc"] == "D":
275 writer.vgprs.coutRowPtrBias = writer.vgprPool.checkOut(1, "coutRowPtrBias")
276 if kernel["LocalSplitU"] > 1:
277 writer.vgprs.coord0InMT = writer.vgprPool.checkOut(1, "coord0InMT")
278 writer.vgprs.coord1InMT = writer.vgprPool.checkOut(1, "coord1InMT")
279 else:
280 writer.vgprs.coord0InMT = tid0
281 writer.vgprs.coord1InMT = tid1
282 lsuTid0 = writer.vgprs.coord0InMT
283 lsuTid1 = writer.vgprs.coord1InMT
284
285 wave_id = writer.vgprPool.checkOut(1, "tmpWaveID")
286
287 tmpVgpr0 = writer.vgprPool.checkOut(1,"tmpVgpr0")
288 tmpVgpr1 = writer.vgprPool.checkOutAligned(2,2,"tmpVgpr1")
289 #lsu_id = tmpVgpr1
290 tmpVgpr1Res = ContinuousRegister(tmpVgpr1, 2)
291 dummy = writer.vgprPool.checkOut(1,"dummy")
292
293 with writer.allocTmpSgpr(1) as tmpSgprInfo:
294 tmpSgpr = tmpSgprInfo.idx
295
296 # constant
297 MIBShape0 = kernel["MatrixInstM"] * kernel["MatrixInstBM"]
298 MIBShape1 = kernel["MatrixInstN"] * kernel["MatrixInstBN"]
299
300 matrixInstM = kernel["MatrixInstM"] * kernel["MatrixInstBM"] if (kernel["MatrixInstM"] == 4) else kernel["MatrixInstM"]
301 matrixInstN = kernel["MatrixInstN"] * kernel["MatrixInstBN"] if (kernel["MatrixInstN"] == 4) else kernel["MatrixInstN"]
302
303 module = Module("ComputeStoreVgprsMFMASwap")
304
305
306 # coord 1 : wave part
307 module.add(vectorStaticDivide(wave_id, "Serial", writer.states.kernel["WavefrontSize"], tmpVgpr1Res))
308 module.add(vectorStaticDivide(tmpVgpr0, wave_id, kernel["MIWaveGroup"][0], tmpVgpr1Res))
309 if kernel["LocalSplitU"] > 1:
310 module.add(vectorStaticRemainder(dummy, tmpVgpr0, tmpVgpr0, kernel["MIWaveGroup"][1], tmpVgpr1Res, tmpSgprInfo))
311 module.add(VMulLOU32(dst=vgpr(tmpVgpr0), src0=hex(MIBShape1), src1=vgpr(tmpVgpr0), comment="wave coordination offset 1"))
312
313 # coord 1 : thread part
314 module.add(vectorStaticRemainder(dummy, tid1, "Serial", writer.states.kernel["WavefrontSize"], tmpVgpr1Res, tmpSgprInfo))
315 module.add(vectorStaticDivide(tid1, tid1, matrixInstM, tmpVgpr1Res))
316 module.add(vectorStaticMultiply(vgpr(tid1), vgpr(tid1), kernel["MIOutputVectorWidth"], tmpSgprInfo, "thread0 * continuous_output"))
317 module.add(VAddLShiftLeftU32(dst=vgpr(lsuTid1), src0=vgpr(tmpVgpr0), src1=vgpr(tid1), shiftHex=log2(kernel["VectorWidthB"]), comment="coordination 1 = vwB *(wave_id1 + tid1)"))
318
319 # coord 1 : offset part
320 packedC1 = kernel["PackedC1IndicesX"]
321 strideC1 = "StrideC%s" % (writer.states.indexChars[packedC1[0]])
322 strideD1 = "StrideD%s" % (writer.states.indexChars[packedC1[0]])
323 module.add(VMulLOU32(dst=vgpr(writer.vgprs.cinRowPtr), src0=vgpr(lsuTid1), src1=sgpr(strideC1), comment=" offset 1"))
324 module.add(VMulLOU32(dst=vgpr(writer.vgprs.coutRowPtrD), src0=vgpr(lsuTid1), src1=sgpr(strideD1), comment=" offset 1"))
325 if kernel["ProblemType"]["UseE"] and (kernel["GlobalSplitU"] == 1 or kernel["GlobalSplitU"] == -1):
326 module.add(VMovB32(dst=vgpr(writer.vgprs.coutRowPtrE), src=vgpr(lsuTid1), comment=" save offset 1 for E"))
327 if writer.vgprs.coutRowPtrBias != -1:
328 index = packedC1[0] - 1
329 strideW1 = "Size%s" % "I" if index == 0 else ("J" if index == 1 else (writer.states.indexChars[index]))
330 module.add(VMulLOU32(dst=vgpr(writer.vgprs.coutRowPtrBias), src0=vgpr(lsuTid1), src1=sgpr(strideW1), comment=" offset 1"))
331
332 # coord 0 : wave part
333 module.add(vectorStaticRemainder(dummy, tid0, wave_id, kernel["MIWaveGroup"][0], tmpVgpr1Res, tmpSgprInfo))
334 module.add(VMulLOU32(dst=vgpr(tid0), src0=hex(MIBShape0), src1=vgpr(tid0), comment="wave coordination offset 0"))
335
336 # coord 0 : thread part
337 module.add(vectorStaticRemainder(dummy, tmpVgpr0, "Serial", matrixInstM, tmpVgpr1Res, tmpSgprInfo))
338 module.add(VAddLShiftLeftU32(dst=vgpr(lsuTid0), src0=vgpr(tmpVgpr0), src1=vgpr(tid0), shiftHex=log2(kernel["VectorWidthA"]), comment="coordination 0 = vwA * (wave_id0 + tid0)"))
339
340 wg0="WorkGroup0"
341 wg1="WorkGroup1"
342
343 # macro tile 0 part
344 module.add(SMulI32(dst=sgpr(tmpSgpr), src0=kernel["MacroTile0"], src1=sgpr(wg0), comment="wgp0 * MT0"))
345 module.add(VAddU32(dst=vgpr(tid0), src0=sgpr(tmpSgpr), src1=vgpr(lsuTid0), comment="coord 0 = (tid0/MI_m)*4 + waveG0*MIB_m + MT0*SG0"))
346
347 # macro tile 1 part
348 module.add(SMulI32(dst=sgpr(tmpSgpr), src0=kernel["MacroTile1"], src1=sgpr(wg1), comment="wgp1 * MT1"))
349 module.add(VAddU32(dst=vgpr(tid1), src0=sgpr(tmpSgpr), src1=vgpr(lsuTid1), comment="coord 1 = (tid0%MI_m) + waveG1*MIB_n + MT1*SG1"))
350
351 # release resource
352 writer.vgprPool.checkIn(dummy)
353 writer.vgprPool.checkIn(tmpVgpr1)
354 writer.vgprPool.checkIn(tmpVgpr0)
355 writer.vgprPool.checkIn(wave_id)
356
357 # StoreRemap: calculate
358 # 1. local read address
359 # 2. local write address
360 # 3. global write coord0 and coord1
361 if kernel["StoreRemapVectorWidth"]:
362 module.add(writer.storeRemapComputeStoreVgprs(kernel))
363
364 writer.vgprs.coord0 = tid0
365 writer.vgprs.coord1 = tid1
366
367 return module