Skip to content

[CPU] Support clz in C and C host code generation - #3609

Open
sepcnt wants to merge 1 commit into
tile-ai:mainfrom
sepcnt:cpu-clz-codegen
Open

sepcnt wants to merge 1 commit into
tile-ai:mainfrom
sepcnt:cpu-clz-codegen

Conversation

@sepcnt

@sepcnt sepcnt commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Split the C/CPU clz support out of #3265 for independent review.

Add scalar clz lowering for signed and unsigned 32-bit and 64-bit integers in the CPU C and C host code generators.

  • Share helpers using GCC/Clang builtins, MSVC intrinsics, or portable fallbacks.
  • Return the input bit width for zero and emit helpers only when needed.
  • Include CPU regression tests and C host tests covering CUDA graph capture and replay.

Summary

  • Add shared tl_clz32 and tl_clz64 helpers for C and C++ code generation. They return the input bit width for zero and use compiler builtins, MSVC intrinsics, or fallback logic.
  • Update the CPU C and C host code generators to support scalar signed and unsigned 32-bit and 64-bit tirx.clz inputs. Each generator emits the helper definitions once per initialization.
  • Add CPU tests for all four integer types. Add host tests for CUDA graph capture and replay.

C++ style / lint notes

  • The PR adds C++ code. The C++ style guide describes the C++ API style audit, but the available evidence does not show whether the changes touch a specific documented style rule.
  • CI runs the C++ API Style Audit (warning only) step. No findings or audit results were provided.

@coderabbitai

coderabbitai Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Important

Review skipped

The saved review base belongs to an older reviewed commit. This saved history cannot establish the base for an incremental review. Comment @coderabbitai full review to establish a new review baseline. No full review was started, and the last reviewed checkpoint was preserved.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
📥 Commits

Reviewing files that changed from the base of the PR and between 7f6b2f9 and 0af8b45.

📒 Files selected for processing: 1
  • src/transform/loop_vectorize.cc

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 4 remain after this review.

📝 Walkthrough

This change adds shared 32-bit and 64-bit leading-zero-count helpers for CPU and host C code generation. It also changes VectorizeLoop to pass a cloned analyzer to VectorizeRewriter. New tests cover CPU execution and CUDA host code generation across four integer dtypes.

Changes: C/C++ CLZ code generation

Layer / File(s) Summary
Shared leading-zero-count helpers
src/backend/common/codegen/c_bit_intrinsics.h
Adds 32-bit and 64-bit helpers with zero handling and compiler-specific or fallback implementations.
CPU and host codegen integration
src/backend/common/codegen/codegen_c_host.h, src/backend/common/codegen/codegen_c_host.cc, src/cpu/codegen/codegen_c.h, src/cpu/codegen/codegen_c.cc
Both code generators validate supported tirx.clz inputs, emit the shared definitions once per initialization, and call the matching width-specific helper with an unsigned argument.
CPU and CUDA-host coverage
testing/python/cpu/test_tilelang_cpu_clz.py, testing/python/jit/test_tilelang_jit_host_clz.py
Adds tests for four integer dtypes, generated helper names, leading-zero results, and CUDA graph replay outputs.

Vectorization analyzer handling

Layer / File(s) Summary
Clone analyzer for vectorization
src/transform/loop_vectorize.cc
VectorizeLoop passes a cloned analyzer to VectorizeRewriter.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Feature

Suggested labels: enhancement, cpu

Suggested reviewers: siriusneo, ljc00118, penguin-wwy

Merge Risk: ⚪ Minimal · up to 0af8b

No actionable merge-blocking risk is established from the reviewed changes.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 5.56% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 18 functions across 8 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Title check Passed The title clearly summarizes the main change: adding scalar clz support to CPU C and C host code generation.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown

👋 Hi! Thank you for contributing to the TileLang project.

Please remember to run pre-commit run --all-files in the root directory of the project to ensure your changes are properly linted and formatted. This will help ensure your contribution passes the format check.

We appreciate you taking this step! Our team will review your contribution, and we look forward to your awesome work! 🚀

@sepcnt
sepcnt marked this pull request as ready for review October 22, 2026 11:14
@coderabbitai coderabbitai Bot added cpu enhancement New feature or request labels Oct 22, 2026
@sepcnt sepcnt changed the title [CPU] Support clz in C and C host code generation [CodeGen] Support clz across TileLang targets Oct 22, 2026
@sepcnt sepcnt changed the title [CodeGen] Support clz across TileLang targets [CPU] Support clz in C and C host code generation Oct 22, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cpu enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant