从 MACA 路径解析 mxcc#27
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a helper function _find_mxcc to dynamically locate the mxcc compiler using the MACA_PATH environment variable or the system PATH, and adds corresponding unit tests. The review feedback suggests falling back to the default /opt/maca path when MACA_PATH is not set to align with CMake lookup logic, and mocking os.path.isfile in the unit tests to ensure test hermeticity regardless of the host environment.
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.
| def _find_mxcc(): | ||
| """Find mxcc from MACA_PATH or PATH.""" | ||
| maca_path = os.environ.get("MACA_PATH") | ||
| if maca_path: | ||
| candidate = os.path.join(maca_path, "mxgpu_llvm", "bin", "mxcc") | ||
| if os.path.isfile(candidate): | ||
| return candidate | ||
| mxcc = shutil.which("mxcc") | ||
| if mxcc: | ||
| return mxcc | ||
| raise RuntimeError("Cannot find mxcc. Set MACA_PATH or add mxcc to PATH.") |
There was a problem hiding this comment.
在 _find_mxcc 函数中,如果环境变量 MACA_PATH 未设置,当前实现只会尝试通过 shutil.which("mxcc") 从 PATH 中查找。然而,根据 find_maca_path() 以及 FindMACA.cmake 中的逻辑,/opt/maca 是一个默认的 MACA 安装路径。如果用户没有设置 MACA_PATH 且 mxcc 不在 PATH 中,但系统上存在默认的 /opt/maca/mxgpu_llvm/bin/mxcc,当前实现将会报错。
建议将 maca_path 的默认值设为 /opt/maca,以保持与现有默认行为和 CMake 查找逻辑的一致性。
| def _find_mxcc(): | |
| """Find mxcc from MACA_PATH or PATH.""" | |
| maca_path = os.environ.get("MACA_PATH") | |
| if maca_path: | |
| candidate = os.path.join(maca_path, "mxgpu_llvm", "bin", "mxcc") | |
| if os.path.isfile(candidate): | |
| return candidate | |
| mxcc = shutil.which("mxcc") | |
| if mxcc: | |
| return mxcc | |
| raise RuntimeError("Cannot find mxcc. Set MACA_PATH or add mxcc to PATH.") | |
| def _find_mxcc(): | |
| """Find mxcc from MACA_PATH or PATH.""" | |
| maca_path = os.environ.get("MACA_PATH") or "/opt/maca" | |
| candidate = os.path.join(maca_path, "mxgpu_llvm", "bin", "mxcc") | |
| if os.path.isfile(candidate): | |
| return candidate | |
| mxcc = shutil.which("mxcc") | |
| if mxcc: | |
| return mxcc | |
| raise RuntimeError("Cannot find mxcc. Set MACA_PATH or add mxcc to PATH.") |
| def test_find_mxcc_from_path(self): | ||
| with patch.dict(os.environ, {}, clear=True): | ||
| with patch.object(mxcc.shutil, "which", return_value="/usr/bin/mxcc"): | ||
| self.assertEqual(mxcc._find_mxcc(), "/usr/bin/mxcc") |
There was a problem hiding this comment.
为了确保单测的密闭性(Hermeticity),避免受到运行测试的宿主机上是否存在 /opt/maca/mxgpu_llvm/bin/mxcc 的影响,建议在 test_find_mxcc_from_path 中对 os.path.isfile 进行 mock,使其返回 False。这样可以确保测试始终能够走到 shutil.which 的分支。
| def test_find_mxcc_from_path(self): | |
| with patch.dict(os.environ, {}, clear=True): | |
| with patch.object(mxcc.shutil, "which", return_value="/usr/bin/mxcc"): | |
| self.assertEqual(mxcc._find_mxcc(), "/usr/bin/mxcc") | |
| def test_find_mxcc_from_path(self): | |
| with patch.dict(os.environ, {}, clear=True): | |
| with patch.object(mxcc.os.path, "isfile", return_value=False): | |
| with patch.object(mxcc.shutil, "which", return_value="/usr/bin/mxcc"): | |
| self.assertEqual(mxcc._find_mxcc(), "/usr/bin/mxcc") |
该 PR 增加从 MACA 安装目录解析 mxcc 的逻辑,提升容器内工具链布局变化时的兼容性。
这个修改面向沐曦 GPU 适配场景中比较容易影响开发、构建或验证稳定性的环节,把原来需要人工排查的问题前移到工具链、运行前检查或基准脚本中处理。实现上保持对现有默认行为的兼容,只在检测到明确配置、输入或环境异常时给出更直接的诊断,避免引入额外运行依赖,也方便维护者独立审阅该分支。
已在沐曦算力环境中完成对应分支验证,验证记录包含真实运行日志、命令输出和失败路径检查,本地归档目录为:E:/Documents/muxi/测试报告/mcTVM_new_toolchain_validation_20260608。提交分支:
mengz/resolve-mxcc-from-maca-path,目标仓库:MetaX-MACA/mcTVM。