forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LtoBackendAction.java
295 lines (274 loc) · 11 KB
/
LtoBackendAction.java
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright 2016 The Bazel Authors. All rights reserved.
//
// 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.
package com.google.devtools.build.lib.rules.cpp;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static java.util.stream.Collectors.joining;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.devtools.build.lib.actions.AbstractAction;
import com.google.devtools.build.lib.actions.ActionEnvironment;
import com.google.devtools.build.lib.actions.ActionExecutionContext;
import com.google.devtools.build.lib.actions.ActionExecutionException;
import com.google.devtools.build.lib.actions.ActionKeyContext;
import com.google.devtools.build.lib.actions.ActionOwner;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.CommandLineExpansionException;
import com.google.devtools.build.lib.actions.CommandLines;
import com.google.devtools.build.lib.actions.CommandLines.CommandLineLimits;
import com.google.devtools.build.lib.actions.ResourceSetOrBuilder;
import com.google.devtools.build.lib.actions.RunfilesSupplier;
import com.google.devtools.build.lib.analysis.actions.SpawnAction;
import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.collect.nestedset.Order;
import com.google.devtools.build.lib.server.FailureDetails.FailureDetail;
import com.google.devtools.build.lib.server.FailureDetails.LtoAction;
import com.google.devtools.build.lib.server.FailureDetails.LtoAction.Code;
import com.google.devtools.build.lib.util.DetailedExitCode;
import com.google.devtools.build.lib.util.Fingerprint;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;
/**
* Action used by LtoBackendArtifacts to create an LtoBackendAction. Similar to {@link SpawnAction},
* except that inputs are discovered from the imports file created by the ThinLTO indexing step for
* each backend artifact.
*
* <p>See {@link LtoBackendArtifacts} for a high level description of the ThinLTO build process. The
* LTO indexing step takes all bitcode .o files and decides which other .o file symbols can be
* imported/inlined. The additional input files for each backend action are then written to an
* imports file. Therefore, these new inputs must be discovered here by subsetting the imports paths
* from the set of all bitcode artifacts, before executing the backend action.
*
* <p>For more information on ThinLTO see
* http://blog.llvm.org/2016/06/thinlto-scalable-and-incremental-lto.html.
*/
public final class LtoBackendAction extends SpawnAction {
private static final String GUID = "72ce1eca-4625-4e24-a0d8-bb91bb8b0e0e";
private final NestedSet<Artifact> mandatoryInputs;
private final BitcodeFiles bitcodeFiles;
private final Artifact imports;
private boolean inputsDiscovered = false;
public LtoBackendAction(
NestedSet<Artifact> inputs,
@Nullable BitcodeFiles allBitcodeFiles,
@Nullable Artifact importsFile,
ImmutableSet<Artifact> outputs,
ActionOwner owner,
CommandLines argv,
CommandLineLimits commandLineLimits,
ActionEnvironment env,
Map<String, String> executionInfo,
CharSequence progressMessage,
RunfilesSupplier runfilesSupplier,
String mnemonic) {
super(
owner,
NestedSetBuilder.emptySet(Order.STABLE_ORDER),
inputs,
outputs,
AbstractAction.DEFAULT_RESOURCE_SET,
argv,
commandLineLimits,
env,
ImmutableMap.copyOf(executionInfo),
progressMessage,
runfilesSupplier,
mnemonic,
null,
/*stripOutputPaths=*/ false);
mandatoryInputs = inputs;
Preconditions.checkState(
(allBitcodeFiles == null) == (importsFile == null),
"Either both or neither bitcodeFiles and imports files should be null");
bitcodeFiles = allBitcodeFiles;
imports = importsFile;
}
@Override
public boolean discoversInputs() {
return imports != null;
}
@Override
protected boolean inputsDiscovered() {
return inputsDiscovered;
}
@Override
protected void setInputsDiscovered(boolean inputsDiscovered) {
this.inputsDiscovered = inputsDiscovered;
}
private NestedSet<Artifact> computeBitcodeInputs(HashSet<PathFragment> inputPaths) {
NestedSetBuilder<Artifact> bitcodeInputs = NestedSetBuilder.stableOrder();
for (Artifact inputArtifact : bitcodeFiles.getFiles().toList()) {
if (inputPaths.contains(inputArtifact.getExecPath())) {
bitcodeInputs.add(inputArtifact);
}
}
return bitcodeInputs.build();
}
@Nullable
@Override
public NestedSet<Artifact> discoverInputs(ActionExecutionContext actionExecutionContext)
throws ActionExecutionException {
ImmutableList<String> lines;
try {
lines = FileSystemUtils.readLinesAsLatin1(actionExecutionContext.getInputPath(imports));
} catch (IOException e) {
String message =
String.format(
"error reading imports file %s: %s",
actionExecutionContext.getInputPath(imports), e.getMessage());
DetailedExitCode code = createDetailedExitCode(message, Code.IMPORTS_READ_IO_EXCEPTION);
throw new ActionExecutionException(message, e, this, false, code);
}
// Build set of files this LTO backend artifact will import from.
HashSet<PathFragment> importSet = new HashSet<>();
for (String line : lines) {
if (line.isEmpty()) {
continue;
}
PathFragment execPath = PathFragment.create(line);
if (execPath.isAbsolute()) {
String message =
String.format(
"Absolute paths not allowed in imports file %s: %s",
actionExecutionContext.getInputPath(imports), execPath);
DetailedExitCode code =
createDetailedExitCode(message, Code.INVALID_ABSOLUTE_PATH_IN_IMPORTS);
throw new ActionExecutionException(message, this, false, code);
}
importSet.add(execPath);
}
// Convert the import set of paths to the set of bitcode file artifacts.
NestedSet<Artifact> bitcodeInputSet = computeBitcodeInputs(importSet);
if (bitcodeInputSet.memoizedFlattenAndGetSize() != importSet.size()) {
Set<PathFragment> missingInputs =
Sets.difference(
importSet,
bitcodeInputSet.toList().stream()
.map(Artifact::getExecPath)
.collect(toImmutableSet()));
String message =
String.format(
"error computing inputs from imports file: %s, missing bitcode files (first 10): %s",
actionExecutionContext.getInputPath(imports),
// Limit the reported count to protect against a large error message.
missingInputs.stream()
.map(Object::toString)
.sorted()
.limit(10)
.collect(joining(", ")));
DetailedExitCode code = createDetailedExitCode(message, Code.MISSING_BITCODE_FILES);
throw new ActionExecutionException(message, this, false, code);
}
updateInputs(
NestedSetBuilder.fromNestedSet(bitcodeInputSet).addTransitive(mandatoryInputs).build());
return bitcodeInputSet;
}
@Override
protected NestedSet<Artifact> getOriginalInputs() {
return mandatoryInputs;
}
private static DetailedExitCode createDetailedExitCode(String message, Code detailedCode) {
return DetailedExitCode.of(
FailureDetail.newBuilder()
.setMessage(message)
.setLtoAction(LtoAction.newBuilder().setCode(detailedCode))
.build());
}
@Override
public NestedSet<Artifact> getMandatoryInputs() {
return mandatoryInputs;
}
@Override
public NestedSet<Artifact> getAllowedDerivedInputs() {
return bitcodeFiles.getFiles();
}
@Override
protected void computeKey(
ActionKeyContext actionKeyContext,
@Nullable Artifact.ArtifactExpander artifactExpander,
Fingerprint fp)
throws InterruptedException {
fp.addString(GUID);
try {
fp.addStrings(getArguments());
} catch (CommandLineExpansionException e) {
throw new AssertionError("LtoBackendAction command line expansion cannot fail", e);
}
fp.addString(getMnemonic());
fp.addPaths(getRunfilesSupplier().getRunfilesDirs());
ImmutableList<Artifact> runfilesManifests = getRunfilesSupplier().getManifests();
for (Artifact runfilesManifest : runfilesManifests) {
fp.addPath(runfilesManifest.getExecPath());
}
for (Artifact input : mandatoryInputs.toList()) {
fp.addPath(input.getExecPath());
}
if (imports != null) {
bitcodeFiles.addToFingerprint(fp);
fp.addPath(imports.getExecPath());
}
getEnvironment().addTo(fp);
fp.addStringMap(getExecutionInfo());
}
/** Builder class to construct {@link LtoBackendAction} instances. */
public static class Builder extends SpawnAction.Builder {
private BitcodeFiles bitcodeFiles;
private Artifact imports;
@CanIgnoreReturnValue
public Builder addImportsInfo(BitcodeFiles allBitcodeFiles, Artifact importsFile) {
this.bitcodeFiles = allBitcodeFiles;
this.imports = importsFile;
return this;
}
@Override
protected SpawnAction createSpawnAction(
ActionOwner owner,
NestedSet<Artifact> tools,
NestedSet<Artifact> inputsAndTools,
ImmutableSet<Artifact> outputs,
ResourceSetOrBuilder resourceSetOrBuilder,
CommandLines commandLines,
CommandLineLimits commandLineLimits,
ActionEnvironment env,
@Nullable BuildConfigurationValue configuration,
ImmutableMap<String, String> executionInfo,
CharSequence progressMessage,
RunfilesSupplier runfilesSupplier,
String mnemonic) {
return new LtoBackendAction(
inputsAndTools,
bitcodeFiles,
imports,
outputs,
owner,
commandLines,
commandLineLimits,
env,
executionInfo,
progressMessage,
runfilesSupplier,
mnemonic);
}
}
}