File: cmd_graph.cpp

package info (click to toggle)
xir 2.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,308 kB
  • sloc: cpp: 14,358; sh: 109; makefile: 12
file content (158 lines) | stat: -rw-r--r-- 5,660 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Copyright 2019 Xilinx Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "cmd_graph.hpp"

#include <iostream>
#include <tuple>

using namespace std;
template <typename... T>
struct SupportedAttryTypes {
  using types = std::tuple<T...>;
};

using bytes_t = std::vector<int8_t>;

using ListOfSupportedAttryType1 = SupportedAttryTypes<
    /** begin supported list of attr types */
    bool,                                             //
    int8_t,                                           //
    uint8_t,                                          //
    int16_t, uint16_t,                                //
    int32_t, uint32_t,                                //
    int64_t, uint64_t,                                //
    float, double,                                    //
    std::string,                                      //
    bytes_t,                                          //
    std::vector<bool>,                                //
    std::vector<int8_t>, std::vector<uint8_t>,        //
    std::vector<int16_t>, std::vector<uint16_t>,      //
    std::vector<int32_t>, std::vector<uint32_t>,      //
    std::vector<int64_t>, std::vector<uint64_t>,      //
    std::vector<float>, std::vector<double>,          //
    std::vector<std::string>,                         //
    std::vector<bytes_t>,                             //
    std::map<std::string, int8_t>,                    //
    std::map<std::string, uint8_t>,                   //
    std::map<std::string, int16_t>,                   //
    std::map<std::string, uint16_t>,                  //
    std::map<std::string, int32_t>,                   //
    std::map<std::string, uint32_t>,                  //
    std::map<std::string, int64_t>,                   //
    std::map<std::string, uint64_t>,                  //
    std::map<std::string, float>,                     //
    std::map<std::string, double>,                    //
    std::map<std::string, std::string>,               //
    std::map<std::string, bytes_t>,                   //
    std::map<std::string, std::vector<bool>>,         //
    std::map<std::string, std::vector<int8_t>>,       //
    std::map<std::string, std::vector<uint8_t>>,      //
    std::map<std::string, std::vector<int16_t>>,      //
    std::map<std::string, std::vector<uint16_t>>,     //
    std::map<std::string, std::vector<int32_t>>,      //
    std::map<std::string, std::vector<uint32_t>>,     //
    std::map<std::string, std::vector<int64_t>>,      //
    std::map<std::string, std::vector<uint64_t>>,     //
    std::map<std::string, std::vector<float>>,        //
    std::map<std::string, std::vector<double>>,       //
    std::map<std::string, std::vector<std::string>>,  //
    std::map<std::string, std::vector<bytes_t>>,      //
    nullptr_t>;

using ListOfSupportedAttryType =
    SupportedAttryTypes<std::string,                        //
                        std::map<std::string, std::string>  //
                        >;

template <typename Op>
struct Apply {
  std::string do_it(const std::any& x) {
    return do_it(ListOfSupportedAttryType(), x);
  }
  template <typename T0, typename... T>
  std::string do_it(SupportedAttryTypes<T0, T...> tag, const std::any& x) {
    if (x.type() == typeid(T0)) {
      return Op()(std::any_cast<T0>(x));
    }
    return do_it(SupportedAttryTypes<T...>(), x);
  }
  std::string do_it(SupportedAttryTypes<> tag, const std::any& x) {
    return "NA";
  };
};

template <typename T, class = void>
struct is_cout_able : public std::false_type {};
template <typename T>
struct is_cout_able<
    T, std::void_t<decltype(declval<std::ostream&>() << declval<T>())>>
    : public std::true_type {};

template <typename T>
constexpr bool is_cout_able_v = is_cout_able<T>::value;

template <typename K, typename V>
std::enable_if_t<is_cout_able_v<K> && is_cout_able_v<V>, std::ostream&>
operator<<(std::ostream& out, const std::map<K, V>& v) {
  out << "{";
  for (const auto& x : v) {
    out << "\n\t\"" << x.first << "\" = " << x.second;
  }
  out << "\n}";
  return out;
}

struct ToString {
  template <typename T>
  std::enable_if_t<is_cout_able_v<T>, std::string> operator()(const T& x) {
    std::ostringstream str;
    str << x;
    return str.str();
  }
  template <typename T>
  std::enable_if_t<!is_cout_able_v<T>, std::string> operator()(const T& x) {
    std::ostringstream str;
    str << "unknwon type: " << typeid(x).name() << " and " << is_cout_able_v<T>;
    return str.str();
  }
};

static void show_attr(const xir::Attrs* attr) {
  for (auto key : attr->get_keys()) {
    cout << '"' << key
         << "\" = " << Apply<ToString>().do_it(attr->get_attr(key));
    cout << endl;
  }
}

CmdGraph::CmdGraph(const std::string& name) : Cmd(name) {}

int CmdGraph::main(int argc, char* argv[]) {
  auto xmodel = std::string(argv[1]);
  auto graph = xir::Graph::deserialize(xmodel);
  show_attr(graph->get_attrs().get());
  return 0;
}

std::string CmdGraph::help() const {
  std::ostringstream str;
  str << "xir " << get_name()
      << " <xmodel>\n\t"
         "show graph properties\n"
      << endl;
  return str.str();
}