From 250cc1dc921fb18cfea899f3bd6cc73e8cf719f4 Mon Sep 17 00:00:00 2001 From: Mads Ager Date: Thu, 14 Jan 2021 13:38:19 +0100 Subject: [PATCH] [JVM] Never treat arguments to methods as locals that can be removed. Fixes KT-44347 --- .../codegen/optimization/common/Util.kt | 14 ++++++++++++ .../FirBlackBoxCodegenTestGenerated.java | 6 +++++ .../capturedVarsOptimization/kt44347.kt | 22 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 +++++ .../IrBlackBoxCodegenTestGenerated.java | 6 +++++ .../LightAnalysisModeTestGenerated.java | 5 +++++ .../IrJsCodegenBoxES6TestGenerated.java | 5 +++++ .../IrJsCodegenBoxTestGenerated.java | 5 +++++ .../semantics/JsCodegenBoxTestGenerated.java | 5 +++++ .../IrCodegenBoxWasmTestGenerated.java | 5 +++++ 10 files changed, 79 insertions(+) create mode 100644 compiler/testData/codegen/box/closures/capturedVarsOptimization/kt44347.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/common/Util.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/common/Util.kt index 7052f44419c..2d0a16e6328 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/common/Util.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/common/Util.kt @@ -119,6 +119,20 @@ fun MethodNode.removeEmptyCatchBlocks() { fun MethodNode.removeUnusedLocalVariables() { val used = BooleanArray(maxLocals) { false } + + // Arguments are always used whether or not they are in the local variable table + // or used by instructions. + var argumentIndex = 0 + val isStatic = (access and Opcodes.ACC_STATIC) != 0 + if (!isStatic) { + used[argumentIndex++] = true + } + for (argumentType in Type.getArgumentTypes(desc)) { + for (i in 0 until argumentType.size) { + used[argumentIndex++] = true + } + } + for (insn in instructions) { when (insn) { is VarInsnNode -> { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index 6a28527ffad..943feeabdd5 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -5591,6 +5591,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt17588.kt"); } + @Test + @TestMetadata("kt44347.kt") + public void testKt44347() throws Exception { + runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt44347.kt"); + } + @Test @TestMetadata("sharedSlotsWithCapturedVars.kt") public void testSharedSlotsWithCapturedVars() throws Exception { diff --git a/compiler/testData/codegen/box/closures/capturedVarsOptimization/kt44347.kt b/compiler/testData/codegen/box/closures/capturedVarsOptimization/kt44347.kt new file mode 100644 index 00000000000..a714bfdcd52 --- /dev/null +++ b/compiler/testData/codegen/box/closures/capturedVarsOptimization/kt44347.kt @@ -0,0 +1,22 @@ +// WITH_RUNTIME + +import kotlin.coroutines.* + +fun launch(block: suspend (Long) -> String): String { + var result = "" + block.startCoroutine(0L, Continuation(EmptyCoroutineContext) { result = it.getOrThrow() }) + return result +} + +suspend fun g() {} + +class C { + suspend fun f(i: Long): String { + var x = 0 + listOf().map { x } + g() + return "OK" + } +} + +fun box(): String = launch(C()::f) \ No newline at end of file diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java index 79c2c172c23..bb729d71633 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java @@ -5591,6 +5591,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt17588.kt"); } + @Test + @TestMetadata("kt44347.kt") + public void testKt44347() throws Exception { + runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt44347.kt"); + } + @Test @TestMetadata("sharedSlotsWithCapturedVars.kt") public void testSharedSlotsWithCapturedVars() throws Exception { diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java index 54487ff41c1..22a5574719e 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java @@ -5591,6 +5591,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt17588.kt"); } + @Test + @TestMetadata("kt44347.kt") + public void testKt44347() throws Exception { + runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt44347.kt"); + } + @Test @TestMetadata("sharedSlotsWithCapturedVars.kt") public void testSharedSlotsWithCapturedVars() throws Exception { diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 8622fa157c0..a6bad3af3a6 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -4878,6 +4878,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt17588.kt"); } + @TestMetadata("kt44347.kt") + public void testKt44347() throws Exception { + runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt44347.kt"); + } + @TestMetadata("sharedSlotsWithCapturedVars.kt") public void testSharedSlotsWithCapturedVars() throws Exception { runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/sharedSlotsWithCapturedVars.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java index 28c85588981..a878893ba18 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java @@ -3993,6 +3993,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt17588.kt"); } + @TestMetadata("kt44347.kt") + public void testKt44347() throws Exception { + runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt44347.kt"); + } + @TestMetadata("sharedSlotsWithCapturedVars.kt") public void testSharedSlotsWithCapturedVars() throws Exception { runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/sharedSlotsWithCapturedVars.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java index e60fa3485e9..7dfd2fabe16 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java @@ -3993,6 +3993,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt17588.kt"); } + @TestMetadata("kt44347.kt") + public void testKt44347() throws Exception { + runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt44347.kt"); + } + @TestMetadata("sharedSlotsWithCapturedVars.kt") public void testSharedSlotsWithCapturedVars() throws Exception { runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/sharedSlotsWithCapturedVars.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index da8c458c93d..f3fd0c1d277 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -3993,6 +3993,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt17588.kt"); } + @TestMetadata("kt44347.kt") + public void testKt44347() throws Exception { + runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt44347.kt"); + } + @TestMetadata("sharedSlotsWithCapturedVars.kt") public void testSharedSlotsWithCapturedVars() throws Exception { runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/sharedSlotsWithCapturedVars.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java index 36b3661044b..13221bc458a 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java @@ -2864,6 +2864,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt17200.kt"); } + @TestMetadata("kt44347.kt") + public void testKt44347() throws Exception { + runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt44347.kt"); + } + @TestMetadata("sharedSlotsWithCapturedVars.kt") public void testSharedSlotsWithCapturedVars() throws Exception { runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/sharedSlotsWithCapturedVars.kt");