Skip to content

Commit 6e9224c

Browse files
committed
[Distributed] Introduce SWIFT_DUMP_ACCESSIBLE_FUNCTIONS
1 parent 138f145 commit 6e9224c

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

stdlib/public/runtime/AccessibleFunction.cpp

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
#include "swift/Demangling/Demangler.h"
2121
#include "swift/Runtime/AccessibleFunction.h"
2222
#include "swift/Runtime/Concurrent.h"
23+
#include "swift/Runtime/EnvironmentVariables.h"
2324
#include "swift/Runtime/Metadata.h"
25+
#include "swift/Threading/Once.h"
2426
#include "Tracing.h"
2527

2628
#include <cstdint>
@@ -98,6 +100,29 @@ static Lazy<AccessibleFunctionsState> Functions;
98100

99101
} // end anonymous namespace
100102

103+
LLVM_ATTRIBUTE_UNUSED
104+
static void _dumpAccessibleFunctionRecords(void *context) {
105+
auto &S = Functions.get();
106+
107+
fprintf(stderr, "==== Accessible Function Records ====\n");
108+
int count = 0;
109+
for (const auto &section : S.SectionsToScan.snapshot()) {
110+
for (auto &record : section) {
111+
auto recordName =
112+
swift::Demangle::makeSymbolicMangledNameStringRef(record.Name.get());
113+
auto demangledRecordName =
114+
swift::Demangle::demangleSymbolAsString(recordName);
115+
fprintf(stderr, "Record name: %s\n", recordName.data());
116+
fprintf(stderr, " Demangled: %s\n", demangledRecordName.c_str());
117+
fprintf(stderr, " Function Ptr: %p\n", record.Function.get());
118+
fprintf(stderr, " Flags.IsDistributed: %d\n", record.Flags.isDistributed());
119+
++count;
120+
}
121+
}
122+
fprintf(stderr, "Record count: %d\n", count);
123+
fprintf(stderr, "==== End of Accessible Function Records ====\n");
124+
}
125+
101126
static void _registerAccessibleFunctions(AccessibleFunctionsState &C,
102127
AccessibleFunctionsSection section) {
103128
C.SectionsToScan.push_back(section);
@@ -119,27 +144,6 @@ void swift::addImageAccessibleFunctionsBlockCallback(
119144
addImageAccessibleFunctionsBlockCallbackUnsafe(baseAddress, functions, size);
120145
}
121146

122-
// TODO(distributed): expose dumping records via a flag
123-
LLVM_ATTRIBUTE_UNUSED
124-
static void _dumpAccessibleFunctionRecords() {
125-
auto &S = Functions.get();
126-
127-
fprintf(stderr, "==== Accessible Function Records ====\n");
128-
int count = 0;
129-
for (const auto &section : S.SectionsToScan.snapshot()) {
130-
for (auto &record : section) {
131-
auto recordName =
132-
swift::Demangle::makeSymbolicMangledNameStringRef(record.Name.get());
133-
fprintf(stderr, "Record name: %s\n", recordName.data());
134-
fprintf(stderr, " Function Ptr: %p\n", record.Function.get());
135-
fprintf(stderr, " Flags.IsDistributed: %d\n", record.Flags.isDistributed());
136-
++count;
137-
}
138-
}
139-
fprintf(stderr, "Record count: %d\n", count);
140-
fprintf(stderr, "==== End of Accessible Function Records ====\n");
141-
}
142-
143147
static const AccessibleFunctionRecord *
144148
_searchForFunctionRecord(AccessibleFunctionsState &S, llvm::StringRef name) {
145149
auto traceState = runtime::trace::accessible_function_scan_begin(name);
@@ -160,13 +164,19 @@ const AccessibleFunctionRecord *
160164
swift::runtime::swift_findAccessibleFunction(const char *targetNameStart,
161165
size_t targetNameLength) {
162166
auto &S = Functions.get();
163-
164167
llvm::StringRef name{targetNameStart, targetNameLength};
168+
169+
if (swift::runtime::environment::SWIFT_DUMP_ACCESSIBLE_FUNCTIONS()) {
170+
static swift::once_t dumpAccessibleFunctionsToken;
171+
swift::once(dumpAccessibleFunctionsToken, _dumpAccessibleFunctionRecords, nullptr);
172+
}
173+
165174
// Look for an existing entry.
166175
{
167176
auto snapshot = S.Cache.snapshot();
168-
if (auto E = snapshot.find(name))
177+
if (auto E = snapshot.find(name)) {
169178
return E->getRecord();
179+
}
170180
}
171181

172182
// If entry doesn't exist (either record doesn't exist, hasn't been loaded, or

stdlib/public/runtime/EnvironmentVariables.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,10 @@ VARIABLE(SWIFT_IS_CURRENT_EXECUTOR_LEGACY_MODE_OVERRIDE, string, "",
128128
" 'legacy' (Legacy behavior), "
129129
" 'swift6' (Swift 6.0+ behavior)")
130130

131+
VARIABLE(SWIFT_DUMP_ACCESSIBLE_FUNCTIONS, string, "",
132+
"Dump a listing of all 'AccessibleFunctionRecord's upon first access. "
133+
"These are used to obtain function pointers from accessible function "
134+
"record names, e.g. by the Distributed runtime to invoke distributed "
135+
"functions.")
136+
131137
#undef VARIABLE

0 commit comments

Comments
 (0)