Skip to content

Commit fcab7f1

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.
1 parent 0d00797 commit fcab7f1

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
@@ -180,21 +180,52 @@ FindTaskAddresses(TaskInspector &task_inspector,
180180
return task_addrs;
181181
}
182182

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

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

200231
bool OperatingSystemSwiftTasks::UpdateThreadList(ThreadList &old_thread_list,
@@ -208,35 +239,29 @@ bool OperatingSystemSwiftTasks::UpdateThreadList(ThreadList &old_thread_list,
208239
llvm::SmallVector<std::optional<addr_t>> task_addrs =
209240
FindTaskAddresses(m_task_inspector, locked_core_threads);
210241

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

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

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

0 commit comments

Comments
 (0)