Skip to content

Commit fe78930

Browse files
[lldb][nfc] Prepare OperatingSystemSwiftTasks for vectorized memory reads part 2
This NFC commit converts the helper function FindTaskId into FindTaskIds, operating on an array of task addresses instead of a single task address. This will make the subsequent diff to use a vectorized memory read much smaller. (cherry picked from commit fcab7f1)
1 parent 3ad6f45 commit fe78930

File tree

1 file changed

+55
-30
lines changed

1 file changed

+55
-30
lines changed

lldb/source/Plugins/OperatingSystem/SwiftTasks/OperatingSystemSwiftTasks.cpp

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -179,21 +179,52 @@ FindTaskAddresses(TaskInspector &task_inspector,
179179
return task_addrs;
180180
}
181181

182-
static std::optional<uint64_t> FindTaskId(addr_t task_addr, Process &process) {
183-
size_t ptr_size = process.GetAddressByteSize();
182+
static llvm::SmallVector<std::optional<uint64_t>>
183+
FindTaskIds(llvm::ArrayRef<std::optional<addr_t>> maybe_task_addrs,
184+
Process &process) {
185+
llvm::SmallVector<std::optional<uint64_t>> final_results;
186+
llvm::SmallVector<addr_t> to_read;
187+
final_results.reserve(maybe_task_addrs.size());
188+
to_read.reserve(maybe_task_addrs.size());
189+
184190
// Offset of a Task ID inside a Task data structure, guaranteed by the ABI.
185191
// See Job in swift/RemoteInspection/RuntimeInternals.h.
192+
size_t ptr_size = process.GetAddressByteSize();
186193
const offset_t job_id_offset = 4 * ptr_size + 4;
187194

188-
Status error;
189-
// The Task ID is at offset job_id_offset from the Task pointer.
190-
constexpr uint32_t num_bytes_task_id = 4;
191-
auto task_id = process.ReadUnsignedIntegerFromMemory(
192-
task_addr + job_id_offset, num_bytes_task_id, LLDB_INVALID_ADDRESS,
193-
error);
194-
if (error.Fail())
195-
return {};
196-
return task_id;
195+
/// Propagate input errors directly to the output, forward proper inputs to
196+
/// `to_read`.
197+
for (std::optional<addr_t> maybe_ptr : maybe_task_addrs) {
198+
if (!maybe_ptr)
199+
final_results.emplace_back(std::nullopt);
200+
else {
201+
final_results.push_back(0);
202+
to_read.push_back(*maybe_ptr + job_id_offset);
203+
}
204+
}
205+
206+
/// TODO: replace this loop with a vectorized memory read.
207+
llvm::SmallVector<std::optional<addr_t>> read_results;
208+
for (addr_t task_id_addr : to_read) {
209+
Status error;
210+
// The Task ID is at offset job_id_offset from the Task pointer.
211+
constexpr uint32_t num_bytes_task_id = 4;
212+
auto task_id = process.ReadUnsignedIntegerFromMemory(
213+
task_id_addr, num_bytes_task_id, LLDB_INVALID_ADDRESS, error);
214+
if (error.Fail())
215+
read_results.push_back(std::nullopt);
216+
else
217+
read_results.push_back(task_id);
218+
}
219+
220+
// Move the results into the slots not filled by errors from the input.
221+
llvm::ArrayRef<std::optional<addr_t>> results_ref = read_results;
222+
for (std::optional<addr_t> &maybe_result : final_results)
223+
if (maybe_result)
224+
maybe_result = results_ref.consume_front();
225+
assert(results_ref.empty());
226+
227+
return final_results;
197228
}
198229

199230
bool OperatingSystemSwiftTasks::UpdateThreadList(ThreadList &old_thread_list,
@@ -207,35 +238,29 @@ bool OperatingSystemSwiftTasks::UpdateThreadList(ThreadList &old_thread_list,
207238
llvm::SmallVector<std::optional<addr_t>> task_addrs =
208239
FindTaskAddresses(m_task_inspector, locked_core_threads);
209240

210-
for (const auto &[real_thread, task_addr] :
211-
llvm::zip(locked_core_threads, task_addrs)) {
212-
// If this is not a thread running a Task, add it to the list as is.
213-
if (!task_addr) {
214-
new_thread_list.AddThread(real_thread);
215-
LLDB_LOGF(log,
216-
"OperatingSystemSwiftTasks: thread %" PRIx64
217-
" is not executing a Task",
218-
real_thread->GetID());
219-
continue;
220-
}
241+
assert(m_process != nullptr);
242+
llvm::SmallVector<std::optional<addr_t>> task_ids =
243+
FindTaskIds(task_addrs, *m_process);
221244

222-
assert(m_process != nullptr);
223-
std::optional<uint64_t> task_id = FindTaskId(*task_addr, *m_process);
245+
for (const auto &[real_thread, task_addr, task_id] :
246+
llvm::zip(locked_core_threads, task_addrs, task_ids)) {
247+
// If this is not a thread running a Task, add it to the list as is.
224248
if (!task_id) {
225-
LLDB_LOG(log, "OperatingSystemSwiftTasks: could not get ID of Task {0:x}",
226-
*task_addr);
249+
LLDB_LOG(log,
250+
"OperatingSystemSwiftTasks: thread {0:x} is not running a task",
251+
real_thread->GetID());
252+
new_thread_list.AddThread(real_thread);
227253
continue;
228254
}
229255

256+
LLDB_LOG(log, "OperatingSystemSwiftTasks: TID to task ID: {0:x} -> {1:x}",
257+
real_thread->GetID(), *task_id);
230258
ThreadSP swift_thread = FindOrCreateSwiftThread(old_thread_list, *task_id);
231259
static_cast<SwiftTaskThreadMemory &>(*swift_thread)
232260
.UpdateBackingThread(real_thread, *task_addr);
233261
new_thread_list.AddThread(swift_thread);
234-
LLDB_LOGF(log,
235-
"OperatingSystemSwiftTasks: mapping thread IDs: %" PRIx64
236-
" -> %" PRIx64,
237-
real_thread->GetID(), swift_thread->GetID());
238262
}
263+
239264
return true;
240265
}
241266

0 commit comments

Comments
 (0)