Skip to content

Commit

Permalink
Fix duplicate typedesc creation for each type desc expression
Browse files Browse the repository at this point in the history
  • Loading branch information
rdulmina committed Nov 25, 2024
1 parent 7e04bf7 commit c0fa12d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,8 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import javax.xml.XMLConstants;

import static org.ballerinalang.model.tree.NodeKind.CLASS_DEFN;
Expand Down Expand Up @@ -2235,7 +2233,7 @@ public void visit(BLangXMLAccessExpr xmlAccessExpr) {

@Override
public void visit(BLangTypedescExpr accessExpr) {
createNewTypedescInst(accessExpr.resolvedType, accessExpr.pos);
this.env.targetOperand = new BIROperand(getTypedescVariable(accessExpr.resolvedType, accessExpr.pos));
}

@Override
Expand Down Expand Up @@ -2410,23 +2408,24 @@ private BIRNonTerminator.NewStructure createNewStructureInst(BIROperand typeDesc
}

private BIRVariableDcl getTypedescVariable(BType type, Location pos) {
Supplier<BIRVariableDcl>[] checks = new Supplier[] {
() -> findInPackageScope(type),
() -> findInLocalSymbolVarMap(type, env.symbolVarMap, false),
() -> findInLocalSymbolVarMap(type, env.symbolVarMap, true),
() -> findInGlobalSymbolVarMap(type, this.globalVarMap, false),
() -> findInGlobalSymbolVarMap(type, this.globalVarMap, true)
};
BIRVariableDcl variableDcl = findInPackageScope(type);
if (variableDcl != null && variableDcl.initialized) return variableDcl;

// Iterate through the suppliers and return the first non-null result
for (Supplier<BIRVariableDcl> check : checks) {
BIRVariableDcl variableDcl = check.get();
if (variableDcl != null) {
return variableDcl;
}
}
variableDcl = findInLocalSymbolVarMap(type, env.symbolVarMap, false);
if (variableDcl != null && variableDcl.initialized) return variableDcl;

variableDcl = findInLocalSymbolVarMap(type, env.symbolVarMap, true);
if (variableDcl != null && variableDcl.initialized) return variableDcl;

variableDcl = findInGlobalSymbolVarMap(type, this.globalVarMap, false);
if (variableDcl != null && variableDcl.initialized) return variableDcl;

variableDcl = findInGlobalSymbolVarMap(type, this.globalVarMap, true);
if (variableDcl != null && variableDcl.initialized) return variableDcl;

// TODO: we need to remove typedesc creating completely from here and handle it in the Desugar phase
// Create new type desc instruction if the typedesc variable is not found
// TODO: we need to handle duplicate typedesc creation for some cases.
// eg: function params ie `foo(record {int a;})`
createNewTypedescInst(type, pos);
return this.env.targetOperand.variableDcl;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public static class BIRVariableDcl extends BIRDocumentableNode {
public BIRBasicBlock startBB;
public int insOffset;
public boolean onlyUsedInSingleBB;
public boolean initialized = false;

// Stores the scope of the current instruction with respect to local variables.
public BirScope insScope;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public Move(Location pos, BIROperand fromOperand, BIROperand toOperand) {
super(pos, InstructionKind.MOVE);
this.rhsOp = fromOperand;
this.lhsOp = toOperand;
toOperand.variableDcl.initialized = true;
}

@Override
Expand Down

0 comments on commit c0fa12d

Please sign in to comment.