xref: /aosp_15_r20/external/executorch/backends/apple/coreml/runtime/sdk/program_path.mm (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1//
2// program_path.mm
3//
4// Copyright © 2024 Apple Inc. All rights reserved.
5//
6// Please refer to the license found in the LICENSE file in the root directory of the source tree.
7
8#import <Foundation/Foundation.h>
9
10#import "program_path.h"
11
12namespace {
13using namespace executorchcoreml::modelstructure;
14
15template <typename LAST, typename CURRENT>
16void append_component(CURRENT component, std::vector<Path::Component>& components) {
17    LAST *lastComponent = nullptr;
18    if (components.size() > 0) {
19        lastComponent = std::get_if<LAST>(&(components.back()));
20    }
21
22    NSCAssert(components.size() > 0 && lastComponent != nullptr, @"Failed to append %s component, last component is not a %s", CURRENT::kTypeName, LAST::kTypeName);
23    components.emplace_back(std::move(component));
24}
25
26void append_component(Path::Program component, std::vector<Path::Component>& components) {
27    NSCAssert(components.size() == 0, @"Failed to append %s component, components is not empty.", Path::Program::kTypeName);
28    components.emplace_back(std::move(component));
29}
30
31void append_component(Path::Program::Function component, std::vector<Path::Component>& components) {
32    append_component<Path::Program, Path::Program::Function>(std::move(component), components);
33}
34
35void append_component(Path::Program::Block component, std::vector<Path::Component>& components) {
36    if (component.index >= 0) {
37        append_component<Path::Program::Operation, Path::Program::Block>(std::move(component), components);
38    } else {
39        append_component<Path::Program::Function, Path::Program::Block>(std::move(component), components);
40    }
41}
42
43void append_component(Path::Program::Operation component, std::vector<Path::Component>& components) {
44    append_component<Path::Program::Block, Path::Program::Operation>(std::move(component), components);
45}
46}
47
48namespace executorchcoreml {
49namespace modelstructure {
50
51const char *Path::kTypeKeyName = "Type";
52
53const char *Path::Program::kTypeName = "Program";
54
55const char *Path::Program::Function::kTypeName = "Function";
56const char *Path::Program::Function::kNameKeyName = "Name";
57
58const char *Path::Program::Block::kTypeName = "Block";
59const char *Path::Program::Block::kIndexKeyName = "Index";
60
61const char *Path::Program::Operation::kTypeName = "Operation";
62const char *Path::Program::Operation::kOutputKeyName = "Output";
63
64void Path::append_component(Path::Component component) noexcept {
65    std::visit([&](auto&& arg){
66        return ::append_component(arg, components_);
67    }, component);
68}
69} // namespace modelstructure
70} // namespace executorchcoreml
71