支持环境变量注入 mxcc 额外参数#24
Conversation
There was a problem hiding this comment.
Code Review
This pull request adds support for reading MXCC compiler options from the TVM_MXCC_OPTIONS environment variable using shlex.split. It also adds corresponding unit tests. The reviewer suggested simplifying the unit tests by directly importing the _get_env_mxcc_options function from tvm.contrib.mxcc instead of dynamically parsing and executing the source file using ast and exec, which is fragile and hard to maintain.
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.
| import ast | ||
| import os | ||
| from pathlib import Path | ||
| from unittest.mock import patch | ||
|
|
||
|
|
||
| def _load_env_options_helper(): | ||
| source_path = ( | ||
| Path(__file__).resolve().parents[3] / "python" / "tvm" / "contrib" / "mxcc.py" | ||
| ) | ||
| tree = ast.parse(source_path.read_text(encoding="utf-8")) | ||
| nodes = [ | ||
| node | ||
| for node in tree.body | ||
| if ( | ||
| isinstance(node, ast.Import) | ||
| and all(alias.name in {"os", "shlex"} for alias in node.names) | ||
| ) | ||
| or (isinstance(node, ast.FunctionDef) and node.name == "_get_env_mxcc_options") | ||
| ] | ||
| module = ast.Module(body=nodes, type_ignores=[]) | ||
| ast.fix_missing_locations(module) | ||
| namespace = {} | ||
| exec(compile(module, str(source_path), "exec"), namespace) | ||
| return namespace["_get_env_mxcc_options"] | ||
|
|
||
|
|
||
| def test_env_mxcc_options_are_shell_split(): | ||
| get_options = _load_env_options_helper() | ||
| with patch.dict(os.environ, {"TVM_MXCC_OPTIONS": "--flag 'two words'"}, clear=True): | ||
| assert get_options() == ["--flag", "two words"] | ||
|
|
||
|
|
||
| def test_env_mxcc_options_default_empty(): | ||
| get_options = _load_env_options_helper() | ||
| with patch.dict(os.environ, {}, clear=True): | ||
| assert get_options() == [] | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| test_env_mxcc_options_are_shell_split() | ||
| test_env_mxcc_options_default_empty() |
There was a problem hiding this comment.
使用 ast.parse 和 exec 动态解析并执行源文件来提取 _get_env_mxcc_options 函数非常脆弱且难以维护。如果未来该函数依赖了其他未被提取的导入或模块级变量,测试将会失败。在 TVM 的测试环境中,通常可以直接导入 tvm 及其子模块。建议直接导入 _get_env_mxcc_options,这样可以显著简化测试代码并提高可维护性。
import os
from unittest.mock import patch
from tvm.contrib.mxcc import _get_env_mxcc_options
def test_env_mxcc_options_are_shell_split():
with patch.dict(os.environ, {"TVM_MXCC_OPTIONS": "--flag 'two words'"}, clear=True):
assert _get_env_mxcc_options() == ["--flag", "two words"]
def test_env_mxcc_options_default_empty():
with patch.dict(os.environ, {}, clear=True):
assert _get_env_mxcc_options() == []
if __name__ == "__main__":
test_env_mxcc_options_are_shell_split()
test_env_mxcc_options_default_empty()
该 PR 支持通过环境变量为 mxcc 注入额外编译选项,便于在不修改源码的情况下测试不同架构、调试或优化参数。
这个修改面向沐曦 GPU 适配场景中比较容易影响开发、构建或验证稳定性的环节,把原来需要人工排查的问题前移到工具链、运行前检查或基准脚本中处理。实现上保持对现有默认行为的兼容,只在检测到明确配置、输入或环境异常时给出更直接的诊断,避免引入额外运行依赖,也方便维护者独立审阅该分支。
已在沐曦算力环境中完成对应分支验证,验证记录包含真实运行日志、命令输出和失败路径检查,本地归档目录为:E:/Documents/muxi/测试报告/mcTVM_new_toolchain_validation_20260608。提交分支:
mengz/mxcc-extra-options-env,目标仓库:MetaX-MACA/mcTVM。