From 8af7a25f8e089aca5f9fe2d5321a4a2fb91eecf4 Mon Sep 17 00:00:00 2001 From: Mikhael Bogdanov Date: Thu, 17 Aug 2017 16:30:46 +0200 Subject: [PATCH] Update maxStack on synthetic instruction insertion #KT-19723 Fixed --- .../RedundantNullCheckMethodTransformer.kt | 30 ++++++++++++++----- .../boxInline/anonymousObject/kt19723.kt | 21 +++++++++++++ .../IrBlackBoxInlineCodegenTestGenerated.java | 6 ++++ ...otlinAgainstInlineKotlinTestGenerated.java | 6 ++++ .../BlackBoxInlineCodegenTestGenerated.java | 6 ++++ ...otlinAgainstInlineKotlinTestGenerated.java | 6 ++++ 6 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 compiler/testData/codegen/boxInline/anonymousObject/kt19723.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/nullCheck/RedundantNullCheckMethodTransformer.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/nullCheck/RedundantNullCheckMethodTransformer.kt index f8cec744e50..de09fc234c8 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/nullCheck/RedundantNullCheckMethodTransformer.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/nullCheck/RedundantNullCheckMethodTransformer.kt @@ -44,13 +44,14 @@ class RedundantNullCheckMethodTransformer : MethodTransformer() { private var changes = false fun run(): Boolean { - val checkedReferenceTypes = analyzeTypesAndRemoveDeadCode() - eliminateRedundantChecks(checkedReferenceTypes) + val stackOnThrowExceptions = hashMapOf() + val checkedReferenceTypes = analyzeTypesAndRemoveDeadCode(stackOnThrowExceptions) + eliminateRedundantChecks(checkedReferenceTypes, stackOnThrowExceptions) return changes } - private fun analyzeTypesAndRemoveDeadCode(): Map { + private fun analyzeTypesAndRemoveDeadCode(stackOnThrowExceptionsHolder: MutableMap): Map { val insns = methodNode.instructions.toArray() val frames = analyze(internalClassName, methodNode, OptimizationBasicInterpreter()) @@ -64,6 +65,9 @@ class RedundantNullCheckMethodTransformer : MethodTransformer() { else if (insn.isCheckParameterIsNotNull() || insn.isCheckExpressionValueIsNotNull()) { checkedReferenceTypes[insn] = frame?.peek(1)?.type ?: continue } + else if (insn.isThrowNpeIntrinsic()) { + stackOnThrowExceptionsHolder[insn] = frame?.maxStackSize ?: continue + } } val dceResult = DeadCodeEliminationMethodTransformer().removeDeadCodeByFrames(methodNode, frames) @@ -74,8 +78,11 @@ class RedundantNullCheckMethodTransformer : MethodTransformer() { return checkedReferenceTypes } - private fun eliminateRedundantChecks(checkedReferenceTypes: Map) { - val nullabilityAssumptions = injectNullabilityAssumptions(checkedReferenceTypes) + private fun eliminateRedundantChecks( + checkedReferenceTypes: Map, + stackOnThrowExceptions: MutableMap + ) { + val nullabilityAssumptions = injectNullabilityAssumptions(checkedReferenceTypes, stackOnThrowExceptions) val nullabilityMap = analyzeNullabilities() @@ -84,8 +91,10 @@ class RedundantNullCheckMethodTransformer : MethodTransformer() { transformTrivialChecks(nullabilityMap) } - private fun injectNullabilityAssumptions(checkedReferenceTypes: Map) = - NullabilityAssumptionsBuilder(checkedReferenceTypes).injectNullabilityAssumptions() + private fun injectNullabilityAssumptions( + checkedReferenceTypes: Map, + stackOnThrowExceptions: MutableMap + ) = NullabilityAssumptionsBuilder(checkedReferenceTypes, stackOnThrowExceptions).injectNullabilityAssumptions() private fun analyzeNullabilities(): Map { val frames = analyze(internalClassName, methodNode, NullabilityInterpreter()) @@ -168,7 +177,10 @@ class RedundantNullCheckMethodTransformer : MethodTransformer() { } } - private inner class NullabilityAssumptionsBuilder(val checkedReferenceTypes: Map) { + private inner class NullabilityAssumptionsBuilder( + val checkedReferenceTypes: Map, + val stackOnThrowExceptions: MutableMap + ) { private val checksDependingOnVariable = HashMap>() @@ -362,6 +374,8 @@ class RedundantNullCheckMethodTransformer : MethodTransformer() { athrow() }) } + + methodNode.maxStack = Math.max(methodNode.maxStack, (stackOnThrowExceptions[insn] ?: -1) + 1) } } diff --git a/compiler/testData/codegen/boxInline/anonymousObject/kt19723.kt b/compiler/testData/codegen/boxInline/anonymousObject/kt19723.kt new file mode 100644 index 00000000000..b02b61375c8 --- /dev/null +++ b/compiler/testData/codegen/boxInline/anonymousObject/kt19723.kt @@ -0,0 +1,21 @@ +// FILE: 1.kt +package test + +inline fun String.fire(message: String? = null) { + val res = this + message!! +} + +// FILE: 2.kt +// NO_CHECK_LAMBDA_INLINING + +import test.* + +fun box(): String { + val receiver = "receiver" + "".let { + { + receiver.fire() + } + } + return "OK" +} \ No newline at end of file diff --git a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java index 419365c376d..b510fa962ef 100644 --- a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java @@ -224,6 +224,12 @@ public class IrBlackBoxInlineCodegenTestGenerated extends AbstractIrBlackBoxInli doTest(fileName); } + @TestMetadata("kt19723.kt") + public void testKt19723() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/anonymousObject/kt19723.kt"); + doTest(fileName); + } + @TestMetadata("kt6552.kt") public void testKt6552() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/anonymousObject/kt6552.kt"); diff --git a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java index 719ca8d9cd9..bbdd4282ca0 100644 --- a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java +++ b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java @@ -224,6 +224,12 @@ public class IrCompileKotlinAgainstInlineKotlinTestGenerated extends AbstractIrC doTest(fileName); } + @TestMetadata("kt19723.kt") + public void testKt19723() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/anonymousObject/kt19723.kt"); + doTest(fileName); + } + @TestMetadata("kt6552.kt") public void testKt6552() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/anonymousObject/kt6552.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java index 03d13d07939..c8da2d68c1c 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java @@ -224,6 +224,12 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo doTest(fileName); } + @TestMetadata("kt19723.kt") + public void testKt19723() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/anonymousObject/kt19723.kt"); + doTest(fileName); + } + @TestMetadata("kt6552.kt") public void testKt6552() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/anonymousObject/kt6552.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java index a831d603a3b..936a348dbcd 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java @@ -224,6 +224,12 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi doTest(fileName); } + @TestMetadata("kt19723.kt") + public void testKt19723() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/anonymousObject/kt19723.kt"); + doTest(fileName); + } + @TestMetadata("kt6552.kt") public void testKt6552() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/anonymousObject/kt6552.kt");