Add (from,to) rewrite pairs based on the given shape. These rewrite pairs are used to generate methods for args and results.
| 125 | // Add (from,to) rewrite pairs based on the given shape. These rewrite pairs |
| 126 | // are used to generate methods for args and results. |
| 127 | Status AddRewritesForShape(int i, const xla::Shape& shape, |
| 128 | std::vector<std::pair<string, string>>* rewrites) { |
| 129 | string type; |
| 130 | TF_RETURN_IF_ERROR(XLATypeToCpp(shape.element_type(), &type)); |
| 131 | std::vector<string> dim_vars; |
| 132 | string dim_sizes, indices; |
| 133 | if (shape.rank() == 0 || |
| 134 | (shape.dimensions_size() == 1 && shape.dimensions(0) == 1)) { |
| 135 | dim_sizes = "[1]"; |
| 136 | indices = "[0]"; |
| 137 | } else { |
| 138 | for (int dim = 0; dim < shape.dimensions_size(); ++dim) { |
| 139 | dim_vars.push_back(absl::StrCat("size_t dim", dim)); |
| 140 | dim_sizes += absl::StrCat("[", shape.dimensions(dim), "]"); |
| 141 | indices += absl::StrCat("[dim", dim, "]"); |
| 142 | } |
| 143 | } |
| 144 | rewrites->push_back({"{{I}}", absl::StrCat(i)}); |
| 145 | rewrites->push_back({"{{TYPE}}", type}); |
| 146 | rewrites->push_back({"{{DIM_VARS}}", absl::StrJoin(dim_vars, ", ")}); |
| 147 | rewrites->push_back({"{{DIM_SIZES}}", dim_sizes}); |
| 148 | rewrites->push_back({"{{INDICES}}", indices}); |
| 149 | return Status::OK(); |
| 150 | } |
| 151 | |
| 152 | // Returns code rewritten by replacing all rewrite pairs, with an extra rewrite |
| 153 | // for the name. Note that the rewriting strategy is roughly O(N*M), where N is |
no test coverage detected