From 26149be44a4ca96e80922e0e9efac53c536f9719 Mon Sep 17 00:00:00 2001 From: Vladimir Sukharev Date: Thu, 21 Jul 2022 21:45:03 +0000 Subject: [PATCH] KT53100: Optimization: (CONSTANT_PRIMITIVE(x: T?)) => x Merge-request: KT-MR-6684 Merged-by: Vladimir Sukharev --- .../konan/lower/RedundantCoercionsCleaner.kt | 13 +++++++++-- .../backend.native/tests/build.gradle | 5 +++++ .../tests/codegen/boxing/kt53100_casts.kt | 22 +++++++++++++++++++ .../tests/codegen/boxing/kt53100_casts.out | 2 ++ .../tests/filecheck/kt49847_class.kt | 2 -- .../tests/filecheck/kt49847_generic.kt | 2 -- 6 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 kotlin-native/backend.native/tests/codegen/boxing/kt53100_casts.kt create mode 100644 kotlin-native/backend.native/tests/codegen/boxing/kt53100_casts.out diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/RedundantCoercionsCleaner.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/RedundantCoercionsCleaner.kt index 30a8fc75a9a..687c91f936a 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/RedundantCoercionsCleaner.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/RedundantCoercionsCleaner.kt @@ -7,11 +7,9 @@ package org.jetbrains.kotlin.backend.konan.lower import org.jetbrains.kotlin.backend.common.FileLoweringPass import org.jetbrains.kotlin.backend.konan.Context -import org.jetbrains.kotlin.backend.konan.DECLARATION_ORIGIN_INLINE_CLASS_SPECIAL_FUNCTION import org.jetbrains.kotlin.backend.konan.getInlinedClassNative import org.jetbrains.kotlin.backend.konan.ir.isBoxOrUnboxCall import org.jetbrains.kotlin.ir.IrElement -import org.jetbrains.kotlin.ir.IrStatement import org.jetbrains.kotlin.ir.declarations.IrClass import org.jetbrains.kotlin.ir.declarations.IrFile import org.jetbrains.kotlin.ir.declarations.IrFunction @@ -19,6 +17,7 @@ import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.impl.* import org.jetbrains.kotlin.ir.symbols.impl.IrReturnableBlockSymbolImpl import org.jetbrains.kotlin.ir.types.classifierOrFail +import org.jetbrains.kotlin.ir.types.classifierOrNull import org.jetbrains.kotlin.ir.util.dump import org.jetbrains.kotlin.ir.util.getArgumentsWithIr import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid @@ -266,6 +265,16 @@ internal class RedundantCoercionsCleaner(val context: Context) : FileLoweringPas } } + is IrConstantPrimitive -> + if (expression.value.type == coercion.type) { + // In case the initial and final types are equal, then + // the whole sequence of transformations is no-op, and we can remove them all + PossiblyFoldedExpression(expression.value, true) + } else { + // Types are not equal for ex. in kt53100_casts.kt + PossiblyFoldedExpression(expression.transformIfAsked(), false) + } + else -> PossiblyFoldedExpression(expression.transformIfAsked(), false) } } diff --git a/kotlin-native/backend.native/tests/build.gradle b/kotlin-native/backend.native/tests/build.gradle index 7895419be1a..4c15bb414ef 100644 --- a/kotlin-native/backend.native/tests/build.gradle +++ b/kotlin-native/backend.native/tests/build.gradle @@ -2581,6 +2581,11 @@ task boxing15(type: KonanLocalTest) { source = "codegen/boxing/boxing15.kt" } +task kt53100_casts(type: KonanLocalTest) { + useGoldenData = true + source = "codegen/boxing/kt53100_casts.kt" +} + task boxCache0(type: KonanLocalTest) { useGoldenData = true source = "codegen/boxing/box_cache0.kt" diff --git a/kotlin-native/backend.native/tests/codegen/boxing/kt53100_casts.kt b/kotlin-native/backend.native/tests/codegen/boxing/kt53100_casts.kt new file mode 100644 index 00000000000..71d16a48a7f --- /dev/null +++ b/kotlin-native/backend.native/tests/codegen/boxing/kt53100_casts.kt @@ -0,0 +1,22 @@ +/* + * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package codegen.boxing.kt53100_casts + +import kotlin.test.* + +// Reproducer is copied from FloatingPointParser.unaryMinus() +inline fun unaryMinus(value: T): T { + return when (value) { + is Float -> -value as T + is Double -> -value as T + else -> throw NumberFormatException() + } +} + +@Test fun runTest(){ + println(unaryMinus(0.0)) + println(unaryMinus(0.0f)) +} diff --git a/kotlin-native/backend.native/tests/codegen/boxing/kt53100_casts.out b/kotlin-native/backend.native/tests/codegen/boxing/kt53100_casts.out new file mode 100644 index 00000000000..e6ffe0d430b --- /dev/null +++ b/kotlin-native/backend.native/tests/codegen/boxing/kt53100_casts.out @@ -0,0 +1,2 @@ +-0.0 +-0.0 diff --git a/kotlin-native/backend.native/tests/filecheck/kt49847_class.kt b/kotlin-native/backend.native/tests/filecheck/kt49847_class.kt index 5148e7b2eb3..c54ff9878db 100644 --- a/kotlin-native/backend.native/tests/filecheck/kt49847_class.kt +++ b/kotlin-native/backend.native/tests/filecheck/kt49847_class.kt @@ -9,8 +9,6 @@ class C { // CHECK-LABEL: define void @"kfun:#main(){}"() // CHECK-NOT: Int-box -// TODO Remove next check after fix of https://youtrack.jetbrains.com/issue/KT-53100/Optimization-needed-T-unboxCONSTANTPRIMITIVEx-T-x -// CHECK: Int-unbox // CHECK-NOT: Int-unbox // CHECK: ret void fun main() { diff --git a/kotlin-native/backend.native/tests/filecheck/kt49847_generic.kt b/kotlin-native/backend.native/tests/filecheck/kt49847_generic.kt index 19f3ffb813e..5a7e8930487 100644 --- a/kotlin-native/backend.native/tests/filecheck/kt49847_generic.kt +++ b/kotlin-native/backend.native/tests/filecheck/kt49847_generic.kt @@ -9,8 +9,6 @@ class C { // CHECK-LABEL: define void @"kfun:#main(){}"() // CHECK-NOT: Int-box -// TODO Remove next check after fix of https://youtrack.jetbrains.com/issue/KT-53100/Optimization-needed-T-unboxCONSTANTPRIMITIVEx-T-x -// CHECK: Int-unbox // CHECK-NOT: Int-unbox // CHECK: ret void fun main() {