Skip to content

记录 mxcc 编译命令#22

Open
ghangz wants to merge 2 commits into
MetaX-MACA:mainfrom
ghangz:mengz/log-mxcc-compile-command
Open

记录 mxcc 编译命令#22
ghangz wants to merge 2 commits into
MetaX-MACA:mainfrom
ghangz:mengz/log-mxcc-compile-command

Conversation

@ghangz

@ghangz ghangz commented Jun 8, 2026

Copy link
Copy Markdown

该 PR 在 mxcc 编译流程中记录实际执行命令,便于复现失败、分析编译参数和提交问题报告。

这个修改面向沐曦 GPU 适配场景中比较容易影响开发、构建或验证稳定性的环节,把原来需要人工排查的问题前移到工具链、运行前检查或基准脚本中处理。实现上保持对现有默认行为的兼容,只在检测到明确配置、输入或环境异常时给出更直接的诊断,避免引入额外运行依赖,也方便维护者独立审阅该分支。

已在沐曦算力环境中完成对应分支验证,验证记录包含真实运行日志、命令输出和失败路径检查,本地归档目录为:E:/Documents/muxi/测试报告/mcTVM_new_toolchain_validation_20260608。提交分支:mengz/log-mxcc-compile-command,目标仓库:MetaX-MACA/mcTVM

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a mechanism to log compilation commands for the MACA backend in mxcc.py to a JSONL file specified by the TVM_MACA_COMPILE_COMMAND_LOG environment variable, along with a new test suite. The reviewer feedback highlights two key improvement opportunities: first, replacing the manual directory existence check with os.makedirs(log_dir, exist_ok=True) to prevent race conditions in concurrent environments; second, replacing the fragile AST-based dynamic loading mechanism in the test file with a direct import of the function under test.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +40 to +41
if log_dir and not os.path.isdir(log_dir):
os.makedirs(log_dir)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

使用 os.path.isdir 配合 os.makedirs 在多进程或多线程并发执行(例如 TVM 并行编译或 tuning)时存在竞态条件(Race Condition),可能会导致抛出 FileExistsError 异常。建议直接使用 os.makedirs(log_dir, exist_ok=True),这样既安全又更加简洁。

Suggested change
if log_dir and not os.path.isdir(log_dir):
os.makedirs(log_dir)
if log_dir:
os.makedirs(log_dir, exist_ok=True)

Comment on lines +1 to +31
import ast
import json
import os
from pathlib import Path
from unittest.mock import patch


def _load_mxcc_logging_helper():
source_path = (
Path(__file__).resolve().parents[3] / "python" / "tvm" / "contrib" / "mxcc.py"
)
tree = ast.parse(source_path.read_text(encoding="utf-8"))
allowed_imports = {"json", "os"}
nodes = [
node
for node in tree.body
if (
isinstance(node, ast.Import)
and all(alias.name in allowed_imports for alias in node.names)
)
or (isinstance(node, ast.FunctionDef) and node.name == "_write_compile_command_log")
]
module = ast.Module(body=nodes, type_ignores=[])
ast.fix_missing_locations(module)
namespace = {}
exec(compile(module, str(source_path), "exec"), namespace)
return namespace["_write_compile_command_log"]


def test_mxcc_compile_command_log_is_jsonl(tmp_path):
write_log = _load_mxcc_logging_helper()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

在测试代码中使用 ast.parseexec 动态解析并执行 _write_compile_command_log 的做法非常脆弱且难以维护。如果未来 mxcc.py 发生变更(例如引入了其他辅助函数、修改了导入模块或使用了模块级变量),这种基于 AST 的加载方式极易崩溃。由于这是标准的 TVM 测试套件,可以直接从 tvm.contrib.mxcc 导入该函数,这样更加简单、健壮且符合常规测试规范。

import json
import os
from pathlib import Path
from unittest.mock import patch

from tvm.contrib.mxcc import _write_compile_command_log


def test_mxcc_compile_command_log_is_jsonl(tmp_path):
    write_log = _write_compile_command_log

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