-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathCompileJobCacheResult.cpp
185 lines (162 loc) · 5.95 KB
/
CompileJobCacheResult.cpp
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//===--- CompileJobCacheResult.cpp - compile cache result schema ----------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file contains the cache schema for swift cached compile job result.
//
//===----------------------------------------------------------------------===//
#include "swift/Frontend/CompileJobCacheResult.h"
#include "swift/Basic/FileTypes.h"
using namespace swift;
using namespace swift::cas;
using namespace llvm;
using namespace llvm::cas;
Error CompileJobCacheResult::forEachOutput(
llvm::function_ref<Error(Output)> Callback) const {
size_t Count = getNumOutputs();
for (size_t I = 0; I < Count; ++I) {
file_types::ID Kind = getOutputKind(I);
ObjectRef Object = getOutputObject(I);
if (auto Err = Callback({Object, Kind}))
return Err;
}
return Error::success();
}
Error CompileJobCacheResult::forEachLoadedOutput(
llvm::function_ref<Error(Output, std::optional<ObjectProxy>)> Callback) {
// Kick-off materialization for all outputs concurrently.
SmallVector<std::future<AsyncProxyValue>, 4> FutureOutputs;
size_t Count = getNumOutputs();
for (size_t I = 0; I < Count; ++I) {
ObjectRef Ref = getOutputObject(I);
FutureOutputs.push_back(getCAS().getProxyFuture(Ref));
}
// Make sure all the outputs have materialized.
std::optional<Error> OccurredError;
SmallVector<std::optional<ObjectProxy>, 4> Outputs;
for (auto &FutureOutput : FutureOutputs) {
auto Obj = FutureOutput.get().take();
if (!Obj) {
if (!OccurredError)
OccurredError = Obj.takeError();
else
OccurredError =
llvm::joinErrors(std::move(*OccurredError), Obj.takeError());
continue;
}
Outputs.push_back(*Obj);
}
if (OccurredError)
return std::move(*OccurredError);
// Pass the loaded outputs.
for (size_t I = 0; I < Count; ++I) {
if (auto Err = Callback({getOutputObject(I), getOutputKind(I)}, Outputs[I]))
return Err;
}
return Error::success();
}
CompileJobCacheResult::Output CompileJobCacheResult::getOutput(size_t I) const {
return Output{getOutputObject(I), getOutputKind(I)};
}
std::optional<CompileJobCacheResult::Output>
CompileJobCacheResult::getOutput(file_types::ID Kind) const {
size_t Count = getNumOutputs();
for (size_t I = 0; I < Count; ++I) {
file_types::ID K = getOutputKind(I);
if (Kind == K)
return Output{getOutputObject(I), Kind};
}
return {};
}
Error CompileJobCacheResult::print(llvm::raw_ostream &OS) {
return forEachOutput([&](Output O) -> Error {
OS << file_types::getTypeName(O.Kind) << " " << getCAS().getID(O.Object)
<< '\n';
return Error::success();
});
}
size_t CompileJobCacheResult::getNumOutputs() const { return getData().size(); }
ObjectRef CompileJobCacheResult::getOutputObject(size_t I) const {
return getReference(I);
}
file_types::ID
CompileJobCacheResult::getOutputKind(size_t I) const {
return static_cast<file_types::ID>(getData()[I]);
}
CompileJobCacheResult::CompileJobCacheResult(const ObjectProxy &Obj)
: ObjectProxy(Obj) {}
struct CompileJobCacheResult::Builder::PrivateImpl {
SmallVector<ObjectRef> Objects;
SmallVector<file_types::ID> Kinds;
struct KindMap {
file_types::ID Kind;
std::string Path;
};
SmallVector<KindMap> KindMaps;
};
CompileJobCacheResult::Builder::Builder() : Impl(*new PrivateImpl) {}
CompileJobCacheResult::Builder::~Builder() { delete &Impl; }
void CompileJobCacheResult::Builder::addKindMap(file_types::ID Kind,
StringRef Path) {
Impl.KindMaps.push_back({Kind, std::string(Path)});
}
void CompileJobCacheResult::Builder::addOutput(file_types::ID Kind,
ObjectRef Object) {
Impl.Kinds.push_back(Kind);
Impl.Objects.push_back(Object);
}
Error CompileJobCacheResult::Builder::addOutput(StringRef Path,
ObjectRef Object) {
Impl.Objects.push_back(Object);
for (auto &KM : Impl.KindMaps) {
if (KM.Path == Path) {
Impl.Kinds.push_back(KM.Kind);
return Error::success();
}
}
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"cached output file has unknown path '" +
Path + "'");
}
Expected<ObjectRef> CompileJobCacheResult::Builder::build(ObjectStore &CAS) {
CompileJobResultSchema Schema(CAS);
// The resulting Refs contents are:
// Object 0...N, SchemaKind
SmallVector<ObjectRef> Refs;
std::swap(Impl.Objects, Refs);
Refs.push_back(Schema.getKindRef());
return CAS.store(Refs, {(char *)Impl.Kinds.begin(), Impl.Kinds.size()});
}
static constexpr llvm::StringLiteral CompileJobResultSchemaName =
"swift::cas::schema::compile_job_result::v1";
char CompileJobResultSchema::ID = 0;
CompileJobResultSchema::CompileJobResultSchema(ObjectStore &CAS)
: CompileJobResultSchema::RTTIExtends(CAS),
KindRef(
llvm::cantFail(CAS.storeFromString({}, CompileJobResultSchemaName))) {
}
Expected<CompileJobCacheResult>
CompileJobResultSchema::load(ObjectRef Ref) const {
auto Proxy = CAS.getProxy(Ref);
if (!Proxy)
return Proxy.takeError();
if (!isNode(*Proxy))
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"not a compile job result");
return CompileJobCacheResult(*Proxy);
}
bool CompileJobResultSchema::isRootNode(const ObjectProxy &Node) const {
return isNode(Node);
}
bool CompileJobResultSchema::isNode(const ObjectProxy &Node) const {
size_t N = Node.getNumReferences();
return N && Node.getReference(N - 1) == getKindRef();
}