xref: /aosp_15_r20/external/compiler-rt/lib/profile/InstrProfilingPort.h (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot /*===- InstrProfilingPort.h- Support library for PGO instrumentation ------===*\
2*7c3d14c8STreehugger Robot |*
3*7c3d14c8STreehugger Robot |*                     The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot |*
5*7c3d14c8STreehugger Robot |* This file is distributed under the University of Illinois Open Source
6*7c3d14c8STreehugger Robot |* License. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot |*
8*7c3d14c8STreehugger Robot \*===----------------------------------------------------------------------===*/
9*7c3d14c8STreehugger Robot 
10*7c3d14c8STreehugger Robot #ifndef PROFILE_INSTRPROFILING_PORT_H_
11*7c3d14c8STreehugger Robot #define PROFILE_INSTRPROFILING_PORT_H_
12*7c3d14c8STreehugger Robot 
13*7c3d14c8STreehugger Robot #ifdef _MSC_VER
14*7c3d14c8STreehugger Robot #define COMPILER_RT_ALIGNAS(x) __declspec(align(x))
15*7c3d14c8STreehugger Robot #define COMPILER_RT_VISIBILITY
16*7c3d14c8STreehugger Robot /* FIXME: selectany does not have the same semantics as weak. */
17*7c3d14c8STreehugger Robot #define COMPILER_RT_WEAK __declspec(selectany)
18*7c3d14c8STreehugger Robot /* Need to include <windows.h> */
19*7c3d14c8STreehugger Robot #define COMPILER_RT_ALLOCA _alloca
20*7c3d14c8STreehugger Robot /* Need to include <stdio.h> and <io.h> */
21*7c3d14c8STreehugger Robot #define COMPILER_RT_FTRUNCATE(f,l) _chsize(_fileno(f),l)
22*7c3d14c8STreehugger Robot #elif __GNUC__
23*7c3d14c8STreehugger Robot #define COMPILER_RT_ALIGNAS(x) __attribute__((aligned(x)))
24*7c3d14c8STreehugger Robot #define COMPILER_RT_VISIBILITY __attribute__((visibility("hidden")))
25*7c3d14c8STreehugger Robot #define COMPILER_RT_WEAK __attribute__((weak))
26*7c3d14c8STreehugger Robot #define COMPILER_RT_ALLOCA __builtin_alloca
27*7c3d14c8STreehugger Robot #define COMPILER_RT_FTRUNCATE(f,l) ftruncate(fileno(f),l)
28*7c3d14c8STreehugger Robot #endif
29*7c3d14c8STreehugger Robot 
30*7c3d14c8STreehugger Robot #if defined(__APPLE__)
31*7c3d14c8STreehugger Robot #define COMPILER_RT_SEG "__DATA,"
32*7c3d14c8STreehugger Robot #else
33*7c3d14c8STreehugger Robot #define COMPILER_RT_SEG ""
34*7c3d14c8STreehugger Robot #endif
35*7c3d14c8STreehugger Robot 
36*7c3d14c8STreehugger Robot #ifdef _MSC_VER
37*7c3d14c8STreehugger Robot #define COMPILER_RT_SECTION(Sect) __declspec(allocate(Sect))
38*7c3d14c8STreehugger Robot #else
39*7c3d14c8STreehugger Robot #define COMPILER_RT_SECTION(Sect) __attribute__((section(Sect)))
40*7c3d14c8STreehugger Robot #endif
41*7c3d14c8STreehugger Robot 
42*7c3d14c8STreehugger Robot #define COMPILER_RT_MAX_HOSTLEN 128
43*7c3d14c8STreehugger Robot #ifdef _MSC_VER
44*7c3d14c8STreehugger Robot #define COMPILER_RT_GETHOSTNAME(Name, Len) gethostname(Name, Len)
45*7c3d14c8STreehugger Robot #elif defined(__ORBIS__)
46*7c3d14c8STreehugger Robot #define COMPILER_RT_GETHOSTNAME(Name, Len) ((void)(Name), (void)(Len), (-1))
47*7c3d14c8STreehugger Robot #else
48*7c3d14c8STreehugger Robot #define COMPILER_RT_GETHOSTNAME(Name, Len) lprofGetHostName(Name, Len)
49*7c3d14c8STreehugger Robot #define COMPILER_RT_HAS_UNAME 1
50*7c3d14c8STreehugger Robot #endif
51*7c3d14c8STreehugger Robot 
52*7c3d14c8STreehugger Robot #if COMPILER_RT_HAS_ATOMICS == 1
53*7c3d14c8STreehugger Robot #ifdef _MSC_VER
54*7c3d14c8STreehugger Robot #include <windows.h>
55*7c3d14c8STreehugger Robot #if _MSC_VER < 1900
56*7c3d14c8STreehugger Robot #define snprintf _snprintf
57*7c3d14c8STreehugger Robot #endif
58*7c3d14c8STreehugger Robot #if defined(_WIN64)
59*7c3d14c8STreehugger Robot #define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV)                              \
60*7c3d14c8STreehugger Robot   (InterlockedCompareExchange64((LONGLONG volatile *)Ptr, (LONGLONG)NewV,      \
61*7c3d14c8STreehugger Robot                                 (LONGLONG)OldV) == (LONGLONG)OldV)
62*7c3d14c8STreehugger Robot #define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr)                    \
63*7c3d14c8STreehugger Robot   (DomType *)InterlockedExchangeAdd64((LONGLONG volatile *)&PtrVar,            \
64*7c3d14c8STreehugger Robot                                       (LONGLONG)sizeof(DomType) * PtrIncr)
65*7c3d14c8STreehugger Robot #else /* !defined(_WIN64) */
66*7c3d14c8STreehugger Robot #define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV)                              \
67*7c3d14c8STreehugger Robot   (InterlockedCompareExchange((LONG volatile *)Ptr, (LONG)NewV, (LONG)OldV) == \
68*7c3d14c8STreehugger Robot    (LONG)OldV)
69*7c3d14c8STreehugger Robot #define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr)                    \
70*7c3d14c8STreehugger Robot   (DomType *)InterlockedExchangeAdd((LONG volatile *)&PtrVar,                  \
71*7c3d14c8STreehugger Robot                                     (LONG)sizeof(DomType) * PtrIncr)
72*7c3d14c8STreehugger Robot #endif
73*7c3d14c8STreehugger Robot #else /* !defined(_MSC_VER) */
74*7c3d14c8STreehugger Robot #define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV)                              \
75*7c3d14c8STreehugger Robot   __sync_bool_compare_and_swap(Ptr, OldV, NewV)
76*7c3d14c8STreehugger Robot #define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr)                    \
77*7c3d14c8STreehugger Robot   (DomType *)__sync_fetch_and_add((long *)&PtrVar, sizeof(DomType) * PtrIncr)
78*7c3d14c8STreehugger Robot #endif
79*7c3d14c8STreehugger Robot #else /* COMPILER_RT_HAS_ATOMICS != 1 */
80*7c3d14c8STreehugger Robot #include "InstrProfilingUtil.h"
81*7c3d14c8STreehugger Robot #define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV)                              \
82*7c3d14c8STreehugger Robot   lprofBoolCmpXchg((void **)Ptr, OldV, NewV)
83*7c3d14c8STreehugger Robot #define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr)                    \
84*7c3d14c8STreehugger Robot   (DomType *)lprofPtrFetchAdd((void **)&PtrVar, sizeof(DomType) * PtrIncr)
85*7c3d14c8STreehugger Robot #endif
86*7c3d14c8STreehugger Robot 
87*7c3d14c8STreehugger Robot #define PROF_ERR(Format, ...)                                                  \
88*7c3d14c8STreehugger Robot   fprintf(stderr, "LLVM Profile Error: " Format, __VA_ARGS__);
89*7c3d14c8STreehugger Robot 
90*7c3d14c8STreehugger Robot #define PROF_WARN(Format, ...)                                                 \
91*7c3d14c8STreehugger Robot   fprintf(stderr, "LLVM Profile Warning: " Format, __VA_ARGS__);
92*7c3d14c8STreehugger Robot 
93*7c3d14c8STreehugger Robot #define PROF_NOTE(Format, ...)                                                 \
94*7c3d14c8STreehugger Robot   fprintf(stderr, "LLVM Profile Note: " Format, __VA_ARGS__);
95*7c3d14c8STreehugger Robot 
96*7c3d14c8STreehugger Robot #if defined(__FreeBSD__)
97*7c3d14c8STreehugger Robot 
98*7c3d14c8STreehugger Robot #include <inttypes.h>
99*7c3d14c8STreehugger Robot #include <sys/types.h>
100*7c3d14c8STreehugger Robot 
101*7c3d14c8STreehugger Robot #else /* defined(__FreeBSD__) */
102*7c3d14c8STreehugger Robot 
103*7c3d14c8STreehugger Robot #include <inttypes.h>
104*7c3d14c8STreehugger Robot #include <stdint.h>
105*7c3d14c8STreehugger Robot 
106*7c3d14c8STreehugger Robot #endif /* defined(__FreeBSD__) && defined(__i386__) */
107*7c3d14c8STreehugger Robot 
108*7c3d14c8STreehugger Robot #endif /* PROFILE_INSTRPROFILING_PORT_H_ */
109