Skip to content

Commit 829b10c

Browse files
authored
Merge pull request #17719 from dcci/removedependency
[Runtime] Remove the dependency on LLVM's Compiler.h
2 parents ffedc8a + cb8d946 commit 829b10c

File tree

8 files changed

+97
-28
lines changed

8 files changed

+97
-28
lines changed

include/swift/Runtime/Config.h

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,79 @@
1717
#ifndef SWIFT_RUNTIME_CONFIG_H
1818
#define SWIFT_RUNTIME_CONFIG_H
1919

20-
// Bring in visibility attribute macros for library visibility.
21-
#include "llvm/Support/Compiler.h"
20+
/// \macro SWIFT_RUNTIME_GNUC_PREREQ
21+
/// Extend the default __GNUC_PREREQ even if glibc's features.h isn't
22+
/// available.
23+
#ifndef SWIFT_RUNTIME_GNUC_PREREQ
24+
# if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
25+
# define SWIFT_RUNTIME_GNUC_PREREQ(maj, min, patch) \
26+
((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) + __GNUC_PATCHLEVEL__ >= \
27+
((maj) << 20) + ((min) << 10) + (patch))
28+
# elif defined(__GNUC__) && defined(__GNUC_MINOR__)
29+
# define SWIFT_RUNTIME_GNUC_PREREQ(maj, min, patch) \
30+
((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) >= ((maj) << 20) + ((min) << 10))
31+
# else
32+
# define SWIFT_RUNTIME_GNUC_PREREQ(maj, min, patch) 0
33+
# endif
34+
#endif
35+
36+
/// SWIFT_RUNTIME_LIBRARY_VISIBILITY - If a class marked with this attribute is
37+
/// linked into a shared library, then the class should be private to the
38+
/// library and not accessible from outside it. Can also be used to mark
39+
/// variables and functions, making them private to any shared library they are
40+
/// linked into.
41+
/// On PE/COFF targets, library visibility is the default, so this isn't needed.
42+
#if (__has_attribute(visibility) || SWIFT_RUNTIME_GNUC_PREREQ(4, 0, 0)) && \
43+
!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_WIN32)
44+
#define SWIFT_RUNTIME_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
45+
#else
46+
#define SWIFT_RUNTIME_LIBRARY_VISIBILITY
47+
#endif
48+
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
2293

2394
/// Does the current Swift platform support "unbridged" interoperation
2495
/// with Objective-C? If so, the implementations of various types must

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;

stdlib/private/StdlibUnittest/GetOSVersion.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#include "swift/Runtime/Config.h"
1717

18-
SWIFT_CC(swift) LLVM_LIBRARY_VISIBILITY extern "C"
18+
SWIFT_CC(swift) SWIFT_RUNTIME_LIBRARY_VISIBILITY extern "C"
1919
const char *
2020
getSystemVersionPlistProperty(const char *PropertyName) {
2121
// This function is implemented in Objective-C because Swift does not support

stdlib/private/StdlibUnittest/InterceptTraps.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static void CrashCatcher(int Sig) {
3333
_exit(0);
3434
}
3535

36-
SWIFT_CC(swift) LLVM_LIBRARY_VISIBILITY extern "C"
36+
SWIFT_CC(swift) SWIFT_RUNTIME_LIBRARY_VISIBILITY extern "C"
3737
void installTrapInterceptor() {
3838
// Disable buffering on stdout so that everything is printed before crashing.
3939
setbuf(stdout, 0);

stdlib/private/StdlibUnittest/OpaqueIdentityFunctions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212

1313
#include "swift/Runtime/Config.h"
1414

15-
SWIFT_CC(swift) LLVM_LIBRARY_VISIBILITY extern "C"
15+
SWIFT_CC(swift) SWIFT_RUNTIME_LIBRARY_VISIBILITY extern "C"
1616
void *getPointer(void *x) { return x; }
1717

stdlib/private/StdlibUnittestFoundationExtras/UnavailableFoundationMethodThunks.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <Foundation/Foundation.h>
1414
#include "swift/Runtime/Config.h"
1515

16-
SWIFT_CC(swift) LLVM_LIBRARY_VISIBILITY
16+
SWIFT_CC(swift) SWIFT_RUNTIME_LIBRARY_VISIBILITY
1717
extern "C" void
1818
NSArray_getObjects(NSArray SWIFT_NS_RELEASES_ARGUMENT *_Nonnull nsArray,
1919
id *objects, NSUInteger rangeLocation,
@@ -22,7 +22,7 @@
2222
SWIFT_CC_PLUSONE_GUARD([nsArray release]);
2323
}
2424

25-
SWIFT_CC(swift) LLVM_LIBRARY_VISIBILITY
25+
SWIFT_CC(swift) SWIFT_RUNTIME_LIBRARY_VISIBILITY
2626
extern "C" void
2727
NSDictionary_getObjects(NSDictionary *_Nonnull nsDictionary,
2828
id *objects, id *keys) {

0 commit comments

Comments
 (0)