feat(codegen): Add EmitC tile debug print support - #994
Open
Crystal-wzy wants to merge 1 commit into
Open
Conversation
Crystal-wzy
force-pushed
the
main
branch
2 times, most recently
from
July 25, 2026 03:47
646d4a3 to
0afdbd3
Compare
Zhendong404
requested changes
Jul 25, 2026
| 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 |
Collaborator
There was a problem hiding this comment.
tprint只能在emitc后端用吗?vpto后端的实现方案是什么?
Collaborator
Author
There was a problem hiding this comment.
目前 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 方案。
Crystal-wzy
force-pushed
the
main
branch
7 times, most recently
from
July 28, 2026 02:59
2c3b929 to
79e7d4c
Compare
Zhendong404
requested changes
Jul 28, 2026
Crystal-wzy
force-pushed
the
main
branch
9 times, most recently
from
July 30, 2026 02:56
7297be4 to
7899014
Compare
Zhendong404
requested changes
Jul 30, 2026
Crystal-wzy
force-pushed
the
main
branch
5 times, most recently
from
July 30, 2026 14:26
355e92d to
36e131e
Compare
Crystal-wzy
force-pushed
the
main
branch
9 times, most recently
from
August 2, 2026 02:44
d5ec558 to
5e4558a
Compare
Zhendong404
requested changes
Aug 3, 2026
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); | ||
| } | ||
|
|
Collaborator
Author
There was a problem hiding this comment.
这段代码是在 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
force-pushed
the
main
branch
17 times, most recently
from
August 5, 2026 06:25
80718df to
d542ad6
Compare
## 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`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
pto.tile.printandPrintFormatbindings so PTODSL can emitpto.tprintwith optional scratch views and format attributespto.tprintthrough the EmitC backend with the required TPrintheaders,
asc_printfworkaround, and sync-pipe handling while rejectingunsupported VPTO lowering
ptoas --fatobjsupport for EmitC native builds, includingsingle-child EmitC containers and mixed-backend VPTO ABI call shims
formats, backend restrictions, fatobj validation, and simulator stdout
Testing
git diff --cached --checkpython -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