Skip to content

Commit 0d98bf5

Browse files
lberkicopybara-github
authored andcommitted
Automated rollback of commit 4a2e51b.
*** Reason for rollback *** Breaks targets on the nightly TGP. Reproduction: blaze build //third_party/bazel_rules/rules_kotlin/tests/android/java/com/google/jni:AndroidJniTest --compilation_mode=opt --flaky_test_attempts=2 --fat_apk_cpu=x86 --android_platforms=//buildenv/platforms/android:x86 --incompatible_enable_android_toolchain_resolution=1 --collect_code_coverage=1 --instrumentation_filter=//java/com/google/android/samples/apps/topeka[/:],//third_party/bazel_rules/rules_kotlin[/:],//tools/build_defs/kotlin[/:] TGP link: [] *** Original change description *** Make `getPrimaryOutput()` always return the first artifact in `getOutputs()`. This is already the case everywhere except `CppCompileAction` but was not documented, leading to `SpawnAction` unnecessarily storing a field for the primary output when it's already just the first element in its outputs. This change saves 8 bytes per `SpawnAction` and `GenRuleAction`, and moves other `SpawnAction` subclasses closer to an 8-byte threshold. `CppCompileAction` had been using the coverage artifact (if pr *** RELNOTES: None. PiperOrigin-RevId: 523634597 Change-Id: I0aa70851fe4d403afabc56e808546d6638a9f2b7
1 parent d43737f commit 0d98bf5

13 files changed

+149
-78
lines changed

src/main/java/com/google/devtools/build/lib/actions/AbstractAction.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,10 @@ public Artifact getPrimaryInput() {
309309
}
310310

311311
@Override
312-
public final Artifact getPrimaryOutput() {
313-
return Iterables.get(outputs, 0);
312+
public Artifact getPrimaryOutput() {
313+
// Default behavior is to return the first output artifact.
314+
// Use the method rather than field in case of overriding in subclasses.
315+
return Iterables.getFirst(getOutputs(), null);
314316
}
315317

316318
@Override

src/main/java/com/google/devtools/build/lib/actions/ActionAnalysisMetadata.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,7 @@ NestedSet<Artifact> getInputFilesForExtraAction(ActionExecutionContext actionExe
186186
Artifact getPrimaryInput();
187187

188188
/**
189-
* Returns the "primary" output of this action, which is the same as the first artifact in {@link
190-
* #getOutputs}.
189+
* Returns the "primary" output of this action.
191190
*
192191
* <p>For example, the linked library would be the primary output of a LinkAction.
193192
*

src/main/java/com/google/devtools/build/lib/actions/ActionExecutionMetadata.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ default String getProgressMessage(RepositoryMapping mainRepositoryMapping) {
4848
}
4949

5050
/**
51-
* Returns a human-readable description of the inputs to {@link #getKey}. Used in the output from
52-
* '--explain', and in error messages for '--check_up_to_date' and '--check_tests_up_to_date'. May
53-
* return null, meaning no extra information is available.
51+
* Returns a human-readable description of the inputs to {@link #getKey(ActionKeyContext)}. Used
52+
* in the output from '--explain', and in error messages for '--check_up_to_date' and
53+
* '--check_tests_up_to_date'. May return null, meaning no extra information is available.
5454
*
5555
* <p>If the return value is non-null, for consistency it should be a multiline message of the
5656
* form:

src/main/java/com/google/devtools/build/lib/analysis/actions/SpawnAction.java

+31-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
package com.google.devtools.build.lib.analysis.actions;
1616

17-
import static com.google.common.collect.ImmutableSet.toImmutableSet;
1817
import static com.google.devtools.build.lib.actions.ActionAnalysisMetadata.mergeMaps;
1918
import static com.google.devtools.build.lib.packages.ExecGroup.DEFAULT_EXEC_GROUP_NAME;
2019

@@ -104,7 +103,7 @@ public interface ExtraActionInfoSupplier {
104103

105104
private static final String GUID = "ebd6fce3-093e-45ee-adb6-bf513b602f0d";
106105

107-
private static final Interner<ImmutableSortedMap<String, String>> executionInfoInterner =
106+
public static final Interner<ImmutableSortedMap<String, String>> executionInfoInterner =
108107
BlazeInterners.newWeakInterner();
109108

110109
private final CommandLines commandLines;
@@ -119,6 +118,7 @@ public interface ExtraActionInfoSupplier {
119118
private final ImmutableMap<String, String> executionInfo;
120119

121120
private final ExtraActionInfoSupplier extraActionInfoSupplier;
121+
private final Artifact primaryOutput;
122122
private final Consumer<Pair<ActionExecutionContext, List<SpawnResult>>> resultConsumer;
123123
private final boolean stripOutputPaths;
124124

@@ -132,6 +132,7 @@ public interface ExtraActionInfoSupplier {
132132
* @param inputs the set of all files potentially read by this action; must not be subsequently
133133
* modified.
134134
* @param outputs the set of all files written by this action; must not be subsequently modified.
135+
* @param primaryOutput the primary output of this action
135136
* @param resourceSetOrBuilder the resources consumed by executing this Action.
136137
* @param env the action environment
137138
* @param commandLines the command lines to execute. This includes the main argv vector and any
@@ -147,6 +148,7 @@ public SpawnAction(
147148
NestedSet<Artifact> tools,
148149
NestedSet<Artifact> inputs,
149150
Iterable<Artifact> outputs,
151+
Artifact primaryOutput,
150152
ResourceSetOrBuilder resourceSetOrBuilder,
151153
CommandLines commandLines,
152154
CommandLineLimits commandLineLimits,
@@ -159,6 +161,7 @@ public SpawnAction(
159161
tools,
160162
inputs,
161163
outputs,
164+
primaryOutput,
162165
resourceSetOrBuilder,
163166
commandLines,
164167
commandLineLimits,
@@ -185,6 +188,7 @@ public SpawnAction(
185188
* @param inputs the set of all files potentially read by this action; must not be subsequently
186189
* modified
187190
* @param outputs the set of all files written by this action; must not be subsequently modified.
191+
* @param primaryOutput the primary output of this action
188192
* @param resourceSetOrBuilder the resources consumed by executing this Action.
189193
* @param env the action's environment
190194
* @param executionInfo out-of-band information for scheduling the spawn
@@ -202,6 +206,7 @@ public SpawnAction(
202206
NestedSet<Artifact> tools,
203207
NestedSet<Artifact> inputs,
204208
Iterable<? extends Artifact> outputs,
209+
Artifact primaryOutput,
205210
ResourceSetOrBuilder resourceSetOrBuilder,
206211
CommandLines commandLines,
207212
CommandLineLimits commandLineLimits,
@@ -216,6 +221,7 @@ public SpawnAction(
216221
Consumer<Pair<ActionExecutionContext, List<SpawnResult>>> resultConsumer,
217222
boolean stripOutputPaths) {
218223
super(owner, tools, inputs, runfilesSupplier, outputs, env);
224+
this.primaryOutput = primaryOutput;
219225
this.resourceSetOrBuilder = resourceSetOrBuilder;
220226
this.executionInfo =
221227
executionInfo.isEmpty()
@@ -232,6 +238,11 @@ public SpawnAction(
232238
this.stripOutputPaths = stripOutputPaths;
233239
}
234240

241+
@Override
242+
public Artifact getPrimaryOutput() {
243+
return primaryOutput;
244+
}
245+
235246
@VisibleForTesting
236247
public CommandLines getCommandLines() {
237248
return commandLines;
@@ -250,10 +261,12 @@ public List<String> getArguments() throws CommandLineExpansionException, Interru
250261
}
251262

252263
@Override
253-
public Sequence<CommandLineArgsApi> getStarlarkArgs() {
264+
public Sequence<CommandLineArgsApi> getStarlarkArgs() throws EvalException {
254265
ImmutableList.Builder<CommandLineArgsApi> result = ImmutableList.builder();
255266
ImmutableSet<Artifact> directoryInputs =
256-
getInputs().toList().stream().filter(Artifact::isDirectory).collect(toImmutableSet());
267+
getInputs().toList().stream()
268+
.filter(artifact -> artifact.isDirectory())
269+
.collect(ImmutableSet.toImmutableSet());
257270

258271
for (CommandLineAndParamFileInfo commandLine : commandLines.getCommandLines()) {
259272
result.add(Args.forRegisteredAction(commandLine, directoryInputs));
@@ -523,7 +536,7 @@ public ExtraActionInfo.Builder getExtraActionInfo(ActionKeyContext actionKeyCont
523536
* <p>Subclasses of SpawnAction may override this in order to provide action-specific behaviour.
524537
* This can be necessary, for example, when the action discovers inputs.
525538
*/
526-
private SpawnInfo getExtraActionSpawnInfo()
539+
protected SpawnInfo getExtraActionSpawnInfo()
527540
throws CommandLineExpansionException, InterruptedException {
528541
SpawnInfo.Builder info = SpawnInfo.newBuilder();
529542
Spawn spawn = getSpawnForExtraAction();
@@ -588,7 +601,7 @@ private ActionSpawn(
588601
throws CommandLineExpansionException {
589602
super(
590603
arguments,
591-
ImmutableMap.of(),
604+
ImmutableMap.<String, String>of(),
592605
parent.getExecutionInfo(),
593606
parent.getRunfilesSupplier(),
594607
parent,
@@ -792,7 +805,8 @@ private SpawnAction buildSpawnAction(
792805
owner,
793806
tools,
794807
inputsAndTools,
795-
ImmutableSet.copyOf(outputs),
808+
ImmutableList.copyOf(outputs),
809+
outputs.get(0),
796810
resourceSetOrBuilder,
797811
commandLines,
798812
commandLineLimits,
@@ -812,7 +826,8 @@ protected SpawnAction createSpawnAction(
812826
ActionOwner owner,
813827
NestedSet<Artifact> tools,
814828
NestedSet<Artifact> inputsAndTools,
815-
ImmutableSet<Artifact> outputs,
829+
ImmutableList<Artifact> outputs,
830+
Artifact primaryOutput,
816831
ResourceSetOrBuilder resourceSetOrBuilder,
817832
CommandLines commandLines,
818833
CommandLineLimits commandLineLimits,
@@ -828,6 +843,7 @@ protected SpawnAction createSpawnAction(
828843
tools,
829844
inputsAndTools,
830845
outputs,
846+
primaryOutput,
831847
resourceSetOrBuilder,
832848
commandLines,
833849
commandLineLimits,
@@ -933,6 +949,13 @@ public Builder addOutputs(Iterable<Artifact> artifacts) {
933949
return this;
934950
}
935951

952+
/**
953+
* Checks whether the action produces any outputs
954+
*/
955+
public boolean hasOutputs() {
956+
return !outputs.isEmpty();
957+
}
958+
936959
/**
937960
* Sets RecourceSet for builder. If ResourceSetBuilder set, then ResourceSetBuilder will
938961
* override setResources.

src/main/java/com/google/devtools/build/lib/analysis/actions/StarlarkAction.java

+9-7
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import com.google.common.annotations.VisibleForTesting;
1919
import com.google.common.collect.ImmutableList;
2020
import com.google.common.collect.ImmutableMap;
21-
import com.google.common.collect.ImmutableSet;
22-
import com.google.common.collect.Iterables;
2321
import com.google.common.collect.Maps;
2422
import com.google.common.collect.Sets;
2523
import com.google.devtools.build.lib.actions.Action;
@@ -83,6 +81,7 @@ public final class StarlarkAction extends SpawnAction implements ActionCacheAwar
8381
* @param inputs the set of all files potentially read by this action; must not be subsequently
8482
* modified
8583
* @param outputs the set of all files written by this action; must not be subsequently modified.
84+
* @param primaryOutput the primary output of this action
8685
* @param resourceSetOrBuilder the resources consumed by executing this Action
8786
* @param commandLines the command lines to execute. This includes the main argv vector and any
8887
* param file-backed command lines.
@@ -104,6 +103,7 @@ private StarlarkAction(
104103
NestedSet<Artifact> tools,
105104
NestedSet<Artifact> inputs,
106105
Iterable<Artifact> outputs,
106+
Artifact primaryOutput,
107107
ResourceSetOrBuilder resourceSetOrBuilder,
108108
CommandLines commandLines,
109109
CommandLineLimits commandLineLimits,
@@ -123,6 +123,7 @@ private StarlarkAction(
123123
? createInputs(shadowedAction.get().getInputs(), inputs)
124124
: inputs,
125125
outputs,
126+
primaryOutput,
126127
resourceSetOrBuilder,
127128
commandLines,
128129
commandLineLimits,
@@ -375,7 +376,8 @@ protected SpawnAction createSpawnAction(
375376
ActionOwner owner,
376377
NestedSet<Artifact> tools,
377378
NestedSet<Artifact> inputsAndTools,
378-
ImmutableSet<Artifact> outputs,
379+
ImmutableList<Artifact> outputs,
380+
Artifact primaryOutput,
379381
ResourceSetOrBuilder resourceSetOrBuilder,
380382
CommandLines commandLines,
381383
CommandLineLimits commandLineLimits,
@@ -401,6 +403,7 @@ protected SpawnAction createSpawnAction(
401403
tools,
402404
inputsAndTools,
403405
outputs,
406+
primaryOutput,
404407
resourceSetOrBuilder,
405408
commandLines,
406409
commandLineLimits,
@@ -412,7 +415,7 @@ protected SpawnAction createSpawnAction(
412415
mnemonic,
413416
unusedInputsList,
414417
shadowedAction,
415-
stripOutputPaths(mnemonic, inputsAndTools, outputs, configuration));
418+
stripOutputPaths(mnemonic, inputsAndTools, primaryOutput, configuration));
416419
}
417420

418421
/**
@@ -432,7 +435,7 @@ protected SpawnAction createSpawnAction(
432435
private static boolean stripOutputPaths(
433436
String mnemonic,
434437
NestedSet<Artifact> inputs,
435-
ImmutableSet<Artifact> outputs,
438+
Artifact primaryOutput,
436439
BuildConfigurationValue configuration) {
437440
ImmutableList<String> qualifyingMnemonics =
438441
ImmutableList.of(
@@ -453,8 +456,7 @@ private static boolean stripOutputPaths(
453456
CoreOptions coreOptions = configuration.getOptions().get(CoreOptions.class);
454457
return coreOptions.outputPathsMode == OutputPathsMode.STRIP
455458
&& qualifyingMnemonics.contains(mnemonic)
456-
&& PathStripper.isPathStrippable(
457-
inputs, Iterables.get(outputs, 0).getExecPath().subFragment(0, 1));
459+
&& PathStripper.isPathStrippable(inputs, primaryOutput.getExecPath().subFragment(0, 1));
458460
}
459461
}
460462
}

src/main/java/com/google/devtools/build/lib/analysis/extra/ExtraAction.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414

1515
package com.google.devtools.build.lib.analysis.extra;
1616

17+
import com.google.common.base.Function;
1718
import com.google.common.base.Preconditions;
1819
import com.google.common.collect.ImmutableMap;
20+
import com.google.common.collect.Iterables;
1921
import com.google.common.collect.Sets;
2022
import com.google.devtools.build.lib.actions.AbstractAction;
2123
import com.google.devtools.build.lib.actions.Action;
@@ -54,6 +56,9 @@ public final class ExtraAction extends SpawnAction {
5456
private final boolean createDummyOutput;
5557
private final NestedSet<Artifact> extraActionInputs;
5658

59+
public static final Function<ExtraAction, Action> GET_SHADOWED_ACTION =
60+
e -> e != null ? e.getShadowedAction() : null;
61+
5762
ExtraAction(
5863
NestedSet<Artifact> extraActionInputs,
5964
RunfilesSupplier runfilesSupplier,
@@ -73,6 +78,7 @@ public final class ExtraAction extends SpawnAction {
7378
NestedSetBuilder.emptySet(Order.STABLE_ORDER),
7479
extraActionInputs),
7580
outputs,
81+
Iterables.getFirst(outputs, null),
7682
AbstractAction.DEFAULT_RESOURCE_SET,
7783
CommandLines.of(argv),
7884
CommandLineLimits.UNLIMITED,
@@ -122,7 +128,7 @@ public NestedSet<Artifact> discoverInputs(ActionExecutionContext actionExecution
122128
updateInputs(
123129
createInputs(shadowedAction.getInputs(), inputFilesForExtraAction, extraActionInputs));
124130
return NestedSetBuilder.wrap(
125-
Order.STABLE_ORDER, Sets.difference(getInputs().toSet(), oldInputs.toSet()));
131+
Order.STABLE_ORDER, Sets.<Artifact>difference(getInputs().toSet(), oldInputs.toSet()));
126132
}
127133

128134
private static NestedSet<Artifact> createInputs(

src/main/java/com/google/devtools/build/lib/analysis/test/TestRunnerAction.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ public void setupEnvVariables(Map<String, String> env, Duration timeout) {
676676
env.put("TEST_RANDOM_SEED", Integer.toString(getRunNumber() + 1));
677677
}
678678
// TODO(b/184206260): Actually set TEST_RANDOM_SEED with random seed.
679-
// The above TEST_RANDOM_SEED has historically been set with the run number, but we should
679+
// The above TEST_RANDOM_SEED has histroically been set with the run number, but we should
680680
// explicitly set TEST_RUN_NUMBER to indicate the run number and actually set TEST_RANDOM_SEED
681681
// with a random seed. However, much code has come to depend on it being set to the run number
682682
// and this is an externally documented behavior. Modifying TEST_RANDOM_SEED should be done
@@ -923,6 +923,11 @@ public String getRunfilesPrefix() {
923923
return workspaceName;
924924
}
925925

926+
@Override
927+
public Artifact getPrimaryOutput() {
928+
return testLog;
929+
}
930+
926931
public PackageSpecificationProvider getNetworkAllowlist() {
927932
return networkAllowlist;
928933
}

src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -1732,7 +1732,7 @@ private void createModuleCodegenAction(
17321732
configuration, featureConfiguration, builder, ruleErrorConsumer);
17331733
CppCompileAction compileAction = builder.buildOrThrowRuleError(ruleErrorConsumer);
17341734
actionRegistry.registerAction(compileAction);
1735-
Artifact objectFile = compileAction.getPrimaryOutput();
1735+
Artifact objectFile = compileAction.getOutputFile();
17361736
if (pic) {
17371737
result.addPicObjectFile(objectFile);
17381738
} else {
@@ -1775,7 +1775,7 @@ private void createHeaderAction(
17751775
configuration, featureConfiguration, builder, ruleErrorConsumer);
17761776
CppCompileAction compileAction = builder.buildOrThrowRuleError(ruleErrorConsumer);
17771777
actionRegistry.registerAction(compileAction);
1778-
Artifact tokenFile = compileAction.getPrimaryOutput();
1778+
Artifact tokenFile = compileAction.getOutputFile();
17791779
result.addHeaderTokenFile(tokenFile);
17801780
}
17811781

@@ -1868,13 +1868,13 @@ private ImmutableList<Artifact> createSourceAction(
18681868
configuration, featureConfiguration, picBuilder, ruleErrorConsumer);
18691869
CppCompileAction picAction = picBuilder.buildOrThrowRuleError(ruleErrorConsumer);
18701870
actionRegistry.registerAction(picAction);
1871-
directOutputs.add(picAction.getPrimaryOutput());
1871+
directOutputs.add(picAction.getOutputFile());
18721872
if (addObject) {
1873-
result.addPicObjectFile(picAction.getPrimaryOutput());
1873+
result.addPicObjectFile(picAction.getOutputFile());
18741874

18751875
if (bitcodeOutput) {
18761876
result.addLtoBitcodeFile(
1877-
picAction.getPrimaryOutput(), ltoIndexingFile, getCopts(sourceArtifact, sourceLabel));
1877+
picAction.getOutputFile(), ltoIndexingFile, getCopts(sourceArtifact, sourceLabel));
18781878
}
18791879
}
18801880
if (dwoFile != null) {
@@ -1939,7 +1939,7 @@ private ImmutableList<Artifact> createSourceAction(
19391939
configuration, featureConfiguration, builder, ruleErrorConsumer);
19401940
CppCompileAction compileAction = builder.buildOrThrowRuleError(ruleErrorConsumer);
19411941
actionRegistry.registerAction(compileAction);
1942-
Artifact objectFile = compileAction.getPrimaryOutput();
1942+
Artifact objectFile = compileAction.getOutputFile();
19431943
directOutputs.add(objectFile);
19441944
if (addObject) {
19451945
result.addObjectFile(objectFile);
@@ -2118,7 +2118,7 @@ private ImmutableList<Artifact> createTempsActions(
21182118
CppCompileAction sdAction = sdBuilder.buildOrThrowRuleError(ruleErrorConsumer);
21192119
actionRegistry.registerAction(sdAction);
21202120

2121-
return ImmutableList.of(dAction.getPrimaryOutput(), sdAction.getPrimaryOutput());
2121+
return ImmutableList.of(dAction.getOutputFile(), sdAction.getOutputFile());
21222122
}
21232123

21242124
/**

0 commit comments

Comments
 (0)