Path: blob/main/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingPort.h
35233 views
/*===- InstrProfilingPort.h- Support library for PGO instrumentation ------===*\1|*2|* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3|* See https://llvm.org/LICENSE.txt for license information.4|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5|*6\*===----------------------------------------------------------------------===*/78/* This header must be included after all others so it can provide fallback9definitions for stuff missing in system headers. */1011#ifndef PROFILE_INSTRPROFILING_PORT_H_12#define PROFILE_INSTRPROFILING_PORT_H_1314#ifdef _MSC_VER15#define COMPILER_RT_ALIGNAS(x) __declspec(align(x))16#define COMPILER_RT_VISIBILITY17/* FIXME: selectany does not have the same semantics as weak. */18#define COMPILER_RT_WEAK __declspec(selectany)19/* Need to include <windows.h> */20#define COMPILER_RT_ALLOCA _alloca21/* Need to include <stdio.h> and <io.h> */22#define COMPILER_RT_FTRUNCATE(f,l) _chsize(_fileno(f),l)23#define COMPILER_RT_ALWAYS_INLINE __forceinline24#define COMPILER_RT_CLEANUP(x)25#define COMPILER_RT_USED26#elif __GNUC__27#ifdef _WIN3228#define COMPILER_RT_FTRUNCATE(f, l) _chsize(fileno(f), l)29#define COMPILER_RT_VISIBILITY30#define COMPILER_RT_WEAK __attribute__((selectany))31#else32#define COMPILER_RT_FTRUNCATE(f, l) ftruncate(fileno(f), l)33#define COMPILER_RT_VISIBILITY __attribute__((visibility("hidden")))34#define COMPILER_RT_WEAK __attribute__((weak))35#endif36#define COMPILER_RT_ALIGNAS(x) __attribute__((aligned(x)))37#define COMPILER_RT_ALLOCA __builtin_alloca38#define COMPILER_RT_ALWAYS_INLINE inline __attribute((always_inline))39#define COMPILER_RT_CLEANUP(x) __attribute__((cleanup(x)))40#define COMPILER_RT_USED __attribute__((used))41#endif4243#if defined(__APPLE__)44#define COMPILER_RT_SEG "__DATA,"45#else46#define COMPILER_RT_SEG ""47#endif4849#ifdef _MSC_VER50#define COMPILER_RT_SECTION(Sect) __declspec(allocate(Sect))51#else52#define COMPILER_RT_SECTION(Sect) __attribute__((section(Sect)))53#endif5455#define COMPILER_RT_MAX_HOSTLEN 12856#ifdef __ORBIS__57#define COMPILER_RT_GETHOSTNAME(Name, Len) ((void)(Name), (void)(Len), (-1))58#else59#define COMPILER_RT_GETHOSTNAME(Name, Len) lprofGetHostName(Name, Len)60#endif6162#if COMPILER_RT_HAS_ATOMICS == 163#ifdef _WIN3264#include <windows.h>65#if defined(_MSC_VER) && _MSC_VER < 190066#define snprintf _snprintf67#endif68#if defined(_WIN64)69#define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV) \70(InterlockedCompareExchange64((LONGLONG volatile *)Ptr, (LONGLONG)NewV, \71(LONGLONG)OldV) == (LONGLONG)OldV)72#define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr) \73(DomType *)InterlockedExchangeAdd64((LONGLONG volatile *)&PtrVar, \74(LONGLONG)sizeof(DomType) * PtrIncr)75#else /* !defined(_WIN64) */76#define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV) \77(InterlockedCompareExchange((LONG volatile *)Ptr, (LONG)NewV, (LONG)OldV) == \78(LONG)OldV)79#define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr) \80(DomType *)InterlockedExchangeAdd((LONG volatile *)&PtrVar, \81(LONG)sizeof(DomType) * PtrIncr)82#endif83#else /* !defined(_WIN32) */84#define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV) \85__sync_bool_compare_and_swap(Ptr, OldV, NewV)86#define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr) \87(DomType *)__sync_fetch_and_add((long *)&PtrVar, sizeof(DomType) * PtrIncr)88#endif89#else /* COMPILER_RT_HAS_ATOMICS != 1 */90#include "InstrProfilingUtil.h"91#define COMPILER_RT_BOOL_CMPXCHG(Ptr, OldV, NewV) \92lprofBoolCmpXchg((void **)Ptr, OldV, NewV)93#define COMPILER_RT_PTR_FETCH_ADD(DomType, PtrVar, PtrIncr) \94(DomType *)lprofPtrFetchAdd((void **)&PtrVar, sizeof(DomType) * PtrIncr)95#endif9697#if defined(_WIN32)98#define DIR_SEPARATOR '\\'99#define DIR_SEPARATOR_2 '/'100#else101#define DIR_SEPARATOR '/'102#endif103104#ifndef DIR_SEPARATOR_2105#define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR)106#else /* DIR_SEPARATOR_2 */107#define IS_DIR_SEPARATOR(ch) \108(((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2))109#endif /* DIR_SEPARATOR_2 */110111#if defined(_WIN32)112#include <windows.h>113static inline size_t getpagesize() {114SYSTEM_INFO S;115GetNativeSystemInfo(&S);116return S.dwPageSize;117}118#else /* defined(_WIN32) */119#include <unistd.h>120#endif /* defined(_WIN32) */121122#define PROF_ERR(Format, ...) \123fprintf(stderr, "LLVM Profile Error: " Format, __VA_ARGS__);124125#define PROF_WARN(Format, ...) \126fprintf(stderr, "LLVM Profile Warning: " Format, __VA_ARGS__);127128#define PROF_NOTE(Format, ...) \129fprintf(stderr, "LLVM Profile Note: " Format, __VA_ARGS__);130131#ifndef MAP_FILE132#define MAP_FILE 0133#endif134135#ifndef O_BINARY136#define O_BINARY 0137#endif138139#if defined(__FreeBSD__)140141#include <inttypes.h>142#include <sys/types.h>143144#else /* defined(__FreeBSD__) */145146#include <inttypes.h>147#include <stdint.h>148149#endif /* defined(__FreeBSD__) && defined(__i386__) */150151#endif /* PROFILE_INSTRPROFILING_PORT_H_ */152153154