Skip to content

[Refactor][CodeGen] Refactor CodeGen part for multi-backend decoupling#2121

Merged
SiriusNEO merged 1 commit into
tile-ai:mainfrom
SiriusNEO:chaofan/refactor_cmake_0428
May 6, 2026
Merged

[Refactor][CodeGen] Refactor CodeGen part for multi-backend decoupling#2121
SiriusNEO merged 1 commit into
tile-ai:mainfrom
SiriusNEO:chaofan/refactor_cmake_0428

Conversation

@SiriusNEO
Copy link
Copy Markdown
Collaborator

@SiriusNEO SiriusNEO commented Apr 29, 2026

As titled. Currently we put the c and c_host under the original folder. Note that metal backend uses CodeGenC to build. So the wrapper under backend/metal/codegen is just a proxy.

Summary by CodeRabbit

  • New Features

    • Added Metal backend codegen/compile entry to build TileLang for Metal.
    • Added a helper to construct TileLang C-host modules from an IR module and target.
  • Chores

    • Reorganized build inputs and source layout across CUDA, ROCm, and Metal backends.
    • Updated header include paths to match the restructured source organization.

@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! 🚀

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 29, 2026

📝 Walkthrough

Walkthrough

This PR moves backend-specific codegen implementations and stub sources from src/target/... into backend-local paths under src/backend/{cuda,metal,rocm}/codegen/, updates CMake inputs and many #include directives to match the new layout, adds a Metal FFI entry point, and declares a public tl::BuildTileLangCHost(IRModule, Target) helper.

Changes

Backend relocation & include refactor

Layer / File(s) Summary
Root build config
CMakeLists.txt
Prepends src/ to include paths and replaces intrin_rule*.cc glob with explicit backend intrin_rule files (intrin_rule_cuda.cc, intrin_rule_hip.cc).
Backend CMake wiring
src/backend/cuda/CMakeLists.txt, src/backend/rocm/CMakeLists.txt, src/backend/metal/CMakeLists.txt
Relocates stub library sources and codegen/runtime source lists from src/target/... to src/backend/.../codegen/... paths and updates per-backend TILE_LANG_*_SRCS lists.
CUDA codegen includes
src/backend/cuda/codegen/*.cc
Switches multiple files from relative ../ includes to project-root style includes (e.g., backend/cuda/codegen/ptx.h, target/codegen_utils.h, target/utils.h, transform/common/attr.h).
ROCm codegen includes
src/backend/rocm/codegen/*.cc
Adjusts include paths to new layout (e.g., op/builtin.h, target/utils.h, support/ffi_aliases.h).
Cross-module includes
src/op/builtin.cc, src/op/utils.h, src/runtime/runtime.cc
Updated references to CUDA stub headers and target utilities to backend/cuda/codegen/stubs/... and target/... paths.

Metal entry + public C host API

Layer / File(s) Summary
Public API (declaration)
src/target/codegen_c_host.h
Adds declaration: ::tvm::ffi::Module BuildTileLangCHost(::tvm::IRModule mod, ::tvm::Target target); in tvm::tl.
Metal runtime module
src/backend/metal/codegen/rt_mod_metal.cc
Adds BuildTileLangMetal(IRModule, Target) which delegates to tl::BuildTileLangCHost and registers it as FFI global "target.build.tilelang_metal".
Build wiring
src/backend/metal/CMakeLists.txt
Switches rt_mod_metal.cc source in the Metal backend build to src/backend/metal/codegen/rt_mod_metal.cc.

Sequence Diagram(s)

sequenceDiagram
    participant Caller as Client / FFI caller
    participant Registry as TVM FFI registry
    participant MetalEntrypoint as BuildTileLangMetal
    participant CHost as tl::BuildTileLangCHost
    Caller->>Registry: invoke "target.build.tilelang_metal"(IRModule, Target)
    Registry->>MetalEntrypoint: call BuildTileLangMetal(IRModule, Target)
    MetalEntrypoint->>CHost: delegate to BuildTileLangCHost(mod, target)
    CHost->>CHost: generate C host module (Metal context respected)
    CHost-->>Registry: return ffi::Module
    Registry-->>Caller: return ffi::Module
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related issues

Possibly related PRs

Suggested labels

enhancement

Suggested reviewers

  • LeiWang1999
  • oraluben

Poem

🐰
I hopped through paths both new and old,
Moved stubs and code to tidy fold,
Metal woke with a registered song,
C‑host helper joins the throng,
Builds now march with bunny-bold.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main objective of the pull request: refactoring CodeGen to decouple multi-backend support, which aligns with the substantial reorganization of CUDA, ROCm, and Metal backend files across the changeset.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@SiriusNEO SiriusNEO force-pushed the chaofan/refactor_cmake_0428 branch from f941a85 to 423b0bf Compare May 6, 2026 10:44
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
src/backend/cuda/codegen/codegen_cutedsl.cc (1)

2-3: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Stale \file path in doc comment.

The file has moved to src/backend/cuda/codegen/codegen_cutedsl.cc but the doxygen tag still references the old location.

📝 Proposed fix
-/*!
- * \file target/codegen_cutedsl.cc
- */
+/*!
+ * \file backend/cuda/codegen/codegen_cutedsl.cc
+ */
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/backend/cuda/codegen/codegen_cutedsl.cc` around lines 2 - 3, Update the
doxygen file tag at the top of this source: replace the stale "\file
target/codegen_cutedsl.cc" comment with the current path "\file
src/backend/cuda/codegen/codegen_cutedsl.cc" so the documentation reflects the
file's new location; locate the doxygen header in codegen_cutedsl.cc and update
the \file tag accordingly.
src/backend/cuda/codegen/codegen_cuda.cc (1)

2-2: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Update stale \file doxygen path.

The file has been relocated to src/backend/cuda/codegen/codegen_cuda.cc, but the doc comment still references the old path target/codegen.cc.

📝 Proposed fix
-/*!
- * \file target/codegen.cc
- */
+/*!
+ * \file backend/cuda/codegen/codegen_cuda.cc
+ */
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/backend/cuda/codegen/codegen_cuda.cc` at line 2, Update the stale doxygen
file tag: replace the existing "\file target/codegen.cc" doc comment with the
correct path for this translation unit (e.g. "\file
src/backend/cuda/codegen/codegen_cuda.cc") so the top-of-file doxygen \file tag
in codegen_cuda.cc matches the file's new location.
🧹 Nitpick comments (2)
src/backend/metal/codegen/rt_mod_metal.cc (1)

1-27: 💤 Low value

Metal FFI proxy looks correct; cppcheck warning is a false positive.

BuildTileLangMetal cleanly delegates to tl::BuildTileLangCHost, and the FFI registration via TVM_FFI_STATIC_INIT_BLOCK is the standard pattern. The cppcheck "syntax error" reported on line 21 is the usual false positive that tools raise on TVM's FFI init macros and can be ignored.

One small note: since BuildTileLangMetal is only referenced via the FFI registry, consider giving it internal linkage (static or anonymous namespace) to avoid leaking a symbol into tvm::codegen.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/backend/metal/codegen/rt_mod_metal.cc` around lines 1 - 27, The
BuildTileLangMetal symbol is only used via the FFI registry and can be given
internal linkage to avoid leaking a public symbol; change the free function
BuildTileLangMetal to have internal linkage (e.g., mark it static or place it in
an anonymous namespace) and keep the TVM_FFI_STATIC_INIT_BLOCK registration
intact so the refl::GlobalDef still registers "target.build.tilelang_metal" with
BuildTileLangMetal.
CMakeLists.txt (1)

174-174: 💤 Low value

Include precedence: prepending src/ at position 0 may shadow TVM headers.

list(INSERT TILE_LANG_INCLUDES 0 ...) puts TileLang's src/ ahead of TVM_INCLUDES. TileLang's src/target/ and src/op/ overlap with TVM's own target/ and op/ namespaces in path (e.g., target/utils.h, op/builtin.h); if TVM ever exposes a same-named header through its include dirs, the TileLang one will silently win. Consider appending instead of inserting at 0, unless the override is intentional.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@CMakeLists.txt` at line 174, The current insertion "list(INSERT
TILE_LANG_INCLUDES 0 \"${CMAKE_CURRENT_SOURCE_DIR}/src\")" prepends TileLang's
src/ ahead of TVM headers and can unintentionally shadow TVM headers; change it
to append or insert after TVM include entries so TILE_LANG_INCLUDES is added
after TVM_INCLUDES (use list(APPEND TILE_LANG_INCLUDES ...) or insert at the
end) to preserve TVM include precedence and avoid accidental header overrides.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/backend/cuda/codegen/codegen_cuda.cc`:
- Line 2: Update the stale doxygen file tag: replace the existing "\file
target/codegen.cc" doc comment with the correct path for this translation unit
(e.g. "\file src/backend/cuda/codegen/codegen_cuda.cc") so the top-of-file
doxygen \file tag in codegen_cuda.cc matches the file's new location.

In `@src/backend/cuda/codegen/codegen_cutedsl.cc`:
- Around line 2-3: Update the doxygen file tag at the top of this source:
replace the stale "\file target/codegen_cutedsl.cc" comment with the current
path "\file src/backend/cuda/codegen/codegen_cutedsl.cc" so the documentation
reflects the file's new location; locate the doxygen header in
codegen_cutedsl.cc and update the \file tag accordingly.

---

Nitpick comments:
In `@CMakeLists.txt`:
- Line 174: The current insertion "list(INSERT TILE_LANG_INCLUDES 0
\"${CMAKE_CURRENT_SOURCE_DIR}/src\")" prepends TileLang's src/ ahead of TVM
headers and can unintentionally shadow TVM headers; change it to append or
insert after TVM include entries so TILE_LANG_INCLUDES is added after
TVM_INCLUDES (use list(APPEND TILE_LANG_INCLUDES ...) or insert at the end) to
preserve TVM include precedence and avoid accidental header overrides.

In `@src/backend/metal/codegen/rt_mod_metal.cc`:
- Around line 1-27: The BuildTileLangMetal symbol is only used via the FFI
registry and can be given internal linkage to avoid leaking a public symbol;
change the free function BuildTileLangMetal to have internal linkage (e.g., mark
it static or place it in an anonymous namespace) and keep the
TVM_FFI_STATIC_INIT_BLOCK registration intact so the refl::GlobalDef still
registers "target.build.tilelang_metal" with BuildTileLangMetal.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 6ff4ad41-17f5-4fbe-b67b-a6e8bd6b8e52

📥 Commits

Reviewing files that changed from the base of the PR and between f941a85 and 423b0bf.

📒 Files selected for processing (33)
  • CMakeLists.txt
  • src/backend/cuda/CMakeLists.txt
  • src/backend/cuda/codegen/codegen_cuda.cc
  • src/backend/cuda/codegen/codegen_cuda.h
  • src/backend/cuda/codegen/codegen_cutedsl.cc
  • src/backend/cuda/codegen/codegen_cutedsl.h
  • src/backend/cuda/codegen/codegen_py.cc
  • src/backend/cuda/codegen/codegen_py.h
  • src/backend/cuda/codegen/intrin_rule_cuda.cc
  • src/backend/cuda/codegen/ptx.cc
  • src/backend/cuda/codegen/ptx.h
  • src/backend/cuda/codegen/rt_mod_cuda.cc
  • src/backend/cuda/codegen/rt_mod_cutedsl.cc
  • src/backend/cuda/codegen/stubs/cuda.cc
  • src/backend/cuda/codegen/stubs/cuda.h
  • src/backend/cuda/codegen/stubs/cudart.cc
  • src/backend/cuda/codegen/stubs/nvrtc.cc
  • src/backend/cuda/codegen/stubs/vendor/cuda.h
  • src/backend/metal/CMakeLists.txt
  • src/backend/metal/codegen/rt_mod_metal.cc
  • src/backend/rocm/CMakeLists.txt
  • src/backend/rocm/codegen/codegen_hip.cc
  • src/backend/rocm/codegen/codegen_hip.h
  • src/backend/rocm/codegen/intrin_rule_hip.cc
  • src/backend/rocm/codegen/rt_mod_hip.cc
  • src/backend/rocm/codegen/stubs/hip.cc
  • src/backend/rocm/codegen/stubs/hip.h
  • src/backend/rocm/codegen/stubs/hiprtc.cc
  • src/backend/rocm/codegen/stubs/vendor/hip_runtime.h
  • src/op/builtin.cc
  • src/op/utils.h
  • src/runtime/runtime.cc
  • src/target/codegen_c_host.h
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/op/utils.h
  • src/target/codegen_c_host.h

@SiriusNEO SiriusNEO merged commit 9e2cf42 into tile-ai:main May 6, 2026
11 of 12 checks passed
Calaweh pushed a commit to Calaweh/tilelang that referenced this pull request May 20, 2026
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