Skip to content

Commit a4814df

Browse files
Add swiftcc attribute for runtime functions called from Swift
1 parent b8e40c0 commit a4814df

File tree

12 files changed

+76
-24
lines changed

12 files changed

+76
-24
lines changed

include/swift/Demangling/Demangle.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@ llvm::StringRef makeSymbolicMangledNameStringRef(const char *base);
637637
//// define what these will be.
638638
/// \returns the demangled name. Returns nullptr if the input String is not a
639639
/// Swift mangled name.
640+
SWIFT_CC(swift)
640641
SWIFT_RUNTIME_EXPORT
641642
char *swift_demangle(const char *mangledName,
642643
size_t mangledNameLength,

include/swift/Runtime/HeapObject.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,13 @@ void swift_nonatomic_release_n(HeapObject *object, uint32_t n);
205205

206206
// Refcounting observation hooks for memory tools. Don't use these.
207207
SWIFT_RUNTIME_EXPORT
208+
SWIFT_CC(swift)
208209
size_t swift_retainCount(HeapObject *object);
209210
SWIFT_RUNTIME_EXPORT
211+
SWIFT_CC(swift)
210212
size_t swift_unownedRetainCount(HeapObject *object);
211213
SWIFT_RUNTIME_EXPORT
214+
SWIFT_CC(swift)
212215
size_t swift_weakRetainCount(HeapObject *object);
213216

214217
/// Is this pointer a non-null unique reference to an object

lib/IRGen/GenBuiltin.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,17 @@ if (Builtin.ID == BuiltinValueKind::id) { \
398398
call->addAttribute(llvm::AttributeList::FirstArgIndex + 1,
399399
llvm::Attribute::ReadOnly);
400400

401-
auto attrs = call->getAttributes();
402-
IGF.IGM.addSwiftSelfAttributes(attrs, 0);
403-
IGF.IGM.addSwiftErrorAttributes(attrs, 1);
404-
call->setAttributes(attrs);
401+
// Remove swiftself and swifterror attribute to match signature generated from
402+
// RuntimeFunctions.def. These two parameters are passed using swifterror and swiftself,
403+
// but the definition of swift_willThrow generated from the def file doesn't has those
404+
// attributes due to the def file limitation. In WebAssembly context, these attributes are
405+
// lowered as usual parameters, so this doesn't have any side effects.
406+
if (IGF.IGM.TargetInfo.OutputObjectFormat != llvm::Triple::Wasm) {
407+
auto attrs = call->getAttributes();
408+
IGF.IGM.addSwiftSelfAttributes(attrs, 0);
409+
IGF.IGM.addSwiftErrorAttributes(attrs, 1);
410+
call->setAttributes(attrs);
411+
}
405412

406413
IGF.Builder.CreateStore(llvm::ConstantPointerNull::get(IGF.IGM.ErrorPtrTy),
407414
errorBuffer);

lib/IRGen/GenKeyPath.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ getLayoutFunctionForComputedComponent(IRGenModule &IGM,
278278

279279
auto layoutFn = llvm::Function::Create(fnTy,
280280
llvm::GlobalValue::PrivateLinkage, "keypath_get_arg_layout", IGM.getModule());
281-
281+
layoutFn->setCallingConv(IGM.SwiftCC);
282+
282283
{
283284
IRGenFunction IGF(IGM, layoutFn);
284285
if (IGM.DebugInfo)
@@ -378,6 +379,7 @@ getWitnessTableForComputedComponent(IRGenModule &IGM,
378379
/*vararg*/ false);
379380
auto destroyFn = llvm::Function::Create(destroyType,
380381
llvm::GlobalValue::PrivateLinkage, "keypath_destroy", IGM.getModule());
382+
destroyFn->setCallingConv(IGM.SwiftCC);
381383
destroy = destroyFn;
382384

383385
IRGenFunction IGF(IGM, destroyFn);
@@ -426,6 +428,7 @@ getWitnessTableForComputedComponent(IRGenModule &IGM,
426428
/*vararg*/ false);
427429
auto copyFn = llvm::Function::Create(copyType,
428430
llvm::GlobalValue::PrivateLinkage, "keypath_copy", IGM.getModule());
431+
copyFn->setCallingConv(IGM.SwiftCC);
429432
copy = copyFn;
430433

431434
IRGenFunction IGF(IGM, copyFn);
@@ -539,6 +542,8 @@ getInitializerForComputedComponent(IRGenModule &IGM,
539542

540543
auto initFn = llvm::Function::Create(fnTy,
541544
llvm::GlobalValue::PrivateLinkage, "keypath_arg_init", IGM.getModule());
545+
initFn->setCallingConv(IGM.SwiftCC);
546+
542547

543548
{
544549
IRGenFunction IGF(IGM, initFn);

stdlib/public/SwiftShims/HeapObject.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,25 @@ struct HeapObject {
7474
#ifdef __cplusplus
7575
extern "C" {
7676
#endif
77+
#if __has_attribute(swiftcall)
78+
#define SWIFT_CC_swift __attribute__((swiftcall))
79+
#else
80+
#define SWIFT_CC_swift
81+
#endif
7782

7883
SWIFT_RUNTIME_STDLIB_API
7984
void _swift_instantiateInertHeapObject(void *address,
8085
const HeapMetadata *metadata);
8186

87+
SWIFT_CC_swift
8288
SWIFT_RUNTIME_STDLIB_API
8389
__swift_size_t swift_retainCount(HeapObject *obj);
8490

91+
SWIFT_CC_swift
8592
SWIFT_RUNTIME_STDLIB_API
8693
__swift_size_t swift_unownedRetainCount(HeapObject *obj);
8794

95+
SWIFT_CC_swift
8896
SWIFT_RUNTIME_STDLIB_API
8997
__swift_size_t swift_weakRetainCount(HeapObject *obj);
9098

stdlib/public/runtime/Demangle.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ swift::_swift_buildDemanglingForMetadata(const Metadata *type,
638638
// NB: This function is not used directly in the Swift codebase, but is
639639
// exported for Xcode support and is used by the sanitizers. Please coordinate
640640
// before changing.
641+
SWIFT_CC(swift)
641642
char *swift_demangle(const char *mangledName,
642643
size_t mangledNameLength,
643644
char *outputBuffer,

stdlib/public/runtime/ErrorObject.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,21 @@ SWIFT_RUNTIME_STDLIB_API
206206
void swift_errorRelease(SwiftError *object);
207207

208208
/// Breakpoint hook for debuggers.
209+
#ifdef __wasm__
210+
// Notes:
211+
// Remove swiftself and swifterror attribute to match signature generated from
212+
// RuntimeFunctions.def. These two parameters are passed using swifterror and swiftself,
213+
// but the definition of swift_willThrow generated from the def file doesn't has those
214+
// attributes due to the def file limitation. In WebAssembly context, these attributes are
215+
// lowered as usual parameters, so this doesn't have any side effects.
216+
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API
217+
void swift_willThrow(void *unused,
218+
SwiftError **object);
219+
#else
209220
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API
210221
void swift_willThrow(SWIFT_CONTEXT void *unused,
211222
SWIFT_ERROR_RESULT SwiftError **object);
223+
#endif
212224

213225
/// Halt in response to an error.
214226
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API LLVM_ATTRIBUTE_NORETURN

stdlib/public/runtime/ErrorObjectCommon.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,17 @@ using namespace swift;
2727
void (*swift::_swift_willThrow)(SwiftError *error);
2828

2929
/// Breakpoint hook for debuggers, and calls _swift_willThrow if set.
30+
#ifdef __wasm__
31+
// Notes:
32+
// The reason of this ifdef is described in header file.
33+
SWIFT_CC(swift) void
34+
swift::swift_willThrow(void *unused, SwiftError **error)
35+
#else
3036
SWIFT_CC(swift) void
3137
swift::swift_willThrow(SWIFT_CONTEXT void *unused,
32-
SWIFT_ERROR_RESULT SwiftError **error) {
38+
SWIFT_ERROR_RESULT SwiftError **error)
39+
#endif
40+
{
3341
// Cheap check to bail out early, since we expect there to be no callbacks
3442
// the vast majority of the time.
3543
if (SWIFT_LIKELY(!_swift_willThrow))

stdlib/public/runtime/HeapObject.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ class BoxCacheEntry {
253253

254254
static SimpleGlobalCache<BoxCacheEntry> Boxes;
255255

256+
SWIFT_CC(swift)
256257
BoxPair swift::swift_makeBoxUnique(OpaqueValue *buffer, const Metadata *type,
257258
size_t alignMask) {
258259
auto *inlineBuffer = reinterpret_cast<ValueBuffer*>(buffer);
@@ -278,6 +279,7 @@ BoxPair swift::swift_makeBoxUnique(OpaqueValue *buffer, const Metadata *type,
278279
}
279280
}
280281

282+
SWIFT_CC(swift)
281283
BoxPair swift::swift_allocBox(const Metadata *type) {
282284
// Get the heap metadata for the box.
283285
auto metadata = &Boxes.getOrInsert(type).first->Data;
@@ -420,16 +422,19 @@ void swift::swift_nonatomic_release_n(HeapObject *object, uint32_t n) {
420422
object->refCounts.decrementAndMaybeDeinitNonAtomic(n);
421423
}
422424

425+
SWIFT_CC(swift)
423426
size_t swift::swift_retainCount(HeapObject *object) {
424427
if (isValidPointerForNativeRetain(object))
425428
return object->refCounts.getCount();
426429
return 0;
427430
}
428431

432+
SWIFT_CC(swift)
429433
size_t swift::swift_unownedRetainCount(HeapObject *object) {
430434
return object->refCounts.getUnownedCount();
431435
}
432436

437+
SWIFT_CC(swift)
433438
size_t swift::swift_weakRetainCount(HeapObject *object) {
434439
return object->refCounts.getWeakCount();
435440
}

stdlib/public/runtime/Numeric.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ static T convert(IntegerLiteral value) {
5050
return result;
5151
}
5252

53+
SWIFT_CC(swift)
5354
float swift::swift_intToFloat32(IntegerLiteral value) {
5455
return convert<float>(value);
5556
}
5657

58+
SWIFT_CC(swift)
5759
double swift::swift_intToFloat64(IntegerLiteral value) {
5860
return convert<double>(value);
5961
}

stdlib/public/runtime/RuntimeInvocationsTracking.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ static uint16_t RuntimeFunctionCountersOffsets[] = {
128128
/// Public APIs
129129

130130
/// Get the runtime object state associated with an object.
131-
void _swift_getObjectRuntimeFunctionCounters(
131+
SWIFT_CC(swift) void _swift_getObjectRuntimeFunctionCounters(
132132
HeapObject *object, RuntimeFunctionCountersState *result) {
133133
auto &theSentinel = RuntimeObjectStateCache.get();
134134
StaticScopedReadLock lock(theSentinel.Lock);
@@ -137,7 +137,7 @@ void _swift_getObjectRuntimeFunctionCounters(
137137

138138
/// Set the runtime object state associated with an object from a provided
139139
/// state.
140-
void _swift_setObjectRuntimeFunctionCounters(
140+
SWIFT_CC(swift) void _swift_setObjectRuntimeFunctionCounters(
141141
HeapObject *object, RuntimeFunctionCountersState *state) {
142142
auto &theSentinel = RuntimeObjectStateCache.get();
143143
StaticScopedWriteLock lock(theSentinel.Lock);
@@ -146,14 +146,14 @@ void _swift_setObjectRuntimeFunctionCounters(
146146

147147
/// Get the global runtime state containing the total numbers of invocations for
148148
/// each runtime function of interest.
149-
void _swift_getGlobalRuntimeFunctionCounters(
149+
SWIFT_CC(swift) void _swift_getGlobalRuntimeFunctionCounters(
150150
RuntimeFunctionCountersState *result) {
151151
StaticScopedReadLock lock(RuntimeGlobalFunctionCountersState.Lock);
152152
*result = RuntimeGlobalFunctionCountersState.State;
153153
}
154154

155155
/// Set the global runtime state of function pointers from a provided state.
156-
void _swift_setGlobalRuntimeFunctionCounters(
156+
SWIFT_CC(swift) void _swift_setGlobalRuntimeFunctionCounters(
157157
RuntimeFunctionCountersState *state) {
158158
StaticScopedWriteLock lock(RuntimeGlobalFunctionCountersState.Lock);
159159
RuntimeGlobalFunctionCountersState.State = *state;
@@ -162,19 +162,19 @@ void _swift_setGlobalRuntimeFunctionCounters(
162162
/// Return the names of the runtime functions being tracked.
163163
/// Their order is the same as the order of the counters in the
164164
/// RuntimeObjectState structure. All these strings are null terminated.
165-
const char **_swift_getRuntimeFunctionNames() {
165+
SWIFT_CC(swift) const char **_swift_getRuntimeFunctionNames() {
166166
return RuntimeFunctionNames;
167167
}
168168

169169
/// Return the offsets of the runtime function counters being tracked.
170170
/// Their order is the same as the order of the counters in the
171171
/// RuntimeObjectState structure.
172-
const uint16_t *_swift_getRuntimeFunctionCountersOffsets() {
172+
SWIFT_CC(swift) const uint16_t *_swift_getRuntimeFunctionCountersOffsets() {
173173
return RuntimeFunctionCountersOffsets;
174174
}
175175

176176
/// Return the number of runtime functions being tracked.
177-
uint64_t _swift_getNumRuntimeFunctionCounters() {
177+
SWIFT_CC(swift) uint64_t _swift_getNumRuntimeFunctionCounters() {
178178
return ID_LastRuntimeFunctionName;
179179
}
180180

@@ -202,15 +202,15 @@ void _swift_dumpObjectsRuntimeFunctionPointers() {
202202

203203
/// Set mode for global runtime function counters.
204204
/// Return the old value of this flag.
205-
int _swift_setGlobalRuntimeFunctionCountersMode(int mode) {
205+
SWIFT_CC(swift) int _swift_setGlobalRuntimeFunctionCountersMode(int mode) {
206206
int oldMode = UpdateGlobalRuntimeFunctionCounters;
207207
UpdateGlobalRuntimeFunctionCounters = mode ? 1 : 0;
208208
return oldMode;
209209
}
210210

211211
/// Set mode for per object runtime function counters.
212212
/// Return the old value of this flag.
213-
int _swift_setPerObjectRuntimeFunctionCountersMode(int mode) {
213+
SWIFT_CC(swift) int _swift_setPerObjectRuntimeFunctionCountersMode(int mode) {
214214
int oldMode = UpdatePerObjectRuntimeFunctionCounters;
215215
UpdatePerObjectRuntimeFunctionCounters = mode ? 1 : 0;
216216
return oldMode;

stdlib/public/runtime/RuntimeInvocationsTracking.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,47 +61,47 @@ using RuntimeFunctionCountersUpdateHandler =
6161

6262
/// Get the runtime object state associated with an object and store it
6363
/// into the result.
64-
SWIFT_RUNTIME_EXPORT void
64+
SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT void
6565
_swift_getObjectRuntimeFunctionCounters(HeapObject *object,
6666
RuntimeFunctionCountersState *result);
6767

6868
/// Get the global runtime state containing the total numbers of invocations for
6969
/// each runtime function of interest and store it into the result.
70-
SWIFT_RUNTIME_EXPORT void _swift_getGlobalRuntimeFunctionCounters(
70+
SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT void _swift_getGlobalRuntimeFunctionCounters(
7171
swift::RuntimeFunctionCountersState *result);
7272

7373
/// Return the names of the runtime functions being tracked.
7474
/// Their order is the same as the order of the counters in the
7575
/// RuntimeObjectState structure.
76-
SWIFT_RUNTIME_EXPORT const char **_swift_getRuntimeFunctionNames();
76+
SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT const char **_swift_getRuntimeFunctionNames();
7777

7878
/// Return the offsets of the runtime function counters being tracked.
7979
/// Their order is the same as the order of the counters in the
8080
/// RuntimeFunctionCountersState structure.
81-
SWIFT_RUNTIME_EXPORT const uint16_t *_swift_getRuntimeFunctionCountersOffsets();
81+
SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT const uint16_t *_swift_getRuntimeFunctionCountersOffsets();
8282

8383
/// Return the number of runtime functions being tracked.
84-
SWIFT_RUNTIME_EXPORT uint64_t _swift_getNumRuntimeFunctionCounters();
84+
SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT uint64_t _swift_getNumRuntimeFunctionCounters();
8585

8686
/// Dump all per-object runtime function pointers.
8787
SWIFT_RUNTIME_EXPORT void _swift_dumpObjectsRuntimeFunctionPointers();
8888

8989
/// Set mode for global runtime function counters.
9090
/// Return the old value of this flag.
91-
SWIFT_RUNTIME_EXPORT int
91+
SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT int
9292
_swift_setPerObjectRuntimeFunctionCountersMode(int mode);
9393

9494
/// Set mode for per object runtime function counters.
9595
/// Return the old value of this flag.
96-
SWIFT_RUNTIME_EXPORT int _swift_setGlobalRuntimeFunctionCountersMode(int mode);
96+
SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT int _swift_setGlobalRuntimeFunctionCountersMode(int mode);
9797

9898
/// Set the global runtime state of function pointers from a provided state.
99-
SWIFT_RUNTIME_EXPORT void _swift_setGlobalRuntimeFunctionCounters(
99+
SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT void _swift_setGlobalRuntimeFunctionCounters(
100100
swift::RuntimeFunctionCountersState *state);
101101

102102
/// Set the runtime object state associated with an object from a provided
103103
/// state.
104-
SWIFT_RUNTIME_EXPORT void
104+
SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT void
105105
_swift_setObjectRuntimeFunctionCounters(HeapObject *object,
106106
RuntimeFunctionCountersState *state);
107107

0 commit comments

Comments
 (0)