Skip to content

Commit

Permalink
Add experimental facilities to create a second, smaller C++ module fr…
Browse files Browse the repository at this point in the history
…om the same

CcCompilationHelper and CcCompilationContext.

RELNOTES: None.
PiperOrigin-RevId: 356477447
  • Loading branch information
djasper authored and copybara-github committed Feb 9, 2021
1 parent 31db460 commit 547d2a9
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,12 @@ public IncludeScanningHeaderData.Builder createIncludeScanningHeaderData(
treeArtifacts,
isModule,
modularHeaders);
handleHeadersForIncludeScanning(
transitiveHeaderInfo.separateModuleHeaders,
pathToLegalArtifact,
treeArtifacts,
isModule,
modularHeaders);
handleHeadersForIncludeScanning(
transitiveHeaderInfo.textualHeaders,
pathToLegalArtifact,
Expand All @@ -452,6 +458,7 @@ public IncludeScanningHeaderData.Builder createIncludeScanningHeaderData(
transitiveHeaderCount.compareAndSet(-1, pathToLegalArtifact.size());
removeArtifactsFromSet(modularHeaders, headerInfo.modularHeaders);
removeArtifactsFromSet(modularHeaders, headerInfo.textualHeaders);
removeArtifactsFromSet(modularHeaders, headerInfo.separateModuleHeaders);
return new IncludeScanningHeaderData.Builder(pathToLegalArtifact, modularHeaders);
}

Expand All @@ -460,11 +467,13 @@ public IncludeScanningHeaderData.Builder createIncludeScanningHeaderData(
* the modules that they are in.
*/
public Collection<Artifact.DerivedArtifact> computeUsedModules(
boolean usePic, Set<Artifact> includes) {
boolean usePic, Set<Artifact> includes, boolean separate) {
CompactHashSet<Artifact.DerivedArtifact> modules = CompactHashSet.create();
for (HeaderInfo transitiveHeaderInfo : headerInfo.getTransitiveCollection()) {
Artifact.DerivedArtifact module = transitiveHeaderInfo.getModule(usePic);
if (module == null) {
// If we don't have a main module, there is also not going to be a separate module. This is
// verified when constructing HeaderInfo instances.
continue;
}
// Not using range-based for loops here as often there is exactly one element in this list
Expand All @@ -476,11 +485,18 @@ public Collection<Artifact.DerivedArtifact> computeUsedModules(
break;
}
}
for (int i = 0; i < transitiveHeaderInfo.separateModuleHeaders.size(); i++) {
Artifact header = transitiveHeaderInfo.separateModuleHeaders.get(i);
if (includes.contains(header)) {
modules.add(transitiveHeaderInfo.getSeparateModule(usePic));
break;
}
}
}
// Do not add the module of the current rule for both:
// 1. the module compile itself
// 2. compiles of other translation units of the same rule.
modules.remove(headerInfo.getModule(usePic));
modules.remove(separate ? headerInfo.getSeparateModule(usePic) : headerInfo.getModule(usePic));
return modules;
}

Expand Down Expand Up @@ -521,15 +537,28 @@ public Iterable<Artifact> getDirectModuleMaps() {
return directModuleMaps;
}

Artifact.DerivedArtifact getHeaderModule(boolean usePic) {
return headerInfo.getModule(usePic);
}

Artifact.DerivedArtifact getSeparateHeaderModule(boolean usePic) {
return headerInfo.getSeparateModule(usePic);
}

/**
* @return all declared headers of the current module if the current target is compiled as a
* module.
*/
ImmutableSet<Artifact> getHeaderModuleSrcs() {
ImmutableList<Artifact> getHeaderModuleSrcs(boolean separateModule) {
if (separateModule) {
return headerInfo.separateModuleHeaders;
}
return new ImmutableSet.Builder<Artifact>()
.addAll(headerInfo.modularHeaders)
.addAll(headerInfo.textualHeaders)
.build();
.addAll(headerInfo.separateModuleHeaders)
.build()
.asList();
}

/**
Expand Down Expand Up @@ -723,14 +752,14 @@ public Builder mergeDependentCcCompilationContext(
looseHdrsDirs.addTransitive(otherCcCompilationContext.getLooseHdrsDirs());
declaredIncludeSrcs.addTransitive(otherCcCompilationContext.getDeclaredIncludeSrcs());
headerInfoBuilder.addDep(otherCcCompilationContext.headerInfo);

transitiveModules.addTransitive(otherCcCompilationContext.transitiveModules);
if (otherCcCompilationContext.headerInfo.headerModule != null) {
transitiveModules.add(otherCcCompilationContext.headerInfo.headerModule);
}
addIfNotNull(transitiveModules, otherCcCompilationContext.headerInfo.headerModule);
addIfNotNull(transitiveModules, otherCcCompilationContext.headerInfo.separateModule);

transitivePicModules.addTransitive(otherCcCompilationContext.transitivePicModules);
if (otherCcCompilationContext.headerInfo.picHeaderModule != null) {
transitivePicModules.add(otherCcCompilationContext.headerInfo.picHeaderModule);
}
addIfNotNull(transitivePicModules, otherCcCompilationContext.headerInfo.picHeaderModule);
addIfNotNull(transitivePicModules, otherCcCompilationContext.headerInfo.separatePicModule);

nonCodeInputs.addTransitive(otherCcCompilationContext.nonCodeInputs);

Expand All @@ -750,6 +779,13 @@ public Builder mergeDependentCcCompilationContext(
return this;
}

private static void addIfNotNull(
NestedSetBuilder<Artifact> builder, @Nullable Artifact artifact) {
if (artifact != null) {
builder.add(artifact);
}
}

/**
* Merges the given {@code CcCompilationContext}s into this one by adding the contents of their
* attributes.
Expand Down Expand Up @@ -891,6 +927,14 @@ public Builder addTextualHdrs(Collection<Artifact> headers) {
return this;
}

public Builder setSeparateModuleHdrs(
Collection<Artifact> headers,
Artifact.DerivedArtifact separateModule,
Artifact.DerivedArtifact separatePicModule) {
this.headerInfoBuilder.setSeparateModuleHdrs(headers, separateModule, separatePicModule);
return this;
}

/** Add a set of required non-code compilation input. */
public Builder addNonCodeInputs(Iterable<Artifact> inputs) {
nonCodeInputs.addAll(inputs);
Expand Down Expand Up @@ -1106,6 +1150,12 @@ public static final class HeaderInfo {
/** All textual header files that are contained in this module. */
private final ImmutableList<Artifact> textualHeaders;

/** Headers that can be compiled into a separate, smaller module for performance reasons. */
private final ImmutableList<Artifact> separateModuleHeaders;

private final Artifact.DerivedArtifact separateModule;
private final Artifact.DerivedArtifact separatePicModule;

/** HeaderInfos of direct dependencies of C++ target represented by this context. */
private final ImmutableList<HeaderInfo> deps;

Expand All @@ -1118,6 +1168,9 @@ public static final class HeaderInfo {
ImmutableList<Artifact> modularPublicHeaders,
ImmutableList<Artifact> modularPrivateHeaders,
ImmutableList<Artifact> textualHeaders,
ImmutableList<Artifact> separateModuleHeaders,
Artifact.DerivedArtifact separateModule,
Artifact.DerivedArtifact separatePicModule,
ImmutableList<HeaderInfo> deps) {
this.headerModule = headerModule;
this.picHeaderModule = picHeaderModule;
Expand All @@ -1126,13 +1179,20 @@ public static final class HeaderInfo {
this.modularHeaders =
ImmutableList.copyOf(Iterables.concat(modularPublicHeaders, modularPrivateHeaders));
this.textualHeaders = textualHeaders;
this.separateModuleHeaders = separateModuleHeaders;
this.separateModule = separateModule;
this.separatePicModule = separatePicModule;
this.deps = deps;
}

Artifact.DerivedArtifact getModule(boolean pic) {
return pic ? picHeaderModule : headerModule;
}

Artifact.DerivedArtifact getSeparateModule(boolean pic) {
return pic ? separatePicModule : separateModule;
}

public Collection<HeaderInfo> getTransitiveCollection() {
if (deps.isEmpty()) {
return ImmutableList.of(this);
Expand Down Expand Up @@ -1232,6 +1292,9 @@ public static class Builder {
private final Set<Artifact> modularPublicHeaders = new HashSet<>();
private final Set<Artifact> modularPrivateHeaders = new HashSet<>();
private final Set<Artifact> textualHeaders = new HashSet<>();
private Collection<Artifact> separateModuleHeaders = ImmutableList.of();
private Artifact.DerivedArtifact separateModule = null;
private Artifact.DerivedArtifact separatePicModule = null;
private final List<HeaderInfo> deps = new ArrayList<>();

Builder setHeaderModule(Artifact.DerivedArtifact headerModule) {
Expand Down Expand Up @@ -1280,6 +1343,16 @@ public Builder addTextualHeaders(Collection<Artifact> headers) {
return this;
}

public Builder setSeparateModuleHdrs(
Collection<Artifact> headers,
Artifact.DerivedArtifact separateModule,
Artifact.DerivedArtifact separatePicModule) {
this.separateModuleHeaders = headers;
this.separateModule = separateModule;
this.separatePicModule = separatePicModule;
return this;
}

/** Adds the headers of the given {@code HeaderInfo} into the one being built. */
public Builder mergeHeaderInfo(HeaderInfo otherHeaderInfo) {
this.modularPublicHeaders.addAll(otherHeaderInfo.modularPublicHeaders);
Expand All @@ -1289,12 +1362,21 @@ public Builder mergeHeaderInfo(HeaderInfo otherHeaderInfo) {
}

public HeaderInfo build() {
Preconditions.checkState(
(separateModule == null || headerModule != null)
&& (separatePicModule == null || picHeaderModule != null),
"Separate module cannot be used without main module",
separateModule,
separatePicModule);
return new HeaderInfo(
headerModule,
picHeaderModule,
ImmutableList.copyOf(modularPublicHeaders),
ImmutableList.copyOf(modularPrivateHeaders),
ImmutableList.copyOf(textualHeaders),
ImmutableList.copyOf(separateModuleHeaders),
separateModule,
separatePicModule,
ImmutableList.copyOf(deps));
}
}
Expand Down
Loading

0 comments on commit 547d2a9

Please sign in to comment.