Debian Codegraph @

tensilelite/Tensile/Components/LSU.py

raw symbols SCIP
1 ################################################################################
2 #
3 # Copyright (C) 2024-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.code import Module, Label, RegSet
24 from rocisa.container import DSModifiers, ContinuousRegister
25 from rocisa.instruction import DSLoadB128, DSLoadB32, DSLoadB64, DSStoreB128, \
26 DSStoreB32, DSStoreB64, SAndB32, SCBranchSCC0, SCmpEQU32, SMovB32, SWaitCnt, \
27 VAddF32, VAddI32, VAddU32, VAndB32, VLShiftLeftAddU32, VMovB32, VMulLOU32
28 from rocisa.functions import vectorStaticDivide
29 from copy import deepcopy
30 from ..Common import log2, ceilDivide
31 from ..Component import Component
32 from ..KernelWriterModules import *
33 from ..AsmStoreState import StoreState, VectorDataTypes
34 #import abc
35
36 class LSU(Component):
37 """
38 LSU block.
39 """
40
41 class LSUOff(LSU):
42 kernel = {"LocalSplitU": 1}
43
44 class LSUOn(LSU):
45
46 @classmethod
47 def matches(cls, writer, debug=False):
48 return writer.states.kernel["LocalSplitU"] > 1
49
50 def __call__(self):
51 assert(0)
52
53 def splitOutputData(self, writer, kernel):
54 self.LSUelemCoord0 = []
55 self.LSUelemCoord1 = []
56 self.LSUelements = []
57 self.LSUfullVw = []
58 (vwdummy, eledummy, self.LSUfullVw, self.LSUelements) = writer.notLocalFullTileElements(kernel, False)
59 storevw = self.LSUfullVw
60 atomic = False # atomic is for GSU > 1
61 beta = True
62 vectorDataTypes = VectorDataTypes()
63 ss = StoreState(writer, kernel, storevw, False, beta, atomic, self.LSUelements, vectorDataTypes, dim=0)
64 self.LSUelemCoord0, self.LSUelemCoord1 = ss.getStoreElementsInfoForBatch(kernel, self.LSUelements)
65
66 # search for valid lsu wave offset
67 maxtt1 = 0
68 maxtt0 = 0
69 maxvc1 = 0
70 maxvc0 = 0
71 validOffset = -1
72 validOffset0 = -1
73 validOffset1 = -1
74 self.LSUelementsArchIdx = [[] for i in range(4)]
75 self.LSUelementsPerLSUWave = []
76 self.LSUelemCoord0PerLSUWave = []
77 self.LSUelemCoord1PerLSUWave = []
78
79 # Check valid LSU/VW combination
80 if len(self.LSUelements) >= kernel["LocalSplitU"]:
81 if kernel["LocalSplitU"] == 4:
82 idxGrp = 1
83 for idxGrp in range(1, len(self.LSUelements)//4 + 1):
84 for i in range(idxGrp):
85 i0 = i
86 i1 = i + 1 * idxGrp
87 i2 = i + 2 * idxGrp
88 i3 = i + 3 * idxGrp
89 offset0 = self.LSUelemCoord0[i0] + self.LSUelemCoord1[i0] * kernel["MacroTile0"]
90 offset1 = self.LSUelemCoord0[i1] + self.LSUelemCoord1[i1] * kernel["MacroTile0"]
91 offset2 = self.LSUelemCoord0[i2] + self.LSUelemCoord1[i2] * kernel["MacroTile0"]
92 offset3 = self.LSUelemCoord0[i3] + self.LSUelemCoord1[i3] * kernel["MacroTile0"]
93 if (offset3 - offset2 == offset2 - offset1) and (offset2 - offset1 == offset1 - offset0):
94 validOffset0 = self.LSUelemCoord0[i1] - self.LSUelemCoord0[i0]
95 validOffset1 = self.LSUelemCoord1[i1] - self.LSUelemCoord1[i0]
96 if self.LSUelemCoord0[i2] - self.LSUelemCoord0[i1] == validOffset0 \
97 and self.LSUelemCoord0[i3] - self.LSUelemCoord0[i2] == validOffset0 \
98 and self.LSUelemCoord1[i2] - self.LSUelemCoord1[i1] == validOffset1 \
99 and self.LSUelemCoord1[i3] - self.LSUelemCoord1[i2] == validOffset1:
100 validOffset = offset1 - offset0
101 break
102 if validOffset != -1:
103 break
104 for idx in range(0, len(self.LSUelements), 4*idxGrp):
105 for idx2 in range(idxGrp):
106 self.LSUelementsArchIdx[0].append(self.LSUfullVw*(idx + idx2))
107 self.LSUelementsArchIdx[1].append(self.LSUfullVw*(idx + 1*idxGrp + idx2))
108 self.LSUelementsArchIdx[2].append(self.LSUfullVw*(idx + 2*idxGrp + idx2))
109 self.LSUelementsArchIdx[3].append(self.LSUfullVw*(idx + 3*idxGrp + idx2))
110 self.LSUelementsPerLSUWave.append(self.LSUelements[idx + idx2])
111 self.LSUelemCoord0PerLSUWave.append(self.LSUelemCoord0[idx + idx2])
112 self.LSUelemCoord1PerLSUWave.append(self.LSUelemCoord1[idx + idx2])
113 elif kernel["LocalSplitU"] == 2:
114 i = 0
115 offset0 = self.LSUelemCoord0[i] + self.LSUelemCoord1[i] * kernel["MacroTile0"]
116 offset1 = self.LSUelemCoord0[i + 1] + self.LSUelemCoord1[i + 1] * kernel["MacroTile0"]
117 validOffset = offset1 - offset0
118 validOffset0 = self.LSUelemCoord0[i + 1] - self.LSUelemCoord0[i]
119 validOffset1 = self.LSUelemCoord1[i + 1] - self.LSUelemCoord1[i]
120 for idx in range(0, len(self.LSUelements), 2):
121 self.LSUelementsArchIdx[0].append(self.LSUfullVw*idx)
122 self.LSUelementsArchIdx[1].append(self.LSUfullVw*(idx+1))
123 self.LSUelementsPerLSUWave.append(self.LSUelements[idx])
124 self.LSUelemCoord0PerLSUWave.append(self.LSUelemCoord0[idx])
125 self.LSUelemCoord1PerLSUWave.append(self.LSUelemCoord1[idx])
126 else:
127 assert 0, "No valid LSU offset found."
128
129 if validOffset == -1:
130 assert 0, "No valid LSU offset found."
131 self.LSUValidOffset0 = validOffset0
132 self.LSUValidOffset1 = validOffset1
133 return validOffset
134
135 ##############################################################################
136 # LocalSplitU: Local Write, Read, and Reduction
137 ##############################################################################
138 def writeReadReduction(self, writer, kernel):
139 module = Module("localSplitULocalWriteAndRead")
140 module.addComment2("LocalSplitU Reduction")
141 module.add(writer._syncThreads(kernel))
142 module.add(Label("localSplitULocalWriteAndRead", ""))
143
144 acc2arch, arch2acc = accToArchMapper(kernel)
145
146 # prepare the data that is to be Reduction in this wave
147 # the output LSUelementsArchIdx has all arch-indices.
148 validOffset = self.splitOutputData(writer, kernel)
149
150 numAccIdx = len(self.LSUelementsArchIdx[0])
151 numSetAccIdx = ceilDivide(numAccIdx, kernel["LocalSplitUReuseLDS"])
152 maxLDSConstOffset = writer.states.regCaps["maxLDSConstOffset"]
153 # computeStoreVgprs
154 if kernel["EnableMatrixInstruction"]:
155 module.add(writer.computeStoreVgprs(kernel))
156 else:
157 # new method. output self.vgprs.coord0InMT/coord1InMT
158 module.add(writer.computeStoreVgprs(kernel, \
159 divisor = kernel["MacroTile0"] // kernel["GlobalWriteVectorWidth"], \
160 tid0Scale = kernel["GlobalWriteVectorWidth"], \
161 tid1Scale = 1))
162
163 # Checkout local read resource
164 bpr = 4 #bytes per register
165 bytesPerElem = kernel["ProblemType"]["ComputeDataType"].numBytes()
166 bytesPerVector = self.LSUfullVw * bytesPerElem
167 numWaves = kernel["MIWaveGroup"][0] * kernel["MIWaveGroup"][1]
168 regsPerStep = int((bytesPerVector+3)//4)
169 elementStep = bytesPerVector // bytesPerElem
170 numTotalAccVgprLdsReduction = len(self.LSUelements)*regsPerStep*(self.LSUfullVw//elementStep)
171 assert (numTotalAccVgprLdsReduction%kernel["LocalSplitU"]) == 0, "AccVgprLdsRedcution % LSU != 0"
172 numTotalAccVgprLdsReduction = numTotalAccVgprLdsReduction // kernel["LocalSplitU"]
173 self.accVgprLdsReduction = writer.vgprPool.checkOutAligned(numTotalAccVgprLdsReduction, 4, "LsuReduction")
174 module.add(RegSet("v", "vgprLsuReduction", self.accVgprLdsReduction))
175 module.addComment0("Size of vgprLsuReduction is %u"%(numTotalAccVgprLdsReduction))
176 writer.states.c.startVgprValu = self.accVgprLdsReduction
177
178 # Local Read VGPR idx
179 localReadVgprIdx = 0
180
181 lsu_id = writer.vgprPool.checkOut(1,"lsu_id")
182 wave_id = writer.vgprPool.checkOut(1,"wave_id")
183 tmpVgpr = writer.vgprPool.checkOutAligned(2, 2, "tmpVgpr")
184 tmpVgprRes = ContinuousRegister(tmpVgpr, 2)
185
186 module.add(vectorStaticDivide(wave_id, "Serial", \
187 kernel["WavefrontSize"], tmpVgprRes))
188 module.add(vectorStaticDivide(lsu_id, wave_id, numWaves, tmpVgprRes, \
189 comment="Get LSU wave ID"))
190 module.add(VAndB32(vgpr(wave_id), hex(numWaves - 1), vgpr(wave_id), \
191 comment="Get wave ID"))
192
193 # accVgprs read in GSUAMBSK
194 codeAccVgprRead = []
195 if kernel["GlobalSplitUAlgorithm"] == "MultipleBufferSingleKernel":
196 codeAccVgprRead = deepcopy(writer.codes.accVgprRead) if writer.states.serializedStore else None
197
198 for reUseIdx in range(kernel["LocalSplitUReuseLDS"]):
199 module.addComment1("LocalSplitU: local write %d/%d"%(reUseIdx+1,kernel["LocalSplitUReuseLDS"]))
200 module.add(Label("localSplitULocalWriteAndRead_%d"%(reUseIdx+1), ""))
201
202 startLSUaccIdxSet = reUseIdx * numSetAccIdx
203 endLSUaccIdxSet = min(numAccIdx, startLSUaccIdxSet + numSetAccIdx)
204
205 #scan the needed accVGPRIdx
206 neededAccVGPRIdx = [[] for i in range(kernel["LocalSplitU"])]
207 numAccVgpr = 0
208 for lsu in range(kernel["LocalSplitU"]):
209 for i in range(startLSUaccIdxSet, endLSUaccIdxSet):
210 for j in range(self.LSUfullVw):
211 accIdx = arch2acc[self.LSUelementsArchIdx[lsu][i] + j]
212 neededAccVGPRIdx[lsu].append(accIdx)
213 numAccVgpr += 1
214
215 # lsuProcessOffset is used when local read
216 numVgprPerLSU = len(neededAccVGPRIdx[0])
217 lsuProcessOffset = numVgprPerLSU * kernel["WavefrontSize"] * 4
218
219 # break here instead of assert to release resource and do a post-rejection
220 # caused by range(startLSUaccIdxSet, endLSUaccIdxSet) = range(1,1)
221 if numAccVgpr <= 0:
222 writer.states.invalidLSUCode = True
223 break
224
225 assert numAccVgpr > 0,"startLSUaccIdxSet=%u,endLSUaccIdxSet=%u,numAccIdx=%u"%(startLSUaccIdxSet,endLSUaccIdxSet,numAccIdx)
226 accVgprRes = writer.vgprPool.checkOutAligned(numAccVgpr, 4, "accLSUVgprRes")
227
228 destIdx = 0
229 for lsu in range(kernel["LocalSplitU"]):
230 for i in range(numVgprPerLSU):
231 srcIdx = neededAccVGPRIdx[lsu][i]
232 readInst = writer.accVgprReadWriteFunction(kernel, srcIdx, True)
233 srcVgpr = writer.accVgprReadWriteIndex(kernel, srcIdx)
234 module.add(readInst(dst=vgpr(accVgprRes+destIdx),
235 src=srcVgpr,
236 comment="copy acc[%u] to vreg[%u], LSU%u will process" % (srcIdx,destIdx,lsu)))
237 destIdx += 1
238
239 dataPerWave = numAccVgpr * kernel["WavefrontSize"] * 4
240 ldsStride = dataPerWave * numWaves
241 # Prepare Write/Read instruction info
242 if bytesPerVector % 16 == 0:
243 DSStoreBX = DSStoreB128
244 DSLoadBX = DSLoadB128
245 numInstPerVW = bytesPerVector // 16
246 regsPerStore = 4
247 elif bytesPerVector % 8 == 0:
248 DSStoreBX = DSStoreB64
249 DSLoadBX = DSLoadB64
250 numInstPerVW = bytesPerVector // 8
251 regsPerStore = 2
252 else:
253 DSStoreBX = DSStoreB32
254 DSLoadBX = DSLoadB32
255 numInstPerVW = bytesPerVector // 4
256 regsPerStore = 1
257
258 maxOffset = (kernel["LocalSplitU"] -1) * ldsStride + ((numVgprPerLSU // self.LSUfullVw -1) * numInstPerVW + (numInstPerVW -1)) * regsPerStore * (bpr * kernel["WavefrontSize"])
259 numAddr = maxOffset // maxLDSConstOffset + 1
260 addr = writer.vgprPool.checkOut(numAddr,"addr")
261 with writer.allocTmpSgpr(1) as tmpSgprInfo:
262 tmpSgpr = tmpSgprInfo.idx
263 module.add(SMovB32(dst=sgpr(tmpSgpr), src=hex(dataPerWave), \
264 comment="dataPerWave (%d)"%dataPerWave))
265 module.add(VAndB32(vgpr(addr), hex(kernel["WavefrontSize"]-1), vgpr("Serial"), \
266 comment="initial addr"))
267 module.add(VMulLOU32(dst=vgpr(tmpVgpr), src0=sgpr(tmpSgpr), src1=vgpr(wave_id), \
268 comment="tmp = waveId * dataPerWave"))
269 module.add(VLShiftLeftAddU32(dst=vgpr(addr), shiftHex=log2(regsPerStore * bpr), src0=vgpr(addr), src1=vgpr(tmpVgpr), \
270 comment="addr = initial addr + tmp"))
271 module.add(SMovB32(dst=sgpr(tmpSgpr), src=hex(ldsStride), \
272 comment="ldsStride = waveNum * dataPerWave (%d)"%ldsStride))
273 module.add(VMulLOU32(dst=vgpr(tmpVgpr), src0=sgpr(tmpSgpr), src1=vgpr(lsu_id), \
274 comment="tmp = (waveNum * dataPerWave) * lsu_id"))
275 module.add(VAddU32(vgpr(addr), vgpr(tmpVgpr), vgpr(addr), \
276 comment="addr += tmp"))
277
278 module.add(SWaitCnt(dscnt=0, comment="wait for all writes"))
279 module.add(writer._syncThreads(kernel, "pre-lsu local write"))
280
281 module.add(Label("localSplitULocalWrite_%d"%(reUseIdx+1), ""))
282
283 # Do Local Write
284 for i in range(0, numAccVgpr // self.LSUfullVw):
285 for v in range(numInstPerVW):
286 regIdx = (i * numInstPerVW + v) * regsPerStore
287 module.add(DSStoreBX(dstAddr=vgpr(addr), src=vgpr(accVgprRes+regIdx, regsPerStore), \
288 ds=DSModifiers(offset=(regIdx * (bpr * kernel["WavefrontSize"]))), \
289 comment="arch[%d]"%(i * numInstPerVW + v)))
290
291 # Release local write resource
292 writer.vgprPool.checkIn(accVgprRes)
293
294 module.addComment1("LocalSplitU: local read %d/%d"%(reUseIdx+1,kernel["LocalSplitUReuseLDS"]))
295
296 # Calculate offset for wave id and lsu id
297 with writer.allocTmpSgpr(1) as tmpSgprInfo:
298 tmpSgpr = tmpSgprInfo.idx
299 module.add(VAndB32(vgpr(addr), hex(kernel["WavefrontSize"]-1), vgpr("Serial"), \
300 comment="initial addr"))
301 module.add(SMovB32(dst=sgpr(tmpSgpr), src=hex(dataPerWave), \
302 comment="wave offset (%d)"%dataPerWave))
303 module.add(VMulLOU32(dst=vgpr(tmpVgpr), src0=sgpr(tmpSgpr), src1=vgpr(wave_id), \
304 comment="wave offset = wave_id * wave offset"))
305 module.add(VLShiftLeftAddU32(dst=vgpr(addr), shiftHex=log2(regsPerStore * bpr), src0=vgpr(addr), src1=vgpr(tmpVgpr), \
306 comment="addr = initial addr + wave offset"))
307 module.add(SMovB32(dst=sgpr(tmpSgpr), \
308 src=hex(lsuProcessOffset), comment="LSU Process Offset %d"%(lsuProcessOffset)))
309 module.add(VMulLOU32(dst=vgpr(tmpVgpr), src0=sgpr(tmpSgpr), src1=vgpr(lsu_id), \
310 comment="lsu offset = lsu_id * LSU Process Offset"))
311 module.add(VAddU32(dst=vgpr(addr), src0=vgpr(addr), src1=vgpr(tmpVgpr), \
312 comment="addr += lsu offset"))
313 for i in range(1,numAddr):
314 module.add(VAddU32(vgpr(addr+i), maxLDSConstOffset*i, vgpr(addr), \
315 comment="addr += maxLDSConstOffset*%u"%(i)))
316
317 module.add(SWaitCnt(dscnt=0, comment="wait for all writes"))
318 module.add(writer._syncThreads(kernel, "post-lsu local write"))
319 module.add(Label("localSplitULocalRead_%d"%(reUseIdx+1), ""))
320
321 moduleReduction = Module("LocalSplitU_Reduction")
322 inLoopTmpVgpr = writer.vgprPool.checkOutAligned(numVgprPerLSU*(kernel["LocalSplitU"]-1), 4, "TempLsuReduction")
323
324 # Do Local Read
325 for i in range(0, numVgprPerLSU // self.LSUfullVw):
326 for v in range(numInstPerVW):
327 for r in range(0, kernel["LocalSplitU"]):
328 regIdx = (i * numInstPerVW + v) * regsPerStore
329 offset = r * ldsStride + regIdx * (bpr * kernel["WavefrontSize"])
330 num = offset // maxLDSConstOffset
331 offset -= num * maxLDSConstOffset
332 srcvgpr = vgpr(addr+num)
333 if r == 0:
334 vgprStr = "LsuReduction+%u"%(localReadVgprIdx)
335 else:
336 vgprStr = inLoopTmpVgpr + (numVgprPerLSU * (r - 1) + regIdx)
337 module.add(DSLoadBX(dst=vgpr(vgprStr, regsPerStore), src=srcvgpr, \
338 ds=DSModifiers(offset=(offset)), \
339 comment="r=%u i=%u, from acc[%d]"%(r, (i * numInstPerVW + v), neededAccVGPRIdx[0][(i * numInstPerVW + v)])))
340 # Generate Reduction code at the same time.
341 if r == 0:
342 # Insert waitcnt code here
343 numTotalInst = numVgprPerLSU // self.LSUfullVw * numInstPerVW * kernel["LocalSplitU"]
344 numPassedInst = (i * numInstPerVW + (v + 1)) * kernel["LocalSplitU"]
345 numLRWaitCnt = numTotalInst - numPassedInst
346 moduleReduction.add(SWaitCnt(dscnt=numLRWaitCnt, comment="wait count is (%u-%u)"%(numTotalInst, numPassedInst)))
347 if r > 0:
348 for regToAdd in range(regsPerStore):
349 if kernel["ProblemType"]["ComputeDataType"].isSingle():
350 moduleReduction.add(VAddF32(dst=vgpr("LsuReduction+%u"%(localReadVgprIdx+regToAdd)), src0=vgpr(vgprStr+regToAdd), \
351 src1=vgpr("LsuReduction+%u"%(localReadVgprIdx+regToAdd)), comment=""))
352 elif kernel["ProblemType"]["ComputeDataType"].isInt32():
353 moduleReduction.add(VAddI32(dst=vgpr("LsuReduction+%u"%(localReadVgprIdx+regToAdd)), src0=vgpr(vgprStr+regToAdd), \
354 src1=vgpr("LsuReduction+%u"%(localReadVgprIdx+regToAdd)), comment=""))
355 else:
356 # TODO: hpa_half, int8
357 assert(0) # unsupported data type, need to modify here and LSU write/read code
358
359 if kernel["GlobalSplitUAlgorithm"] == "MultipleBufferSingleKernel":
360 if not kernel["MIArchVgpr"]:
361 # write to accvgpr
362 for regToAdd in range(regsPerStore):
363 codeAccVgprReadInst = codeAccVgprRead.popFirstItem() # v_accvgpr_read_b32
364 accIdx = codeAccVgprReadInst.srcs[0].regIdx
365 writeInst = writer.accVgprReadWriteFunction(kernel, accIdx, False)
366 dstVgpr = writer.accVgprReadWriteIndex(kernel, accIdx)
367 moduleReduction.add(writeInst(dst=dstVgpr, src=vgpr("LsuReduction+%u"%(localReadVgprIdx+regToAdd)),
368 comment="copy vreg[%u] to acc[%u]" % (localReadVgprIdx+regToAdd, accIdx)))
369 else:
370 # write to vgpr
371 for regToAdd in range(regsPerStore):
372 codeAccVgprReadInst = codeAccVgprRead.popFirstItem() # v_mov_b32
373 dstVgpr = codeAccVgprReadInst.srcs[0]
374 moduleReduction.add(VMovB32(dst=dstVgpr, src=vgpr("LsuReduction+%u"%(localReadVgprIdx+regToAdd)),
375 comment="copy lsu results to vreg"))
376 localReadVgprIdx += regsPerStore
377
378 # Release write/read resource
379 writer.vgprPool.checkIn(addr)
380
381 # Do Reduction
382 module.add(moduleReduction)
383
384 # Release reduction resource
385 writer.vgprPool.checkIn(inLoopTmpVgpr)
386
387 # Release all resource
388 writer.vgprPool.checkIn(lsu_id)
389 writer.vgprPool.checkIn(wave_id)
390 writer.vgprPool.checkIn(tmpVgpr)
391 # reset vgprValuC register
392 module.add(RegSet("v", "vgprValuC", self.accVgprLdsReduction))
393
394 return module
395
396 ##############################################################################
397 # LocalSplitU: Global Write Indices
398 ##############################################################################
399 def globalWriteIndices(self, writer, kernel):
400 module = Module("localSplitUGlobalWriteIndices")
401
402 # Add LSU Offset back
403 packedC1 = kernel["PackedC1IndicesX"]
404 strideC1 = "StrideC%s" % (writer.states.indexChars[packedC1[0]])
405 strideD1 = "StrideD%s" % (writer.states.indexChars[packedC1[0]])
406 wave_id = writer.vgprPool.checkOut(1, "tmpWaveID")
407 tmpVgpr1 = writer.vgprPool.checkOutAligned(2,2,"tmpVgpr1")
408 tmpVgpr1Res = ContinuousRegister(tmpVgpr1, 2)
409 module.add(vectorStaticDivide(wave_id, "Serial", kernel["WavefrontSize"], tmpVgpr1Res))
410 numWaves = kernel["MIWaveGroup"][0] * kernel["MIWaveGroup"][1]
411 module.add(vectorStaticDivide(wave_id, wave_id, numWaves, tmpVgpr1Res))
412
413 with writer.allocTmpSgpr(1) as tmpSgprInfo:
414 tmpSgpr = tmpSgprInfo.idx
415 if self.LSUValidOffset0 > 0:
416 module.add(SMovB32(dst=sgpr(tmpSgpr), \
417 src=hex(self.LSUValidOffset0), comment="a valid offset"))
418 module.add(VMulLOU32(dst=vgpr(tmpVgpr1), src0=vgpr(wave_id), src1=sgpr(tmpSgpr), comment="wave LSU offset"))
419 module.add(VAddU32(dst=vgpr(writer.vgprs.coord0), src0=vgpr(tmpVgpr1), src1=vgpr(writer.vgprs.coord0), comment="coord0 += LSU offset0"))
420 else:
421 module.addComment0("valid offset coord0 is zero.")
422
423 if self.LSUValidOffset1 > 0:
424 module.add(SMovB32(dst=sgpr(tmpSgpr), \
425 src=hex(self.LSUValidOffset1), comment="a valid offset"))
426 module.add(VMulLOU32(dst=vgpr(tmpVgpr1), src0=vgpr(wave_id), src1=sgpr(tmpSgpr), comment="wave LSU offset"))
427 module.add(VAddU32(dst=vgpr(writer.vgprs.coord1), src0=vgpr(tmpVgpr1), src1=vgpr(writer.vgprs.coord1), comment="coord1 += LSU offset1"))
428 module.add(VAddU32(dst=vgpr(writer.vgprs.coord1InMT), src0=vgpr(tmpVgpr1), src1=vgpr(writer.vgprs.coord1InMT), comment="coord1InMT += LSU offset1"))
429
430 # this code is from CouputeStoreVgprs. coord 1 : offset part
431 packedC1 = kernel["PackedC1IndicesX"]
432 strideC1 = "StrideC%s" % (writer.states.indexChars[packedC1[0]])
433 strideD1 = "StrideD%s" % (writer.states.indexChars[packedC1[0]])
434 module.add(VMulLOU32(dst=vgpr(writer.vgprs.cinRowPtr), src0=vgpr(writer.vgprs.coord1InMT), src1=sgpr(strideC1), comment=" offset 1"))
435 module.add(VMulLOU32(dst=vgpr(writer.vgprs.coutRowPtrD), src0=vgpr(writer.vgprs.coord1InMT), src1=sgpr(strideD1), comment=" offset 1"))
436 if kernel["ProblemType"]["UseE"] and (kernel["GlobalSplitU"] == 1 or kernel["GlobalSplitU"] == -1):
437 module.add(VMovB32(dst=vgpr(writer.vgprs.coutRowPtrE), src=vgpr(writer.vgprs.coord1InMT), comment=" save offset 1 for E"))
438 if writer.vgprs.coutRowPtrBias != -1:
439 index = packedC1[0] - 1
440 strideW1 = "Size%s" % "I" if index == 0 else ("J" if index == 1 else (writer.states.indexChars[index]))
441 module.add(VMulLOU32(dst=vgpr(writer.vgprs.coutRowPtrBias), src0=vgpr(writer.vgprs.coord1InMT), src1=sgpr(strideW1), comment=" offset 1"))
442 else:
443 module.addComment0("valid offset coord1 is zero.")
444
445 writer.vgprPool.checkIn(tmpVgpr1)
446 writer.vgprPool.checkIn(wave_id)
447 writer.vgprPool.checkIn(writer.vgprs.coord0InMT)
448 writer.vgprPool.checkIn(writer.vgprs.coord1InMT)
449
450 if kernel["BufferStore"]:
451 #print "----AddressC-LocalSplitU"
452 #print self.vgprPool.state()
453 writer.vgprs.addrE = -1
454 writer.vgprs.addrD = -1
455 writer.vgprs.addrC = -1
456 writer.vgprs.addrBias = -1
457 writer.vgprs.addrScaleAVec = -1
458 writer.vgprs.addrScaleBVec = -1
459 writer.vgprs.addrScaleAlphaVec = -1
460 else:
461 writer.vgprs.addrD = writer.vgprPool.checkOut(2)
462 module.add(VMovB32(
463 dst=vgpr(writer.vgprs.addrD+0), \
464 src=sgpr("AddressD+0"), \
465 comment="sgpr -> vgpr"))
466 module.add(VMovB32(
467 dst=vgpr(writer.vgprs.addrD+1), \
468 src=sgpr("AddressD+1"), \
469 comment="sgpr -> vgpr"))
470 writer.vgprs.addrC = writer.vgprPool.checkOut(2)
471 module.add(VMovB32(
472 dst=vgpr(writer.vgprs.addrC+0), \
473 src=sgpr("AddressC+0"), \
474 comment="sgpr -> vgpr"))
475 module.add(VMovB32(
476 dst=vgpr(writer.vgprs.addrC+1), \
477 src=sgpr("AddressC+1"), \
478 comment="sgpr -> vgpr"))
479
480 if kernel["GlobalSplitU"] != 0:
481 gsuLabel = Label(label=writer.labels.getNameInc("GSU"), comment="")
482 with writer.allocTmpSgpr(1) as tmpSgprGSU:
483 module.add(SAndB32(dst=sgpr(tmpSgprGSU.idx), src0=sgpr("GSU"), src1=hex(0x3FFF), comment="Restore GSU"))
484 module.add(SCmpEQU32(src0=sgpr(tmpSgprGSU.idx), src1=1, comment="GSU == 1 ?"))
485 module.add(SCBranchSCC0(labelName=gsuLabel.getLabelName(), comment="branch if GSU != 1"))
486 if kernel["ProblemType"]["UseE"]:
487 writer.vgprs.addrE = writer.vgprPool.checkOut(2, 'addrE')
488 module.add(VMovB32( \
489 dst=vgpr(writer.vgprs.addrE+0), \
490 src=sgpr("AddressE+0"), \
491 comment="sgpr -> vgpr"))
492 module.add(VMovB32( \
493 dst=vgpr(writer.vgprs.addrE+1), \
494 src=sgpr("AddressE+1"), \
495 comment="sgpr -> vgpr"))
496 if writer.states.useBias == DataDirection.READ:
497 writer.vgprs.addrBias = writer.vgprPool.checkOut(2, 'addrBias')
498 module.add(VMovB32( \
499 dst=vgpr(writer.vgprs.addrBias+0), \
500 src=sgpr("AddressBias+0"), \
501 comment="sgpr -> vgpr"))
502 module.add(VMovB32( \
503 dst=vgpr(writer.vgprs.addrBias+1), \
504 src=sgpr("AddressBias+1"), \
505 comment="sgpr -> vgpr"))
506 if (kernel["ProblemType"]["UseScaleAB"] == "Vector"):
507 writer.vgprs.addrScaleAVec = writer.vgprPool.checkOut(2, 'addrScaleAVec')
508 module.add(VMovB32( \
509 dst=vgpr(writer.vgprs.addrScaleAVec+0), \
510 src=sgpr("AddressScaleA+0"), \
511 comment="sgpr -> vgpr"))
512 module.add(VMovB32( \
513 dst=vgpr(writer.vgprs.addrScaleAVec+1), \
514 src=sgpr("AddressScaleA+1"), \
515 comment="sgpr -> vgpr"))
516 writer.vgprs.addrScaleBVec = writer.vgprPool.checkOut(2, 'addrScaleVVec')
517 module.add(VMovB32( \
518 dst=vgpr(writer.vgprs.addrScaleBVec+0), \
519 src=sgpr("AddressScaleB+0"), \
520 comment="sgpr -> vgpr"))
521 module.add(VMovB32( \
522 dst=vgpr(writer.vgprs.addrScaleBVec+1), \
523 src=sgpr("AddressScaleB+1"), \
524 comment="sgpr -> vgpr"))
525 if kernel["ProblemType"]["UseScaleAlphaVec"]:
526 writer.vgprs.addrScaleAlphaVec = writer.vgprPool.checkOut(2, 'addrScaleAlphaVec')
527 module.add(VMovB32( \
528 dst=vgpr(self.vgprs.addrScaleAlphaVec+0), \
529 src=sgpr("AddressScaleAlphaVec+0"), \
530 comment="sgpr -> vgpr"))
531 module.add(VMovB32( \
532 dst=vgpr(self.vgprs.addrScaleAlphaVec+1), \
533 src=sgpr("AddressScaleAlphaVec+1"), \
534 comment="sgpr -> vgpr"))
535 if kernel["GlobalSplitU"] != 0:
536 module.add(gsuLabel)
537
538 return module
539
540
541 ##############################################################################
542 # LocalSplitU: Global Write
543 ##############################################################################
544 def globalWrite(self, writer, kernel, tPA, tPB):
545 if not writer.do["PostLoop"]: return ""
546
547 elements_0 = [[] for y in range(2)]
548 elements_1 = [[] for y in range(2)]
549 elements_f0 = [[] for y in range(2)]
550 elements_f1 = [[] for y in range(2)]
551 (fullVw, elements_0[False], fullVw_1, elements_1[False]) = writer.notLocalFullTileElements(kernel, False)
552 (edgeVw, elements_0[True], edgeVw_1, elements_1[True] ) = writer.notLocalFullTileElements(kernel, True)
553 edgeScaled_0 = len(elements_0[True]) // len(elements_1[False])
554 edgeScaled_1 = len(elements_1[True]) // len(elements_1[False])
555 noEgScaled_0 = len(elements_0[False]) // len(elements_1[False])
556
557 for i in range(0, len(elements_1[False])):
558 element = elements_1[False][i]
559 if element in self.LSUelementsPerLSUWave:
560 elements_f1[False].append(element)
561 for j in range(0, edgeScaled_0):
562 # in general, edge will affect vc0 dimension.
563 element = elements_0[True][i*edgeScaled_0+j]
564 elements_f0[True].append(element)
565 for j in range(0, edgeScaled_1):
566 # in general, edge will affect vc0 dimension.
567 element = elements_1[True][i*edgeScaled_1+j]
568 elements_f1[True].append(element)
569 for j in range(0, noEgScaled_0):
570 # in general, edge will affect vc0 dimension.
571 element = elements_0[False][i*noEgScaled_0+j]
572 elements_f0[False].append(element)
573
574 vectorWidths = [fullVw, edgeVw]
575 vectorWidths_1 = [fullVw_1, edgeVw_1]
576
577 noGSUBranch = (kernel["GlobalSplitU"] == 0 and kernel["StreamK"] != 3)
578 module = Module("localSplitUGlobalWrite")
579 module.add(writer.globalWriteElements(kernel, tPA, tPB, vectorWidths, vectorWidths_1, elements_f0, elements_f1, noGSUBranch=noGSUBranch))
580 writer.cleanupGlobalWrite(kernel)
581 writer.vgprPool.checkIn(self.accVgprLdsReduction)
582 return module