1/**
2 * Copyright (c) Glow Contributors. See CONTRIBUTORS file.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef GLOW_PASSMANAGER_PASSCONFIGTUILS_H
17#define GLOW_PASSMANAGER_PASSCONFIGTUILS_H
18
19#include "llvm/Support/YAMLParser.h"
20#include "llvm/Support/YAMLTraits.h"
21
22LLVM_YAML_STRONG_TYPEDEF(unsigned, CompilationModes)
23
24namespace llvm {
25namespace yaml {
26template <> struct ScalarEnumerationTraits<glow::ConvergenceMode> {
27 static void enumeration(IO &io, glow::ConvergenceMode &mode) {
28 io.enumCase(mode, "one_pass", glow::ConvergenceMode::OnePass);
29 io.enumCase(mode, "until_fixed_point",
30 glow::ConvergenceMode::UntilFixedPoint);
31 }
32};
33
34template <> struct ScalarEnumerationTraits<glow::DCERequiredMode> {
35 static void enumeration(IO &io, glow::DCERequiredMode &mode) {
36 io.enumCase(mode, "none", glow::DCERequiredMode::None);
37 io.enumCase(mode, "before_pass", glow::DCERequiredMode::BeforePass);
38 }
39};
40
41template <> struct ScalarBitSetTraits<CompilationModes> {
42 static void bitset(IO &io, CompilationModes &value) {
43 io.bitSetCase(value, "infer",
44 1 << convertEnumToUnsigned(glow::CompilationMode::Infer));
45 io.bitSetCase(value, "train",
46 1 << convertEnumToUnsigned(glow::CompilationMode::Train));
47 }
48};
49
50template <typename Helper> struct SequenceTraits<std::vector<Helper>> {
51 static size_t size(IO &io, std::vector<Helper> &configs) {
52 return configs.size();
53 }
54
55 static Helper &element(IO &io, std::vector<Helper> &configs, size_t index) {
56 if (index >= configs.size()) {
57 configs.resize(index + 1);
58 }
59 return configs[index];
60 }
61};
62} // namespace yaml
63} // namespace llvm
64
65template <typename T> static T deserializeFromYaml(llvm::StringRef fileName) {
66 T result;
67 llvm::outs() << "Deserialize from " << fileName << "\n";
68 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> text =
69 llvm::MemoryBuffer::getFileAsStream(fileName);
70 assert(!text.getError() && "Unable to open file");
71
72 std::unique_ptr<llvm::MemoryBuffer> buffer = std::move(*text);
73 llvm::yaml::Input yin(buffer->getBuffer());
74 yin >> result;
75
76 assert(!yin.error() && "Error reading yaml file");
77
78 return result;
79}
80
81template <typename T>
82static void serializeToYaml(llvm::StringRef fileName, T &value) {
83 std::error_code EC;
84 llvm::raw_fd_ostream os(fileName, EC);
85 CHECK(!EC) << "Could not open output file";
86 llvm::yaml::Output yout(os);
87 yout << value;
88}
89
90#endif // GLOW_PASSMANAGER_PASSCONFIGTUILS_H
91