Skip to content

[Runtime] Remove the dependency on LLVM's Compiler.h #17719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 73 additions & 2 deletions include/swift/Runtime/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,79 @@
#ifndef SWIFT_RUNTIME_CONFIG_H
#define SWIFT_RUNTIME_CONFIG_H

// Bring in visibility attribute macros for library visibility.
#include "llvm/Support/Compiler.h"
/// \macro SWIFT_RUNTIME_GNUC_PREREQ
/// Extend the default __GNUC_PREREQ even if glibc's features.h isn't
/// available.
#ifndef SWIFT_RUNTIME_GNUC_PREREQ
# if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
# define SWIFT_RUNTIME_GNUC_PREREQ(maj, min, patch) \
((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) + __GNUC_PATCHLEVEL__ >= \
((maj) << 20) + ((min) << 10) + (patch))
# elif defined(__GNUC__) && defined(__GNUC_MINOR__)
# define SWIFT_RUNTIME_GNUC_PREREQ(maj, min, patch) \
((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) >= ((maj) << 20) + ((min) << 10))
# else
# define SWIFT_RUNTIME_GNUC_PREREQ(maj, min, patch) 0
# endif
#endif

/// SWIFT_RUNTIME_LIBRARY_VISIBILITY - If a class marked with this attribute is
/// linked into a shared library, then the class should be private to the
/// library and not accessible from outside it. Can also be used to mark
/// variables and functions, making them private to any shared library they are
/// linked into.
/// On PE/COFF targets, library visibility is the default, so this isn't needed.
#if (__has_attribute(visibility) || SWIFT_RUNTIME_GNUC_PREREQ(4, 0, 0)) && \
!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_WIN32)
#define SWIFT_RUNTIME_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
#else
#define SWIFT_RUNTIME_LIBRARY_VISIBILITY
#endif

/// Attributes.
/// SWIFT_RUNTIME_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
/// mark a method "not for inlining".
#if __has_attribute(noinline) || SWIFT_RUNTIME_GNUC_PREREQ(3, 4, 0)
#define SWIFT_RUNTIME_ATTRIBUTE_NOINLINE __attribute__((noinline))
#elif defined(_MSC_VER)
#define SWIFT_RUNTIME_ATTRIBUTE_NOINLINE __declspec(noinline)
#else
#define SWIFT_RUNTIME_ATTRIBUTE_NOINLINE
#endif

/// SWIFT_RUNTIME_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
/// so, mark a method "always inline" because it is performance sensitive. GCC
/// 3.4 supported this but is buggy in various cases and produces unimplemented
/// errors, just use it in GCC 4.0 and later.
#if __has_attribute(always_inline) || SWIFT_RUNTIME_GNUC_PREREQ(4, 0, 0)
#define SWIFT_RUNTIME_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline))
#elif defined(_MSC_VER)
#define SWIFT_RUNTIME_ATTRIBUTE_ALWAYS_INLINE __forceinline
#else
#define SWIFT_RUNTIME_ATTRIBUTE_ALWAYS_INLINE
#endif

#ifdef __GNUC__
#define SWIFT_RUNTIME_ATTRIBUTE_NORETURN __attribute__((noreturn))
#elif defined(_MSC_VER)
#define SWIFT_RUNTIME_ATTRIBUTE_NORETURN __declspec(noreturn)
#else
#define SWIFT_RUNTIME_ATTRIBUTE_NORETURN
#endif

/// SWIFT_RUNTIME_BUILTIN_TRAP - On compilers which support it, expands to an expression
/// which causes the program to exit abnormally.
#if __has_builtin(__builtin_trap) || SWIFT_RUNTIME_GNUC_PREREQ(4, 3, 0)
# define SWIFT_RUNTIME_BUILTIN_TRAP __builtin_trap()
#elif defined(_MSC_VER)
// The __debugbreak intrinsic is supported by MSVC, does not require forward
// declarations involving platform-specific typedefs (unlike RaiseException),
// results in a call to vectored exception handlers, and encodes to a short
// instruction that still causes the trapping behavior we want.
# define SWIFT_RUNTIME_BUILTIN_TRAP __debugbreak()
#else
# define SWIFT_RUNTIME_BUILTIN_TRAP *(volatile int*)0x11 = 0
#endif

/// Does the current Swift platform support "unbridged" interoperation
/// with Objective-C? If so, the implementations of various types must
Expand Down
33 changes: 16 additions & 17 deletions include/swift/Runtime/Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#ifndef _SWIFT_RUNTIME_DEBUG_HELPERS_
#define _SWIFT_RUNTIME_DEBUG_HELPERS_

#include <llvm/Support/Compiler.h>
#include <cstdarg>
#include <cstdio>
#include <stdint.h>
Expand All @@ -41,23 +40,23 @@ struct crashreporter_annotations_t {
};

extern "C" {
LLVM_LIBRARY_VISIBILITY
SWIFT_RUNTIME_LIBRARY_VISIBILITY
extern struct crashreporter_annotations_t gCRAnnotations;
}

LLVM_ATTRIBUTE_ALWAYS_INLINE
SWIFT_RUNTIME_ATTRIBUTE_ALWAYS_INLINE
static inline void CRSetCrashLogMessage(const char *message) {
gCRAnnotations.message = reinterpret_cast<uint64_t>(message);
}

LLVM_ATTRIBUTE_ALWAYS_INLINE
SWIFT_RUNTIME_ATTRIBUTE_ALWAYS_INLINE
static inline const char *CRGetCrashLogMessage() {
return reinterpret_cast<const char *>(gCRAnnotations.message);
}

#else

LLVM_ATTRIBUTE_ALWAYS_INLINE
SWIFT_RUNTIME_ATTRIBUTE_ALWAYS_INLINE
static inline void CRSetCrashLogMessage(const char *) {}

#endif
Expand All @@ -73,18 +72,18 @@ using Metadata = TargetMetadata<InProcess>;
// swift::crash() halts with a crash log message,
// but otherwise tries not to disturb register state.

LLVM_ATTRIBUTE_NORETURN
LLVM_ATTRIBUTE_ALWAYS_INLINE // Minimize trashed registers
SWIFT_RUNTIME_ATTRIBUTE_NORETURN
SWIFT_RUNTIME_ATTRIBUTE_ALWAYS_INLINE // Minimize trashed registers
static inline void crash(const char *message) {
CRSetCrashLogMessage(message);

LLVM_BUILTIN_TRAP;
SWIFT_RUNTIME_BUILTIN_TRAP;
swift_runtime_unreachable("Expected compiler to crash.");
}

// swift::fatalError() halts with a crash log message,
// but makes no attempt to preserve register state.
LLVM_ATTRIBUTE_NORETURN
SWIFT_RUNTIME_ATTRIBUTE_NORETURN
extern void
fatalError(uint32_t flags, const char *format, ...);

Expand All @@ -94,15 +93,15 @@ warning(uint32_t flags, const char *format, ...);

// swift_dynamicCastFailure halts using fatalError()
// with a description of a failed cast's types.
LLVM_ATTRIBUTE_NORETURN
SWIFT_RUNTIME_ATTRIBUTE_NORETURN
void
swift_dynamicCastFailure(const Metadata *sourceType,
const Metadata *targetType,
const char *message = nullptr);

// swift_dynamicCastFailure halts using fatalError()
// with a description of a failed cast's types.
LLVM_ATTRIBUTE_NORETURN
SWIFT_RUNTIME_ATTRIBUTE_NORETURN
void
swift_dynamicCastFailure(const void *sourceType, const char *sourceName,
const void *targetType, const char *targetName,
Expand All @@ -112,19 +111,19 @@ SWIFT_RUNTIME_EXPORT
void swift_reportError(uint32_t flags, const char *message);

// Halt due to an overflow in swift_retain().
LLVM_ATTRIBUTE_NORETURN LLVM_ATTRIBUTE_NOINLINE
SWIFT_RUNTIME_ATTRIBUTE_NORETURN SWIFT_RUNTIME_ATTRIBUTE_NOINLINE
void swift_abortRetainOverflow();

// Halt due to reading an unowned reference to a dead object.
LLVM_ATTRIBUTE_NORETURN LLVM_ATTRIBUTE_NOINLINE
SWIFT_RUNTIME_ATTRIBUTE_NORETURN SWIFT_RUNTIME_ATTRIBUTE_NOINLINE
void swift_abortRetainUnowned(const void *object);

// Halt due to an overflow in swift_unownedRetain().
LLVM_ATTRIBUTE_NORETURN LLVM_ATTRIBUTE_NOINLINE
SWIFT_RUNTIME_ATTRIBUTE_NORETURN SWIFT_RUNTIME_ATTRIBUTE_NOINLINE
void swift_abortUnownedRetainOverflow();

// Halt due to an overflow in incrementWeak().
LLVM_ATTRIBUTE_NORETURN LLVM_ATTRIBUTE_NOINLINE
SWIFT_RUNTIME_ATTRIBUTE_NORETURN SWIFT_RUNTIME_ATTRIBUTE_NOINLINE
void swift_abortWeakRetainOverflow();

/// This function dumps one line of a stack trace. It is assumed that \p framePC
Expand All @@ -134,7 +133,7 @@ void swift_abortWeakRetainOverflow();
void dumpStackTraceEntry(unsigned index, void *framePC,
bool shortOutput = false);

LLVM_ATTRIBUTE_NOINLINE
SWIFT_RUNTIME_ATTRIBUTE_NOINLINE
void printCurrentBacktrace(unsigned framesToSkip = 1);

/// Debugger breakpoint ABI. This structure is passed to the debugger (and needs
Expand Down Expand Up @@ -221,7 +220,7 @@ SWIFT_RUNTIME_STDLIB_SPI
bool _swift_shouldReportFatalErrorsToDebugger();


LLVM_ATTRIBUTE_ALWAYS_INLINE
SWIFT_RUNTIME_ATTRIBUTE_ALWAYS_INLINE
inline static int swift_asprintf(char **strp, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
Expand Down
2 changes: 0 additions & 2 deletions include/swift/Runtime/Heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@
#ifndef SWIFT_RUNTIME_HEAP_H
#define SWIFT_RUNTIME_HEAP_H

#include <llvm/Support/Compiler.h>

#endif /* SWIFT_RUNTIME_HEAP_H */
5 changes: 3 additions & 2 deletions include/swift/Runtime/Unreachable.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
#ifndef SWIFT_RUNTIME_UNREACHABLE_H
#define SWIFT_RUNTIME_UNREACHABLE_H

#include "llvm/Support/Compiler.h"
#include <assert.h>
#include <stdlib.h>

LLVM_ATTRIBUTE_NORETURN
#include "swift/Runtime/Config.h"

SWIFT_RUNTIME_ATTRIBUTE_NORETURN
inline static void swift_runtime_unreachable(const char *msg) {
assert(false && msg);
(void)msg;
Expand Down
2 changes: 1 addition & 1 deletion stdlib/private/StdlibUnittest/GetOSVersion.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

#include "swift/Runtime/Config.h"

SWIFT_CC(swift) LLVM_LIBRARY_VISIBILITY extern "C"
SWIFT_CC(swift) SWIFT_RUNTIME_LIBRARY_VISIBILITY extern "C"
const char *
getSystemVersionPlistProperty(const char *PropertyName) {
// This function is implemented in Objective-C because Swift does not support
Expand Down
2 changes: 1 addition & 1 deletion stdlib/private/StdlibUnittest/InterceptTraps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static void CrashCatcher(int Sig) {
_exit(0);
}

SWIFT_CC(swift) LLVM_LIBRARY_VISIBILITY extern "C"
SWIFT_CC(swift) SWIFT_RUNTIME_LIBRARY_VISIBILITY extern "C"
void installTrapInterceptor() {
// Disable buffering on stdout so that everything is printed before crashing.
setbuf(stdout, 0);
Expand Down
2 changes: 1 addition & 1 deletion stdlib/private/StdlibUnittest/OpaqueIdentityFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@

#include "swift/Runtime/Config.h"

SWIFT_CC(swift) LLVM_LIBRARY_VISIBILITY extern "C"
SWIFT_CC(swift) SWIFT_RUNTIME_LIBRARY_VISIBILITY extern "C"
void *getPointer(void *x) { return x; }

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <Foundation/Foundation.h>
#include "swift/Runtime/Config.h"

SWIFT_CC(swift) LLVM_LIBRARY_VISIBILITY
SWIFT_CC(swift) SWIFT_RUNTIME_LIBRARY_VISIBILITY
extern "C" void
NSArray_getObjects(NSArray SWIFT_NS_RELEASES_ARGUMENT *_Nonnull nsArray,
id *objects, NSUInteger rangeLocation,
Expand All @@ -22,7 +22,7 @@
SWIFT_CC_PLUSONE_GUARD([nsArray release]);
}

SWIFT_CC(swift) LLVM_LIBRARY_VISIBILITY
SWIFT_CC(swift) SWIFT_RUNTIME_LIBRARY_VISIBILITY
extern "C" void
NSDictionary_getObjects(NSDictionary *_Nonnull nsDictionary,
id *objects, id *keys) {
Expand Down