Skip to content

Commit dc4f819

Browse files
committed
[compiler-rt] Add baremetal version of profile library.
Adds a flag COMPILER_RT_PROFILE_BAREMETAL, which disables the parts of the profile runtime which require a filesystem or malloc. This minimal library only requires string.h from the C library. This is useful for profiling or code coverage of baremetal images, which don't have filesystem APIs, and might not have malloc configured (or have limited heap space). There's some room for improvement here in the future for doing profiling and code coverage for baremetal. If we revised the profiling format, and introduced some additional host tooling, we could move some of the metadata into non-allocated sections, and construct the profraw file on the host. But this patch is sufficient for some use-cases.
1 parent 7e9db96 commit dc4f819

File tree

9 files changed

+40
-21
lines changed

9 files changed

+40
-21
lines changed

compiler-rt/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ option(COMPILER_RT_USE_BUILTINS_LIBRARY
309309

310310
option(COMPILER_RT_USE_ATOMIC_LIBRARY "Use compiler-rt atomic instead of libatomic" OFF)
311311

312+
option(COMPILER_RT_PROFILE_BAREMETAL "Build minimal baremetal profile library" OFF)
313+
312314
include(config-ix)
313315

314316
#================================

compiler-rt/lib/profile/CMakeLists.txt

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,9 @@ int main() {
6060
add_compiler_rt_component(profile)
6161

6262
set(PROFILE_SOURCES
63-
GCDAProfiling.c
6463
InstrProfiling.c
6564
InstrProfilingInternal.c
66-
InstrProfilingValue.c
6765
InstrProfilingBuffer.c
68-
InstrProfilingFile.c
6966
InstrProfilingMerge.c
7067
InstrProfilingMergeFile.c
7168
InstrProfilingNameVar.c
@@ -77,10 +74,25 @@ set(PROFILE_SOURCES
7774
InstrProfilingPlatformLinux.c
7875
InstrProfilingPlatformOther.c
7976
InstrProfilingPlatformWindows.c
80-
InstrProfilingRuntime.cpp
81-
InstrProfilingUtil.c
8277
)
8378

79+
if (NOT COMPILER_RT_PROFILE_BAREMETAL)
80+
# For baremetal, exclude the following:
81+
# - Anything that contains filesystem operations (InstrProfilingFile.c,
82+
# InstrProfilingUtils.c)
83+
# - Initialization, because it isn't necesary without the filesystem bits
84+
# on ELF targets (InstrProfilingRuntime.cpp).
85+
# - Value profiling, because it requires malloc (InstrProfilingValue.c).
86+
# This could be optional if someone needs it.
87+
# - GCDA profiling, which is unrelated (GCDAProfiling.c)
88+
list(APPEND PROFILE_SOURCES GCDAProfiling.c
89+
InstrProfilingFile.c
90+
InstrProfilingRuntime.cpp
91+
InstrProfilingUtil.c
92+
InstrProfilingValue.c
93+
)
94+
endif()
95+
8496
set(PROFILE_HEADERS
8597
InstrProfiling.h
8698
InstrProfilingInternal.h
@@ -135,6 +147,12 @@ if(COMPILER_RT_TARGET_HAS_UNAME)
135147
-DCOMPILER_RT_HAS_UNAME=1)
136148
endif()
137149

150+
if(COMPILER_RT_PROFILE_BAREMETAL)
151+
set(EXTRA_FLAGS
152+
${EXTRA_FLAGS}
153+
-DCOMPILER_RT_PROFILE_BAREMETAL=1)
154+
endif()
155+
138156
if(MSVC)
139157
# profile historically has only been supported with the static runtime
140158
# on windows

compiler-rt/lib/profile/InstrProfiling.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
// with freestanding compilation. See `darwin_add_builtin_libraries`.
1111

1212
#include <limits.h>
13-
#include <stdio.h>
14-
#include <stdlib.h>
1513
#include <string.h>
1614

1715
#include "InstrProfiling.h"

compiler-rt/lib/profile/InstrProfiling.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
#define PROFILE_INSTRPROFILING_H_
1111

1212
#include "InstrProfilingPort.h"
13+
#include <stddef.h>
14+
#ifndef COMPILER_RT_PROFILE_BAREMETAL
1315
#include <stdio.h>
16+
#endif
1417

1518
// Make sure __LLVM_INSTR_PROFILE_GENERATE is always defined before
1619
// including instr_prof_interface.h so the interface functions are
@@ -200,7 +203,9 @@ int __llvm_profile_write_file(void);
200203
* copying the old profile file to new profile file and this function is usually
201204
* used when the proess doesn't have permission to open file.
202205
*/
206+
#ifndef COMPILER_RT_PROFILE_BAREMETAL
203207
int __llvm_profile_set_file_object(FILE *File, int EnableMerge);
208+
#endif
204209

205210
/*! \brief Register to write instrumentation data to file at exit. */
206211
int __llvm_profile_register_write_file_atexit(void);

compiler-rt/lib/profile/InstrProfilingMerge.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#include "InstrProfiling.h"
1313
#include "InstrProfilingInternal.h"
14-
#include "InstrProfilingUtil.h"
1514

1615
#define INSTR_PROF_VALUE_PROF_DATA
1716
#include "profile/InstrProfData.inc"
@@ -131,9 +130,11 @@ COMPILER_RT_VISIBILITY
131130
int __llvm_profile_merge_from_buffer(const char *ProfileData,
132131
uint64_t ProfileSize) {
133132
if (__llvm_profile_get_version() & VARIANT_MASK_TEMPORAL_PROF) {
133+
#ifndef COMPILER_RT_PROFILE_BAREMETAL
134134
PROF_ERR("%s\n",
135135
"Temporal profiles do not support profile merging at runtime. "
136136
"Instead, merge raw profiles using the llvm-profdata tool.");
137+
#endif
137138
return 1;
138139
}
139140

compiler-rt/lib/profile/InstrProfilingMergeFile.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
#include "InstrProfiling.h"
1515
#include "InstrProfilingInternal.h"
16-
#include "InstrProfilingUtil.h"
1716

1817
#define INSTR_PROF_VALUE_PROF_DATA
1918
#include "profile/InstrProfData.inc"

compiler-rt/lib/profile/InstrProfilingPlatformLinux.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@
88

99
#if defined(__linux__) || defined(__FreeBSD__) || defined(__Fuchsia__) || \
1010
(defined(__sun__) && defined(__svr4__)) || defined(__NetBSD__) || \
11-
defined(_AIX) || defined(__wasm__) || defined(__HAIKU__)
11+
defined(_AIX) || defined(__wasm__) || defined(__HAIKU__) || \
12+
defined(COMPILER_RT_PROFILE_BAREMETAL)
1213

14+
#ifndef COMPILER_RT_PROFILE_BAREMETAL
1315
#if !defined(_AIX) && !defined(__wasm__)
1416
#include <elf.h>
1517
#include <link.h>
1618
#endif
1719
#include <stdlib.h>
1820
#include <string.h>
21+
#endif
1922

2023
#include "InstrProfiling.h"
2124
#include "InstrProfilingInternal.h"

compiler-rt/lib/profile/InstrProfilingPlatformOther.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
#if !defined(__APPLE__) && !defined(__linux__) && !defined(__FreeBSD__) && \
1010
!defined(__Fuchsia__) && !(defined(__sun__) && defined(__svr4__)) && \
1111
!defined(__NetBSD__) && !defined(_WIN32) && !defined(_AIX) && \
12-
!defined(__wasm__) && !defined(__HAIKU__)
12+
!defined(__wasm__) && !defined(__HAIKU__) && \
13+
!defined(COMPILER_RT_PROFILE_BAREMETAL)
1314

1415
#include <stdlib.h>
1516
#include <stdio.h>

compiler-rt/lib/profile/InstrProfilingPort.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ static inline size_t getpagesize(void) {
117117
return S.dwPageSize;
118118
}
119119
#else /* defined(_WIN32) */
120+
#ifndef COMPILER_RT_PROFILE_BAREMETAL
120121
#include <unistd.h>
122+
#endif
121123
#endif /* defined(_WIN32) */
122124

123125
#define PROF_ERR(Format, ...) \
@@ -137,16 +139,6 @@ static inline size_t getpagesize(void) {
137139
#define O_BINARY 0
138140
#endif
139141

140-
#if defined(__FreeBSD__)
141-
142-
#include <inttypes.h>
143-
#include <sys/types.h>
144-
145-
#else /* defined(__FreeBSD__) */
146-
147-
#include <inttypes.h>
148142
#include <stdint.h>
149143

150-
#endif /* defined(__FreeBSD__) && defined(__i386__) */
151-
152144
#endif /* PROFILE_INSTRPROFILING_PORT_H_ */

0 commit comments

Comments
 (0)