From 16f0ba8e461f9c40ccb1993f4a949b815ed3da02 Mon Sep 17 00:00:00 2001 From: Igor Chevdar Date: Mon, 9 Aug 2021 19:37:58 +0500 Subject: [PATCH] [K/N][optmz] Reworked a bit work with fields --- .../backend/konan/optimizations/DFGBuilder.kt | 28 ++++++++----------- .../backend/konan/optimizations/DataFlowIR.kt | 4 ++- .../optimizations/DevirtualizationAnalysis.kt | 7 ++--- .../konan/optimizations/EscapeAnalysis.kt | 10 +++---- 4 files changed, 23 insertions(+), 26 deletions(-) diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/DFGBuilder.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/DFGBuilder.kt index b377df1100d..e77d6015649 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/DFGBuilder.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/DFGBuilder.kt @@ -448,6 +448,16 @@ internal class ModuleDFGBuilder(val context: Context, val irModule: IrModuleFrag private val reinterpret = symbols.reinterpret private val objCObjectRawValueGetter = symbols.interopObjCObjectRawValueGetter + private val fields = mutableMapOf() + private fun IrField.toDataFlowIRField() = fields.getOrPut(this) { + val name = name.asString() + DataFlowIR.Field( + symbolTable.mapType(type), + 1 + fields.size, + takeName { name } + ) + } + private class Scoped(val value: T, val scope: DataFlowIR.Node.Scope) private inner class FunctionDFGBuilder(val expressionValuesExtractor: ExpressionValuesExtractor, @@ -836,16 +846,9 @@ internal class ModuleDFGBuilder(val context: Context, val irModule: IrModuleFrag is IrGetField -> { val receiver = value.receiver?.let { expressionToEdge(it) } - val receiverType = value.receiver?.let { symbolTable.mapType(it.type) } - val name = value.symbol.owner.name.asString() DataFlowIR.Node.FieldRead( receiver, - DataFlowIR.Field( - receiverType, - symbolTable.mapType(value.symbol.owner.type), - name.localHash.value, - takeName { name } - ), + value.symbol.owner.toDataFlowIRField(), mapReturnType(value.type, value.symbol.owner.type), value ) @@ -853,16 +856,9 @@ internal class ModuleDFGBuilder(val context: Context, val irModule: IrModuleFrag is IrSetField -> { val receiver = value.receiver?.let { expressionToEdge(it) } - val receiverType = value.receiver?.let { symbolTable.mapType(it.type) } - val name = value.symbol.owner.name.asString() DataFlowIR.Node.FieldWrite( receiver, - DataFlowIR.Field( - receiverType, - symbolTable.mapType(value.symbol.owner.type), - name.localHash.value, - takeName { name } - ), + value.symbol.owner.toDataFlowIRField(), expressionToEdge(value.value), mapReturnType(value.value.type, value.symbol.owner.type) ) diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/DataFlowIR.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/DataFlowIR.kt index aeb486e5054..5aa0fcd65cd 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/DataFlowIR.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/DataFlowIR.kt @@ -206,7 +206,9 @@ internal object DataFlowIR { } } - data class Field(val receiverType: Type?, val type: Type, val hash: Long, val name: String? = null) + class Field(val type: Type, val index: Int, val name: String? = null) { + override fun toString() = "Field(type=$type, index=$index, name=$name)" + } class Edge(val castToType: Type?) { diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/DevirtualizationAnalysis.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/DevirtualizationAnalysis.kt index b17e9862f02..4a0ae656981 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/DevirtualizationAnalysis.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/DevirtualizationAnalysis.kt @@ -180,8 +180,7 @@ internal object DevirtualizationAnalysis { val voidNode = addNode { Node.Ordinary(it, { "Void" }) } val virtualNode = addNode { Node.Source(it, VIRTUAL_TYPE_ID, { "Virtual" }) } - val arrayItemField = DataFlowIR.Field(null, - symbolTable.mapClassReferenceType(context.irBuiltIns.anyClass.owner), 1, "Array\$Item") + val arrayItemField = DataFlowIR.Field(symbolTable.mapClassReferenceType(context.irBuiltIns.anyClass.owner), -1, "Array\$Item") val functions = mutableMapOf() val externalFunctions = mutableMapOf, Node>() val fields = mutableMapOf() // Do not distinguish receivers. @@ -1221,14 +1220,14 @@ internal object DevirtualizationAnalysis { val type = node.field.type.resolved() if (entryPoint == null && type.isFinal) addInstantiatingClass(type) - readField(node.field, type) + readField(node.field, node.type.resolved()) } is DataFlowIR.Node.FieldWrite -> { val type = node.field.type.resolved() if (entryPoint == null && type.isFinal) addInstantiatingClass(type) - writeField(node.field, type, edgeToConstraintNode(node.value)) + writeField(node.field, node.type.resolved(), edgeToConstraintNode(node.value)) constraintGraph.voidNode } diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/EscapeAnalysis.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/EscapeAnalysis.kt index b0138bd3aa6..3a5db71dcf5 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/EscapeAnalysis.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/EscapeAnalysis.kt @@ -108,12 +108,12 @@ internal object EscapeAnalysis { // A special marker field for external types implemented in the runtime (mainly, arrays). // The types being passed to the constructor are not used in the analysis - just put there anything. - private val intestinesField = DataFlowIR.Field(null, DataFlowIR.Type.Virtual, 1L, "inte\$tines") + private val intestinesField = DataFlowIR.Field(DataFlowIR.Type.Virtual, -1, "inte\$tines") // A special marker field for return values. // Basically we substitute [return x] with [ret.v@lue = x]. // This is done in order to not handle return parameter somewhat specially. - private val returnsValueField = DataFlowIR.Field(null, DataFlowIR.Type.Virtual, 2L, "v@lue") + private val returnsValueField = DataFlowIR.Field(DataFlowIR.Type.Virtual, -2, "v@lue") // Roles in which particular object reference is being used. private enum class Role { @@ -303,8 +303,8 @@ internal object EscapeAnalysis { for (i in path.indices) { if (i >= other.path.size) return 1 - if (path[i].hash != other.path[i].hash) - return path[i].hash.compareTo(other.path[i].hash) + if (path[i].index != other.path[i].index) + return path[i].index.compareTo(other.path[i].index) } if (path.size < other.path.size) return -1 return 0 @@ -327,7 +327,7 @@ internal object EscapeAnalysis { append(root ?: kind.toString()) path.forEach { append('.') - append(it.name ?: "") + append(it.name ?: "") } }