|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +from importlib.util import find_spec |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from executorch.backends.cortex_m.compile_config import CortexMCompileConfig |
| 12 | + |
| 13 | +_HAS_CMSIS_NN = find_spec("cmsis_nn") is not None |
| 14 | + |
| 15 | + |
| 16 | +class TestCortexMCompileConfig: |
| 17 | + def test_default_is_m55_mve(self): |
| 18 | + config = CortexMCompileConfig() |
| 19 | + assert config.cpu == "cortex-m55" |
| 20 | + assert config.isa == "mve" |
| 21 | + |
| 22 | + @pytest.mark.parametrize( |
| 23 | + "target_string,expected_cpu,expected_isa", |
| 24 | + [ |
| 25 | + ("cortex-m0+int8", "cortex-m0", "scalar"), |
| 26 | + ("cortex-m0plus+int8", "cortex-m0plus", "scalar"), |
| 27 | + ("cortex-m3+int8", "cortex-m3", "scalar"), |
| 28 | + ("cortex-m4+int8", "cortex-m4", "dsp"), |
| 29 | + ("cortex-m7+int8", "cortex-m7", "dsp"), |
| 30 | + ("cortex-m23+int8", "cortex-m23", "scalar"), |
| 31 | + ("cortex-m33+int8", "cortex-m33", "dsp"), |
| 32 | + ("cortex-m35p+int8", "cortex-m35p", "dsp"), |
| 33 | + ("cortex-m52+int8", "cortex-m52", "mve"), |
| 34 | + ("cortex-m55+int8", "cortex-m55", "mve"), |
| 35 | + ("cortex-m85+int8", "cortex-m85", "mve"), |
| 36 | + ], |
| 37 | + ) |
| 38 | + def test_from_target_string(self, target_string, expected_cpu, expected_isa): |
| 39 | + config = CortexMCompileConfig.from_target_string(target_string) |
| 40 | + assert config.cpu == expected_cpu |
| 41 | + assert config.isa == expected_isa |
| 42 | + |
| 43 | + def test_from_target_string_rejects_unknown_cpu(self): |
| 44 | + with pytest.raises(ValueError, match="cortex-m999"): |
| 45 | + CortexMCompileConfig.from_target_string("cortex-m999+int8") |
| 46 | + |
| 47 | + @pytest.mark.parametrize( |
| 48 | + "target_string", |
| 49 | + [ |
| 50 | + "cortex-m55", # missing feature suffix |
| 51 | + "cortex-m55+int8+int16", # unsupported extra feature |
| 52 | + "cortex-m55+", # trailing plus |
| 53 | + "cortex-m55+fp16", # unknown feature |
| 54 | + ], |
| 55 | + ) |
| 56 | + def test_from_target_string_rejects_invalid_features(self, target_string): |
| 57 | + with pytest.raises(ValueError): |
| 58 | + CortexMCompileConfig.from_target_string(target_string) |
| 59 | + |
| 60 | + def test_default_matches_m55_target_string(self): |
| 61 | + # Regression guard: pre-Phase-1 behavior was M55+MVE; the default |
| 62 | + # constructor must remain equivalent to parsing the existing target. |
| 63 | + assert CortexMCompileConfig() == CortexMCompileConfig.from_target_string( |
| 64 | + "cortex-m55+int8" |
| 65 | + ) |
| 66 | + |
| 67 | + def test_is_hashable_and_frozen(self): |
| 68 | + from dataclasses import FrozenInstanceError |
| 69 | + |
| 70 | + config = CortexMCompileConfig(cpu="cortex-m33") |
| 71 | + assert hash(config) == hash(CortexMCompileConfig(cpu="cortex-m33")) |
| 72 | + assert {config, CortexMCompileConfig(cpu="cortex-m33")} == {config} |
| 73 | + with pytest.raises(FrozenInstanceError): |
| 74 | + config.cpu = "cortex-m55" # type: ignore[misc] |
| 75 | + |
| 76 | + def test_explicit_isa_override(self): |
| 77 | + config = CortexMCompileConfig(cpu="cortex-m33", isa="scalar") |
| 78 | + assert config.cpu == "cortex-m33" |
| 79 | + assert config.isa == "scalar" |
| 80 | + |
| 81 | + |
| 82 | +@pytest.mark.skipif( |
| 83 | + not _HAS_CMSIS_NN, reason="cortex_m passes require cmsis_nn" |
| 84 | +) |
| 85 | +class TestPassManagerConfigWiring: |
| 86 | + def test_default_config_is_m55(self): |
| 87 | + from executorch.backends.cortex_m.passes.cortex_m_pass_manager import ( |
| 88 | + CortexMPassManager, |
| 89 | + ) |
| 90 | + |
| 91 | + pm = CortexMPassManager(exported_program=None) |
| 92 | + assert pm.config.cpu == "cortex-m55" |
| 93 | + assert pm.config.isa == "mve" |
| 94 | + |
| 95 | + def test_explicit_config_threaded(self): |
| 96 | + from executorch.backends.cortex_m.passes.cortex_m_pass_manager import ( |
| 97 | + CortexMPassManager, |
| 98 | + ) |
| 99 | + |
| 100 | + config = CortexMCompileConfig(cpu="cortex-m33") |
| 101 | + pm = CortexMPassManager(exported_program=None, config=config) |
| 102 | + assert pm.config.cpu == "cortex-m33" |
| 103 | + assert pm.config.isa == "dsp" |
0 commit comments