Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Filter return Unit from tail-optimized suspend funs #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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 @@ -25,10 +25,12 @@ object KotlinCoroutineTarget {
nop() // assertFullyCovered()
} // assertFullyCovered()

private suspend fun suspendingFunctionWithTailCallOptimization() { // assertEmpty()
nop() // assertFullyCovered()
anotherSuspendingFunction() // assertFullyCovered()
} // assertFullyCovered()
private suspend fun suspendingFunctionWithTailCallOptimization(b: Boolean) { // assertEmpty()
if (b)
anotherSuspendingFunction() // assertFullyCovered()
else
anotherSuspendingFunction() // assertFullyCovered()
} // assertEmpty()

private suspend fun anotherSuspendingFunction() {
nop() // assertFullyCovered()
Expand All @@ -42,7 +44,8 @@ object KotlinCoroutineTarget {
nop(x) // assertFullyCovered()
suspendingFunction() // assertFullyCovered()
nop(x) // assertFullyCovered()
suspendingFunctionWithTailCallOptimization()
suspendingFunctionWithTailCallOptimization(true) // assertFullyCovered()
suspendingFunctionWithTailCallOptimization(false) // assertFullyCovered()
} // assertFullyCovered()

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,13 @@ public void should_filter_suspending_functions_with_tail_call_optimization() {
context.classAnnotations
.add(KotlinGeneratedFilter.KOTLIN_METADATA_DESC);

final Label exit = new Label();
final Label[] labels = new Label[] { new Label(), new Label(),
new Label(), new Label(), new Label(), new Label() };

m.visitLabel(labels[0]);
m.visitVarInsn(Opcodes.ILOAD, 1);
final Label next = new Label();
m.visitJumpInsn(Opcodes.IFEQ, next);
m.visitLabel(labels[2]);
m.visitJumpInsn(Opcodes.IFEQ, labels[1]);

m.visitVarInsn(Opcodes.ALOAD, 0);
m.visitVarInsn(Opcodes.ALOAD, 2);
Expand All @@ -408,16 +410,15 @@ public void should_filter_suspending_functions_with_tail_call_optimization() {
m.visitMethodInsn(Opcodes.INVOKESTATIC,
"kotlin/coroutines/intrinsics/IntrinsicsKt",
"getCOROUTINE_SUSPENDED", "()Ljava/lang/Object;", false);
final Label label = new Label();
m.visitJumpInsn(Opcodes.IF_ACMPNE, label);
m.visitJumpInsn(Opcodes.IF_ACMPNE, labels[3]);
m.visitInsn(Opcodes.ARETURN);
m.visitLabel(label);
m.visitLabel(labels[3]);
m.visitInsn(Opcodes.POP);
range1.toInclusive = m.instructions.getLast();
}

m.visitJumpInsn(Opcodes.GOTO, exit);
m.visitLabel(next);
m.visitJumpInsn(Opcodes.GOTO, labels[4]);
m.visitLabel(labels[1]);

m.visitVarInsn(Opcodes.ALOAD, 0);
m.visitVarInsn(Opcodes.ALOAD, 2);
Expand All @@ -430,22 +431,26 @@ public void should_filter_suspending_functions_with_tail_call_optimization() {
m.visitMethodInsn(Opcodes.INVOKESTATIC,
"kotlin/coroutines/intrinsics/IntrinsicsKt",
"getCOROUTINE_SUSPENDED", "()Ljava/lang/Object;", false);
final Label label = new Label();
m.visitJumpInsn(Opcodes.IF_ACMPNE, label);
m.visitJumpInsn(Opcodes.IF_ACMPNE, labels[5]);
m.visitInsn(Opcodes.ARETURN);
m.visitLabel(label);
m.visitLabel(labels[5]);
m.visitInsn(Opcodes.POP);
range2.toInclusive = m.instructions.getLast();
}

m.visitLabel(exit);
m.visitFieldInsn(Opcodes.GETSTATIC, "kotlin/Unit", "INSTANCE",
"Lkotlin/Unit;");
m.visitInsn(Opcodes.ARETURN);
m.visitLabel(labels[4]);
final Range range3 = new Range();
{
m.visitFieldInsn(Opcodes.GETSTATIC, "kotlin/Unit", "INSTANCE",
"Lkotlin/Unit;");
range3.fromInclusive = m.instructions.getLast();
m.visitInsn(Opcodes.ARETURN);
range3.toInclusive = m.instructions.getLast();
}

filter.filter(m, context, output);

assertIgnored(range1, range2);
// range3 is ignored twice, once for each suspend call
assertIgnored(range1, range3, range2, range3);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,20 @@ final void nextIsSwitch() {
}
}

final void nextIsLabel() {
/**
* Moves {@link #cursor} to next instruction if it has given opcode,
* otherwise sets it to <code>null</code>.
*/
if (cursor == null) {
return;
}
cursor = cursor.getNext();
if (cursor != null && cursor.getType() != AbstractInsnNode.LABEL) {
cursor = null;
}
}

/**
* Moves {@link #cursor} to next instruction if it has given opcode,
* otherwise sets it to <code>null</code>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.JumpInsnNode;
import org.objectweb.asm.tree.LabelNode;
import org.objectweb.asm.tree.LdcInsnNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
Expand Down Expand Up @@ -62,10 +63,40 @@ private void matchOptimizedTailCall(final MethodNode methodNode,
"kotlin/coroutines/intrinsics/IntrinsicsKt",
"getCOROUTINE_SUSPENDED", "()Ljava/lang/Object;");
nextIs(Opcodes.IF_ACMPNE);
JumpInsnNode jump = (JumpInsnNode) cursor;
nextIs(Opcodes.ARETURN);
nextIsLabel();
LabelNode label = (LabelNode) cursor;
nextIs(Opcodes.POP);
if (cursor != null) {
if (cursor != null && jump.label == label) {
output.ignore(i.getNext(), cursor);
AbstractInsnNode pop = cursor;

// If we find a GOTO, follow it, the return Unit
// instructions may be there
nextIs(Opcodes.GOTO);
if (cursor != null) {
LabelNode gotoTarget = ((JumpInsnNode) cursor).label;
for (AbstractInsnNode j = cursor; j != null; j = j
.getNext()) {
if (j == gotoTarget) {
cursor = j;
break;
}
}
} else {
// else reset to the pop instruction
cursor = pop;
}

// ignore the return-unit path as well
nextIsField(Opcodes.GETSTATIC, "kotlin/Unit", "INSTANCE",
"Lkotlin/Unit;");
AbstractInsnNode getUnit = cursor;
nextIs(Opcodes.ARETURN);
if (cursor != null) {
output.ignore(getUnit, cursor);
}
}
}
}
Expand Down