Rickard Bolin | bc6ee58 | 2022-11-04 08:24:29 +0000 | [diff] [blame^] | 1 | # SPDX-FileCopyrightText: Copyright 2020-2022 Arm Limited and/or its affiliates <open-source-office@arm.com> |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 2 | # |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the License); you may |
| 6 | # not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
Rickard Bolin | bc6ee58 | 2022-11-04 08:24:29 +0000 | [diff] [blame^] | 16 | # |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 17 | # Description: |
| 18 | # Allocate tensor addresses using a greedy algorithm. |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 19 | from . import numeric_util |
| 20 | |
| 21 | |
| 22 | class GreedyAllocator: |
Tim Hall | cda4fcb | 2022-05-19 12:36:58 +0100 | [diff] [blame] | 23 | def __init__(self, live_ranges): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 24 | self.live_ranges = live_ranges |
| 25 | self.memory_required = 0 |
| 26 | |
| 27 | self.current_allocs = [] |
| 28 | |
| 29 | def alloc(self, new_lr): |
| 30 | size = new_lr.size |
| 31 | current_top = 0 |
| 32 | if self.current_allocs: |
| 33 | current_top = max(start_addr + lr.size for start_addr, lr in self.current_allocs) |
| 34 | best_offset = numeric_util.round_up(current_top, new_lr.get_alignment()) |
| 35 | best_offset_fit = (1 << 64) - 1 |
| 36 | |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame] | 37 | aligned_size = numeric_util.round_up(size, new_lr.get_alignment()) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 38 | current_offset = 0 |
| 39 | for start_addr, lr in self.current_allocs: |
| 40 | aligned_current_offset = numeric_util.round_up(current_offset, new_lr.get_alignment()) |
Henrik G Olsson | 807278a | 2021-03-08 18:20:00 +0100 | [diff] [blame] | 41 | if ( |
| 42 | aligned_current_offset + aligned_size <= start_addr |
| 43 | and start_addr - aligned_current_offset < best_offset_fit |
| 44 | ): |
| 45 | best_offset = aligned_current_offset |
| 46 | best_offset_fit = start_addr - aligned_current_offset |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 47 | |
| 48 | current_offset = start_addr + lr.size |
| 49 | |
Charles Xu | 5b3dcd7 | 2020-05-28 07:20:52 +0200 | [diff] [blame] | 50 | best_offset = new_lr.set_address(best_offset) |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame] | 51 | self.memory_required = max(self.memory_required, best_offset + aligned_size) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 52 | self.current_allocs.append((best_offset, new_lr)) |
| 53 | self.current_allocs = list(sorted(self.current_allocs)) |
| 54 | |
| 55 | def dealloc(self, lr_to_dealloc): |
| 56 | self.current_allocs = [(start_addr, lr) for start_addr, lr in self.current_allocs if lr != lr_to_dealloc] |
| 57 | |
Tim Hall | 64556f3 | 2021-05-17 22:57:46 +0100 | [diff] [blame] | 58 | def allocate_live_ranges(self, alignment): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 59 | lrs = set() |
Louis Verhaard | 226ecaf | 2021-03-30 10:18:28 +0200 | [diff] [blame] | 60 | for lr in self.live_ranges.lrs: |
Louis Verhaard | 1356c2a | 2020-09-16 10:25:28 +0200 | [diff] [blame] | 61 | lrs.add((lr.start_time, -lr.end_time, lr)) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 62 | |
| 63 | lrs = sorted(lrs) |
| 64 | |
| 65 | for curr_time, _, new_lr in lrs: |
| 66 | for _, lr in list(self.current_allocs): |
| 67 | if lr.end_time < curr_time: |
| 68 | self.dealloc(lr) |
| 69 | |
| 70 | self.alloc(new_lr) |
| 71 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 72 | return self.memory_required |
| 73 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 74 | |
Tim Hall | cda4fcb | 2022-05-19 12:36:58 +0100 | [diff] [blame] | 75 | def allocate_live_ranges(live_ranges, alignment): |
| 76 | g = GreedyAllocator(live_ranges) |
Tim Hall | 64556f3 | 2021-05-17 22:57:46 +0100 | [diff] [blame] | 77 | return g.allocate_live_ranges(alignment) |