Skip to content

Commit 5c41503

Browse files
committed
[Perf] Drop redundant per-launch update_adstack_sizing_info_size_exprs + use find() over operator[] in arr_reads kNone bump
1 parent d987e9f commit 5c41503

3 files changed

Lines changed: 7 additions & 20 deletions

File tree

docs/source/user_guide/autodiff.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ The evaluation happens in different places depending on the backend, but the res
289289

290290
Either way, the per-thread stride and each adstack's offset / max-size land in a small buffer the main kernel reads on every push. The backing heap grows on demand to match the largest size any launch has needed so far, and is reused across subsequent launches - you do not need to reserve memory up front.
291291

292-
The sized result is cached per task and reused while the formula's inputs are unchanged. Although changing loop bounds at runtime is possible, it comes with some limitations for performance reasons. See [What can go wrong](#what-can-go-wrong) for details.
292+
The sized result is cached per task and reused while the loop bounds are unchanged. Although changing loop bounds at runtime is possible, it comes with some limitations for performance reasons. See [What can go wrong](#what-can-go-wrong) for details.
293293

294294
The on-device sizer relies on two common hardware features (64-bit integer arithmetic and raw-pointer storage-buffer access). Every mainstream GPU from late 2018 onward supports both.
295295

quadrants/program/adstack_size_expr_eval.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,10 +1869,9 @@ void bump_writes_for_kernel_llvm(Program *prog,
18691869
// tracking is invisible to the metadata cache without an explicit bump. Mirrors the SPIR-V `kone_h2d_blit`
18701870
// rule in `bump_writes_for_kernel_spirv`.
18711871
for (int arg_id : task.arr_reads) {
1872-
if (static_cast<size_t>(arg_id) >= ctx->device_allocation_type.size()) {
1873-
continue;
1874-
}
1875-
if (ctx->device_allocation_type[arg_id] != LaunchContextBuilder::DevAllocType::kNone) {
1872+
auto type_it = ctx->device_allocation_type.find(arg_id);
1873+
if (type_it == ctx->device_allocation_type.end() ||
1874+
type_it->second != LaunchContextBuilder::DevAllocType::kNone) {
18761875
continue;
18771876
}
18781877
bump_data_ptr(arg_id);
@@ -1910,10 +1909,9 @@ void bump_writes_for_kernel_llvm(Program *prog,
19101909
// capture per-task arr_reads); skip the loop without raising.
19111910
for (const auto &task_args : arr_reads_per_task) {
19121911
for (int arg_id : task_args) {
1913-
if (static_cast<size_t>(arg_id) >= ctx->device_allocation_type.size()) {
1914-
continue;
1915-
}
1916-
if (ctx->device_allocation_type[arg_id] != LaunchContextBuilder::DevAllocType::kNone) {
1912+
auto type_it = ctx->device_allocation_type.find(arg_id);
1913+
if (type_it == ctx->device_allocation_type.end() ||
1914+
type_it->second != LaunchContextBuilder::DevAllocType::kNone) {
19171915
continue;
19181916
}
19191917
bump_data_ptr(arg_id);

quadrants/runtime/llvm/llvm_adstack_lazy_claim.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -691,17 +691,6 @@ std::size_t LlvmRuntimeExecutor::publish_adstack_metadata(const AdStackSizingInf
691691
if (n_stacks == 0 || num_threads == 0) {
692692
return 0;
693693
}
694-
// Refresh the registry entry's `size_exprs` snapshot from the live (post-push_back, stable)
695-
// ad_stack. The codegen-time registration filled `size_exprs` from `&current_task->ad_stack` but
696-
// that pointer's underlying `unique_ptr<OffloadedTask>` heap was freed by the launcher's
697-
// `current_task = nullptr` after `offloaded_tasks.push_back(*current_task)`; the registry's
698-
// snapshot is now backed by its own heap-owned copy, so this refresh just keeps it in sync with
699-
// any post-codegen mutations a backend might apply to `ad_stack.size_exprs`. Idempotent and
700-
// bounded-cost: copies one `std::vector<SerializedSizeExpr>` per launch per task with adstacks.
701-
if (program_impl_ != nullptr && program_impl_->program != nullptr && ad_stack.registry_id != 0) {
702-
program_impl_->program->adstack_cache().update_adstack_sizing_info_size_exprs(ad_stack.registry_id,
703-
ad_stack.size_exprs);
704-
}
705694
auto align_up_8 = [](std::size_t n) -> std::size_t { return (n + 7u) & ~std::size_t{7u}; };
706695
// Allocate / grow the two device-side metadata arrays. Capacity is in u64 entries, kept at or above n_stacks.
707696
// On GPU these buffers are written exclusively by the device-side sizer kernel (`runtime_eval_adstack_size_expr`);

0 commit comments

Comments
 (0)