Skip to content

Commit

Permalink
wasm gc: fix issues with type inference
Browse files Browse the repository at this point in the history
  • Loading branch information
konsoletyper committed Sep 4, 2024
1 parent a9d46ac commit fcf6d72
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,15 @@

public class WasmGCVariableCategoryProvider implements VariableCategoryProvider {
private ClassHierarchy hierarchy;
private PreciseTypeInference inference;

public WasmGCVariableCategoryProvider(ClassHierarchy hierarchy) {
this.hierarchy = hierarchy;
}

public PreciseTypeInference getTypeInference() {
return inference;
}

@Override
public Object[] getCategories(Program program, MethodReference method) {
inference = new PreciseTypeInference(program, method, hierarchy);
inference.setPhisSkipped(true);
var inference = new PreciseTypeInference(program, method, hierarchy);
inference.setPhisSkipped(false);
inference.setBackPropagation(true);
var result = new Object[program.variableCount()];
for (int i = 0; i < program.variableCount(); ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.teavm.ast.decompilation.Decompiler;
import org.teavm.backend.wasm.BaseWasmFunctionRepository;
import org.teavm.backend.wasm.WasmFunctionTypes;
import org.teavm.backend.wasm.gc.PreciseTypeInference;
import org.teavm.backend.wasm.gc.WasmGCVariableCategoryProvider;
import org.teavm.backend.wasm.gc.vtable.WasmGCVirtualTableProvider;
import org.teavm.backend.wasm.generate.gc.WasmGCNameProvider;
Expand Down Expand Up @@ -220,7 +221,10 @@ private void generateRegularMethodBody(MethodHolder method, WasmFunction functio
allocator.allocateRegisters(method.getReference(), method.getProgram(), friendlyToDebugger);
var ast = decompiler.decompileRegular(method);
var firstVar = method.hasModifier(ElementModifier.STATIC) ? 1 : 0;
var typeInference = categoryProvider.getTypeInference();
var typeInference = new PreciseTypeInference(method.getProgram(), method.getReference(), hierarchy);
typeInference.setPhisSkipped(true);
typeInference.setBackPropagation(true);
typeInference.ensure();

var registerCount = 0;
for (var i = 0; i < method.getProgram().variableCount(); ++i) {
Expand Down
16 changes: 12 additions & 4 deletions core/src/main/java/org/teavm/model/util/RegisterAllocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.teavm.common.DisjointSet;
import org.teavm.common.MutableGraphEdge;
import org.teavm.common.MutableGraphNode;
Expand Down Expand Up @@ -53,7 +54,8 @@ public void allocateRegisters(MethodReference method, Program program, boolean d
program, method.parameterCount(), liveness);
DisjointSet congruenceClasses = buildPhiCongruenceClasses(program);
joinClassNodes(interferenceGraph, congruenceClasses);
removeRedundantCopies(program, interferenceGraph, congruenceClasses);
var categories = variableCategoryProvider.getCategories(program, method);
removeRedundantCopies(program, interferenceGraph, congruenceClasses, categories);
int[] classArray = congruenceClasses.pack(program.variableCount());
renameVariables(program, classArray);
int[] colors = new int[program.variableCount()];
Expand All @@ -68,8 +70,13 @@ public void allocateRegisters(MethodReference method, Program program, boolean d
for (int cls : classArray) {
maxClass = Math.max(maxClass, cls + 1);
}
var categories = variableCategoryProvider.getCategories(program, method);
String[] names = getVariableNames(program, debuggerFriendly);
var newCategories = new Object[categories.length];
for (int i = 0; i < categories.length; ++i) {
var cls = classArray[i];
newCategories[cls] = categories[i];
}
categories = newCategories;
colorer.colorize(MutableGraphNode.toGraph(interferenceGraph), colors, categories, names);

int maxColor = 0;
Expand Down Expand Up @@ -274,7 +281,7 @@ private void insertCopy(Incoming incoming, Map<BasicBlock, BasicBlock> blockMap)
}

private void removeRedundantCopies(Program program, List<MutableGraphNode> interferenceGraph,
DisjointSet congruenceClasses) {
DisjointSet congruenceClasses, Object[] categories) {
for (int i = 0; i < program.basicBlockCount(); ++i) {
BasicBlock block = program.basicBlockAt(i);
Instruction nextInsn;
Expand All @@ -297,7 +304,8 @@ private void removeRedundantCopies(Program program, List<MutableGraphNode> inter
break;
}
}
if (!interfere) {
if (!interfere && Objects.equals(categories[assignment.getReceiver().getIndex()],
categories[assignment.getAssignee().getIndex()])) {
int newClass = congruenceClasses.union(copyClass, origClass);
insn.delete();
if (newClass == interferenceGraph.size()) {
Expand Down

0 comments on commit fcf6d72

Please sign in to comment.