Skip to content

Commit 0e29fdf

Browse files
art049claude
andcommitted
fix(callgrind/tests): prevent printf inlining with noinline wrapper
Wrap printf calls in a __attribute__((noinline)) function to prevent glibc from inlining printf via stdio2.h. This ensures the inline tests only capture the intended inline function calls. The filter_inline script now skips my_printf wrapper function calls to keep the test output focused on the functions being tested. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent f0bcf4a commit 0e29fdf

4 files changed

Lines changed: 28 additions & 5 deletions

File tree

callgrind/tests/Makefile.am

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ AM_CFLAGS += $(AM_FLAG_M3264_PRI)
3434
AM_CXXFLAGS += $(AM_FLAG_M3264_PRI)
3535

3636
# Inline tests need -O2 to enable inlining and -g for debug info
37-
# Use -fno-builtin-printf to prevent compiler from inlining printf
38-
inline_samefile_CFLAGS = $(AM_CFLAGS) -O2 -g -fno-builtin-printf
39-
inline_crossfile_CFLAGS = $(AM_CFLAGS) -O2 -g -fno-builtin-printf
37+
inline_samefile_CFLAGS = $(AM_CFLAGS) -O2 -g
38+
inline_crossfile_CFLAGS = $(AM_CFLAGS) -O2 -g
4039

4140
threads_LDADD = -lpthread

callgrind/tests/filter_inline

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ in_main {
3333
else if (/^cfn=0x/) {
3434
# Skip
3535
}
36+
# Skip my_printf wrapper function calls
37+
else if (/^cfn=my_printf/) {
38+
# Skip
39+
}
3640
else {
3741
print $0
3842
}

callgrind/tests/inline-crossfile.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
#include <stdio.h>
2+
#include <stdarg.h>
23
#include "inline-crossfile-helper1.h"
34
#include "inline-crossfile-helper2.h"
45

6+
// Non-inlinable printf wrapper to prevent glibc from inlining printf
7+
__attribute__((noinline))
8+
static void my_printf(const char *fmt, ...) {
9+
va_list args;
10+
va_start(args, fmt);
11+
vprintf(fmt, args);
12+
va_end(args);
13+
}
14+
515
int main(int argc, char **argv) {
616
int n = 5 + (argc - 1); // n = 5 when argc = 1
717

@@ -11,7 +21,7 @@ int main(int argc, char **argv) {
1121
// Call second inline function from helper2.h
1222
int prod = compute_product(n);
1323

14-
printf("sum=%d, product=%d\n", sum, prod);
24+
my_printf("sum=%d, product=%d\n", sum, prod);
1525

1626
return 0;
1727
}

callgrind/tests/inline-samefile.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
#include <stdio.h>
2+
#include <stdarg.h>
3+
4+
// Non-inlinable printf wrapper to prevent glibc from inlining printf
5+
__attribute__((noinline))
6+
static void my_printf(const char *fmt, ...) {
7+
va_list args;
8+
va_start(args, fmt);
9+
vprintf(fmt, args);
10+
va_end(args);
11+
}
212

313
// Function 1: Iterative fibonacci - WILL be inlined
414
static inline int fibonacci_iterative(int n) {
@@ -34,7 +44,7 @@ int main(int argc, char **argv) {
3444
// Call the recursive fibonacci (should NOT be inlined)
3545
int fib_rec = fibonacci_recursive(10);
3646

37-
printf("Iterative fib(%d): %d, Recursive fib(10): %d\n", n, fib_iter, fib_rec);
47+
my_printf("Iterative fib(%d): %d, Recursive fib(10): %d\n", n, fib_iter, fib_rec);
3848

3949
return 0;
4050
}

0 commit comments

Comments
 (0)