-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tighten alignment promises for halide_malloc() #7222
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
#include "runtime_internal.h" | ||
|
||
extern "C" WEAK_INLINE int halide_malloc_alignment() { | ||
extern "C" { | ||
|
||
WEAK_INLINE int halide_internal_malloc_alignment() { | ||
return 128; | ||
} | ||
} // extern "C" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
#include "runtime_internal.h" | ||
|
||
extern "C" WEAK_INLINE int halide_malloc_alignment() { | ||
extern "C" { | ||
|
||
WEAK_INLINE int halide_internal_malloc_alignment() { | ||
return 32; | ||
} | ||
} // extern "C" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
#include "runtime_internal.h" | ||
|
||
extern "C" WEAK_INLINE int halide_malloc_alignment() { | ||
extern "C" { | ||
|
||
WEAK_INLINE int halide_internal_malloc_alignment() { | ||
return 64; | ||
} | ||
} // extern "C" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include "HalideRuntime.h" | ||
#include "runtime_internal.h" | ||
|
||
extern "C" { | ||
|
||
extern void *malloc(size_t); | ||
extern void free(void *); | ||
|
||
// An implementation of aligned_alloc() that is layered on top of malloc()/free(). | ||
WEAK_INLINE void *halide_internal_aligned_alloc(size_t alignment, size_t size) { | ||
// Alignment must be a power of two and >= sizeof(void*) | ||
halide_debug_assert(nullptr, is_power_of_two(alignment) && alignment >= sizeof(void *)); | ||
|
||
// Allocate enough space for aligning the pointer we return. | ||
// | ||
// Always round allocations up to alignment size, | ||
// so that all allocators follow the behavior of aligned_alloc() and | ||
// return aligned pointer *and* aligned length. | ||
const size_t aligned_size = align_up(size + alignment, alignment); | ||
|
||
void *orig = ::malloc(aligned_size); | ||
if (orig == nullptr) { | ||
// Will result in a failed assertion and a call to halide_error | ||
return nullptr; | ||
} | ||
|
||
// malloc() and friends must return a pointer aligned to at least | ||
// alignof(std::max_align_t); we can't reasonably check that in | ||
// the runtime, but we can realistically assume it's at least | ||
// 8-aligned. | ||
halide_debug_assert(nullptr, (((uintptr_t)orig) % 8) == 0); | ||
|
||
// We want to store the original pointer prior to the pointer we return. | ||
void *ptr = (void *)align_up((uintptr_t)orig + sizeof(void *), alignment); | ||
((void **)ptr)[-1] = orig; | ||
return ptr; | ||
} | ||
|
||
WEAK_INLINE void halide_internal_aligned_free(void *ptr) { | ||
::free(((void **)ptr)[-1]); | ||
} | ||
|
||
} // extern "C" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we decided this "additionally" sentence wasn't necessary now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have evidence to the contrary; I have at least one test case were we access 4 bytes before the allocation start. (Your pending work should fix this, but until it lands, the comment is still accurate.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My conclusion from examining CodeGen_LLVM's load handling was that the offset downwards could only every improve alignment relative to the allocation base, so underreads were impossible. It's possible that the underread you found is a different bug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or my reading was wrong - it's tricky code. One way to check is to see if my strided load branch heals the underread.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll go back and doublecheck, but we are definitely underreading in this particular case (verified by ASAN); whether or not its an unrelated bug, we want to fix it. (Not sure how hard a repro case will be....)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the more reason to get rid of it :-)