From c9094c089e632a6492d07cd7c7fb52c20b4dc813 Mon Sep 17 00:00:00 2001 From: papertager <2567587994@qq.com> Date: Sun, 7 Jun 2026 17:37:49 +0800 Subject: [PATCH 1/2] contrib: allow mxcc options from environment --- python/tvm/contrib/mxcc.py | 9 ++++ tests/python/contrib/test_mxcc_env_options.py | 42 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 tests/python/contrib/test_mxcc_env_options.py diff --git a/python/tvm/contrib/mxcc.py b/python/tvm/contrib/mxcc.py index c5ad9eca5969..9012b3632504 100644 --- a/python/tvm/contrib/mxcc.py +++ b/python/tvm/contrib/mxcc.py @@ -19,6 +19,7 @@ import re import os +import shlex import subprocess import warnings from typing import Tuple @@ -31,6 +32,13 @@ from . import utils +def _get_env_mxcc_options(): + value = os.environ.get("TVM_MXCC_OPTIONS") + if not value: + return [] + return shlex.split(value) + + def compile_maca( code, target_format="mcbin", arch=None, options=None, path_target=None ): # pylint: disable=unused-argument @@ -99,6 +107,7 @@ def compile_maca( cmd += options else: raise ValueError("options must be str of list of str") + cmd += _get_env_mxcc_options() cmd += ["-D__FAST_HALF_CVT__", "-o", file_target] cmd += [temp_code] diff --git a/tests/python/contrib/test_mxcc_env_options.py b/tests/python/contrib/test_mxcc_env_options.py new file mode 100644 index 000000000000..85e59a97e288 --- /dev/null +++ b/tests/python/contrib/test_mxcc_env_options.py @@ -0,0 +1,42 @@ +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() From 60a38cde10311d323cfc68d5393c276de308b6fb Mon Sep 17 00:00:00 2001 From: ghangz <152254226+ghangz@users.noreply.github.com> Date: Wed, 24 Jun 2026 17:05:31 +0800 Subject: [PATCH 2/2] Simplify mxcc env option tests --- tests/python/contrib/test_mxcc_env_options.py | 30 ++----------------- 1 file changed, 3 insertions(+), 27 deletions(-) diff --git a/tests/python/contrib/test_mxcc_env_options.py b/tests/python/contrib/test_mxcc_env_options.py index 85e59a97e288..25778270ddba 100644 --- a/tests/python/contrib/test_mxcc_env_options.py +++ b/tests/python/contrib/test_mxcc_env_options.py @@ -1,40 +1,16 @@ -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"] +from tvm.contrib.mxcc import _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"] + assert _get_env_mxcc_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() == [] + assert _get_env_mxcc_options() == [] if __name__ == "__main__":