Skip to content

[Refactor] Extract per-task adstack codegen state and helpers from TaskCodeGenLLVM into TaskCodeGenLLVMAdStack#612

Draft
duburcqa wants to merge 2 commits intomainfrom
duburcqa/codegen_llvm_adstack_factor_out
Draft

[Refactor] Extract per-task adstack codegen state and helpers from TaskCodeGenLLVM into TaskCodeGenLLVMAdStack#612
duburcqa wants to merge 2 commits intomainfrom
duburcqa/codegen_llvm_adstack_factor_out

Conversation

@duburcqa
Copy link
Copy Markdown
Contributor

@duburcqa duburcqa commented May 2, 2026

Extract per-task adstack codegen state into TaskCodeGenLLVMAdStack

Stacked on #611. Pure refactor - no behaviour change. Moves all adstack-related per-task fields and helper emission methods out of TaskCodeGenLLVM (quadrants/codegen/llvm/codegen_llvm.h) into a new TaskCodeGenLLVMAdStack class held by composition. The visitors (visit(AdStack*Stmt)) stay on TaskCodeGenLLVM and delegate to adstack_->....

Why

codegen_llvm.h had a ~110-line block of adstack-only state (19 fields: heap strides, per-stack offsets, cached SSA bases, lazy-claim row id, bootstrap pushes, captured bound-expr, ...) plus 9 helper method declarations. codegen_llvm.cpp had the matching 184 field accesses / method definitions interleaved with the rest of the LLVM codegen. Hard to read, hard to extend, dragged the heavy static_adstack_analysis.h / adstack_size_expr.h includes into every translation unit that pulls in codegen_llvm.h.

Mechanism

  • New header quadrants/codegen/llvm/codegen_llvm_adstack.h declares TaskCodeGenLLVMAdStack. Owns the 19 state fields and the 9 helper methods (ensure_heap_base_split_llvm, ensure_metadata_llvm, ensure_metadata_split_llvm, ensure_row_id_var_float_llvm, emit_row_claim_llvm, ensure_count_alloca_llvm, emit_single_slot_ptr, get_base_llvm, emit_top_slot_ptr). Holds a TaskCodeGenLLVM& back-reference for accessing builder / entry_block / llvm_context / call / get_runtime / task_codegen_id / llvm_val from the helpers.
  • New translation unit quadrants/codegen/llvm/codegen_llvm_adstack.cpp defines the helpers. Bodies are mechanical translations of the originals with builder -> codegen_.builder, entry_block -> codegen_.entry_block, etc.
  • codegen_llvm.h: the 110-line state / method block becomes std::unique_ptr<TaskCodeGenLLVMAdStack> adstack_; plus a forward-declared TaskCodeGenLLVMAdStack. ~TaskCodeGenLLVM() switched from = default in the header to an out-of-line definition in the .cpp (PIMPL pattern: required because the unique_ptr holds a forward-declared type, also keeps the heavy adstack includes confined to the .cpp side).
  • codegen_llvm.cpp: 184 field-access / method-call references rewired to adstack_->...; the 9 helper definitions deleted (now living in the new .cpp); the constructor adds one line adstack_ = std::make_unique<TaskCodeGenLLVMAdStack>(*this); at the end.
  • CMakeLists.txt: new .cpp added to the llvm_codegen target sources.

The is_compile_time_single_slot static helper in codegen_llvm.cpp stays where it is (used by the visit(AdStack*Stmt) visitors that remain on TaskCodeGenLLVM).

Tests

No new tests: pure refactor with mechanical renames. Local Metal serial: 674 passed, 1 skipped, 7 xfailed (same xfail set as origin/main); mpm_grid repro still captures src=reducer_count effective_rows=128 required_bytes=32768. Existing test_adstack_* covers every adstack codegen path through the renamed accessors.

Side-effect audit

  • Behaviour-preserving: every field access / method call is renamed, no logic moved between methods, no method bodies edited beyond field -> codegen_.field rewrites.
  • ABI-neutral on the LLVM IR side: same LLVMRuntime_get_adstack_* getter calls, same entry_block insertion order, same atomic-rmw / clamp / store sequence at the LCA-block claim site, same per-stack alloca i64 count emission.
  • Header-include reduction: every translation unit including codegen_llvm.h no longer transitively pulls static_adstack_analysis.h / adstack_size_expr.h. Compile-time savings are small but real for unrelated TUs (codegen_cpu, codegen_cuda, codegen_amdgpu, kernel_compiler, struct_llvm).

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 2, 2026

Coverage Report (ecdbc9a12)

File Coverage Missing

Diff coverage: 0% · Overall: 74% · 0 lines, 0 missing

Full annotated report

Base automatically changed from duburcqa/adstack_loop_iter_clip to main May 2, 2026 22:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant