| 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 |
| 24 | from rocisa.container import vgpr, sgpr |
| 25 | from rocisa.instruction import VMovB32, SCmpGeU32 |
| 26 | from ..Component import Component |
| 27 | import abc |
| 28 | |
| 29 | class PersistentLoop(Component): |
| 30 | """ |
| 31 | Persistent loop code. |
| 32 | """ |
| 33 | def __call__(self): |
| 34 | assert(0) |
| 35 | |
| 36 | @abc.abstractmethod |
| 37 | def openPersistentLoop(self, writer, kernel): |
| 38 | pass |
| 39 | |
| 40 | @abc.abstractmethod |
| 41 | def recalcLocalWriteAddresses(self, writer, kernel, tc): |
| 42 | pass |
| 43 | |
| 44 | @abc.abstractmethod |
| 45 | def recalcLocalReadAddressesAB(self, writer, kernel): |
| 46 | pass |
| 47 | |
| 48 | @abc.abstractmethod |
| 49 | def closePersistentLoop(self, writer, kernel): |
| 50 | pass |
| 51 | |
| 52 | |
| 53 | class PersistentLoopOff(PersistentLoop): |
| 54 | kernel = {"StreamK": 0} |
| 55 | |
| 56 | def openPersistentLoop(self, writer, kernel): |
| 57 | module = Module("PersistentLoop Off openPersistentLoop") |
| 58 | return module |
| 59 | |
| 60 | def recalcLocalWriteAddresses(self, writer, kernel, tc): |
| 61 | module = Module("PersistentLoop Off recalcLocalWriteAddresses") |
| 62 | return module |
| 63 | |
| 64 | def recalcLocalReadAddressesAB(self, writer, kernel): |
| 65 | module = Module("PersistentLoop Off recalcLocalReadAddressesAB") |
| 66 | return module |
| 67 | |
| 68 | def closePersistentLoop(self, writer, kernel): |
| 69 | module = Module("PersistentLoop Off closePersistentLoop") |
| 70 | return module |
| 71 | |
| 72 | |
| 73 | class PersistentLoopOn(PersistentLoop): |
| 74 | # Stream-K persistent loop |
| 75 | |
| 76 | @classmethod |
| 77 | def matches(cls, writer, debug=False): |
| 78 | return writer.states.kernel["StreamK"] > 0 |
| 79 | |
| 80 | def openPersistentLoop(self, writer, kernel): |
| 81 | module = Module("PersistentLoop On openPersistentLoop") |
| 82 | |
| 83 | # Label start of persistent loop |
| 84 | module.addComment2("Persistent Loop Start") |
| 85 | persistentLabel = Label(label="PersistentLoopStart", comment="") |
| 86 | module.add(persistentLabel) |
| 87 | |
| 88 | # TODO remove? |
| 89 | # kStr += inst("s_add_u32", sgpr("PersistentLoopIter"), sgpr("PersistentLoopIter"), hex(1), "Inc PersistentLoop Iter") # Back-up: not needed now |
| 90 | #kStr += str(Code.WaitCnt(self.version, 0,0,"wait for outstanding stores")) |
| 91 | return module |
| 92 | |
| 93 | def recalcLocalWriteAddresses(self, writer, kernel, tc): |
| 94 | module = Module("PersistentLoop On recalcLocalWriteAddresses") |
| 95 | |
| 96 | if getattr(writer, "oriLwa%s" % tc) is None: |
| 97 | setattr(writer, "oriLwa%s" % tc, writer.vgprPool.checkOut(1, "OriLocalWriteddr%s" % tc)) |
| 98 | module.add(VMovB32(dst=vgpr(getattr(writer, "oriLwa%s" % tc)), src=vgpr("LocalWriteAddr%s" % tc), comment="back up LWA for persistent kernel + wider local read")) |
| 99 | |
| 100 | return module |
| 101 | |
| 102 | def recalcLocalReadAddressesAB(self, writer, kernel): |
| 103 | module = Module("PersistentLoop On recalcLocalReadAddressesAB") |
| 104 | |
| 105 | needRecalc = writer.states.numReadsIterCoalescedA > 1 or writer.states.numReadsIterCoalescedB > 1 |
| 106 | # backup LocalReadAddr |
| 107 | # LdsPad + LBSPP case, need to backup LocalReadAddr even if recalc is not done |
| 108 | |
| 109 | # The code below backs up LRA at the start of the tail loop and restores at the end of tail loop. |
| 110 | # This would be needed if we want to support PersistentKernel in the future (without stream-k). |
| 111 | # Backup is not needed for stream-k since LRA is recalculated at the start of the persistent loop for the new tile or partial tile. |
| 112 | # Enabling the backup code would require fixing an interaction with the code that reallocates vgprs for the store code. |
| 113 | |
| 114 | # needBackupLRAddr = needRecalc or (kernel["LdsPadA"] and kernel["LdsBlockSizePerPadA"] or kernel["LdsPadB"] and kernel["LdsBlockSizePerPadB"]) and kernel["StreamK"] == 0 |
| 115 | |
| 116 | # if needBackupLRAddr: |
| 117 | # # need to back-up the LRA before reCalculation for wider local read (when no wlr, no need to do this) |
| 118 | # if writer.oriLraA is None: # and not kernel["DirectToVgprA"]: # no local read code if DirectToVgpr is enabled |
| 119 | # writer.oriLraA = writer.vgprPool.checkOut(1, "OriLocalReadAddrA") |
| 120 | # module.add(VMovB32(dst=vgpr(writer.oriLraA), src=vgpr("LocalReadAddrA"), comment="back up LRA for persistent kernel + wider local read")) |
| 121 | # if writer.oriLraB is None: # and not kernel["DirectToVgprB"]: # no local read code if DirectToVgpr is enabled |
| 122 | # writer.oriLraB = writer.vgprPool.checkOut(1, "OriLocalReadAddrB") |
| 123 | # module.add(VMovB32(dst=vgpr(writer.oriLraB), src=vgpr("LocalReadAddrB"), comment="back up LRA for persistent kernel + wider local read")) |
| 124 | |
| 125 | return module |
| 126 | |
| 127 | def closePersistentLoop(self, writer, kernel): |
| 128 | module = Module("PersistentLoop On closePersistentLoop") |
| 129 | skCloseLoopLabel = Label("SK_CloseLoop", "") |
| 130 | module.add(skCloseLoopLabel) |
| 131 | # endIter = "StreamKIterEnd" if kernel["StreamK"] == 1 else "TotalIters" |
| 132 | endIter = "TotalIters" if kernel["StreamK"] == 2 else "StreamKIterEnd" |
| 133 | module.add(SCmpGeU32(src0=sgpr("StreamKIter"), src1=sgpr(endIter), comment="Check if done all StreamK iterations")) |
| 134 | module.add(writer.longBranchScc0(Label("PersistentLoopStart", ""), posNeg=-1)) |
| 135 | return module |