| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
from rocisa.code import Label, Module |
| 26 |
from rocisa.container import VCC, DSModifiers, vgpr, sgpr, ContinuousRegister |
| 27 |
from rocisa.instruction import DSBPermuteB32, SBranch, SCBranchVCCNZ, \ |
| 28 |
SMovB32, SMovB64, SNop, \ |
| 29 |
SOrSaveExecB32, SOrSaveExecB64, SWaitCnt, \ |
| 30 |
VAddCOU32, \ |
| 31 |
VAndB32, VCmpEQU32, VCmpLtU32, VCmpXEqU32, \ |
| 32 |
VCndMaskB32, VMovB32, VMulI32I24, VLShiftLeftB32, \ |
| 33 |
VLShiftRightB32, VSubU32, VMinI32 |
| 34 |
from rocisa.functions import vectorStaticRemainder, vectorStaticDivide, vectorStaticMultiply |
| 35 |
from ..Common import log2 |
| 36 |
from ..Component import ShiftVectorComponents |
| 37 |
from ..KernelWriterModules import * |
| 38 |
|
| 39 |
class ShiftVectorComponentsVALU(ShiftVectorComponents): |
| 40 |
kernel = {"EnableMatrixInstruction": False} |
| 41 |
|
| 42 |
""" |
| 43 |
Shift Vector Components d0,1 |
| 44 |
""" |
| 45 |
def __call__(self, writer, kernel, tP): |
| 46 |
|
| 47 |
module = Module("ShiftVectorComponentsVALU") |
| 48 |
|
| 49 |
|
| 50 |
tc = tP["tensorChar"] |
| 51 |
glvw = tP["glvw"] |
| 52 |
numThreadInWave = writer.states.kernel["WavefrontSize"] |
| 53 |
vectorWidth = kernel["VectorWidth%s"%tc] |
| 54 |
numVectors = kernel[tP["tt"]]//glvw |
| 55 |
|
| 56 |
|
| 57 |
svrLabels = [] |
| 58 |
sviLabels = [] |
| 59 |
for i in range(0, glvw): |
| 60 |
r = (i+1) % glvw |
| 61 |
|
| 62 |
label = Label(writer.labels.getName("ShiftVectorComponents%u_R%u"%(tP["idx"], r) ), "") |
| 63 |
svrLabels.append(label) |
| 64 |
tmpLabels = [] |
| 65 |
for v in range(0, numVectors): |
| 66 |
|
| 67 |
label = Label(writer.labels.getName("ShiftVectorComponents%u_R%u_V%u"%(tP["idx"], r, v) ), "") |
| 68 |
tmpLabels.append(label) |
| 69 |
sviLabels.append(tmpLabels) |
| 70 |
|
| 71 |
with writer.allocTmpSgpr(writer.states.laneSGPRCount) as tmpSgprInfo: |
| 72 |
|
| 73 |
wg = tP["wg"] |
| 74 |
tmpSgpr = tmpSgprInfo.idx |
| 75 |
dummy = writer.vgprPool.checkOut(1) |
| 76 |
tmpVgpr = writer.vgprPool.checkOut(1,"tmpVgpr") |
| 77 |
wgMT = writer.vgprPool.checkOut(1,"wgMT") |
| 78 |
tmpVgprRes = ContinuousRegister(tmpVgpr, 2) |
| 79 |
module.add(VMovB32(dst=vgpr(wgMT), src=sgpr(wg), comment="")) |
| 80 |
module.add(VMulI32I24(dst=vgpr(wgMT), src0=hex(-kernel[tP["mt"]]), src1=vgpr(wgMT), comment="wg*MT")) |
| 81 |
module.add(VAddCOU32(dst=vgpr(wgMT), dst1=VCC(), src0=sgpr("SizesFree+%u"%tP["idx"]), src1=vgpr(wgMT), comment="wgMT = Size - wg*MT")) |
| 82 |
module.add(VMovB32(dst=vgpr(tmpVgpr), src=hex(kernel[tP["mt"]]), comment="MT")) |
| 83 |
module.add(VMinI32(dst=vgpr(wgMT), src0=vgpr(tmpVgpr), src1=vgpr(wgMT), comment="wgMT = (wgMT < MT) ? wgMT : MT")) |
| 84 |
|
| 85 |
writer.vgprPool.checkIn(tmpVgpr) |
| 86 |
|
| 87 |
|
| 88 |
qReg = writer.vgprPool.checkOut(1,"qReg") |
| 89 |
module.add(vectorStaticDivide(qReg, wgMT, vectorWidth, tmpVgprRes)) |
| 90 |
|
| 91 |
|
| 92 |
rReg = writer.vgprPool.checkOut(1,"rReg") |
| 93 |
module.add(vectorStaticRemainder(dummy, rReg, wgMT, glvw, tmpVgprRes, tmpSgprInfo)) |
| 94 |
|
| 95 |
|
| 96 |
eReg = writer.vgprPool.checkOut(1,"eReg") |
| 97 |
divisor = kernel[tP["sg"]] |
| 98 |
module.add(vectorStaticRemainder(dummy, eReg, qReg, divisor, tmpVgprRes, tmpSgprInfo)) |
| 99 |
|
| 100 |
if tP["isA"]: |
| 101 |
|
| 102 |
thread = writer.vgprPool.checkOut(1,"thread") |
| 103 |
divisor = kernel["SubGroup0"] |
| 104 |
module.add(vectorStaticRemainder(dummy, thread, "Serial", divisor, tmpVgprRes, tmpSgprInfo)) |
| 105 |
else: |
| 106 |
|
| 107 |
sd0 = writer.vgprPool.checkOut(1,"sd0") |
| 108 |
divisor = kernel["SubGroup0"] |
| 109 |
module.add(vectorStaticDivide(sd0, "Serial", divisor, tmpVgprRes)) |
| 110 |
divisor = kernel["SubGroup1"] |
| 111 |
thread = writer.vgprPool.checkOut(1,"thread") |
| 112 |
module.add(vectorStaticRemainder(dummy, thread, sd0, divisor, tmpVgprRes, tmpSgprInfo)) |
| 113 |
writer.vgprPool.checkIn(sd0) |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
if True: |
| 118 |
mvReg = writer.vgprPool.checkOut(1,"mvReg") |
| 119 |
divisor = kernel[tP["sg"]]*vectorWidth |
| 120 |
module.add(vectorStaticDivide(mvReg, wgMT, divisor, tmpVgprRes)) |
| 121 |
if glvw < vectorWidth: |
| 122 |
module.add(VLShiftRightB32(dst=vgpr(mvReg), shiftHex=hex(log2(vectorWidth//glvw)), src=vgpr(mvReg), comment="vId *= GLVW/glvw")) |
| 123 |
|
| 124 |
vReg = writer.vgprPool.checkOut(1,"vReg") |
| 125 |
divisor = vectorWidth |
| 126 |
module.add(vectorStaticRemainder(dummy, vReg, wgMT, divisor, tmpVgprRes, tmpSgprInfo)) |
| 127 |
vRegD = writer.vgprPool.checkOut(1,"vRegD") |
| 128 |
module.add(VMovB32(dst=vgpr(vRegD), src=vgpr(vReg), comment="duplicate")) |
| 129 |
divisor = glvw |
| 130 |
module.add(vectorStaticDivide(vReg, vRegD, divisor, tmpVgprRes)) |
| 131 |
|
| 132 |
if True: |
| 133 |
module.add(VAddCOU32(dst=vgpr(vReg), dst1=VCC(), src0=vgpr(mvReg), src1=vgpr(vReg), comment="vId = 2 components")) |
| 134 |
writer.vgprPool.checkIn(mvReg) |
| 135 |
writer.vgprPool.checkIn(vRegD) |
| 136 |
|
| 137 |
|
| 138 |
for r in range(1, glvw): |
| 139 |
module.add(VCmpEQU32(dst=VCC(), src0=vgpr(rReg), src1=hex(r), comment="wgMT%%GLVW == %u"%r )) |
| 140 |
module.add(SCBranchVCCNZ(labelName=svrLabels[(r-1)%glvw].getLabelName(), comment="shift d%u r=%u"%(tP["idx"], r))) |
| 141 |
module.add(SBranch(labelName=svrLabels[glvw-1].getLabelName(), comment="no shifting" )) |
| 142 |
|
| 143 |
|
| 144 |
for r in range(1, glvw): |
| 145 |
module.addComment2("shift d%u r=%u"%(tP["idx"], r)) |
| 146 |
module.add(svrLabels[r-1]) |
| 147 |
|
| 148 |
|
| 149 |
for vectorIdx in range(0, numVectors): |
| 150 |
module.add(VCmpEQU32(dst=VCC(), src0=vgpr(vReg), src1=hex(vectorIdx), comment="wgMT/(SG*VW) == %u"%vectorIdx )) |
| 151 |
module.add(SCBranchVCCNZ(labelName=sviLabels[(r-1)%glvw][vectorIdx].getLabelName(), comment="shift d%u, r=%u, v=%u"%(tP["idx"], r, vectorIdx))) |
| 152 |
|
| 153 |
|
| 154 |
for vectorIdx in range(0, numVectors): |
| 155 |
module.addComment1("shift d%u r=%u v=%u"%(tP["idx"], r, vectorIdx)) |
| 156 |
module.add(sviLabels[r-1][vectorIdx]) |
| 157 |
|
| 158 |
|
| 159 |
module.add(VCmpXEqU32(dst=sgpr(tmpSgpr, writer.states.laneSGPRCount), src0=vgpr(thread), src1=vgpr(eReg), comment="serial % SG == (wgMT/VECTOR_WIDTH)%SG" )) |
| 160 |
tto = kernel["ThreadTile%u"%((tP["idx"]+1)%2)] |
| 161 |
for tt in range(0, tto): |
| 162 |
for s in range(0, r): |
| 163 |
comment = "" |
| 164 |
if tP["isA"]: |
| 165 |
dst = (s) \ |
| 166 |
+ vectorIdx * glvw + tt * kernel["ThreadTile0"] |
| 167 |
src = (s+glvw-r) \ |
| 168 |
+ vectorIdx * glvw + tt * kernel["ThreadTile0"] |
| 169 |
else: |
| 170 |
dst = (tt) \ |
| 171 |
+ vectorIdx*glvw*kernel["ThreadTile0"] + s * kernel["ThreadTile0"] |
| 172 |
src = (tt) \ |
| 173 |
+ vectorIdx * glvw*kernel["ThreadTile0"] + (s+glvw-r) * kernel["ThreadTile0"] |
| 174 |
|
| 175 |
module.addComment("src=%u, dst=%u" % (src,dst)) |
| 176 |
|
| 177 |
|
| 178 |
for i in range(0, writer.states.bpeCinternal//writer.states.bpr): |
| 179 |
module.add(VMovB32(dst=vgpr(writer.states.c.startVgprValu+dst*writer.states.bpeCinternal//writer.states.bpr+i), \ |
| 180 |
src=vgpr(writer.states.c.startVgprValu+src*writer.states.bpeCinternal//writer.states.bpr+i), \ |
| 181 |
comment=comment)) |
| 182 |
|
| 183 |
|
| 184 |
all1mask = "0xFFFFFFFF" if (kernel["WavefrontSize"] == 32) else "0xFFFFFFFFFFFFFFFF" |
| 185 |
SOrSaveExecBX = SOrSaveExecB64 if kernel["WavefrontSize"] == 64 else SOrSaveExecB32 |
| 186 |
module.add(SMovB32(sgpr(tmpSgpr, writer.states.laneSGPRCount), all1mask, "to restore all threads active")) |
| 187 |
module.add(SOrSaveExecBX(dst=VCC(), src=sgpr(tmpSgpr, writer.states.laneSGPRCount), comment="all threads active")) |
| 188 |
module.add(SBranch(labelName=svrLabels[glvw-1].getLabelName(), comment="done shifting" )) |
| 189 |
module.add(svrLabels[glvw-1]) |
| 190 |
|
| 191 |
|
| 192 |
writer.vgprPool.checkIn(wgMT) |
| 193 |
writer.vgprPool.checkIn(qReg) |
| 194 |
writer.vgprPool.checkIn(rReg) |
| 195 |
writer.vgprPool.checkIn(eReg) |
| 196 |
writer.vgprPool.checkIn(thread) |
| 197 |
writer.vgprPool.checkIn(vReg) |
| 198 |
writer.vgprPool.checkIn(tmpVgpr) |
| 199 |
writer.vgprPool.checkIn(dummy) |
| 200 |
return module |
| 201 |
|
| 202 |
class ShiftVectorComponentsMFMA(ShiftVectorComponents): |
| 203 |
kernel = {"EnableMatrixInstruction": True} |
| 204 |
|
| 205 |
""" |
| 206 |
Shift Vector Components d0,1 |
| 207 |
""" |
| 208 |
def __call__(self, writer, kernel, tP): |
| 209 |
""" |
| 210 |
if (glvw > vectorwidth * continuousOutput * threadsInCoal) |
| 211 |
use all thread algorithm |
| 212 |
else |
| 213 |
use partial thread algorithm |
| 214 |
""" |
| 215 |
|
| 216 |
tc = tP["tensorChar"] |
| 217 |
glvw = tP["glvw"] |
| 218 |
numThreadInWave = writer.states.kernel["WavefrontSize"] |
| 219 |
vectorWidth = kernel["VectorWidth%s"%tc] |
| 220 |
|
| 221 |
|
| 222 |
matrixInstM = kernel["MatrixInstM"] * kernel["MatrixInstBM"] if (kernel["MatrixInstM"] == 4) else kernel["MatrixInstM"] |
| 223 |
matrixInstN = kernel["MatrixInstN"] * kernel["MatrixInstBN"] if (kernel["MatrixInstN"] == 4) else kernel["MatrixInstN"] |
| 224 |
|
| 225 |
|
| 226 |
matrixInstCoal = matrixInstM if tP["isA"] else matrixInstN |
| 227 |
matrixInstPrep = matrixInstN if tP["isA"] else matrixInstM |
| 228 |
|
| 229 |
|
| 230 |
conThInProcDim = kernel["SourceSwap"] ^ tP["isB"] |
| 231 |
numThreadInCoal = matrixInstCoal if conThInProcDim else (numThreadInWave // matrixInstPrep) |
| 232 |
|
| 233 |
|
| 234 |
numContOutCoal = vectorWidth if conThInProcDim else kernel["MIOutputVectorWidth"] * vectorWidth |
| 235 |
allContOutCoal = numContOutCoal |
| 236 |
|
| 237 |
if (glvw > (allContOutCoal * numThreadInCoal)): |
| 238 |
return self.ShiftVectorComponentsMFMAAllThread(writer, kernel, tP) |
| 239 |
else: |
| 240 |
return self.ShiftVectorComponentsMFMAPartialThread (writer, kernel, tP) |
| 241 |
|
| 242 |
|
| 243 |
def ShiftVectorComponentsMFMAPartialThread(self, writer, kernel, tP): |
| 244 |
|
| 245 |
""" when we enable shift ptr with vectorwidth(2), we shift global read on edge block when size % vectorwidth != 0. |
| 246 |
For example if M size == 3 vector width == 2, we want to do global read for [0-1] and [2-3]. |
| 247 |
But 3 is not in memory object, so we shift to do global read [0-1] and [1-2]. |
| 248 |
So buffer become [0, 1, 1, 2], assume result in register is same as input [0, 1, 1, 2] |
| 249 |
We need to shift it back to [0, 1, 2]. |
| 250 |
|
| 251 |
In MFMA outputs, We have numContinuousOutput(4) for each thread. |
| 252 |
We have numThreadInWave(64) threads. |
| 253 |
number of thread in N is sames as kernel["MatrixInstN"] (32) |
| 254 |
number of thread in M is numThreadInWave/numOutputThreads1 = 2 |
| 255 |
stride of continuous output for each thread (numSubOutputPerWave0) is numOutputThreads0 * numContinuousOutput, (8). |
| 256 |
we have numSubOutputGroupsPerWave0 which is 4 (kernel[tP["mt"]](64) // numSubOutputPerWave0(8)) |
| 257 |
|
| 258 |
So we do shift back by below algorithm. |
| 259 |
1. check if M_size % GlobalReadVectorWidth != 0, return if == 0 |
| 260 |
2. decide which subgroup we need to shift, M_size(3) means 3/8 = group 0 |
| 261 |
3. decide which thread we need to shift, we have different groups of thread, (0-31) for first group, (32-63) for second group. |
| 262 |
4. decide which shift block (subTile1) we want to shift. for ex [0-1], [1-2], we want to shift second subtile |
| 263 |
""" |
| 264 |
|
| 265 |
|
| 266 |
tc = tP["tensorChar"] |
| 267 |
regPerElem = kernel["MIRegPerOut"] |
| 268 |
glvw = tP["glvw"] |
| 269 |
numThreadInWave = writer.states.kernel["WavefrontSize"] |
| 270 |
accImOffset = accVgprImagNumOffset(kernel) |
| 271 |
vectorWidth = kernel["VectorWidth%s"%tc] |
| 272 |
|
| 273 |
|
| 274 |
matrixInstM = kernel["MatrixInstM"] * kernel["MatrixInstBM"] if (kernel["MatrixInstM"] == 4) else kernel["MatrixInstM"] |
| 275 |
matrixInstN = kernel["MatrixInstN"] * kernel["MatrixInstBN"] if (kernel["MatrixInstN"] == 4) else kernel["MatrixInstN"] |
| 276 |
matrixInstBM = 1 if (kernel["MatrixInstM"] == 4) else kernel["MatrixInstBM"] |
| 277 |
matrixInstBN = 1 if (kernel["MatrixInstN"] == 4) else kernel["MatrixInstBN"] |
| 278 |
|
| 279 |
|
| 280 |
matrixInstCoal = matrixInstM if tP["isA"] else matrixInstN |
| 281 |
matrixInstPrep = matrixInstN if tP["isA"] else matrixInstM |
| 282 |
matrixInstBCoal = matrixInstBM if tP["isA"] else matrixInstBN |
| 283 |
matrixInstBPrep = matrixInstBN if tP["isA"] else matrixInstBM |
| 284 |
miWaveGroupCoal = kernel["MIWaveGroup"][0] if tP["isA"] else kernel["MIWaveGroup"][1] |
| 285 |
miWGIdStride = numThreadInWave if tP["isA"] else (numThreadInWave * kernel["MIWaveGroup"][0]) |
| 286 |
miWaveTitleCoal = kernel["MIWaveTile"][0] if tP["isA"] else kernel["MIWaveTile"][1] |
| 287 |
miWaveTitlePrep = kernel["MIWaveTile"][1] if tP["isA"] else kernel["MIWaveTile"][0] |
| 288 |
|
| 289 |
|
| 290 |
conThInProcDim = kernel["SourceSwap"] ^ tP["isB"] |
| 291 |
|
| 292 |
threadInterval = 1 if conThInProcDim else matrixInstPrep |
| 293 |
numThreadInCoal = matrixInstCoal if conThInProcDim else (numThreadInWave // matrixInstPrep) |
| 294 |
|
| 295 |
numContOutCoal = vectorWidth if conThInProcDim else kernel["MIOutputVectorWidth"] * vectorWidth |
| 296 |
allContOutCoal = numContOutCoal |
| 297 |
|
| 298 |
OutBlocksInMI = (vectorWidth * matrixInstCoal * matrixInstPrep) // numThreadInWave // numContOutCoal |
| 299 |
OutBlocksInMI = 1 if conThInProcDim else OutBlocksInMI |
| 300 |
|
| 301 |
subMBShapeCoal = (matrixInstCoal * vectorWidth) if conThInProcDim else ((numThreadInWave // matrixInstPrep) * numContOutCoal) |
| 302 |
MBShapeCoal = subMBShapeCoal * OutBlocksInMI |
| 303 |
MIBShapeCoal = MBShapeCoal * matrixInstBCoal |
| 304 |
WGShapeCoal = MIBShapeCoal * miWaveGroupCoal |
| 305 |
miOuterTTCoal = miWaveTitleCoal // vectorWidth |
| 306 |
|
| 307 |
numOutputsPrep = (matrixInstCoal * matrixInstPrep // numThreadInWave) if conThInProcDim else 1 |
| 308 |
numOutputsPrep = numOutputsPrep * matrixInstBPrep * miWaveTitlePrep |
| 309 |
complexMultiplier = 2 if kernel["ProblemType"]["DataType"].isComplex() else 1 |
| 310 |
|
| 311 |
|
| 312 |
regStrideCoal = 1 if tP["isA"] else numOutputsPrep |
| 313 |
regStridePrep = miOuterTTCoal * matrixInstBCoal * OutBlocksInMI * allContOutCoal if tP["isA"] else 1 |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
glvwLabels = [] |
| 318 |
MBblockLabels = [] |
| 319 |
VWBlockLabels = [] |
| 320 |
for i in range(0, glvw): |
| 321 |
r = (i+1) % glvw |
| 322 |
comment = "end shift0" if i == glvw-1 else "" |
| 323 |
label = Label(writer.labels.getName("ShiftVectorComponents%u_GLVW%u" % (tP["idx"], r) ), comment) |
| 324 |
glvwLabels.append(label) |
| 325 |
subMBLabels = [] |
| 326 |
subVWBlockLabels = [] |
| 327 |
for mb in range(0, OutBlocksInMI * matrixInstBCoal * miOuterTTCoal): |
| 328 |
label = Label(writer.labels.getName("ShiftVectorComponents%u_GLVW%u_BM%u" % (tP["idx"], r, mb)), "") |
| 329 |
subMBLabels.append(label) |
| 330 |
sub2VWBlockLabels = [] |
| 331 |
for vw in range(0, max(1, allContOutCoal//glvw)): |
| 332 |
label = Label(writer.labels.getName("ShiftVectorComponents%u_GLVW%u_BM%u_VW%u" % (tP["idx"], r, mb, vw)), "") |
| 333 |
sub2VWBlockLabels.append(label) |
| 334 |
subVWBlockLabels.append(sub2VWBlockLabels) |
| 335 |
MBblockLabels.append(subMBLabels) |
| 336 |
VWBlockLabels.append(subVWBlockLabels) |
| 337 |
|
| 338 |
with writer.allocTmpSgpr(writer.states.laneSGPRCount) as tmpSgprInfo: |
| 339 |
|
| 340 |
tmpSgpr = tmpSgprInfo.idx |
| 341 |
tmpVgpr = writer.vgprPool.checkOutAligned(2,2) |
| 342 |
tmpVgprRes = ContinuousRegister(tmpVgpr, 2) |
| 343 |
dummy = writer.vgprPool.checkOut(1) |
| 344 |
wgMT = writer.vgprPool.checkOut(1) |
| 345 |
wg = tP["wg"] |
| 346 |
|
| 347 |
module = Module("ShiftVectorComponentsMFMA") |
| 348 |
|
| 349 |
|
| 350 |
mtReg = writer.vgprPool.checkOut(1) |
| 351 |
module.add(VMovB32(dst=vgpr(wgMT), src=sgpr(wg))) |
| 352 |
module.add(VMulI32I24(dst=vgpr(wgMT), src0=hex(-kernel[tP["mt"]]), src1=vgpr(wgMT), comment="wg*MT")) |
| 353 |
module.add(VAddCOU32(dst=vgpr(wgMT), dst1=VCC(), src0=sgpr("SizesFree+%u"%tP["idx"]), src1=vgpr(wgMT), comment="wgMT = Size - wg*MT")) |
| 354 |
module.add(VMovB32(dst=vgpr(mtReg), src=hex(kernel[tP["mt"]]), comment="MT")) |
| 355 |
module.add(VCmpLtU32(dst=sgpr(tmpSgpr,writer.states.laneSGPRCount), src0=vgpr(wgMT), src1=vgpr(mtReg), comment="wgMT < MT")) |
| 356 |
module.add(VCndMaskB32(dst=vgpr(wgMT), src0=vgpr(mtReg), src1=vgpr(wgMT), src2=sgpr(tmpSgpr,writer.states.laneSGPRCount), comment="wgMT = (wgMT < MT) ? wgMT : MT" )) |
| 357 |
|
| 358 |
|
| 359 |
wReg = writer.vgprPool.checkOut(1) |
| 360 |
sReg = writer.vgprPool.checkOut(1) |
| 361 |
module.add(vectorStaticDivide(wReg, "Serial", miWGIdStride, tmpVgprRes)) |
| 362 |
module.add(vectorStaticRemainder(dummy, wReg, wReg, miWaveGroupCoal, tmpVgprRes, tmpSgprInfo)) |
| 363 |
module.add(vectorStaticDivide(sReg, wgMT, MIBShapeCoal, tmpVgprRes)) |
| 364 |
module.add(vectorStaticRemainder(dummy, sReg, sReg, miWaveGroupCoal, tmpVgprRes, tmpSgprInfo)) |
| 365 |
module.add(VCmpEQU32(dst=sgpr(tmpSgpr,writer.states.laneSGPRCount), src0=vgpr(sReg), src1=vgpr(wReg), comment="wave_id == block_belong_to_wave?" )) |
| 366 |
module.add(VCndMaskB32(dst=vgpr(wgMT), src0=vgpr(mtReg), src1=vgpr(wgMT), src2=sgpr(tmpSgpr,writer.states.laneSGPRCount), comment="wgMT = (wgMT < MT) ? wgMT : MT" )) |
| 367 |
writer.vgprPool.checkIn(mtReg) |
| 368 |
writer.vgprPool.checkIn(sReg) |
| 369 |
|
| 370 |
|
| 371 |
module.addComment1("mbReg: which mb block need to shift, mb(matrixInstCoal(%u) * VectorWidth(%u))" % (matrixInstCoal, vectorWidth)) |
| 372 |
mbReg = writer.vgprPool.checkOut(1) |
| 373 |
tReg = writer.vgprPool.checkOut(1) |
| 374 |
module.add(vectorStaticDivide(mbReg, wgMT, subMBShapeCoal, tmpVgprRes)) |
| 375 |
module.add(vectorStaticMultiply(vgpr(tReg), vgpr(wReg), (matrixInstBCoal * OutBlocksInMI), tmpSgprInfo)) |
| 376 |
module.add(VSubU32(dst=vgpr(mbReg), src0=vgpr(mbReg), src1=vgpr(tReg))) |
| 377 |
writer.vgprPool.checkIn(tReg) |
| 378 |
|
| 379 |
|
| 380 |
module.addComment1("gbReg: glvw block id") |
| 381 |
gbReg = writer.vgprPool.checkOut(1) |
| 382 |
module.add(vectorStaticDivide(gbReg, wgMT, glvw, tmpVgprRes)) |
| 383 |
|
| 384 |
|
| 385 |
module.addComment1("tgbReg: glvw block id") |
| 386 |
tgbReg = writer.vgprPool.checkOut(1) |
| 387 |
module.add(vectorStaticDivide(tgbReg, "Serial", threadInterval, tmpVgprRes)) |
| 388 |
module.add(vectorStaticRemainder(dummy, tgbReg, tgbReg, numThreadInCoal, tmpVgprRes, tmpSgprInfo)) |
| 389 |
module.add(vectorStaticMultiply(vgpr(tgbReg), vgpr(tgbReg), allContOutCoal, tmpSgprInfo)) |
| 390 |
module.add(vectorStaticDivide(tgbReg, tgbReg, glvw, tmpVgprRes)) |
| 391 |
module.add(vectorStaticMultiply(vgpr(wReg), vgpr(wReg), MIBShapeCoal//glvw, tmpSgprInfo)) |
| 392 |
module.add(VAddCOU32(dst=vgpr(tgbReg), dst1=VCC(), src0=vgpr(wReg), src1=vgpr(tgbReg), comment="tgbReg = (tid_coal * continOut) / GLVW")) |
| 393 |
module.add(VSubU32(dst=vgpr(gbReg), src0=vgpr(gbReg), src1=vgpr(tgbReg))) |
| 394 |
writer.vgprPool.checkIn(wReg) |
| 395 |
writer.vgprPool.checkIn(tgbReg) |
| 396 |
|
| 397 |
|
| 398 |
module.addComment1("vwReg: glvw in which vw block?") |
| 399 |
vwReg = writer.vgprPool.checkOut(1) |
| 400 |
module.add(VAndB32(dst=vgpr(vwReg), src0=allContOutCoal-1, src1=vgpr(wgMT), comment="permute register between threads")) |
| 401 |
module.add(VLShiftRightB32(dst=vgpr(vwReg), shiftHex=log2(glvw), src=vgpr(vwReg), comment="permute register between threads")) |
| 402 |
|
| 403 |
shiftVectorComponentsPlaceHolders = [] |
| 404 |
shiftVectorComponentsPlaceTargets = [] |
| 405 |
shiftVectorComponentsPlaceOperations = [] |
| 406 |
|
| 407 |
|
| 408 |
module.addComment1("rReg : reminder of M_size % GlobalReadVectorWidth") |
| 409 |
rReg = writer.vgprPool.checkOut(1) |
| 410 |
module.add(vectorStaticRemainder(dummy, rReg, wgMT, glvw, tmpVgprRes, tmpSgprInfo)) |
| 411 |
for r in range(1, glvw): |
| 412 |
module.add(VCmpEQU32(dst=VCC(), src0=vgpr(rReg), src1=hex(r), comment="wgMT%%VW == %u"%r )) |
| 413 |
module.add(SCBranchVCCNZ(labelName=glvwLabels[(r-1)].getLabelName(), comment="branch to shift d%u r=%u"%(tP["idx"], r))) |
| 414 |
|
| 415 |
label_name = glvwLabels[glvw-1].getLabelName().replace("label_", "") |
| 416 |
place_holder = "%d_%s_placeholder"%(len(shiftVectorComponentsPlaceHolders), label_name) |
| 417 |
shiftVectorComponentsPlaceHolders.append(place_holder) |
| 418 |
shiftVectorComponentsPlaceTargets.append(label_name) |
| 419 |
shiftVectorComponentsPlaceOperations.append("SBranch") |
| 420 |
shiftVectorComponentsModel = Module(place_holder) |
| 421 |
shiftVectorComponentsModel.addComment1("no shifting") |
| 422 |
module.add(shiftVectorComponentsModel) |
| 423 |
writer.vgprPool.checkIn(rReg) |
| 424 |
|
| 425 |
_, arch2acc = accToArchMapper(kernel) |
| 426 |
|
| 427 |
|
| 428 |
for r in range(1, glvw): |
| 429 |
module.addComment2("shift d%u r=%u"%(tP["idx"], r)) |
| 430 |
module.add(glvwLabels[r-1]) |
| 431 |
for tt in range(0, miOuterTTCoal): |
| 432 |
for bm in range(0, matrixInstBCoal): |
| 433 |
for ob in range(0, OutBlocksInMI): |
| 434 |
label = ob + OutBlocksInMI * (bm + matrixInstBCoal * tt) |
| 435 |
target = ob + OutBlocksInMI * (bm + matrixInstBCoal * miWaveGroupCoal * tt) |
| 436 |
if target > 64: |
| 437 |
src1 = sgpr(tmpSgprInfo.idx) |
| 438 |
module.add(SMovB32(dst=src1, src=hex(target), comment="large mb")) |
| 439 |
else: |
| 440 |
src1 = hex(target) |
| 441 |
module.add(VCmpEQU32(dst=VCC(), src0=vgpr(mbReg), src1=src1)) |
| 442 |
label_name = MBblockLabels[r-1][label].getLabelName().replace("label_", "") |
| 443 |
place_holder = "%d_%s_placeholder"%(len(shiftVectorComponentsPlaceHolders), label_name) |
| 444 |
shiftVectorComponentsPlaceHolders.append(place_holder) |
| 445 |
shiftVectorComponentsPlaceTargets.append(label_name) |
| 446 |
shiftVectorComponentsPlaceOperations.append("SCBranchVCCNZ") |
| 447 |
shiftVectorComponentsModel = Module(place_holder) |
| 448 |
shiftVectorComponentsModel.addComment1("branch to shift d%u r%u mb%u" % (tP["idx"], r, label)) |
| 449 |
module.add(shiftVectorComponentsModel) |
| 450 |
|
| 451 |
for r in range(1, glvw): |
| 452 |
for mb in range(0, miOuterTTCoal * matrixInstBCoal * OutBlocksInMI): |
| 453 |
module.addComment2("shift d%u r=%u mb=%u"%(tP["idx"], r, mb)) |
| 454 |
MBblockLabels[r-1][mb].comment = "r%u mb%u"%(r, mb) |
| 455 |
module.add(MBblockLabels[r-1][mb]) |
| 456 |
for vw in range(0, max(1, allContOutCoal//glvw)): |
| 457 |
module.add(VCmpEQU32(dst=VCC(), src0=vgpr(vwReg), src1=hex(vw))) |
| 458 |
label_name = VWBlockLabels[r-1][mb][vw].getLabelName().replace("label_", "") |
| 459 |
place_holder = "%d_%s_placeholder"%(len(shiftVectorComponentsPlaceHolders), label_name) |
| 460 |
shiftVectorComponentsPlaceHolders.append(place_holder) |
| 461 |
shiftVectorComponentsPlaceTargets.append(label_name) |
| 462 |
shiftVectorComponentsPlaceOperations.append("SCBranchVCCNZ") |
| 463 |
shiftVectorComponentsModel = Module(place_holder) |
| 464 |
shiftVectorComponentsModel.addComment1("branch to shift d%u r%u mb%u vw%u" % (tP["idx"], r, mb, vw)) |
| 465 |
module.add(shiftVectorComponentsModel) |
| 466 |
|
| 467 |
|
| 468 |
tReg = writer.vgprPool.checkOut(min(glvw, allContOutCoal)) |
| 469 |
for r in range(1, glvw): |
| 470 |
for tt in range(0, miOuterTTCoal): |
| 471 |
for bm in range(0, matrixInstBCoal): |
| 472 |
for ob in range(0, OutBlocksInMI): |
| 473 |
mb = ob + OutBlocksInMI * (bm + matrixInstBCoal * tt) |
| 474 |
for vw in range(0, max(1, allContOutCoal//glvw)): |
| 475 |
module.addComment2("shift d%u r=%u mb=%u vw%d"%(tP["idx"], r, mb, vw)) |
| 476 |
VWBlockLabels[r-1][mb][vw].comment = "r%u mb%u vw%u"%(r, mb, vw) |
| 477 |
module.add(VWBlockLabels[r-1][mb][vw]) |
| 478 |
module.add(SMovB32(dst=sgpr(tmpSgpr), src=(((ob*subMBShapeCoal + bm*MBShapeCoal + tt*WGShapeCoal) // glvw) + vw))) |
| 479 |
module.add(VCmpXEqU32(dst=sgpr(tmpSgpr, writer.states.laneSGPRCount), src0=vgpr(gbReg), src1=sgpr(tmpSgpr), comment="is thread in edge glvw region" )) |
| 480 |
module.add(VAndB32(dst=vgpr(tmpVgpr), src0=kernel["WavefrontSize"]-1, src1=vgpr("Serial"), comment="permute register between threads")) |
| 481 |
module.add(VLShiftLeftB32(dst=vgpr(tmpVgpr), shiftHex=log2(writer.states.bpr), src=vgpr(tmpVgpr), comment="permute register between threads")) |
| 482 |
|
| 483 |
for ot in range(numOutputsPrep): |
| 484 |
for c in range(complexMultiplier): |
| 485 |
for nr in range(regPerElem): |
| 486 |
vgprOffsetForSCIU = 0 |
| 487 |
for e in range(min(r, allContOutCoal)): |
| 488 |
src = (e+(glvw-r)) % allContOutCoal |
| 489 |
srcVgpr = (src + (vw * glvw) + allContOutCoal * mb) * regStrideCoal |
| 490 |
srcVgpr = srcVgpr + ot * regStridePrep |
| 491 |
srcVgpr = arch2acc[srcVgpr] * regPerElem + nr + c * accImOffset + vgprOffsetForSCIU |
| 492 |
srcVal = writer.accVgprReadWriteIndex(kernel, srcVgpr) |
| 493 |
copyInst = writer.accVgprReadWriteFunction(kernel, srcVgpr, True) |
| 494 |
module.add(copyInst(dst=vgpr(tReg+e), src=srcVal, comment="glvw %u mb %u tt1 %u r %u" % (r, mb, ot, nr))) |
| 495 |
|
| 496 |
if not kernel["MIArchVgpr"]: |
| 497 |
module.add(SNop(waitState=1, comment="v_accvgpr read vgpr after write vgpr: 2 wait states")) |
| 498 |
|
| 499 |
needWait = False |
| 500 |
for e in range(min(r, allContOutCoal)): |
| 501 |
crossThread = (e+(glvw-r)) // allContOutCoal |
| 502 |
if crossThread != 0: |
| 503 |
ds = DSModifiers(na=1, offset=crossThread*threadInterval*4) |
| 504 |
module.add(DSBPermuteB32(dst=vgpr(tReg+e), src0=vgpr(tmpVgpr), src1=vgpr(tReg+e), ds=ds, comment="permute edge values")) |
| 505 |
needWait = True |
| 506 |
|
| 507 |
if needWait: |
| 508 |
module.add(SWaitCnt(waitAll=True, comment="wait for swizzle operation")) |
| 509 |
|
| 510 |
for e in range(min(r, allContOutCoal)): |
| 511 |
dstVgpr = (e + (vw * glvw) + allContOutCoal * mb) * regStrideCoal |
| 512 |
dstVgpr = dstVgpr + ot * regStridePrep |
| 513 |
dstVgpr = arch2acc[dstVgpr] * regPerElem + nr + c * accImOffset + vgprOffsetForSCIU |
| 514 |
dstStr = writer.accVgprReadWriteIndex(kernel, dstVgpr) |
| 515 |
copyInst = writer.accVgprReadWriteFunction(kernel, dstVgpr, False) |
| 516 |
module.add(copyInst(dst=dstStr, src=vgpr(tReg+e))) |
| 517 |
|
| 518 |
|
| 519 |
all1mask = "0xFFFFFFFF" if (kernel["WavefrontSize"] == 32) else "0xFFFFFFFFFFFFFFFF" |
| 520 |
SMovBX = SMovB64 if kernel["WavefrontSize"] == 64 else SMovB32 |
| 521 |
SOrSaveExecBX = SOrSaveExecB64 if kernel["WavefrontSize"] == 64 else SOrSaveExecB32 |
| 522 |
module.add(SMovBX(dst=sgpr(tmpSgpr, writer.states.laneSGPRCount), src=all1mask, comment="to restore all threads active")) |
| 523 |
module.add(SOrSaveExecBX(dst=VCC(), src=sgpr(tmpSgpr,writer.states.laneSGPRCount), comment="all threads active")) |
| 524 |
label_name = glvwLabels[glvw-1].getLabelName().replace("label_", "") |
| 525 |
place_holder = "%d_%s_placeholder"%(len(shiftVectorComponentsPlaceHolders), label_name) |
| 526 |
shiftVectorComponentsPlaceHolders.append(place_holder) |
| 527 |
shiftVectorComponentsPlaceTargets.append(label_name) |
| 528 |
shiftVectorComponentsPlaceOperations.append("SBranch") |
| 529 |
shiftVectorComponentsModel = Module(place_holder) |
| 530 |
shiftVectorComponentsModel.addComment1("no shifting") |
| 531 |
module.add(shiftVectorComponentsModel) |
| 532 |
module.addSpaceLine() |
| 533 |
|
| 534 |
module.add(glvwLabels[glvw-1]) |
| 535 |
writer.vgprPool.checkIn(tReg) |
| 536 |
|
| 537 |
|
| 538 |
writer.vgprPool.checkIn(tmpVgpr) |
| 539 |
writer.vgprPool.checkIn(wgMT) |
| 540 |
writer.vgprPool.checkIn(dummy) |
| 541 |
writer.vgprPool.checkIn(gbReg) |
| 542 |
writer.vgprPool.checkIn(vwReg) |
| 543 |
writer.vgprPool.checkIn(mbReg) |
| 544 |
writer.updateBranchPlaceHolder(module, shiftVectorComponentsPlaceHolders, shiftVectorComponentsPlaceTargets, shiftVectorComponentsPlaceOperations) |
| 545 |
return module |
| 546 |
|
| 547 |
def ShiftVectorComponentsMFMAAllThread(self, writer, kernel, tP): |
| 548 |
""" |
| 549 |
""" |
| 550 |
|
| 551 |
tc = tP["tensorChar"] |
| 552 |
glvw = tP["glvw"] |
| 553 |
numThreadInWave = kernel["WavefrontSize"] |
| 554 |
vectorWidth = kernel["VectorWidth%s"%tc] |
| 555 |
|
| 556 |
|
| 557 |
matrixInstM = kernel["MatrixInstM"] * kernel["MatrixInstBM"] if (kernel["MatrixInstM"] == 4) else kernel["MatrixInstM"] |
| 558 |
matrixInstN = kernel["MatrixInstN"] * kernel["MatrixInstBN"] if (kernel["MatrixInstN"] == 4) else kernel["MatrixInstN"] |
| 559 |
matrixInstBM = 1 if (kernel["MatrixInstM"] == 4) else kernel["MatrixInstBM"] |
| 560 |
matrixInstBN = 1 if (kernel["MatrixInstN"] == 4) else kernel["MatrixInstBN"] |
| 561 |
|
| 562 |
|
| 563 |
matrixInstCoal = matrixInstM if tP["isA"] else matrixInstN |
| 564 |
matrixInstPrep = matrixInstN if tP["isA"] else matrixInstM |
| 565 |
matrixInstBCoal = matrixInstBM if tP["isA"] else matrixInstBN |
| 566 |
matrixInstBPrep = matrixInstBN if tP["isA"] else matrixInstBM |
| 567 |
miWaveGroupCoal = kernel["MIWaveGroup"][0] if tP["isA"] else kernel["MIWaveGroup"][1] |
| 568 |
miWGIdStride = numThreadInWave if tP["isA"] else (numThreadInWave * kernel["MIWaveGroup"][0]) |
| 569 |
miWaveTitleCoal = kernel["MIWaveTile"][0] if tP["isA"] else kernel["MIWaveTile"][1] |
| 570 |
miWaveTitlePrep = kernel["MIWaveTile"][1] if tP["isA"] else kernel["MIWaveTile"][0] |
| 571 |
|
| 572 |
|
| 573 |
conThInProcDim = kernel["SourceSwap"] ^ tP["isB"] |
| 574 |
|
| 575 |
threadInterval = 1 if conThInProcDim else matrixInstPrep |
| 576 |
numThreadInCoal = matrixInstCoal if conThInProcDim else (numThreadInWave // matrixInstPrep) |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
numContOutCoal = vectorWidth if conThInProcDim else kernel["MIOutputVectorWidth"] * vectorWidth |
| 581 |
allContOutCoal = numContOutCoal |
| 582 |
|
| 583 |
|
| 584 |
OutBlocksInMI = (vectorWidth * matrixInstCoal * matrixInstPrep) // numThreadInWave // numContOutCoal |
| 585 |
OutBlocksInMI = 1 if conThInProcDim else OutBlocksInMI |
| 586 |
|
| 587 |
subMBShapeCoal = (matrixInstCoal * vectorWidth) if conThInProcDim else ((numThreadInWave // matrixInstPrep) * numContOutCoal) |
| 588 |
MBShapeCoal = subMBShapeCoal * OutBlocksInMI |
| 589 |
MIBShapeCoal = MBShapeCoal * matrixInstBCoal |
| 590 |
miOuterTTCoal = miWaveTitleCoal // vectorWidth |
| 591 |
|
| 592 |
numOutputsPrep = (matrixInstCoal * matrixInstPrep // numThreadInWave) if conThInProcDim else 1 |
| 593 |
numOutputsPrep = numOutputsPrep * matrixInstBPrep * miWaveTitlePrep |
| 594 |
|
| 595 |
|
| 596 |
regStrideCoal = 1 if tP["isA"] else numOutputsPrep |
| 597 |
regStridePrep = miOuterTTCoal * matrixInstBCoal * OutBlocksInMI * allContOutCoal if tP["isA"] else 1 |
| 598 |
|
| 599 |
|
| 600 |
shiftLabels = [] |
| 601 |
GLVWBLKLabels = [] |
| 602 |
for i in range(0, glvw): |
| 603 |
r = (i+1) % glvw |
| 604 |
label = Label(writer.labels.getName("ShiftVectorComponents%u_shift%u" % (tP["idx"], r) ), "") |
| 605 |
shiftLabels.append(label) |
| 606 |
subGLVWBLKLabels = [] |
| 607 |
for glvwBlk in range(0, (MIBShapeCoal // glvw) * miOuterTTCoal): |
| 608 |
label = Label(writer.labels.getName("ShiftVectorComponents%u_shift%u_glvwblk%u" % (tP["idx"], r, glvwBlk)), "") |
| 609 |
subGLVWBLKLabels.append(label) |
| 610 |
GLVWBLKLabels.append(subGLVWBLKLabels) |
| 611 |
|
| 612 |
with writer.allocTmpSgpr(writer.states.laneSGPRCount) as tmpSgprInfo: |
| 613 |
|
| 614 |
tmpSgpr = tmpSgprInfo.idx |
| 615 |
tmpVgpr = writer.vgprPool.checkOutAligned(2,2) |
| 616 |
tmpVgprRes = ContinuousRegister(tmpVgpr, 2) |
| 617 |
dummy = writer.vgprPool.checkOut(1) |
| 618 |
wgMT = writer.vgprPool.checkOut(1) |
| 619 |
wg = tP["wg"] |
| 620 |
|
| 621 |
module = Module("ShiftVectorComponentsMFMA") |
| 622 |
|
| 623 |
|
| 624 |
module.addComment1("check which macro tile need to shift") |
| 625 |
mtReg = writer.vgprPool.checkOut(1) |
| 626 |
module.add(VMovB32(dst=vgpr(wgMT), src=sgpr(wg))) |
| 627 |
module.add(VMulI32I24(dst=vgpr(wgMT), src0=hex(-kernel[tP["mt"]]), src1=vgpr(wgMT), comment="wg*MT")) |
| 628 |
module.add(VAddCOU32(dst=vgpr(wgMT), dst1=VCC(), src0=sgpr("SizesFree+%u"%tP["idx"]), src1=vgpr(wgMT), comment="wgMT = Size - wg*MT")) |
| 629 |
module.add(VMovB32(dst=vgpr(mtReg), src=hex(kernel[tP["mt"]]), comment="MT")) |
| 630 |
module.add(VCmpLtU32(dst=sgpr(tmpSgpr,writer.states.laneSGPRCount), src0=vgpr(wgMT), src1=vgpr(mtReg), comment="wgMT < MT")) |
| 631 |
module.add(VCndMaskB32(dst=vgpr(wgMT), src0=vgpr(mtReg), src1=vgpr(wgMT), src2=sgpr(tmpSgpr,writer.states.laneSGPRCount), comment="wgMT = (wgMT < MT) ? wgMT : MT" )) |
| 632 |
module.addSpaceLine() |
| 633 |
|
| 634 |
|
| 635 |
module.addComment1("check which wave need to shift") |
| 636 |
wReg = writer.vgprPool.checkOut(1) |
| 637 |
sReg = writer.vgprPool.checkOut(1) |
| 638 |
module.add(vectorStaticDivide(wReg, "Serial", miWGIdStride, tmpVgprRes)) |
| 639 |
module.add(vectorStaticRemainder(dummy, wReg, wReg, miWaveGroupCoal, tmpVgprRes, tmpSgprInfo)) |
| 640 |
module.add(vectorStaticDivide(sReg, wgMT, MIBShapeCoal, tmpVgprRes)) |
| 641 |
module.add(vectorStaticRemainder(dummy, sReg, sReg, miWaveGroupCoal, tmpVgprRes, tmpSgprInfo)) |
| 642 |
module.add(VCmpEQU32(dst=sgpr(tmpSgpr,writer.states.laneSGPRCount), src0=vgpr(sReg), src1=vgpr(wReg), comment="wave_id == block_belong_to_wave?" )) |
| 643 |
module.add(VCndMaskB32(dst=vgpr(wgMT), src0=vgpr(mtReg), src1=vgpr(wgMT), src2=sgpr(tmpSgpr,writer.states.laneSGPRCount), comment="wgMT = (wgMT < MT) ? wgMT : MT" )) |
| 644 |
module.addSpaceLine() |
| 645 |
|
| 646 |
|
| 647 |
module.addComment1("get id of which glvw block need to shift") |
| 648 |
glvwblkidReg = writer.vgprPool.checkOut(1) |
| 649 |
module.add(VMulI32I24(dst=vgpr(glvwblkidReg), src0=hex(-MIBShapeCoal), src1=vgpr(wReg), comment="wg * MIB")) |
| 650 |
module.add(VAddCOU32(dst=vgpr(glvwblkidReg), dst1=VCC(), src0=vgpr(glvwblkidReg), src1=vgpr(wgMT), comment="wgMT = Size - wg*MIB")) |
| 651 |
module.add(vectorStaticDivide(glvwblkidReg, glvwblkidReg, glvw, tmpVgprRes, "glvw block id")) |
| 652 |
module.addSpaceLine() |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
module.addComment1("dispatch to differet shift block for shift") |
| 657 |
rReg = writer.vgprPool.checkOut(1) |
| 658 |
module.add(vectorStaticRemainder(dummy, rReg, wgMT, glvw, tmpVgprRes, tmpSgprInfo)) |
| 659 |
for r in range(1, glvw): |
| 660 |
module.add(VCmpEQU32(dst=VCC(), src0=vgpr(rReg), src1=hex(r), comment="wgMT%%VW == %u"%r )) |
| 661 |
module.add(SCBranchVCCNZ(labelName=shiftLabels[(r-1)].getLabelName(), comment="branch to shift d%u r=%u"%(tP["idx"], r))) |
| 662 |
module.add(SBranch(labelName=shiftLabels[(glvw-1)].getLabelName(), comment="no shifting")) |
| 663 |
writer.vgprPool.checkIn(rReg) |
| 664 |
|
| 665 |
|
| 666 |
glvwBlkInMIB = MIBShapeCoal // glvw |
| 667 |
numRegInGlvwblkCoal = glvw // numThreadInCoal |
| 668 |
numRegInMIBCoal = MIBShapeCoal // numThreadInCoal |
| 669 |
for r in range(1, glvw): |
| 670 |
module.addComment2("shift d%u shift=%u"%(tP["idx"], r)) |
| 671 |
module.add(shiftLabels[r-1]) |
| 672 |
for tt in range(0, miOuterTTCoal): |
| 673 |
for glvwBlk in range(0, glvwBlkInMIB): |
| 674 |
label = glvwBlk + tt * glvwBlkInMIB |
| 675 |
target = glvwBlk + tt * glvwBlkInMIB * miWaveGroupCoal |
| 676 |
module.add(VCmpEQU32(dst=VCC(), src0=vgpr(glvwblkidReg), src1=hex(target))) |
| 677 |
module.add(SCBranchVCCNZ(labelName=GLVWBLKLabels[r-1][label].getLabelName(), comment="branch to shift d%u shift%u glvwblk%u" % (tP["idx"], r, label))) |
| 678 |
|
| 679 |
_, arch2acc = accToArchMapper(kernel) |
| 680 |
|
| 681 |
permuteIndexReg = writer.vgprPool.checkOut(1) |
| 682 |
threadIdInCoalReg = writer.vgprPool.checkOut(1) |
| 683 |
movReg = writer.vgprPool.checkOut(numContOutCoal*numOutputsPrep) |
| 684 |
module.addComment2("Reg %d-%d"%(movReg, movReg+15)) |
| 685 |
for shift in range(1, glvw): |
| 686 |
for tt in range(0, miOuterTTCoal): |
| 687 |
for glvwBlk in range(0, glvwBlkInMIB): |
| 688 |
label = glvwBlk + tt * glvwBlkInMIB |
| 689 |
module.addComment2("shift d%u shift=%u glvwblk=%u"%(tP["idx"], shift, glvwBlk)) |
| 690 |
module.add(GLVWBLKLabels[shift-1][label]) |
| 691 |
module.add(VAndB32(dst=vgpr(permuteIndexReg), src0=kernel["WavefrontSize"]-1, src1=vgpr("Serial"), comment="permute register between threads")) |
| 692 |
module.add(vectorStaticMultiply(vgpr(permuteIndexReg), vgpr(permuteIndexReg), writer.states.bpr, tmpSgprInfo, comment="permute register between threads")) |
| 693 |
module.add(vectorStaticDivide(threadIdInCoalReg, "Serial", threadInterval, tmpVgprRes)) |
| 694 |
module.add(vectorStaticRemainder(dummy, threadIdInCoalReg, threadIdInCoalReg, numThreadInCoal, tmpVgprRes, tmpSgprInfo)) |
| 695 |
|
| 696 |
for dstMbblkId in range(glvw//(numContOutCoal*numThreadInCoal)): |
| 697 |
for dstThreadId in range(numThreadInCoal): |
| 698 |
skip = True |
| 699 |
for dstContId in range(numContOutCoal): |
| 700 |
dst = dstContId + dstThreadId * numContOutCoal + dstMbblkId * numThreadInCoal * numContOutCoal |
| 701 |
src = dst + (glvw - shift) |
| 702 |
if (src < glvw): |
| 703 |
skip = False |
| 704 |
srcContId = src % numContOutCoal |
| 705 |
srcThreadId = (src // numContOutCoal) % numThreadInCoal |
| 706 |
srcMbblkId = src // (numContOutCoal * numThreadInCoal) |
| 707 |
for ot in range(numOutputsPrep): |
| 708 |
movRegId = movReg + dstContId + ot * numContOutCoal |
| 709 |
srcGpr = srcContId + srcMbblkId * numContOutCoal + glvwBlk * numRegInGlvwblkCoal + tt * numRegInMIBCoal |
| 710 |
srcGpr = srcGpr * regStrideCoal + ot * regStridePrep |
| 711 |
srcGpr = arch2acc[srcGpr] |
| 712 |
srcGprStr = writer.accVgprReadWriteIndex(kernel, srcGpr) |
| 713 |
copyInst = copyInst = writer.accVgprReadWriteFunction(kernel, srcGpr, True) |
| 714 |
module.add(copyInst(dst=vgpr(movRegId), src=srcGprStr, comment="")) |
| 715 |
|
| 716 |
if not skip: |
| 717 |
module.add(SNop(waitState=1, comment="v_accvgpr read vgpr after write vgpr: 2 wait states")) |
| 718 |
|
| 719 |
needWait = False |
| 720 |
for dstContId in range(numContOutCoal): |
| 721 |
dst = dstContId + dstThreadId * numContOutCoal + dstMbblkId * numThreadInCoal * numContOutCoal |
| 722 |
src = dst + (glvw - shift) |
| 723 |
if (src < glvw): |
| 724 |
srcContId = src % numContOutCoal |
| 725 |
srcThreadId = (src // numContOutCoal) % numThreadInCoal |
| 726 |
srcMbblkId = src // (numContOutCoal * numThreadInCoal) |
| 727 |
srcGpr = srcContId + srcMbblkId * numContOutCoal |
| 728 |
if dstThreadId != srcThreadId: |
| 729 |
needWait = True |
| 730 |
permuteOffset = (((srcThreadId - dstThreadId) * threadInterval) % kernel["WavefrontSize"]) * 4 |
| 731 |
for ot in range(numOutputsPrep): |
| 732 |
movRegId = movReg + dstContId + ot * numContOutCoal |
| 733 |
ds = DSModifiers(na=1, offset=permuteOffset) |
| 734 |
module.add(DSBPermuteB32(dst=vgpr(movRegId), src0=vgpr(permuteIndexReg), src1=vgpr(movRegId), ds=ds, comment="permute edge values")) |
| 735 |
|
| 736 |
if needWait: |
| 737 |
module.add(SWaitCnt(dscnt=0, comment="wait for swizzle operation")) |
| 738 |
|
| 739 |
if not skip: |
| 740 |
module.add(SMovB32(dst=sgpr(tmpSgpr), src=dstThreadId, comment="which thread need to shfit in this block")) |
| 741 |
module.add(VCmpXEqU32(dst=sgpr(tmpSgpr, writer.states.laneSGPRCount), src0=vgpr(threadIdInCoalReg), src1=sgpr(tmpSgpr), comment="is thread in edge glvw region")) |
| 742 |
module.add(SNop(waitState=3, comment="wait for exec mask")) |
| 743 |
|
| 744 |
for dstContId in range(numContOutCoal): |
| 745 |
dst = dstContId + dstThreadId * numContOutCoal + dstMbblkId * numThreadInCoal * numContOutCoal |
| 746 |
src = dst + (glvw - shift) |
| 747 |
if (src < glvw): |
| 748 |
for ot in range(numOutputsPrep): |
| 749 |
movRegId = movReg + dstContId + ot * numContOutCoal |
| 750 |
dstGpr = dstContId + dstMbblkId * numContOutCoal + glvwBlk * numRegInGlvwblkCoal + tt * numRegInMIBCoal |
| 751 |
dstGpr = dstGpr * regStrideCoal + ot * regStridePrep |
| 752 |
dstGpr = arch2acc[dstGpr] |
| 753 |
dstGprStr = writer.accVgprReadWriteIndex(kernel, dstGpr) |
| 754 |
copyInst = writer.accVgprReadWriteFunction(kernel, dstGpr, False) |
| 755 |
module.add(copyInst(dst=dstGprStr, src=vgpr(movRegId), comment="")) |
| 756 |
|
| 757 |
if not skip: |
| 758 |
all1mask = "0xFFFFFFFF" if (kernel["WavefrontSize"] == 32) else "0xFFFFFFFFFFFFFFFF" |
| 759 |
SMovBX = SMovB64 if kernel["WavefrontSize"] == 64 else SMovB32 |
| 760 |
SOrSaveExecBX = SOrSaveExecB64 if kernel["WavefrontSize"] == 64 else SOrSaveExecB32 |
| 761 |
module.add(SMovBX(dst=sgpr(tmpSgpr, writer.states.laneSGPRCount), src=all1mask, comment="to restore all threads active")) |
| 762 |
module.add(SOrSaveExecBX(dst=VCC(), src=sgpr(tmpSgpr,writer.states.laneSGPRCount), comment="all threads active")) |
| 763 |
module.add(SNop(waitState=3, comment="wait for exec mask")) |
| 764 |
|
| 765 |
module.add(SBranch(labelName=shiftLabels[glvw-1].getLabelName(), comment="done")) |
| 766 |
|
| 767 |
module.add(shiftLabels[glvw-1]) |
| 768 |
|
| 769 |
writer.vgprPool.checkIn(movReg) |
| 770 |
writer.vgprPool.checkIn(threadIdInCoalReg) |
| 771 |
writer.vgprPool.checkIn(permuteIndexReg) |
| 772 |
|
| 773 |
writer.vgprPool.checkIn(glvwblkidReg) |
| 774 |
writer.vgprPool.checkIn(sReg) |
| 775 |
writer.vgprPool.checkIn(wReg) |
| 776 |
writer.vgprPool.checkIn(mtReg) |
| 777 |
|
| 778 |
|
| 779 |
writer.vgprPool.checkIn(tmpVgpr) |
| 780 |
writer.vgprPool.checkIn(dummy) |
| 781 |
writer.vgprPool.checkIn(wgMT) |
| 782 |
|
| 783 |
return module |