Index: clang/lib/Driver/ToolChains/Fuchsia.cpp =================================================================== --- clang/lib/Driver/ToolChains/Fuchsia.cpp +++ clang/lib/Driver/ToolChains/Fuchsia.cpp @@ -103,6 +103,7 @@ addSanitizerRuntimes(ToolChain, Args, CmdArgs); AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); + ToolChain.addProfileRTLibs(Args, CmdArgs); if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { if (Args.hasArg(options::OPT_static)) Index: compiler-rt/cmake/config-ix.cmake =================================================================== --- compiler-rt/cmake/config-ix.cmake +++ compiler-rt/cmake/config-ix.cmake @@ -547,7 +547,7 @@ endif() if (PROFILE_SUPPORTED_ARCH AND NOT LLVM_USE_SANITIZER AND - OS_NAME MATCHES "Darwin|Linux|FreeBSD|Windows|Android|SunOS") + OS_NAME MATCHES "Darwin|Linux|FreeBSD|Windows|Android|Fuchsia|SunOS") set(COMPILER_RT_HAS_PROFILE TRUE) else() set(COMPILER_RT_HAS_PROFILE FALSE) Index: compiler-rt/lib/profile/CMakeLists.txt =================================================================== --- compiler-rt/lib/profile/CMakeLists.txt +++ compiler-rt/lib/profile/CMakeLists.txt @@ -49,21 +49,29 @@ add_compiler_rt_component(profile) set(PROFILE_SOURCES - GCDAProfiling.c InstrProfiling.c InstrProfilingValue.c InstrProfilingBuffer.c - InstrProfilingFile.c InstrProfilingMerge.c - InstrProfilingMergeFile.c InstrProfilingNameVar.c InstrProfilingWriter.c - InstrProfilingPlatformDarwin.c - InstrProfilingPlatformLinux.c - InstrProfilingPlatformOther.c InstrProfilingRuntime.cc InstrProfilingUtil.c) +if(FUCHSIA) + list(APPEND PROFILE_SOURCES + InstrProfilingPlatformFuchsia.c + InstrProfilingPlatformLinux.c) +else() + list(APPEND PROFILE_SOURCES + GCDAProfiling.c + InstrProfilingFile.c + InstrProfilingMergeFile.c + InstrProfilingPlatformDarwin.c + InstrProfilingPlatformLinux.c + InstrProfilingPlatformOther.c) +endif() + if(WIN32) list(APPEND PROFILE_SOURCES WindowsMMap.c) endif() Index: compiler-rt/lib/profile/InstrProfilingPlatformFuchsia.c =================================================================== --- /dev/null +++ compiler-rt/lib/profile/InstrProfilingPlatformFuchsia.c @@ -0,0 +1,163 @@ +/*===- InstrProfilingPlatformFuchsia.c - Profile data Linux platform ------===*\ +|* +|* The LLVM Compiler Infrastructure +|* +|* This file is distributed under the University of Illinois Open Source +|* License. See LICENSE.TXT for details. +|* +\*===----------------------------------------------------------------------===*/ + +#if defined(__Fuchsia__) + +#include +#include +#include + +#include +#include +#include + +#include "InstrProfiling.h" +#include "InstrProfilingInternal.h" +#include "InstrProfilingUtil.h" + +static const size_t MappingSize = sizeof(uintptr_t) << 32; +const char ProfileSinkName[] = "profile"; + +struct WriterContext { + zx_handle_t Vmo; + uintptr_t Mapping; + void *Buffer; +}; + +static uint32_t lprofVMOWriter(ProfDataWriter *This, + ProfDataIOVec *IOVecs, + uint32_t NumIOVecs) { + struct WriterContext *Ctx = (struct WriterContext *)This->WriterCtx; + zx_status_t Status; + uint32_t I; + + /* Compute the total length of data to be written. */ + size_t Length = 0; + for (I = 0; I < NumIOVecs; I++) + Length += IOVecs[I].ElmSize * IOVecs[I].NumElm; + + /* Check the current VMO size. */ + uint64_t Size; + Status = zx_vmo_get_size(Ctx->Vmo, &Size); + if (Status != ZX_OK) + return 1; + + uint64_t Left = Ctx->Mapping + Size - (uintptr_t)Ctx->Buffer; + if (Left < Length) { + /* There isn't enough space, we need to increase the VMO size. */ + Status = zx_vmo_set_size(Ctx->Vmo, Size + Length - Left); + if (Status != ZX_OK) + return 1; + } + + /* Copy the data into VMO. */ + for (I = 0; I < NumIOVecs; I++) { + size_t Length = IOVecs[I].ElmSize * IOVecs[I].NumElm; + if (IOVecs[I].Data) + memcpy(Ctx->Buffer, IOVecs[I].Data, Length); + Ctx->Buffer += Length; + } + + return 0; +} + +static void initVMOWriter(ProfDataWriter *BufferWriter, + struct WriterContext *Ctx) { + BufferWriter->Write = lprofVMOWriter; + BufferWriter->WriterCtx = Ctx; +} + +static int dump(void) { + if (lprofProfileDumped()) { + PROF_NOTE("Profile data not published: %s.\n", + "already written"); + return 0; + } + + /* Check if there is llvm/runtime version mismatch. */ + if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) { + PROF_ERR("Runtime and instrumentation version mismatch : " + "expected %d, but get %d\n", + INSTR_PROF_RAW_VERSION, + (int)GET_VERSION(__llvm_profile_get_version())); + return -1; + } + + char VmoName[ZX_MAX_NAME_LEN]; + zx_status_t Status; + + /* Get information about the current process. */ + zx_info_handle_basic_t Info; + Status = _zx_object_get_info(_zx_process_self(), ZX_INFO_HANDLE_BASIC, &Info, + sizeof(Info), NULL, NULL); + if (Status != ZX_OK) + return -1; + + /* Create VMO to hold the profile data. */ + zx_handle_t Vmo; + Status = _zx_vmo_create(__llvm_profile_get_size_for_buffer(), 0, &Vmo); + if (Status != ZX_OK) + return -1; + + /* Give the VMO a name including our process KOID so it's easy to spot. */ + snprintf(VmoName, sizeof(VmoName), "%s.%zu", ProfileSinkName, + Info.koid); + _zx_object_set_property(Vmo, ZX_PROP_NAME, VmoName, strlen(VmoName)); + + /* Map the larget possible view we might need into the VMO. */ + uintptr_t Mapping; + Status = _zx_vmar_map(_zx_vmar_root_self(), 0, Vmo, 0, MappingSize, + ZX_VM_FLAG_PERM_READ | ZX_VM_FLAG_PERM_WRITE, &Mapping); + if (Status != ZX_OK) + return -1; + + /* Write the profile data into the mapped region. */ + ProfDataWriter VMOWriter; + struct WriterContext Ctx = { Vmo, Mapping, (void *)Mapping }; + initVMOWriter(&VMOWriter, &Ctx); + if (lprofWriteData(&VMOWriter, lprofGetVPDataReader(), 0) != 0) + return -1; + + /* Publish the VMO which contains profile data to the system. */ + __sanitizer_publish_data(ProfileSinkName, Vmo); + + return 0; +} + +COMPILER_RT_VISIBILITY +int __llvm_profile_dump(void) { + int rc = dump(); + lprofSetProfileDumped(); + return rc; +} + +static void dumpWithoutReturn(void) { dump(); } + +/* This method is invoked by the runtime initialization hook + * InstrProfilingRuntime.o if it is linked in. + */ +COMPILER_RT_VISIBILITY +void __llvm_profile_initialize_file(void) { + /* Unsupported */ +} + +COMPILER_RT_VISIBILITY +int __llvm_profile_register_write_file_atexit(void) { + static int HasBeenRegistered = 0; + + if (HasBeenRegistered) + return 0; + + lprofSetupValueProfiler(); + + HasBeenRegistered = 1; + return atexit(dumpWithoutReturn); +} + +#endif Index: compiler-rt/lib/profile/InstrProfilingPlatformLinux.c =================================================================== --- compiler-rt/lib/profile/InstrProfilingPlatformLinux.c +++ compiler-rt/lib/profile/InstrProfilingPlatformLinux.c @@ -7,7 +7,7 @@ |* \*===----------------------------------------------------------------------===*/ -#if defined(__linux__) || defined(__FreeBSD__) || \ +#if defined(__linux__) || defined(__FreeBSD__) || defined(__Fuchsia__) || \ (defined(__sun__) && defined(__svr4__)) #include Index: compiler-rt/lib/profile/InstrProfilingWriter.c =================================================================== --- compiler-rt/lib/profile/InstrProfilingWriter.c +++ compiler-rt/lib/profile/InstrProfilingWriter.c @@ -208,6 +208,40 @@ return 0; } +//int setupVPDataReader(VPDataReaderType *VPDataReader +// const __llvm_profile_data *DataBegin, +// const __llvm_profile_data *DataEnd) { +// size_t ValueProfDataSize = 0; +// const __llvm_profile_data *DI = 0; +// for (DI = DataBegin; DI < DataEnd; DI++) { +// unsigned I, NumValueKinds = 0; +// ValueProfData VPHeader; +// uint8_t *SiteCountArray[IPVK_Last + 1]; +// +// for (I = 0; I <= IPVK_Last; I++) { +// if (!DI->NumValueSites[I]) +// SiteCountArray[I] = 0; +// else { +// uint32_t Sz = +// VPDataReader->GetValueProfRecordHeaderSize(DI->NumValueSites[I]) - +// offsetof(ValueProfRecord, SiteCountArray); +// /* Only use alloca for this small byte array to avoid excessive +// * stack growth. */ +// SiteCountArray[I] = (uint8_t *)COMPILER_RT_ALLOCA(Sz); +// memset(SiteCountArray[I], 0, Sz); +// } +// } +// +// /* If NumValueKinds returned is 0, there is nothing to write, report +// success and return. This should match the raw profile reader's behavior. */ +// if (!(NumValueKinds = VPDataReader->InitRTRecord(DI, SiteCountArray))) +// continue; +// +// ValueProfDataSize += VPDataReader->GetValueProfDataSize() +// } +// return ValueProfDataSize; +//} + static int writeValueProfData(ProfDataWriter *Writer, VPDataReaderType *VPDataReader, const __llvm_profile_data *DataBegin, Index: llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp =================================================================== --- llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp +++ llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp @@ -691,6 +691,7 @@ // Use linker script magic to get data/cnts/name start/end. if (Triple(M.getTargetTriple()).isOSLinux() || Triple(M.getTargetTriple()).isOSFreeBSD() || + Triple(M.getTargetTriple()).isOSFuchsia() || Triple(M.getTargetTriple()).isPS4CPU()) return false;