blob: f6d7a3a19eb1de796c71a63dc32c333607840d41 [file] [log] [blame]
Tim Hall79d07d22020-04-27 18:20:16 +01001# Copyright (C) 2020 Arm Limited or its affiliates. All rights reserved.
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.
Tim Hall79d07d22020-04-27 18:20:16 +010016# Description:
17# Allocate tensor addresses using a greedy algorithm.
Tim Hall79d07d22020-04-27 18:20:16 +010018from . import numeric_util
19
20
21class GreedyAllocator:
Tim Hallcda4fcb2022-05-19 12:36:58 +010022 def __init__(self, live_ranges):
Tim Hall79d07d22020-04-27 18:20:16 +010023 self.live_ranges = live_ranges
24 self.memory_required = 0
25
26 self.current_allocs = []
27
28 def alloc(self, new_lr):
29 size = new_lr.size
30 current_top = 0
31 if self.current_allocs:
32 current_top = max(start_addr + lr.size for start_addr, lr in self.current_allocs)
33 best_offset = numeric_util.round_up(current_top, new_lr.get_alignment())
34 best_offset_fit = (1 << 64) - 1
35
Jacob Bohlin0628a8c2020-08-28 13:25:14 +020036 aligned_size = numeric_util.round_up(size, new_lr.get_alignment())
Tim Hall79d07d22020-04-27 18:20:16 +010037 current_offset = 0
38 for start_addr, lr in self.current_allocs:
39 aligned_current_offset = numeric_util.round_up(current_offset, new_lr.get_alignment())
Henrik G Olsson807278a2021-03-08 18:20:00 +010040 if (
41 aligned_current_offset + aligned_size <= start_addr
42 and start_addr - aligned_current_offset < best_offset_fit
43 ):
44 best_offset = aligned_current_offset
45 best_offset_fit = start_addr - aligned_current_offset
Tim Hall79d07d22020-04-27 18:20:16 +010046
47 current_offset = start_addr + lr.size
48
Charles Xu5b3dcd72020-05-28 07:20:52 +020049 best_offset = new_lr.set_address(best_offset)
Jacob Bohlin0628a8c2020-08-28 13:25:14 +020050 self.memory_required = max(self.memory_required, best_offset + aligned_size)
Tim Hall79d07d22020-04-27 18:20:16 +010051 self.current_allocs.append((best_offset, new_lr))
52 self.current_allocs = list(sorted(self.current_allocs))
53
54 def dealloc(self, lr_to_dealloc):
55 self.current_allocs = [(start_addr, lr) for start_addr, lr in self.current_allocs if lr != lr_to_dealloc]
56
Tim Hall64556f32021-05-17 22:57:46 +010057 def allocate_live_ranges(self, alignment):
Tim Hall79d07d22020-04-27 18:20:16 +010058 lrs = set()
Louis Verhaard226ecaf2021-03-30 10:18:28 +020059 for lr in self.live_ranges.lrs:
Louis Verhaard1356c2a2020-09-16 10:25:28 +020060 lrs.add((lr.start_time, -lr.end_time, lr))
Tim Hall79d07d22020-04-27 18:20:16 +010061
62 lrs = sorted(lrs)
63
64 for curr_time, _, new_lr in lrs:
65 for _, lr in list(self.current_allocs):
66 if lr.end_time < curr_time:
67 self.dealloc(lr)
68
69 self.alloc(new_lr)
70
Tim Hall79d07d22020-04-27 18:20:16 +010071 return self.memory_required
72
Tim Hall79d07d22020-04-27 18:20:16 +010073
Tim Hallcda4fcb2022-05-19 12:36:58 +010074def allocate_live_ranges(live_ranges, alignment):
75 g = GreedyAllocator(live_ranges)
Tim Hall64556f32021-05-17 22:57:46 +010076 return g.allocate_live_ranges(alignment)