forked from iree-org/iree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap_buffer.cc
190 lines (160 loc) · 7.27 KB
/
heap_buffer.cc
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "third_party/mlir_edge/iree/hal/heap_buffer.h"
#include <cstdint>
#include <cstdlib>
#include <string>
#include <utility>
#include "third_party/absl/types/source_location.h"
#include "third_party/mlir_edge/iree/base/status.h"
#include "third_party/mlir_edge/iree/base/tracing.h"
#include "third_party/mlir_edge/iree/hal/allocator.h"
#include "third_party/mlir_edge/iree/hal/host/host_buffer.h"
namespace iree {
namespace hal {
namespace {
// An allocator that allocates or wraps host-only buffers.
// The resulting buffers are not usable by most devices without a copy and
// using a device allocator is strongly preferred.
class HeapAllocator : public Allocator {
public:
// Returns a singleton heap allocator that can provide buffers that have
// MemoryType::kHostLocal and are allocated with malloc/free.
// These buffers will not be usable by devices directly and may incur
// additional copies.
static Allocator* std_heap();
// TODO(benvanik): specify custom allocator (not malloc/free).
HeapAllocator();
~HeapAllocator() override;
bool CanUseBufferLike(Allocator* source_allocator,
MemoryTypeBitfield memory_type,
BufferUsageBitfield buffer_usage,
BufferUsageBitfield intended_usage) const override;
bool CanAllocate(MemoryTypeBitfield memory_type,
BufferUsageBitfield buffer_usage,
size_t allocation_size) const override;
StatusOr<ref_ptr<Buffer>> Allocate(MemoryTypeBitfield memory_type,
BufferUsageBitfield buffer_usage,
size_t allocation_size) override;
StatusOr<ref_ptr<Buffer>> WrapMutable(MemoryTypeBitfield memory_type,
MemoryAccessBitfield allowed_access,
BufferUsageBitfield buffer_usage,
void* data,
size_t data_length) override;
};
// static
Allocator* HeapAllocator::std_heap() {
static Allocator* std_heap_allocator = new HeapAllocator();
return std_heap_allocator;
}
HeapAllocator::HeapAllocator() = default;
HeapAllocator::~HeapAllocator() = default;
bool HeapAllocator::CanUseBufferLike(Allocator* source_allocator,
MemoryTypeBitfield memory_type,
BufferUsageBitfield buffer_usage,
BufferUsageBitfield intended_usage) const {
// The host can use anything with kHostVisible.
if (!AnyBitSet(memory_type & MemoryType::kHostVisible)) {
return false;
}
// Host currently uses mapping to copy buffers, which is done a lot.
if (!AnyBitSet(buffer_usage & BufferUsage::kMapping)) {
return false;
}
return true;
}
bool HeapAllocator::CanAllocate(MemoryTypeBitfield memory_type,
BufferUsageBitfield buffer_usage,
size_t allocation_size) const {
// This host only allocator cannot serve device visible allocation as we
// can't know which devices these buffers will be used with.
return (memory_type & MemoryType::kHostLocal) == MemoryType::kHostLocal &&
!AnyBitSet(memory_type & MemoryType::kDeviceLocal) &&
!AnyBitSet(memory_type & MemoryType::kDeviceVisible);
}
StatusOr<ref_ptr<Buffer>> HeapAllocator::Allocate(
MemoryTypeBitfield memory_type, BufferUsageBitfield buffer_usage,
size_t allocation_size) {
IREE_TRACE_SCOPE0("HeapAllocator::Allocate");
if (!CanAllocate(memory_type, buffer_usage, allocation_size)) {
return FailedPreconditionErrorBuilder(ABSL_LOC)
<< "Allocation not supported; memory_type="
<< MemoryTypeString(memory_type)
<< ", buffer_usage=" << BufferUsageString(buffer_usage)
<< ", allocation_size=" << allocation_size;
}
void* malloced_data = std::calloc(1, allocation_size);
if (!malloced_data) {
return ResourceExhaustedErrorBuilder(ABSL_LOC)
<< "Failed to malloc " << allocation_size << " bytes";
}
auto buffer =
make_ref<HostBuffer>(this, memory_type, MemoryAccess::kAll, buffer_usage,
allocation_size, malloced_data, true);
return buffer;
}
StatusOr<ref_ptr<Buffer>> HeapAllocator::WrapMutable(
MemoryTypeBitfield memory_type, MemoryAccessBitfield allowed_access,
BufferUsageBitfield buffer_usage, void* data, size_t data_length) {
auto buffer = make_ref<HostBuffer>(this, memory_type, allowed_access,
buffer_usage, data_length, data, false);
return buffer;
}
} // namespace
// static
ref_ptr<Buffer> HeapBuffer::Allocate(MemoryTypeBitfield memory_type,
BufferUsageBitfield usage,
size_t allocation_size) {
auto buffer_or =
HeapAllocator::std_heap()->Allocate(memory_type, usage, allocation_size);
return std::move(buffer_or.ValueOrDie());
}
// static
ref_ptr<Buffer> HeapBuffer::AllocateCopy(BufferUsageBitfield usage,
const void* data, size_t data_length) {
return AllocateCopy(usage, MemoryAccess::kAll, data, data_length);
}
// static
ref_ptr<Buffer> HeapBuffer::AllocateCopy(BufferUsageBitfield usage,
MemoryAccessBitfield allowed_access,
const void* data, size_t data_length) {
IREE_TRACE_SCOPE0("HeapBuffer::AllocateCopy");
// Ensure we can map so that we can copy into it.
usage |= BufferUsage::kMapping;
auto buffer_or = HeapAllocator::std_heap()->Allocate(MemoryType::kHostLocal,
usage, data_length);
auto buffer = std::move(buffer_or.ValueOrDie());
buffer->WriteData(0, data, data_length).IgnoreError();
buffer->set_allowed_access(allowed_access);
return buffer;
}
// static
ref_ptr<Buffer> HeapBuffer::Wrap(MemoryTypeBitfield memory_type,
BufferUsageBitfield usage, const void* data,
size_t data_length) {
auto buffer_or =
HeapAllocator::std_heap()->Wrap(memory_type, usage, data, data_length);
return std::move(buffer_or.ValueOrDie());
}
// static
ref_ptr<Buffer> HeapBuffer::WrapMutable(MemoryTypeBitfield memory_type,
MemoryAccessBitfield allowed_access,
BufferUsageBitfield usage, void* data,
size_t data_length) {
auto buffer_or = HeapAllocator::std_heap()->WrapMutable(
memory_type, allowed_access, usage, data, data_length);
return std::move(buffer_or.ValueOrDie());
}
} // namespace hal
} // namespace iree