Skip to content

Commit

Permalink
Add swiftcc attribute for runtime functions called from Swift
Browse files Browse the repository at this point in the history
  • Loading branch information
kateinoigakukun committed Mar 14, 2020
1 parent b8e40c0 commit a4814df
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 24 deletions.
1 change: 1 addition & 0 deletions include/swift/Demangling/Demangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ llvm::StringRef makeSymbolicMangledNameStringRef(const char *base);
//// define what these will be.
/// \returns the demangled name. Returns nullptr if the input String is not a
/// Swift mangled name.
SWIFT_CC(swift)
SWIFT_RUNTIME_EXPORT
char *swift_demangle(const char *mangledName,
size_t mangledNameLength,
Expand Down
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
15 changes: 11 additions & 4 deletions lib/IRGen/GenBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,17 @@ if (Builtin.ID == BuiltinValueKind::id) { \
call->addAttribute(llvm::AttributeList::FirstArgIndex + 1,
llvm::Attribute::ReadOnly);

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

IGF.Builder.CreateStore(llvm::ConstantPointerNull::get(IGF.IGM.ErrorPtrTy),
errorBuffer);
Expand Down
7 changes: 6 additions & 1 deletion lib/IRGen/GenKeyPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ getLayoutFunctionForComputedComponent(IRGenModule &IGM,

auto layoutFn = llvm::Function::Create(fnTy,
llvm::GlobalValue::PrivateLinkage, "keypath_get_arg_layout", IGM.getModule());

layoutFn->setCallingConv(IGM.SwiftCC);

{
IRGenFunction IGF(IGM, layoutFn);
if (IGM.DebugInfo)
Expand Down Expand Up @@ -378,6 +379,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;

IRGenFunction IGF(IGM, destroyFn);
Expand Down Expand Up @@ -426,6 +428,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;

IRGenFunction IGF(IGM, copyFn);
Expand Down Expand Up @@ -539,6 +542,8 @@ getInitializerForComputedComponent(IRGenModule &IGM,

auto initFn = llvm::Function::Create(fnTy,
llvm::GlobalValue::PrivateLinkage, "keypath_arg_init", IGM.getModule());
initFn->setCallingConv(IGM.SwiftCC);


{
IRGenFunction IGF(IGM, initFn);
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 @@ -74,17 +74,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
1 change: 1 addition & 0 deletions stdlib/public/runtime/Demangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,7 @@ swift::_swift_buildDemanglingForMetadata(const Metadata *type,
// NB: This function is not used directly in the Swift codebase, but is
// exported for Xcode support and is used by the sanitizers. Please coordinate
// before changing.
SWIFT_CC(swift)
char *swift_demangle(const char *mangledName,
size_t mangledNameLength,
char *outputBuffer,
Expand Down
12 changes: 12 additions & 0 deletions stdlib/public/runtime/ErrorObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,21 @@ SWIFT_RUNTIME_STDLIB_API
void swift_errorRelease(SwiftError *object);

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

/// Halt in response to an error.
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API LLVM_ATTRIBUTE_NORETURN
Expand Down
10 changes: 9 additions & 1 deletion stdlib/public/runtime/ErrorObjectCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,17 @@ using namespace swift;
void (*swift::_swift_willThrow)(SwiftError *error);

/// Breakpoint hook for debuggers, and calls _swift_willThrow if set.
#ifdef __wasm__
// Notes:
// The reason of this ifdef is described in header file.
SWIFT_CC(swift) void
swift::swift_willThrow(void *unused, SwiftError **error)
#else
SWIFT_CC(swift) void
swift::swift_willThrow(SWIFT_CONTEXT void *unused,
SWIFT_ERROR_RESULT SwiftError **error) {
SWIFT_ERROR_RESULT SwiftError **error)
#endif
{
// Cheap check to bail out early, since we expect there to be no callbacks
// the vast majority of the time.
if (SWIFT_LIKELY(!_swift_willThrow))
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 @@ -253,6 +253,7 @@ class BoxCacheEntry {

static SimpleGlobalCache<BoxCacheEntry> Boxes;

SWIFT_CC(swift)
BoxPair swift::swift_makeBoxUnique(OpaqueValue *buffer, const Metadata *type,
size_t alignMask) {
auto *inlineBuffer = reinterpret_cast<ValueBuffer*>(buffer);
Expand All @@ -278,6 +279,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 @@ -420,16 +422,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

0 comments on commit a4814df

Please sign in to comment.