Skip to content

Commit cb8d946

Browse files
author
Davide Italiano
committed
[Runtime] Remove the dependency on LLVM's Compiler.h
<rdar://problem/35860874>
1 parent 2dfd3d5 commit cb8d946

File tree

4 files changed

+64
-21
lines changed

4 files changed

+64
-21
lines changed

include/swift/Runtime/Config.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,51 @@
4646
#define SWIFT_RUNTIME_LIBRARY_VISIBILITY
4747
#endif
4848

49+
/// Attributes.
50+
/// SWIFT_RUNTIME_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
51+
/// mark a method "not for inlining".
52+
#if __has_attribute(noinline) || SWIFT_RUNTIME_GNUC_PREREQ(3, 4, 0)
53+
#define SWIFT_RUNTIME_ATTRIBUTE_NOINLINE __attribute__((noinline))
54+
#elif defined(_MSC_VER)
55+
#define SWIFT_RUNTIME_ATTRIBUTE_NOINLINE __declspec(noinline)
56+
#else
57+
#define SWIFT_RUNTIME_ATTRIBUTE_NOINLINE
58+
#endif
59+
60+
/// SWIFT_RUNTIME_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
61+
/// so, mark a method "always inline" because it is performance sensitive. GCC
62+
/// 3.4 supported this but is buggy in various cases and produces unimplemented
63+
/// errors, just use it in GCC 4.0 and later.
64+
#if __has_attribute(always_inline) || SWIFT_RUNTIME_GNUC_PREREQ(4, 0, 0)
65+
#define SWIFT_RUNTIME_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline))
66+
#elif defined(_MSC_VER)
67+
#define SWIFT_RUNTIME_ATTRIBUTE_ALWAYS_INLINE __forceinline
68+
#else
69+
#define SWIFT_RUNTIME_ATTRIBUTE_ALWAYS_INLINE
70+
#endif
71+
72+
#ifdef __GNUC__
73+
#define SWIFT_RUNTIME_ATTRIBUTE_NORETURN __attribute__((noreturn))
74+
#elif defined(_MSC_VER)
75+
#define SWIFT_RUNTIME_ATTRIBUTE_NORETURN __declspec(noreturn)
76+
#else
77+
#define SWIFT_RUNTIME_ATTRIBUTE_NORETURN
78+
#endif
79+
80+
/// SWIFT_RUNTIME_BUILTIN_TRAP - On compilers which support it, expands to an expression
81+
/// which causes the program to exit abnormally.
82+
#if __has_builtin(__builtin_trap) || SWIFT_RUNTIME_GNUC_PREREQ(4, 3, 0)
83+
# define SWIFT_RUNTIME_BUILTIN_TRAP __builtin_trap()
84+
#elif defined(_MSC_VER)
85+
// The __debugbreak intrinsic is supported by MSVC, does not require forward
86+
// declarations involving platform-specific typedefs (unlike RaiseException),
87+
// results in a call to vectored exception handlers, and encodes to a short
88+
// instruction that still causes the trapping behavior we want.
89+
# define SWIFT_RUNTIME_BUILTIN_TRAP __debugbreak()
90+
#else
91+
# define SWIFT_RUNTIME_BUILTIN_TRAP *(volatile int*)0x11 = 0
92+
#endif
93+
4994
/// Does the current Swift platform support "unbridged" interoperation
5095
/// with Objective-C? If so, the implementations of various types must
5196
/// implicitly handle Objective-C pointers.

include/swift/Runtime/Debug.h

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#ifndef _SWIFT_RUNTIME_DEBUG_HELPERS_
1818
#define _SWIFT_RUNTIME_DEBUG_HELPERS_
1919

20-
#include <llvm/Support/Compiler.h>
2120
#include <cstdarg>
2221
#include <cstdio>
2322
#include <stdint.h>
@@ -41,23 +40,23 @@ struct crashreporter_annotations_t {
4140
};
4241

4342
extern "C" {
44-
LLVM_LIBRARY_VISIBILITY
43+
SWIFT_RUNTIME_LIBRARY_VISIBILITY
4544
extern struct crashreporter_annotations_t gCRAnnotations;
4645
}
4746

48-
LLVM_ATTRIBUTE_ALWAYS_INLINE
47+
SWIFT_RUNTIME_ATTRIBUTE_ALWAYS_INLINE
4948
static inline void CRSetCrashLogMessage(const char *message) {
5049
gCRAnnotations.message = reinterpret_cast<uint64_t>(message);
5150
}
5251

53-
LLVM_ATTRIBUTE_ALWAYS_INLINE
52+
SWIFT_RUNTIME_ATTRIBUTE_ALWAYS_INLINE
5453
static inline const char *CRGetCrashLogMessage() {
5554
return reinterpret_cast<const char *>(gCRAnnotations.message);
5655
}
5756

5857
#else
5958

60-
LLVM_ATTRIBUTE_ALWAYS_INLINE
59+
SWIFT_RUNTIME_ATTRIBUTE_ALWAYS_INLINE
6160
static inline void CRSetCrashLogMessage(const char *) {}
6261

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

76-
LLVM_ATTRIBUTE_NORETURN
77-
LLVM_ATTRIBUTE_ALWAYS_INLINE // Minimize trashed registers
75+
SWIFT_RUNTIME_ATTRIBUTE_NORETURN
76+
SWIFT_RUNTIME_ATTRIBUTE_ALWAYS_INLINE // Minimize trashed registers
7877
static inline void crash(const char *message) {
7978
CRSetCrashLogMessage(message);
8079

81-
LLVM_BUILTIN_TRAP;
80+
SWIFT_RUNTIME_BUILTIN_TRAP;
8281
swift_runtime_unreachable("Expected compiler to crash.");
8382
}
8483

8584
// swift::fatalError() halts with a crash log message,
8685
// but makes no attempt to preserve register state.
87-
LLVM_ATTRIBUTE_NORETURN
86+
SWIFT_RUNTIME_ATTRIBUTE_NORETURN
8887
extern void
8988
fatalError(uint32_t flags, const char *format, ...);
9089

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

9594
// swift_dynamicCastFailure halts using fatalError()
9695
// with a description of a failed cast's types.
97-
LLVM_ATTRIBUTE_NORETURN
96+
SWIFT_RUNTIME_ATTRIBUTE_NORETURN
9897
void
9998
swift_dynamicCastFailure(const Metadata *sourceType,
10099
const Metadata *targetType,
101100
const char *message = nullptr);
102101

103102
// swift_dynamicCastFailure halts using fatalError()
104103
// with a description of a failed cast's types.
105-
LLVM_ATTRIBUTE_NORETURN
104+
SWIFT_RUNTIME_ATTRIBUTE_NORETURN
106105
void
107106
swift_dynamicCastFailure(const void *sourceType, const char *sourceName,
108107
const void *targetType, const char *targetName,
@@ -112,19 +111,19 @@ SWIFT_RUNTIME_EXPORT
112111
void swift_reportError(uint32_t flags, const char *message);
113112

114113
// Halt due to an overflow in swift_retain().
115-
LLVM_ATTRIBUTE_NORETURN LLVM_ATTRIBUTE_NOINLINE
114+
SWIFT_RUNTIME_ATTRIBUTE_NORETURN SWIFT_RUNTIME_ATTRIBUTE_NOINLINE
116115
void swift_abortRetainOverflow();
117116

118117
// Halt due to reading an unowned reference to a dead object.
119-
LLVM_ATTRIBUTE_NORETURN LLVM_ATTRIBUTE_NOINLINE
118+
SWIFT_RUNTIME_ATTRIBUTE_NORETURN SWIFT_RUNTIME_ATTRIBUTE_NOINLINE
120119
void swift_abortRetainUnowned(const void *object);
121120

122121
// Halt due to an overflow in swift_unownedRetain().
123-
LLVM_ATTRIBUTE_NORETURN LLVM_ATTRIBUTE_NOINLINE
122+
SWIFT_RUNTIME_ATTRIBUTE_NORETURN SWIFT_RUNTIME_ATTRIBUTE_NOINLINE
124123
void swift_abortUnownedRetainOverflow();
125124

126125
// Halt due to an overflow in incrementWeak().
127-
LLVM_ATTRIBUTE_NORETURN LLVM_ATTRIBUTE_NOINLINE
126+
SWIFT_RUNTIME_ATTRIBUTE_NORETURN SWIFT_RUNTIME_ATTRIBUTE_NOINLINE
128127
void swift_abortWeakRetainOverflow();
129128

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

137-
LLVM_ATTRIBUTE_NOINLINE
136+
SWIFT_RUNTIME_ATTRIBUTE_NOINLINE
138137
void printCurrentBacktrace(unsigned framesToSkip = 1);
139138

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

223222

224-
LLVM_ATTRIBUTE_ALWAYS_INLINE
223+
SWIFT_RUNTIME_ATTRIBUTE_ALWAYS_INLINE
225224
inline static int swift_asprintf(char **strp, const char *fmt, ...) {
226225
va_list args;
227226
va_start(args, fmt);

include/swift/Runtime/Heap.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,4 @@
1717
#ifndef SWIFT_RUNTIME_HEAP_H
1818
#define SWIFT_RUNTIME_HEAP_H
1919

20-
#include <llvm/Support/Compiler.h>
21-
2220
#endif /* SWIFT_RUNTIME_HEAP_H */

include/swift/Runtime/Unreachable.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
#ifndef SWIFT_RUNTIME_UNREACHABLE_H
1919
#define SWIFT_RUNTIME_UNREACHABLE_H
2020

21-
#include "llvm/Support/Compiler.h"
2221
#include <assert.h>
2322
#include <stdlib.h>
2423

25-
LLVM_ATTRIBUTE_NORETURN
24+
#include "swift/Runtime/Config.h"
25+
26+
SWIFT_RUNTIME_ATTRIBUTE_NORETURN
2627
inline static void swift_runtime_unreachable(const char *msg) {
2728
assert(false && msg);
2829
(void)msg;

0 commit comments

Comments
 (0)