Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions apps/hannk/interpreter/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
#include <set>
#include <unordered_set>

// TODO: apparently not part of the public Halide API. Should it be?
extern "C" int halide_malloc_alignment();

namespace hannk {

Interpreter::Interpreter(OpPtr m, InterpreterOptions options)
Expand Down
12 changes: 7 additions & 5 deletions src/runtime/HalideRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,13 +364,15 @@ extern int halide_set_num_threads(int n);
* halide_default_malloc/free.
*
* Note that halide_malloc must return a pointer aligned to the
* maximum meaningful alignment for the platform for the purpose of
* vector loads and stores. The default implementation uses 32-byte
* alignment, which is safe for arm and x86. Additionally, it must be
* safe to read at least 8 bytes before the start and beyond the
* end.
* the value returned by halide_malloc_alignment().
*
* The default implementation of halide_malloc_alignment() will be chosen to be
* the maximum meaningful alignment for the platform for the purpose of
* vector loads and stores for a given architecture. You may override this
* function, but bear in mind that it must return the same value for every call.
*/
//@{
extern int halide_malloc_alignment();
extern void *halide_malloc(void *user_context, size_t x);
extern void halide_free(void *user_context, void *ptr);
extern void *halide_default_malloc(void *user_context, size_t x);
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/alignment_128.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "runtime_internal.h"

extern "C" WEAK_INLINE int halide_malloc_alignment() {
extern "C" WEAK int halide_malloc_alignment() {
return 128;
}
2 changes: 1 addition & 1 deletion src/runtime/alignment_32.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "runtime_internal.h"

extern "C" WEAK_INLINE int halide_malloc_alignment() {
extern "C" WEAK int halide_malloc_alignment() {
return 32;
}
2 changes: 1 addition & 1 deletion src/runtime/alignment_64.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "runtime_internal.h"

extern "C" WEAK_INLINE int halide_malloc_alignment() {
extern "C" WEAK int halide_malloc_alignment() {
return 64;
}
15 changes: 14 additions & 1 deletion src/runtime/posix_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,27 @@

#include "printer.h"

namespace Halide {
namespace Runtime {
namespace Internal {

// Read into a global to avoid making a call to halide_malloc_alignment()
// in every halide_malloc() call (halide_malloc_alignment() is required to
// return the same value every time).
WEAK size_t _alignment = (size_t)halide_malloc_alignment();

} // namespace Internal
} // namespace Runtime
} // namespace Halide

extern "C" {

extern void *malloc(size_t);
extern void free(void *);

WEAK void *halide_default_malloc(void *user_context, size_t x) {
// Allocate enough space for aligning the pointer we return.
const size_t alignment = halide_malloc_alignment();
const size_t alignment = _alignment;
void *orig = malloc(x + alignment);
if (orig == nullptr) {
// Will result in a failed assertion and a call to halide_error
Expand Down
8 changes: 6 additions & 2 deletions src/runtime/qurt_allocator.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "HalideRuntime.h"

extern "C" {

extern void *malloc(size_t);
extern void free(void *);
}
Expand Down Expand Up @@ -54,13 +53,18 @@ WEAK __attribute__((destructor)) void halide_allocator_cleanup() {
}
}

// Read into a global to avoid making a call to halide_malloc_alignment()
// in every halide_malloc() call (halide_malloc_alignment() is required to
// return the same value every time).
WEAK size_t _alignment = (size_t)halide_malloc_alignment();

} // namespace Internal
} // namespace Runtime
} // namespace Halide

WEAK void *halide_default_malloc(void *user_context, size_t x) {
// Hexagon needs up to 128 byte alignment.
const size_t alignment = 128;
const size_t alignment = Halide::Runtime::Internal::_alignment;

if (x <= buffer_size) {
for (int i = 0; i < num_buffers; ++i) {
Expand Down
1 change: 1 addition & 0 deletions src/runtime/runtime_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ extern "C" __attribute__((used)) void *halide_runtime_api_functions[] = {
(void *)&halide_join_thread,
(void *)&halide_load_library,
(void *)&halide_malloc,
(void *)&halide_malloc_alignment,
(void *)&halide_memoization_cache_cleanup,
(void *)&halide_memoization_cache_evict,
(void *)&halide_memoization_cache_lookup,
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/runtime_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ struct halide_pseudostack_slot_t {
WEAK void halide_use_jit_module();
WEAK void halide_release_jit_module();

WEAK_INLINE int halide_malloc_alignment();
WEAK int halide_malloc_alignment();

void halide_thread_yield();

Expand Down