[PGO] Enable building compiler-rt profile support library on Windows

Summary: This change configures Windows builds to build the complier-rt profile support library (clang_rt.profile-i386.lib). Windows API incompatibilities in the compiler-rt profile lib are also fixed.

Reviewers: davidxl, dnovillo

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D15830

llvm-svn: 256848
This commit is contained in:
Nathan Slingerland 2016-01-05 17:27:01 +00:00
parent a7d7f7cf33
commit ba86c9279b
6 changed files with 206 additions and 2 deletions

View File

@ -559,7 +559,7 @@ else()
endif()
if (PROFILE_SUPPORTED_ARCH AND
OS_NAME MATCHES "Darwin|Linux|FreeBSD")
OS_NAME MATCHES "Darwin|Linux|FreeBSD|Windows")
set(COMPILER_RT_HAS_PROFILE TRUE)
else()
set(COMPILER_RT_HAS_PROFILE FALSE)

View File

@ -35,7 +35,8 @@ set(PROFILE_SOURCES
InstrProfilingPlatformLinux.c
InstrProfilingPlatformOther.c
InstrProfilingRuntime.cc
InstrProfilingUtil.c)
InstrProfilingUtil.c
WindowsMMap.c)
if(UNIX)
set(EXTRA_FLAGS

View File

@ -27,8 +27,13 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(_WIN32)
#include "WindowsMMap.h"
#else
#include <sys/mman.h>
#include <sys/file.h>
#endif
#define I386_FREEBSD (defined(__FreeBSD__) && defined(__i386__))
@ -37,6 +42,7 @@
#endif
#if defined(_MSC_VER)
typedef unsigned char uint8_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
#elif I386_FREEBSD

View File

@ -17,6 +17,10 @@
#define UNCONST(ptr) ((void *)(uintptr_t)(ptr))
#ifdef _MSC_VER
#define snprintf _snprintf
#endif
/* Return 1 if there is an error, otherwise return 0. */
static uint32_t fileWriter(ProfDataIOVec *IOVecs, uint32_t NumIOVecs,
void **WriterCtx) {

View File

@ -0,0 +1,128 @@
/*
* This code is derived from uClibc (original license follows).
* https://git.uclibc.org/uClibc/tree/utils/mmap-windows.c
*/
/* mmap() replacement for Windows
*
* Author: Mike Frysinger <vapier@gentoo.org>
* Placed into the public domain
*/
/* References:
* CreateFileMapping: http://msdn.microsoft.com/en-us/library/aa366537(VS.85).aspx
* CloseHandle: http://msdn.microsoft.com/en-us/library/ms724211(VS.85).aspx
* MapViewOfFile: http://msdn.microsoft.com/en-us/library/aa366761(VS.85).aspx
* UnmapViewOfFile: http://msdn.microsoft.com/en-us/library/aa366882(VS.85).aspx
*/
#if defined(_WIN32)
#include "WindowsMMap.h"
#include "InstrProfiling.h"
#ifdef __USE_FILE_OFFSET64
# define DWORD_HI(x) (x >> 32)
# define DWORD_LO(x) ((x) & 0xffffffff)
#else
# define DWORD_HI(x) (0)
# define DWORD_LO(x) (x)
#endif
COMPILER_RT_VISIBILITY
void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
{
if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
return MAP_FAILED;
if (fd == -1) {
if (!(flags & MAP_ANON) || offset)
return MAP_FAILED;
} else if (flags & MAP_ANON)
return MAP_FAILED;
DWORD flProtect;
if (prot & PROT_WRITE) {
if (prot & PROT_EXEC)
flProtect = PAGE_EXECUTE_READWRITE;
else
flProtect = PAGE_READWRITE;
} else if (prot & PROT_EXEC) {
if (prot & PROT_READ)
flProtect = PAGE_EXECUTE_READ;
else if (prot & PROT_EXEC)
flProtect = PAGE_EXECUTE;
} else
flProtect = PAGE_READONLY;
off_t end = length + offset;
HANDLE mmap_fd, h;
if (fd == -1)
mmap_fd = INVALID_HANDLE_VALUE;
else
mmap_fd = (HANDLE)_get_osfhandle(fd);
h = CreateFileMapping(mmap_fd, NULL, flProtect, DWORD_HI(end), DWORD_LO(end), NULL);
if (h == NULL)
return MAP_FAILED;
DWORD dwDesiredAccess;
if (prot & PROT_WRITE)
dwDesiredAccess = FILE_MAP_WRITE;
else
dwDesiredAccess = FILE_MAP_READ;
if (prot & PROT_EXEC)
dwDesiredAccess |= FILE_MAP_EXECUTE;
if (flags & MAP_PRIVATE)
dwDesiredAccess |= FILE_MAP_COPY;
void *ret = MapViewOfFile(h, dwDesiredAccess, DWORD_HI(offset), DWORD_LO(offset), length);
if (ret == NULL) {
CloseHandle(h);
ret = MAP_FAILED;
}
return ret;
}
COMPILER_RT_VISIBILITY
void munmap(void *addr, size_t length)
{
UnmapViewOfFile(addr);
/* ruh-ro, we leaked handle from CreateFileMapping() ... */
}
COMPILER_RT_VISIBILITY
int msync(void *addr, size_t length, int flags)
{
if (flags & MS_INVALIDATE)
return -1; /* Not supported. */
/* Exactly one of MS_ASYNC or MS_SYNC must be specified. */
switch (flags & (MS_ASYNC | MS_SYNC)) {
case MS_SYNC:
case MS_ASYNC:
break;
default:
return -1;
}
if (!FlushViewOfFile(addr, length))
return -1;
if (flags & MS_SYNC) {
/* FIXME: No longer have access to handle from CreateFileMapping(). */
/*
* if (!FlushFileBuffers(h))
* return -1;
*/
}
return 0;
}
COMPILER_RT_VISIBILITY
int flock(int fd, int operation)
{
return -1; /* Not supported. */
}
#undef DWORD_HI
#undef DWORD_LO
#endif /* _WIN32 */

View File

@ -0,0 +1,65 @@
/*===- WindowsMMap.h - Support library for PGO instrumentation ------------===*\
|*
|* The LLVM Compiler Infrastructure
|*
|* This file is distributed under the University of Illinois Open Source
|* License. See LICENSE.TXT for details.
|*
\*===----------------------------------------------------------------------===*/
#ifndef PROFILE_INSTRPROFILING_WINDOWS_MMAP_H
#define PROFILE_INSTRPROFILING_WINDOWS_MMAP_H
#if defined(_WIN32)
#include <BaseTsd.h>
#include <io.h>
#include <sys/types.h>
/*
* mmap() flags
*/
#define PROT_READ 0x1
#define PROT_WRITE 0x2
/* This flag is only available in WinXP+ */
#ifdef FILE_MAP_EXECUTE
#define PROT_EXEC 0x4
#else
#define PROT_EXEC 0x0
#define FILE_MAP_EXECUTE 0
#endif
#define MAP_FILE 0x00
#define MAP_SHARED 0x01
#define MAP_PRIVATE 0x02
#define MAP_ANONYMOUS 0x20
#define MAP_ANON MAP_ANONYMOUS
#define MAP_FAILED ((void *) -1)
/*
* msync() flags
*/
#define MS_ASYNC 0x0001 /* return immediately */
#define MS_INVALIDATE 0x0002 /* invalidate all cached data */
#define MS_SYNC 0x0010 /* msync synchronously */
/*
* flock() operations
*/
#define LOCK_SH 1 /* shared lock */
#define LOCK_EX 2 /* exclusive lock */
#define LOCK_NB 4 /* don't block when locking */
#define LOCK_UN 8 /* unlock */
void *mmap(void *start, size_t length, int prot, int flags, int fd,
off_t offset);
void munmap(void *addr, size_t length);
int msync(void *addr, size_t length, int flags);
int flock(int fd, int operation);
#endif /* _WIN32 */
#endif /* PROFILE_INSTRPROFILING_WINDOWS_MMAP_H */