| 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.container import ContinuousRegister |
| 26 |
from rocisa.enum import RegisterType |
| 27 |
from rocisa.register import RegisterPool |
| 28 |
|
| 29 |
from contextlib import contextmanager |
| 30 |
from typing import List, Optional |
| 31 |
|
| 32 |
def ResourceOverflowException(Exception): |
| 33 |
pass |
| 34 |
|
| 35 |
@contextmanager |
| 36 |
def allocTmpGpr(pool: RegisterPool, num: int, upperLimit: int, alignment: Optional[int]=None, tag: Optional[str]=None, overflowListener=None): |
| 37 |
""" |
| 38 |
A context-manager based temporary resource allocator for given RegisterPool object. |
| 39 |
|
| 40 |
:param pool: `RegisterPool` Resource pool |
| 41 |
:param num: `int` Size to allocate |
| 42 |
:param alignment: `Optional[bool]` Resource to be aligned to spcified alignment. If not specified, alignment set to 2 if `num` > 1 else 1 |
| 43 |
:param tag: `Optional[str]` Specified tag string for code generation |
| 44 |
:raises `ResourceOverflowException` if cannot allocate resource |
| 45 |
:returns: `(offset, size)`, offset: `int` Start offset of allocated resource |
| 46 |
size: `int` Size of allocated resource |
| 47 |
""" |
| 48 |
assert alignment is None or alignment > 0 |
| 49 |
if alignment is None: |
| 50 |
alignment = 1 if num == 1 else 2 |
| 51 |
if tag is None: |
| 52 |
tag = f"allocTmpgpr({num})" |
| 53 |
|
| 54 |
try: |
| 55 |
allocatedSgprIdx = pool.checkOutAligned(num, alignment, tag, False) |
| 56 |
|
| 57 |
if allocatedSgprIdx + num > upperLimit: |
| 58 |
exception = ResourceOverflowException(f"gpr overflow") |
| 59 |
if overflowListener: |
| 60 |
overflowListener(exception) |
| 61 |
else: |
| 62 |
raise exception |
| 63 |
|
| 64 |
yield ContinuousRegister(idx=allocatedSgprIdx, size=num) |
| 65 |
finally: |
| 66 |
pool.checkIn(allocatedSgprIdx) |
| 67 |
|
| 68 |
@contextmanager |
| 69 |
def allocTmpGprList(pool: RegisterPool, nums: List[int], upperLimit: int, alignments: Optional[List[int]]=None, tag: Optional[str]=None, overflowListener=None): |
| 70 |
""" |
| 71 |
A context-manager based temporary resource allocator for given RegisterPool object. |
| 72 |
|
| 73 |
:param pool: `RegisterPool` Resource pool |
| 74 |
:param num: `int` Size to allocate |
| 75 |
:param alignment: `Optional[bool]` Resource to be aligned to spcified alignment. If not specified, alignment set to 2 if `num` > 1 else 1 |
| 76 |
:param tag: `Optional[str]` Specified tag string for code generation |
| 77 |
:raises `ResourceOverflowException` if cannot allocate resource |
| 78 |
:returns: `(offset, size)`, offset: `int` Start offset of allocated resource |
| 79 |
size: `int` Size of allocated resource |
| 80 |
""" |
| 81 |
|
| 82 |
if alignments: |
| 83 |
if len(alignments) == 1: |
| 84 |
for num in nums: |
| 85 |
if num % alignments[0] != 0: |
| 86 |
print("Mod %% hint must == 0") |
| 87 |
assert 0 |
| 88 |
alignments = [alignments[0]] * len(nums) |
| 89 |
else: |
| 90 |
assert len(nums) == len(alignments) |
| 91 |
for num, alignment in zip(nums, alignments): |
| 92 |
if num % alignment != 0: |
| 93 |
print("Mod %% hint must == 0") |
| 94 |
assert 0 |
| 95 |
else: |
| 96 |
alignments = [] |
| 97 |
for num in nums: |
| 98 |
alignments.append(1 if num == 1 else 2) |
| 99 |
|
| 100 |
try: |
| 101 |
allocatedSgprIdxList = [] |
| 102 |
for num, alignment in zip(nums, alignments): |
| 103 |
if tag is None: |
| 104 |
tag = f"allocTmpgpr({num})" |
| 105 |
|
| 106 |
allocatedSgprIdx = pool.checkOutAligned(num, alignment, tag, False) |
| 107 |
|
| 108 |
if allocatedSgprIdx + num > upperLimit: |
| 109 |
exception = ResourceOverflowException(f"gpr overflow") |
| 110 |
if overflowListener: |
| 111 |
overflowListener(exception) |
| 112 |
else: |
| 113 |
raise exception |
| 114 |
allocatedSgprIdxList.append([allocatedSgprIdx, num]) |
| 115 |
|
| 116 |
registerPoolResourceList = [] |
| 117 |
for allocatedSgprIdx, num in allocatedSgprIdxList: |
| 118 |
registerPoolResourceList.append(ContinuousRegister(idx=allocatedSgprIdx, size=num)) |
| 119 |
|
| 120 |
yield registerPoolResourceList |
| 121 |
finally: |
| 122 |
for allocatedSgprIdx, _ in allocatedSgprIdxList: |
| 123 |
pool.checkIn(allocatedSgprIdx) |