Skip to content

feat(codegen): Add EmitC tile debug print support - #994

Open
Crystal-wzy wants to merge 1 commit into
hw-native-sys:mainfrom
Crystal-wzy:main
Open

feat(codegen): Add EmitC tile debug print support#994
Crystal-wzy wants to merge 1 commit into
hw-native-sys:mainfrom
Crystal-wzy:main

Conversation

@Crystal-wzy

@Crystal-wzy Crystal-wzy commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Add pto.tile.print and PrintFormat bindings so PTODSL can emit
    pto.tprint with optional scratch views and format attributes
  • Lower pto.tprint through the EmitC backend with the required TPrint
    headers, asc_printf workaround, and sync-pipe handling while rejecting
    unsupported VPTO lowering
  • Add ptoas --fatobj support for EmitC native builds, including
    single-child EmitC containers and mixed-backend VPTO ABI call shims
  • Document tile debug printing and add lit, JIT, and TileLib ST coverage for
    formats, backend restrictions, fatobj validation, and simulator stdout

Testing

  • git diff --cached --check
  • python -m py_compile ptodsl/ptodsl/_runtime/native_build.py ptodsl/tests/test_jit_compile.py test/tilelib-st/test_tilelib_st.py test/tilelib-st/a5/tprint/case.py

@Crystal-wzy
Crystal-wzy force-pushed the main branch 2 times, most recently from 646d4a3 to 0afdbd3 Compare July 25, 2026 03:47
Comment thread ptodsl/ptodsl/tilelib/templates/a5/tprint.py Outdated
Comment thread lib/PTO/Transforms/ExpandTileOp.cpp Outdated
func.walk([&](Operation *op) {
if (isa<pto::TReshapeOp>(op))
// TReshapeOp (zero-copy alias) and TPrintOp (cce::printf side effect) are
// lowered natively by PTOToEmitC (TRESHAPE / TPRINT) with no vector

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

tprint只能在emitc后端用吗?vpto后端的实现方案是什么?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

目前 tprint 只支持 EmitC。EmitC 已有 PTOPrintToTPRINT lowering,能直接生成 pto-isa TPRINT 调用;VPTO 侧目前没有 TPrintOp/TPRINT 的 emission/runtime debug print 实现。因此这版不做 TileLib 模板展开,并在 VPTO backend下遇到 pto.tprint 时显式报 unsupported,避免静默走错路径。后续 VPTO 支持需要先补 VPTO lowering/runtime 的 print side-effect 方案。

Comment thread ptodsl/ptodsl/tilelib/templates/a5/treshape.py Outdated
Comment thread ptodsl/tests/test_tilelib_catalog.py Outdated
@Crystal-wzy Crystal-wzy changed the title feat: Add tprint and treshape tilelib tile ops feat: Add pto.tile.print debug tile op Jul 27, 2026
@Crystal-wzy
Crystal-wzy force-pushed the main branch 7 times, most recently from 2c3b929 to 79e7d4c Compare July 28, 2026 02:59
Comment thread lib/PTO/Transforms/ExpandTileOp.cpp Outdated
Comment thread ptodsl/docs/user_guide/04-type-system-and-buffer.md Outdated
Comment thread ptodsl/ptodsl/_ops.py
@Crystal-wzy Crystal-wzy changed the title feat: Add pto.tile.print debug tile op feat(ptodsl): Add tile debug print support Jul 28, 2026
@Crystal-wzy
Crystal-wzy force-pushed the main branch 9 times, most recently from 7297be4 to 7899014 Compare July 30, 2026 02:56
Comment thread ptodsl/ptodsl/_runtime/native_build.py Outdated
Comment thread test/lit/pto/tprint_alloc_tile_no_rebind.pto Outdated
Comment thread test/tilelib-st/a5/tprint/case.py Outdated
@Crystal-wzy
Crystal-wzy force-pushed the main branch 5 times, most recently from 355e92d to 36e131e Compare July 30, 2026 14:26
@Crystal-wzy
Crystal-wzy force-pushed the main branch 9 times, most recently from d5ec558 to 5e4558a Compare August 2, 2026 02:44
@Crystal-wzy Crystal-wzy changed the title feat(ptodsl): Add tile debug print support feat(codegen): Add EmitC tile debug print support Aug 3, 2026
Comment thread tools/ptoas/ObjectEmission.cpp Outdated
Comment thread ptodsl/docs/user_guide/04-type-system-and-buffer.md Outdated
Comment thread tools/ptoas/ptoas.cpp
Comment on lines +275 to +362
static bool isCIdentChar(char c) {
return std::isalnum(static_cast<unsigned char>(c)) || c == '_';
}

static std::string makeVPTOABIShimName(llvm::StringRef abiName) {
std::string shimName;
shimName.reserve(abiName.size() + strlen("__ptoas_"));
for (char c : abiName) {
if (c == '.') {
shimName.append("__ptoas_");
continue;
}
shimName.push_back(c);
}
return shimName;
}

static std::optional<std::pair<std::string, std::string>>
rewriteVPTOABIDeclarationLine(std::string &line) {
if (line.find("extern \"C\"") == std::string::npos)
return std::nullopt;

size_t suffixPos = line.find(".vector(");
size_t suffixLen = strlen(".vector");
if (suffixPos == std::string::npos) {
suffixPos = line.find(".cube(");
suffixLen = strlen(".cube");
}
if (suffixPos == std::string::npos)
return std::nullopt;

size_t semiPos = line.find(';', suffixPos);
if (semiPos == std::string::npos || line.find('{', suffixPos) != std::string::npos)
return std::nullopt;

size_t nameStart = suffixPos;
while (nameStart > 0 && isCIdentChar(line[nameStart - 1]))
--nameStart;
if (nameStart == suffixPos)
return std::nullopt;

const size_t nameLen = suffixPos + suffixLen - nameStart;
std::string abiName = line.substr(nameStart, nameLen);
std::string shimName = makeVPTOABIShimName(abiName);
line.replace(nameStart, nameLen, shimName);
semiPos = line.find(';', nameStart + shimName.size());
line.insert(semiPos, " __asm__(\"" + abiName + "\")");
return std::make_pair(std::move(abiName), std::move(shimName));
}

static void replaceAllTokenCalls(std::string &text, llvm::StringRef from,
llvm::StringRef to) {
std::string needle = (from + "(").str();
std::string replacement = (to + "(").str();
size_t pos = 0;
while ((pos = text.find(needle, pos)) != std::string::npos) {
text.replace(pos, needle.size(), replacement);
pos += replacement.size();
}
}

static void rewriteVPTOABIDirectCallShims(std::string &cpp) {
llvm::StringMap<std::string> shimByABIName;
std::string rewritten;
rewritten.reserve(cpp.size());

size_t lineStart = 0;
while (lineStart < cpp.size()) {
size_t lineEnd = cpp.find('\n', lineStart);
bool hasNewline = lineEnd != std::string::npos;
if (!hasNewline)
lineEnd = cpp.size();

std::string line = cpp.substr(lineStart, lineEnd - lineStart);
if (auto mapping = rewriteVPTOABIDeclarationLine(line))
shimByABIName[mapping->first] = mapping->second;

rewritten.append(line);
if (hasNewline)
rewritten.push_back('\n');
lineStart = lineEnd + (hasNewline ? 1 : 0);
}

for (const auto &entry : shimByABIName)
replaceAllTokenCalls(rewritten, entry.getKey(), entry.getValue());
cpp = std::move(rewritten);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

这部分修改的作用是什么?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

这段代码是在 EmitC 生成 C++ 后,把非法的 VPTO ABI 符号名如 external_vadd.vector / external_vadd.cube 改写成合法 C++ shim 名,比如 external_vadd__ptoas_vector,同时用 asm("external_vadd.vector") 保留真实链接符号。
作用:让 C++ 能编译通过,并且 fatobj link 仍然能链接到 VPTO child 导出的 .vector/.cube ABI 符号。解决的是 mixed-backend direct call 下的编译/链接失败。

@Crystal-wzy
Crystal-wzy force-pushed the main branch 17 times, most recently from 80718df to d542ad6 Compare August 5, 2026 06:25
## Summary
- Add `pto.tile.print` and `PrintFormat` bindings so PTODSL can emit
  `pto.tprint` with optional scratch views and format attributes
- Lower `pto.tprint` through the EmitC backend with the required TPrint
  headers, `asc_printf` workaround, and sync-pipe handling while rejecting
  unsupported VPTO lowering
- Add `ptoas --fatobj` support for EmitC native builds, including
  single-child EmitC containers and mixed-backend VPTO ABI call shims
- Document tile debug printing and add lit, JIT, and TileLib ST coverage for
  formats, backend restrictions, fatobj validation, and simulator stdout

## Testing
- [x] `git diff --cached --check`
- [x] `python -m py_compile ptodsl/ptodsl/_runtime/native_build.py ptodsl/tests/test_jit_compile.py test/tilelib-st/test_tilelib_st.py test/tilelib-st/a5/tprint/case.py`
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.

2 participants