Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix testable pkg InvocationData handling logic for Dead Code Elimination #43566

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public void visit(BIRNode.BIRTypeDefinition typeDef) {
}

typeDef.markAsUsed();
currentInvocationData.addToUsedPool(typeDef);
getCurrentInvocationData().addToUsedPool(typeDef);
usedTypeDefAnalyzer.analyzeTypeDef(typeDef);
typeDef.attachedFuncs.forEach(
attachedFunc -> addDependentFunctionAndVisit(typeDef, attachedFunc, typeDef.type.tsymbol.pkgID));
Expand All @@ -197,8 +197,8 @@ public void visit(BIRNode.BIRTypeDefinition typeDef) {
public void visit(BIRNode.BIRBasicBlock birBasicBlock) {
Map<BIRNode.BIRVariableDcl, FunctionPointerData> previousLocalFpHolders = localFpHolders;
currentInstructionArr = birBasicBlock.instructions;
localFpHolders = new HashMap<>(currentInvocationData.globalVarFPDataPool);
localFpHolders.putAll(currentInvocationData.globalVarFPDataPool);
localFpHolders = new HashMap<>(getCurrentInvocationData().globalVarFPDataPool);
localFpHolders.putAll(getCurrentInvocationData().globalVarFPDataPool);

birBasicBlock.instructions.forEach(instruction -> {
if (ANALYZED_INSTRUCTION_KINDS.contains(instruction.getKind())) {
Expand All @@ -225,7 +225,7 @@ public void visit(BIRNode.BIRBasicBlock birBasicBlock) {
public void visit(BIRTerminator.Call call) {
// If function pointers are passed as parameters, they will be bound to the parent function
getUsedLocalFPHolders(call.args).forEach(var -> {
FunctionPointerData fpData = currentInvocationData.getFPData(var);
FunctionPointerData fpData = getCurrentInvocationData().getFPData(var);
if (fpData != null && fpData.lambdaFunction != null) {
visitNode(fpData.lambdaFunction);
}
Expand Down Expand Up @@ -263,9 +263,9 @@ public void visit(BIRTerminator.FPCall fpCall) {

FunctionPointerData fpData;
if (fpPointer instanceof BIRNode.BIRGlobalVariableDcl globalVariableDcl) {
fpData = currentInvocationData.globalVarFPDataPool.get(globalVariableDcl);
fpData = getCurrentInvocationData().globalVarFPDataPool.get(globalVariableDcl);
} else {
fpData = currentInvocationData.getFPData(fpCall.fp.variableDcl);
fpData = getCurrentInvocationData().getFPData(fpCall.fp.variableDcl);
}

if (fpData == null) {
Expand Down Expand Up @@ -327,15 +327,15 @@ private void initializeFpDataUsedState(BIRNode.BIRVariableDcl lambdaPointerVar,
*/
@Override
public void visit(BIRNonTerminator.RecordDefaultFPLoad recordDefaultFPLoad) {
FunctionPointerData fpData = currentInvocationData.getFPData(recordDefaultFPLoad.lhsOp.variableDcl);
FunctionPointerData fpData = getCurrentInvocationData().getFPData(recordDefaultFPLoad.lhsOp.variableDcl);
fpData.recordDefaultFPLoad = recordDefaultFPLoad;
if (fpData.recordDefaultFPLoad.enclosedType.isUsed) {
fpData.lambdaPointerVar.markAsUsed();
visitNode(fpData.lambdaFunction);
}
currentInvocationData.recordDefTypeWiseFPDataPool.putIfAbsent(recordDefaultFPLoad.enclosedType,
getCurrentInvocationData().recordDefTypeWiseFPDataPool.putIfAbsent(recordDefaultFPLoad.enclosedType,
new HashSet<>());
currentInvocationData.recordDefTypeWiseFPDataPool.get(recordDefaultFPLoad.enclosedType).add(fpData);
getCurrentInvocationData().recordDefTypeWiseFPDataPool.get(recordDefaultFPLoad.enclosedType).add(fpData);
}

@Override
Expand Down Expand Up @@ -435,7 +435,7 @@ private void analyzeGlobalVariableDeclUsage(BIRNode.BIRGlobalVariableDcl globalR
getInvocationData(globalRhsVar.pkgId)
.registerNodes(usedTypeDefAnalyzer, this.pkgCache.getBirPkg(globalRhsVar.pkgId));
} else if (isFunctionKindType(globalRhsVar.type)) {
visitNode(currentInvocationData.globalVarFPDataPool.get(globalRhsVar).lambdaFunction);
visitNode(getCurrentInvocationData().globalVarFPDataPool.get(globalRhsVar).lambdaFunction);
}
}

Expand All @@ -445,7 +445,7 @@ private void analyzeFunctionPointerReferences(Set<BIRNode.BIRVariableDcl> rhsVar
}
rhsVars.retainAll(localFpHolders.keySet());
for (BIRNode.BIRVariableDcl var : rhsVars) {
FunctionPointerData fpData = currentInvocationData.getFPData(var);
FunctionPointerData fpData = getCurrentInvocationData().getFPData(var);
if (fpData != null && fpData.lambdaFunction != null) {
visitNode(fpData.lambdaFunction);
}
Expand All @@ -459,7 +459,7 @@ private void registerUsedNativeClassPaths(BIRNode.BIRFunction birFunction) {
BIRNode.ConstValue constValue = ((BIRNode.BIRConstAnnotationAttachment) annot).annotValue;
String filePath =
((Map<String, BIRNode.ConstValue>) constValue.value).get(CLASS).value.toString();
currentInvocationData.usedNativeClassPaths.add(filePath.replace(".", "/") + DOT_CLASS);
getCurrentInvocationData().usedNativeClassPaths.add(filePath.replace(".", "/") + DOT_CLASS);
}
});
}
Expand Down Expand Up @@ -490,6 +490,10 @@ private void addDependentFunctionAndVisit(BIRNode.BIRDocumentableNode parentNode
childInvocationData.startPointNodes.add(childFunction);
}

public InvocationData getCurrentInvocationData() {
return isTestablePkgAnalysis? currentInvocationData.testablePkgInvocationData : currentInvocationData;
}

public static class InvocationData {

// Following Sets are used to whitelist the bal types called through ValueCreator in native dependencies
Expand Down Expand Up @@ -614,9 +618,6 @@ private void addToUsedPool(BIRNode.BIRFunction birFunction) {
void addToUsedPool(BIRNode.BIRTypeDefinition birTypeDef) {
if (this.unusedTypeDefs.remove(birTypeDef)) {
this.usedTypeDefs.add(birTypeDef);
} else if (this.testablePkgInvocationData != null) {
this.testablePkgInvocationData.unusedTypeDefs.remove(birTypeDef);
this.testablePkgInvocationData.usedTypeDefs.add(birTypeDef);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void visitType(BType bType, AnalyzerData data) {
@Override
public void analyzeType(BType bType, AnalyzerData data) {
Set<UsedBIRNodeAnalyzer.FunctionPointerData> fpDataSet =
usedBIRNodeAnalyzer.currentInvocationData.getFpData(bType);
usedBIRNodeAnalyzer.getCurrentInvocationData().getFpData(bType);
if (fpDataSet != null) {
fpDataSet.forEach(fpData -> {
fpData.lambdaPointerVar.markAsUsed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"foo",
"fn",
"testUsageFunction",
"buildPkgFn",
"main",
"bar"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ Running Tests
optimized_test_run_test
[pass] importUsageTest
[pass] simpleTest
[pass] testFPCallToBuildPkgFromTestablePkg
[pass] testFn
[pass] unusedFunctionInvoker


4 passing
5 passing
0 failing
0 skipped

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ Running Tests
optimized_test_run_test
[pass] importUsageTest
[pass] simpleTest
[pass] testFPCallToBuildPkgFromTestablePkg
[pass] testFn
[pass] unusedFunctionInvoker


4 passing
5 passing
0 failing
0 skipped

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@ function testUsageFunction() returns RecB{
RecB recB = {name: 0};
return recB;
}

public function buildPkgFn (string a) returns string{
return "hello";
};
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,11 @@ function testFn() {
function unusedFunctionInvoker(){
test:assertEquals(0, testUsageFunction().name);
}

@test:Config {}
function testFPCallToBuildPkgFromTestablePkg() {
worker StdInReader returns string {
string word = buildPkgFn("foo");
return word;
}
}