1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include "tensorflow/lite/delegates/gpu/gl/compiler/preprocessor.h"
17
18 #include "absl/strings/str_cat.h"
19 #include "tensorflow/lite/delegates/gpu/common/status.h"
20
21 namespace tflite {
22 namespace gpu {
23 namespace gl {
24 namespace {
25
26 // Given input string and a delimiter returns back a substring including
27 // delimiters. If there was only starting delimiter found, returns single char.
FindInlineBlock(absl::string_view s,char delimiter)28 absl::string_view FindInlineBlock(absl::string_view s, char delimiter) {
29 size_t start = s.find(delimiter);
30 if (start != absl::string_view::npos) {
31 size_t end = s.find(delimiter, start + 1);
32 if (end != std::string::npos) {
33 return s.substr(start, end - start + 1);
34 }
35 // Special case to indicate that we didn't find the end.
36 return s.substr(start, 1);
37 }
38 return s.substr(s.size(), 0);
39 }
40
41 // For the given 's' and its substring 'subs' returns new substring of 's' that
42 // begins past 'subs'.
PastSubstr(absl::string_view s,absl::string_view subs)43 absl::string_view PastSubstr(absl::string_view s, absl::string_view subs) {
44 return s.substr(subs.data() + subs.size() - s.data());
45 }
46
47 } // namespace
48
Rewrite(const std::string & input,std::string * output)49 absl::Status TextPreprocessor::Rewrite(const std::string& input,
50 std::string* output) {
51 absl::string_view s = input;
52 std::string result;
53 while (true) {
54 absl::string_view inline_block = FindInlineBlock(s, inline_delimiter_);
55 result.append(s.data(), inline_block.data() - s.data());
56 if (inline_block.empty()) {
57 break;
58 }
59 if (inline_block.size() == 1) {
60 return absl::NotFoundError("Unable to find end of inline block");
61 }
62 s = PastSubstr(s, inline_block);
63 bool processed = false;
64 for (auto& rewrite : inline_rewrites_) {
65 if (processed) {
66 break;
67 }
68 switch (rewrite->Rewrite(inline_block.substr(1, inline_block.size() - 2),
69 &result)) {
70 case RewriteStatus::NOT_RECOGNIZED:
71 // try another rewrite.
72 break;
73 case RewriteStatus::SUCCESS:
74 processed = true;
75 break;
76 case RewriteStatus::ERROR:
77 return absl::InternalError(absl::StrCat("Error while rewriting '",
78 inline_block, "': ", result));
79 }
80 }
81 if (!processed) {
82 if (!keep_unknown_rewrites_) {
83 return absl::NotFoundError(absl::StrCat(
84 "Didn't find inline rewrite for '", inline_block, "'"));
85 }
86 absl::StrAppend(&result, inline_block);
87 }
88 }
89 *output = std::move(result);
90 return absl::OkStatus();
91 }
92
93 } // namespace gl
94 } // namespace gpu
95 } // namespace tflite
96