|
| 1 | +############################################################################# |
| 2 | +## |
| 3 | +## Copyright (c) 2024, Precision Innovations Inc. |
| 4 | +## All rights reserved. |
| 5 | +## |
| 6 | +## BSD 3-Clause License |
| 7 | +## |
| 8 | +## Redistribution and use in source and binary forms, with or without |
| 9 | +## modification, are permitted provided that the following conditions are met: |
| 10 | +## |
| 11 | +## * Redistributions of source code must retain the above copyright notice, this |
| 12 | +## list of conditions and the following disclaimer. |
| 13 | +## |
| 14 | +## * Redistributions in binary form must reproduce the above copyright notice, |
| 15 | +## this list of conditions and the following disclaimer in the documentation |
| 16 | +## and/or other materials provided with the distribution. |
| 17 | +## |
| 18 | +## * Neither the name of the copyright holder nor the names of its |
| 19 | +## contributors may be used to endorse or promote products derived from |
| 20 | +## this software without specific prior written permission. |
| 21 | +## |
| 22 | +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 23 | +## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 24 | +## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 25 | +## ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 26 | +## LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 27 | +## CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 28 | +## SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 29 | +## INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 30 | +## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 31 | +## ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 32 | +## POSSIBILITY OF SUCH DAMAGE. |
| 33 | +############################################################################### |
| 34 | + |
| 35 | +import os |
| 36 | +import tempfile |
| 37 | +import textwrap |
| 38 | +import unittest |
| 39 | +import yaml |
| 40 | +from unittest.mock import patch, MagicMock |
| 41 | + |
| 42 | +from autotuner.utils import parse_flow_variables, parse_tunable_variables |
| 43 | + |
| 44 | + |
| 45 | +class TestParseTunableVariables(unittest.TestCase): |
| 46 | + """Tests for parse_tunable_variables() which reads variables.yaml |
| 47 | + and returns tunable variable names.""" |
| 48 | + |
| 49 | + def test_returns_set(self): |
| 50 | + """parse_tunable_variables should return a set.""" |
| 51 | + result = parse_tunable_variables() |
| 52 | + self.assertIsInstance(result, set) |
| 53 | + |
| 54 | + def test_returns_nonempty(self): |
| 55 | + """There should be at least one tunable variable defined.""" |
| 56 | + result = parse_tunable_variables() |
| 57 | + self.assertGreater(len(result), 0) |
| 58 | + |
| 59 | + def test_known_tunable_variables_present(self): |
| 60 | + """Known tunable variables from variables.yaml should be present.""" |
| 61 | + result = parse_tunable_variables() |
| 62 | + expected_tunable = { |
| 63 | + "CORE_UTILIZATION", |
| 64 | + "CORE_AREA", |
| 65 | + "PLACE_DENSITY_LB_ADDON", |
| 66 | + "CTS_BUF_DISTANCE", |
| 67 | + } |
| 68 | + for var in expected_tunable: |
| 69 | + self.assertIn(var, result, f"{var} should be tunable") |
| 70 | + |
| 71 | + def test_non_tunable_variables_excluded(self): |
| 72 | + """Variables without tunable: 1 should not be in the result.""" |
| 73 | + result = parse_tunable_variables() |
| 74 | + non_tunable = { |
| 75 | + "EQUIVALENCE_CHECK", |
| 76 | + "DETAILED_METRICS", |
| 77 | + "SKIP_REPORT_METRICS", |
| 78 | + } |
| 79 | + for var in non_tunable: |
| 80 | + self.assertNotIn(var, result, f"{var} should not be tunable") |
| 81 | + |
| 82 | + def _run_with_yaml(self, yaml_data): |
| 83 | + """Helper to run parse_tunable_variables with custom YAML data.""" |
| 84 | + with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as tmp: |
| 85 | + yaml.dump(yaml_data, tmp) |
| 86 | + tmp_path = tmp.name |
| 87 | + try: |
| 88 | + with patch("autotuner.utils.os.path.realpath", return_value=tmp_path): |
| 89 | + with patch( |
| 90 | + "autotuner.utils.os.path.dirname", |
| 91 | + return_value=os.path.dirname(tmp_path), |
| 92 | + ): |
| 93 | + with patch( |
| 94 | + "autotuner.utils.os.path.join", |
| 95 | + return_value=tmp_path, |
| 96 | + ): |
| 97 | + return parse_tunable_variables() |
| 98 | + finally: |
| 99 | + os.unlink(tmp_path) |
| 100 | + |
| 101 | + def test_filters_tunable_flag(self): |
| 102 | + """Only variables with tunable == 1 should be returned.""" |
| 103 | + mock_yaml = { |
| 104 | + "VAR_A": {"description": "desc", "tunable": 1}, |
| 105 | + "VAR_B": {"description": "desc", "tunable": 0}, |
| 106 | + "VAR_C": {"description": "desc"}, |
| 107 | + "VAR_D": {"description": "desc", "tunable": 1}, |
| 108 | + } |
| 109 | + result = self._run_with_yaml(mock_yaml) |
| 110 | + self.assertEqual(result, {"VAR_A", "VAR_D"}) |
| 111 | + |
| 112 | + def test_empty_yaml(self): |
| 113 | + """An empty YAML file should return an empty set.""" |
| 114 | + result = self._run_with_yaml({}) |
| 115 | + self.assertEqual(result, set()) |
| 116 | + |
| 117 | + def test_no_tunable_variables(self): |
| 118 | + """When no variables have tunable: 1, return empty set.""" |
| 119 | + mock_yaml = { |
| 120 | + "VAR_A": {"description": "desc", "tunable": 0}, |
| 121 | + "VAR_B": {"description": "desc"}, |
| 122 | + } |
| 123 | + result = self._run_with_yaml(mock_yaml) |
| 124 | + self.assertEqual(result, set()) |
| 125 | + |
| 126 | + |
| 127 | +class TestParseFlowVariables(unittest.TestCase): |
| 128 | + """Tests for parse_flow_variables() which runs make vars and |
| 129 | + parses env() patterns from Tcl scripts.""" |
| 130 | + |
| 131 | + def _setup_flow_dir(self, tmpdir, tcl_content, vars_content=""): |
| 132 | + """Helper to create flow directory structure with Tcl files.""" |
| 133 | + flow_dir = os.path.join(tmpdir, "flow") |
| 134 | + scripts_dir = os.path.join(flow_dir, "scripts") |
| 135 | + os.makedirs(scripts_dir) |
| 136 | + |
| 137 | + tcl_file = os.path.join(scripts_dir, "test.tcl") |
| 138 | + with open(tcl_file, "w") as f: |
| 139 | + f.write(tcl_content) |
| 140 | + |
| 141 | + vars_file = os.path.join(flow_dir, "vars.tcl") |
| 142 | + with open(vars_file, "w") as f: |
| 143 | + f.write(vars_content) |
| 144 | + |
| 145 | + return tcl_file |
| 146 | + |
| 147 | + @patch("autotuner.utils.subprocess.run") |
| 148 | + def test_makefile_failure_exits(self, mock_run): |
| 149 | + """Should exit when make vars fails.""" |
| 150 | + mock_run.return_value = MagicMock(returncode=1) |
| 151 | + with self.assertRaises(SystemExit): |
| 152 | + parse_flow_variables("/fake/base", "asap7") |
| 153 | + |
| 154 | + @patch("autotuner.utils.subprocess.run") |
| 155 | + def test_calls_make_with_platform(self, mock_run): |
| 156 | + """Should call make with correct platform argument.""" |
| 157 | + mock_run.return_value = MagicMock(returncode=1) |
| 158 | + try: |
| 159 | + parse_flow_variables("/some/base", "sky130hd") |
| 160 | + except SystemExit: |
| 161 | + pass |
| 162 | + mock_run.assert_called_once() |
| 163 | + call_args = mock_run.call_args[0][0] |
| 164 | + self.assertIn("PLATFORM=sky130hd", call_args) |
| 165 | + self.assertIn("vars", call_args) |
| 166 | + |
| 167 | + @patch("autotuner.utils.glob.glob") |
| 168 | + @patch("autotuner.utils.subprocess.run") |
| 169 | + def test_parses_env_patterns(self, mock_run, mock_glob): |
| 170 | + """Should extract variable names from env() patterns in Tcl files.""" |
| 171 | + mock_run.return_value = MagicMock(returncode=0) |
| 172 | + |
| 173 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 174 | + tcl_content = textwrap.dedent("""\ |
| 175 | + source $::env(SCRIPTS_DIR)/load.tcl |
| 176 | + set density $::env(PLACE_DENSITY) |
| 177 | + puts $::env(RESULTS_DIR)/output.odb |
| 178 | + """) |
| 179 | + vars_content = textwrap.dedent("""\ |
| 180 | + set ::env(CORE_UTILIZATION) 40 |
| 181 | + set ::env(CTS_BUF_DISTANCE) 100 |
| 182 | + """) |
| 183 | + tcl_file = self._setup_flow_dir(tmpdir, tcl_content, vars_content) |
| 184 | + mock_glob.return_value = [tcl_file] |
| 185 | + |
| 186 | + with patch("autotuner.utils.os.path.exists", return_value=True): |
| 187 | + result = parse_flow_variables(tmpdir, "asap7") |
| 188 | + |
| 189 | + self.assertIn("SCRIPTS_DIR", result) |
| 190 | + self.assertIn("PLACE_DENSITY", result) |
| 191 | + self.assertIn("RESULTS_DIR", result) |
| 192 | + self.assertIn("CORE_UTILIZATION", result) |
| 193 | + self.assertIn("CTS_BUF_DISTANCE", result) |
| 194 | + |
| 195 | + @patch("autotuner.utils.glob.glob") |
| 196 | + @patch("autotuner.utils.subprocess.run") |
| 197 | + def test_variables_are_uppercased(self, mock_run, mock_glob): |
| 198 | + """Variables should be uppercased in the result.""" |
| 199 | + mock_run.return_value = MagicMock(returncode=0) |
| 200 | + |
| 201 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 202 | + tcl_file = self._setup_flow_dir(tmpdir, "$::env(some_variable)\n") |
| 203 | + mock_glob.return_value = [tcl_file] |
| 204 | + |
| 205 | + with patch("autotuner.utils.os.path.exists", return_value=True): |
| 206 | + result = parse_flow_variables(tmpdir, "asap7") |
| 207 | + |
| 208 | + self.assertIn("SOME_VARIABLE", result) |
| 209 | + self.assertNotIn("some_variable", result) |
| 210 | + |
| 211 | + @patch("autotuner.utils.glob.glob") |
| 212 | + @patch("autotuner.utils.subprocess.run") |
| 213 | + def test_deduplicates_variables(self, mock_run, mock_glob): |
| 214 | + """Duplicate env() references should produce a single entry.""" |
| 215 | + mock_run.return_value = MagicMock(returncode=0) |
| 216 | + |
| 217 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 218 | + tcl_file = self._setup_flow_dir(tmpdir, "$::env(MY_VAR)\n$::env(MY_VAR)\n") |
| 219 | + mock_glob.return_value = [tcl_file] |
| 220 | + |
| 221 | + with patch("autotuner.utils.os.path.exists", return_value=True): |
| 222 | + result = parse_flow_variables(tmpdir, "asap7") |
| 223 | + |
| 224 | + self.assertIsInstance(result, set) |
| 225 | + self.assertIn("MY_VAR", result) |
| 226 | + |
| 227 | + @patch("autotuner.utils.subprocess.run") |
| 228 | + def test_missing_vars_tcl_exits(self, mock_run): |
| 229 | + """Should exit when vars.tcl is not generated.""" |
| 230 | + mock_run.return_value = MagicMock(returncode=0) |
| 231 | + with patch("autotuner.utils.os.path.exists", return_value=False): |
| 232 | + with self.assertRaises(SystemExit): |
| 233 | + parse_flow_variables("/fake/base", "asap7") |
| 234 | + |
| 235 | + |
| 236 | +if __name__ == "__main__": |
| 237 | + unittest.main() |
0 commit comments