Skip to content

Add swiftcc attribute for runtime functions called from Swift #30938

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

Closed
Closed
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
3 changes: 3 additions & 0 deletions include/swift/Runtime/HeapObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,13 @@ void swift_nonatomic_release_n(HeapObject *object, uint32_t n);

// Refcounting observation hooks for memory tools. Don't use these.
SWIFT_RUNTIME_EXPORT
SWIFT_CC(swift)
size_t swift_retainCount(HeapObject *object);
SWIFT_RUNTIME_EXPORT
SWIFT_CC(swift)
size_t swift_unownedRetainCount(HeapObject *object);
SWIFT_RUNTIME_EXPORT
SWIFT_CC(swift)
size_t swift_weakRetainCount(HeapObject *object);

/// Is this pointer a non-null unique reference to an object?
Expand Down
8 changes: 6 additions & 2 deletions lib/IRGen/GenKeyPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ getLayoutFunctionForComputedComponent(IRGenModule &IGM,
auto layoutFn = llvm::Function::Create(fnTy,
llvm::GlobalValue::PrivateLinkage, "keypath_get_arg_layout", IGM.getModule());
layoutFn->setAttributes(IGM.constructInitialAttributes());

layoutFn->setCallingConv(IGM.SwiftCC);

{
IRGenFunction IGF(IGM, layoutFn);
if (IGM.DebugInfo)
Expand Down Expand Up @@ -381,6 +382,7 @@ getWitnessTableForComputedComponent(IRGenModule &IGM,
/*vararg*/ false);
auto destroyFn = llvm::Function::Create(destroyType,
llvm::GlobalValue::PrivateLinkage, "keypath_destroy", IGM.getModule());
destroyFn->setCallingConv(IGM.SwiftCC);
destroy = destroyFn;
destroyFn->setAttributes(IGM.constructInitialAttributes());

Expand Down Expand Up @@ -430,6 +432,7 @@ getWitnessTableForComputedComponent(IRGenModule &IGM,
/*vararg*/ false);
auto copyFn = llvm::Function::Create(copyType,
llvm::GlobalValue::PrivateLinkage, "keypath_copy", IGM.getModule());
copyFn->setCallingConv(IGM.SwiftCC);
copy = copyFn;
copyFn->setAttributes(IGM.constructInitialAttributes());

Expand Down Expand Up @@ -545,7 +548,8 @@ getInitializerForComputedComponent(IRGenModule &IGM,
auto initFn = llvm::Function::Create(fnTy,
llvm::GlobalValue::PrivateLinkage, "keypath_arg_init", IGM.getModule());
initFn->setAttributes(IGM.constructInitialAttributes());

initFn->setCallingConv(IGM.SwiftCC);

{
IRGenFunction IGF(IGM, initFn);
if (IGM.DebugInfo)
Expand Down
8 changes: 8 additions & 0 deletions stdlib/public/SwiftShims/HeapObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,25 @@ struct HeapObject {
#ifdef __cplusplus
extern "C" {
#endif
#if __has_attribute(swiftcall)
#define SWIFT_CC_swift __attribute__((swiftcall))
#else
#define SWIFT_CC_swift
#endif

SWIFT_RUNTIME_STDLIB_API
void _swift_instantiateInertHeapObject(void *address,
const HeapMetadata *metadata);

SWIFT_CC_swift
SWIFT_RUNTIME_STDLIB_API
__swift_size_t swift_retainCount(HeapObject *obj);

SWIFT_CC_swift
SWIFT_RUNTIME_STDLIB_API
__swift_size_t swift_unownedRetainCount(HeapObject *obj);

SWIFT_CC_swift
SWIFT_RUNTIME_STDLIB_API
__swift_size_t swift_weakRetainCount(HeapObject *obj);

Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/runtime/Demangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ swift::_swift_buildDemanglingForMetadata(const Metadata *type,
//// define what these will be.
/// \returns the demangled name. Returns nullptr if the input String is not a
/// Swift mangled name.
SWIFT_RUNTIME_EXPORT
SWIFT_CC(swift)
char *swift_demangle(const char *mangledName,
size_t mangledNameLength,
char *outputBuffer,
Expand Down
5 changes: 5 additions & 0 deletions stdlib/public/runtime/HeapObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ class BoxCacheEntry {

static SimpleGlobalCache<BoxCacheEntry, BoxesTag> Boxes;

SWIFT_CC(swift)
BoxPair swift::swift_makeBoxUnique(OpaqueValue *buffer, const Metadata *type,
size_t alignMask) {
auto *inlineBuffer = reinterpret_cast<ValueBuffer*>(buffer);
Expand All @@ -277,6 +278,7 @@ BoxPair swift::swift_makeBoxUnique(OpaqueValue *buffer, const Metadata *type,
}
}

SWIFT_CC(swift)
BoxPair swift::swift_allocBox(const Metadata *type) {
// Get the heap metadata for the box.
auto metadata = &Boxes.getOrInsert(type).first->Data;
Expand Down Expand Up @@ -439,16 +441,19 @@ void swift::swift_nonatomic_release_n(HeapObject *object, uint32_t n) {
object->refCounts.decrementAndMaybeDeinitNonAtomic(n);
}

SWIFT_CC(swift)
size_t swift::swift_retainCount(HeapObject *object) {
if (isValidPointerForNativeRetain(object))
return object->refCounts.getCount();
return 0;
}

SWIFT_CC(swift)
size_t swift::swift_unownedRetainCount(HeapObject *object) {
return object->refCounts.getUnownedCount();
}

SWIFT_CC(swift)
size_t swift::swift_weakRetainCount(HeapObject *object) {
return object->refCounts.getWeakCount();
}
Expand Down
2 changes: 2 additions & 0 deletions stdlib/public/runtime/Numeric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ static T convert(IntegerLiteral value) {
return result;
}

SWIFT_CC(swift)
float swift::swift_intToFloat32(IntegerLiteral value) {
return convert<float>(value);
}

SWIFT_CC(swift)
double swift::swift_intToFloat64(IntegerLiteral value) {
return convert<double>(value);
}
18 changes: 9 additions & 9 deletions stdlib/public/runtime/RuntimeInvocationsTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ static uint16_t RuntimeFunctionCountersOffsets[] = {
/// Public APIs

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

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

/// Get the global runtime state containing the total numbers of invocations for
/// each runtime function of interest.
void _swift_getGlobalRuntimeFunctionCounters(
SWIFT_CC(swift) void _swift_getGlobalRuntimeFunctionCounters(
RuntimeFunctionCountersState *result) {
StaticScopedReadLock lock(RuntimeGlobalFunctionCountersState.Lock);
*result = RuntimeGlobalFunctionCountersState.State;
}

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

/// Return the offsets of the runtime function counters being tracked.
/// Their order is the same as the order of the counters in the
/// RuntimeObjectState structure.
const uint16_t *_swift_getRuntimeFunctionCountersOffsets() {
SWIFT_CC(swift) const uint16_t *_swift_getRuntimeFunctionCountersOffsets() {
return RuntimeFunctionCountersOffsets;
}

/// Return the number of runtime functions being tracked.
uint64_t _swift_getNumRuntimeFunctionCounters() {
SWIFT_CC(swift) uint64_t _swift_getNumRuntimeFunctionCounters() {
return ID_LastRuntimeFunctionName;
}

Expand Down Expand Up @@ -202,15 +202,15 @@ void _swift_dumpObjectsRuntimeFunctionPointers() {

/// Set mode for global runtime function counters.
/// Return the old value of this flag.
int _swift_setGlobalRuntimeFunctionCountersMode(int mode) {
SWIFT_CC(swift) int _swift_setGlobalRuntimeFunctionCountersMode(int mode) {
int oldMode = UpdateGlobalRuntimeFunctionCounters;
UpdateGlobalRuntimeFunctionCounters = mode ? 1 : 0;
return oldMode;
}

/// Set mode for per object runtime function counters.
/// Return the old value of this flag.
int _swift_setPerObjectRuntimeFunctionCountersMode(int mode) {
SWIFT_CC(swift) int _swift_setPerObjectRuntimeFunctionCountersMode(int mode) {
int oldMode = UpdatePerObjectRuntimeFunctionCounters;
UpdatePerObjectRuntimeFunctionCounters = mode ? 1 : 0;
return oldMode;
Expand Down
18 changes: 9 additions & 9 deletions stdlib/public/runtime/RuntimeInvocationsTracking.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,47 +61,47 @@ using RuntimeFunctionCountersUpdateHandler =

/// Get the runtime object state associated with an object and store it
/// into the result.
SWIFT_RUNTIME_EXPORT void
SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT void
_swift_getObjectRuntimeFunctionCounters(HeapObject *object,
RuntimeFunctionCountersState *result);

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

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

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

/// Return the number of runtime functions being tracked.
SWIFT_RUNTIME_EXPORT uint64_t _swift_getNumRuntimeFunctionCounters();
SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT uint64_t _swift_getNumRuntimeFunctionCounters();

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

/// Set mode for global runtime function counters.
/// Return the old value of this flag.
SWIFT_RUNTIME_EXPORT int
SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT int
_swift_setPerObjectRuntimeFunctionCountersMode(int mode);

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

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

/// Set the runtime object state associated with an object from a provided
/// state.
SWIFT_RUNTIME_EXPORT void
SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT void
_swift_setObjectRuntimeFunctionCounters(HeapObject *object,
RuntimeFunctionCountersState *state);

Expand Down