Debian Codegraph @

tensilelite/Tensile/Common/RegisterPool.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.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) # type: ignore
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) # type: ignore