-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WASM] Start emitting a summary for modular compilation.
This initial summary contains type - supertype relationships that will be used to synthesize itables in the bundler. PiperOrigin-RevId: 572957936
- Loading branch information
1 parent
c0c217e
commit b5a3de2
Showing
9 changed files
with
179 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 16 additions & 1 deletion
17
transpiler/java/com/google/j2cl/transpiler/backend/wasm/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
transpiler/java/com/google/j2cl/transpiler/backend/wasm/SummaryBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Copyright 2023 Google Inc. | ||
* | ||
* 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 | ||
* | ||
* https://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.j2cl.transpiler.backend.wasm; | ||
|
||
import static java.util.function.Predicate.not; | ||
|
||
import com.google.j2cl.common.Problems; | ||
import com.google.j2cl.common.Problems.FatalError; | ||
import com.google.j2cl.transpiler.ast.DeclaredTypeDescriptor; | ||
import com.google.j2cl.transpiler.ast.Library; | ||
import com.google.j2cl.transpiler.ast.Type; | ||
import com.google.protobuf.util.JsonFormat; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import javax.annotation.Nullable; | ||
|
||
/** Summarizes information where global knowledge will be required for bundling. */ | ||
public final class SummaryBuilder { | ||
|
||
public static final int NULL_TYPE = 0; | ||
|
||
private final Summary.Builder summary = Summary.newBuilder(); | ||
private final Map<String, Integer> types = new HashMap<>(); | ||
private final Map<String, Integer> declaredTypes = new HashMap<>(); | ||
|
||
private final WasmGenerationEnvironment environment; | ||
|
||
SummaryBuilder(Library library, WasmGenerationEnvironment environment, Problems problems) { | ||
this.environment = environment; | ||
|
||
library.streamTypes().forEach(this::addType); | ||
} | ||
|
||
private void addType(Type type) { | ||
if (type.isInterface()) { | ||
return; | ||
} | ||
|
||
int typeId = getDeclaredTypeId(type.getTypeDescriptor()); | ||
|
||
TypeHierarchyInfo.Builder typeHierarchyInfoBuilder = | ||
TypeHierarchyInfo.newBuilder().setDeclaredTypeId(typeId); | ||
|
||
DeclaredTypeDescriptor superTypeDescriptor = type.getSuperTypeDescriptor(); | ||
if (superTypeDescriptor != null && !superTypeDescriptor.isNative()) { | ||
typeHierarchyInfoBuilder.setExtendsDeclaredTypeId(getDeclaredTypeId(superTypeDescriptor)); | ||
} | ||
|
||
type.getSuperInterfaceTypeDescriptors().stream() | ||
.filter(not(DeclaredTypeDescriptor::isNative)) | ||
.filter(not(DeclaredTypeDescriptor::isJsFunctionInterface)) | ||
.forEach(t -> typeHierarchyInfoBuilder.addImplementsDeclaredTypeId(getDeclaredTypeId(t))); | ||
|
||
summary.addType(typeHierarchyInfoBuilder.build()); | ||
} | ||
|
||
private int getDeclaredTypeId(DeclaredTypeDescriptor typeDescriptor) { | ||
String typeName = environment.getTypeSignature(typeDescriptor); | ||
// Note that the IDs start from '1' to reserve '0' for NULL_TYPE. | ||
return declaredTypes.computeIfAbsent(typeName, x -> declaredTypes.size() + 1); | ||
} | ||
|
||
private Summary build() { | ||
summary.clearTypeReferenceMap(); | ||
String[] typeReferenceMap = new String[types.size()]; | ||
types.forEach((name, i) -> typeReferenceMap[i] = name); | ||
summary.addAllTypeReferenceMap(Arrays.asList(typeReferenceMap)); | ||
String[] declaredTypeMap = new String[declaredTypes.size() + 1]; | ||
// Add a spurious mapping for 0 for the cases where the type is absent, e.g. the supertype | ||
// of java.lang.Object. | ||
declaredTypeMap[NULL_TYPE] = "<no-type>"; | ||
declaredTypes.forEach((name, i) -> declaredTypeMap[i] = name); | ||
summary.addAllDeclaredTypeMap(Arrays.asList(declaredTypeMap)); | ||
return summary.build(); | ||
} | ||
|
||
/** Serialize a LibraryInfo object into a JSON string. */ | ||
@Nullable | ||
public String toJson(Problems problems) { | ||
try { | ||
return JsonFormat.printer().print(build()); | ||
} catch (IOException e) { | ||
problems.fatal(FatalError.CANNOT_WRITE_FILE, e.toString()); | ||
return null; | ||
} | ||
} | ||
|
||
public byte[] toByteArray() { | ||
return build().toByteArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
transpiler/java/com/google/j2cl/transpiler/backend/wasm/summary.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
syntax = "proto3"; | ||
|
||
package j2cl; | ||
|
||
option java_multiple_files = true; | ||
option java_package = "com.google.j2cl.transpiler.backend.wasm"; | ||
|
||
message Summary { | ||
repeated string declared_type_map = 1; | ||
repeated string type_reference_map = 2; | ||
repeated TypeHierarchyInfo type = 3; | ||
} | ||
|
||
message TypeHierarchyInfo { | ||
int32 declared_type_id = 1; | ||
int32 extends_declared_type_id = 2; | ||
repeated int32 implements_declared_type_id = 3; | ||
} |
7 changes: 7 additions & 0 deletions
7
...javatests/com/google/j2cl/readable/java/emptymethod/output_wasm_modular/summary.txtpb.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"declaredTypeMap": ["\u003cno-type\u003e", "$emptymethod.EmptyMethod", "$java.lang.Object"], | ||
"type": [{ | ||
"declaredTypeId": 1, | ||
"extendsDeclaredTypeId": 2 | ||
}] | ||
} |
15 changes: 15 additions & 0 deletions
15
.../javatests/com/google/j2cl/readable/java/interfaces/output_wasm_modular/summary.txtpb.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"declaredTypeMap": ["\u003cno-type\u003e", "$interfaces.Main", "$java.lang.Object", "$interfaces.Main.Implementor", "$interfaces.Main.SubInterface", "$interfaces.Main.Interface", "$interfaces.Main.AbstractImplementor"], | ||
"type": [{ | ||
"declaredTypeId": 1, | ||
"extendsDeclaredTypeId": 2 | ||
}, { | ||
"declaredTypeId": 3, | ||
"extendsDeclaredTypeId": 2, | ||
"implementsDeclaredTypeId": [4, 5] | ||
}, { | ||
"declaredTypeId": 6, | ||
"extendsDeclaredTypeId": 2, | ||
"implementsDeclaredTypeId": [4] | ||
}] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters