Skip to content

Conversation

devnexen
Copy link
Member

…ions.

@llvmbot llvmbot added compiler-rt PGO Profile Guided Optimizations labels Aug 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 17, 2025

@llvm/pr-subscribers-pgo

Author: David CARLIER (devnexen)

Changes

…ions.


Full diff: https://github.com/llvm/llvm-project/pull/154011.diff

1 Files Affected:

  • (modified) compiler-rt/lib/memprof/memprof_malloc_linux.cpp (+18)
diff --git a/compiler-rt/lib/memprof/memprof_malloc_linux.cpp b/compiler-rt/lib/memprof/memprof_malloc_linux.cpp
index 2a028c7d0b489..68fe65475889a 100644
--- a/compiler-rt/lib/memprof/memprof_malloc_linux.cpp
+++ b/compiler-rt/lib/memprof/memprof_malloc_linux.cpp
@@ -50,6 +50,24 @@ INTERCEPTOR(void, cfree, void *ptr) {
 }
 #endif // SANITIZER_INTERCEPT_CFREE
 
+#if SANITIZER_INTERCEPT_FREE_SIZED
+INTERCEPTOR(void, free_sized, void *ptr, uptr size) {
+  if (DlsymAlloc::PointerIsMine(ptr))
+    return DlsymAlloc::Free(ptr);
+  GET_STACK_TRACE_FREE;
+  memprof_delete(ptr, size, 0, &stack, FROM_MALLOC);
+}
+#endif // SANITIZER_INTERCEPT_FREE_SIZED
+
+#if SANITIZER_INTERCEPT_FREE_ALIGNED_SIZED
+INTERCEPTOR(void, free_aligned_sized, void *ptr, uptr alignment, uptr size) {
+  if (DlsymAlloc::PointerIsMine(ptr))
+    return DlsymAlloc::Free(ptr);
+  GET_STACK_TRACE_FREE;
+  memprof_delete(ptr, size, alignment, &stack, FROM_MALLOC);
+}
+#endif // SANITIZER_INTERCEPT_FREE_ALIGNED_SIZED
+
 INTERCEPTOR(void *, malloc, uptr size) {
   if (DlsymAlloc::Use())
     return DlsymAlloc::Allocate(size);

@devnexen devnexen requested a review from snehasish August 17, 2025 11:24
@snehasish
Copy link

Thanks for the contribution. Is it possible to add a test which exercises these new interceptors?

@devnexen
Copy link
Member Author

devnexen commented Aug 17, 2025

It s going to be tough, those are C23 standard however, in practice, libc in general haven't really implemented it (see here for a glibc patch in the work).

Copy link

@snehasish snehasish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To test the interceptors we don't actually need them to be implemented. Here is an example where I patched in your changes and modified an existing test to demonstrate that the interceptors worked. The tests pass on Linux ELF with static and dyanmic linked compiler runtime.

#154228

Can you add a new test for these based on this approach?

Copy link

github-actions bot commented Aug 19, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@devnexen devnexen force-pushed the memprof_sized_deallocations branch 10 times, most recently from 5a17dea to 456b1a8 Compare August 19, 2025 06:29
@devnexen devnexen requested a review from snehasish August 19, 2025 06:34
Copy link

@snehasish snehasish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the test is passing because it intercepts free not the free_sized variants you are interested in. You can verify by adding some Printf logging to the new interceptors. For the test we can simply skip defining the body of the free_sized variants and the memprof runtime will provide the right (interceptor) variant.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
void free_sized(void *ptr, [[maybe_unused]] size_t) { free(ptr); }
void free_sized(void *ptr, size_t);

Comment on lines 10 to 13

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
void free_aligned_sized(void *ptr, [[maybe_unused]] size_t alignment,
[[maybe_unused]] size_t size) {
free(ptr);
}
void free_aligned_sized(void *ptr, size_t alignment, size_t size);

@devnexen devnexen force-pushed the memprof_sized_deallocations branch from 456b1a8 to 8804587 Compare August 19, 2025 22:36
Copy link

@snehasish snehasish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: alignment

@devnexen devnexen force-pushed the memprof_sized_deallocations branch from 8804587 to 4658451 Compare August 19, 2025 23:01
@devnexen devnexen force-pushed the memprof_sized_deallocations branch from 4658451 to 44b70b0 Compare August 19, 2025 23:16
@devnexen devnexen merged commit c9f0376 into llvm:main Aug 19, 2025
9 checks passed
@devnexen devnexen deleted the memprof_sized_deallocations branch August 19, 2025 23:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler-rt PGO Profile Guided Optimizations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants