diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/ir/Ir.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/ir/Ir.kt index 03d0d011827..18adb911cb6 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/ir/Ir.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/common/ir/Ir.kt @@ -207,13 +207,29 @@ open class Symbols(val context: T, private val symb } ) - fun getBinaryOperator(name: Name, lhsType: KotlinType, rhsType: KotlinType) = - symbolTable.referenceFunction(lhsType.memberScope.getContributedFunctions(name, NoLookupLocation.FROM_BACKEND) - .single { it.valueParameters.size == 1 && it.valueParameters[0].type == rhsType } - ) + private val binaryOperatorCache = mutableMapOf, IrFunctionSymbol>() + fun getBinaryOperator(name: Name, lhsType: KotlinType, rhsType: KotlinType): IrFunctionSymbol { + val key = Triple(name, lhsType, rhsType) + var result = binaryOperatorCache[key] + if (result == null) { + result = symbolTable.referenceFunction(lhsType.memberScope.getContributedFunctions(name, NoLookupLocation.FROM_BACKEND) + .single { it.valueParameters.size == 1 && it.valueParameters[0].type == rhsType } + ) + binaryOperatorCache[key] = result + } + return result + } - fun getUnaryOperator(name: Name, receiverType: KotlinType) = - symbolTable.referenceFunction(receiverType.memberScope.getContributedFunctions(name, NoLookupLocation.FROM_BACKEND) - .single { it.valueParameters.isEmpty() } - ) + private val unaryOperatorCache = mutableMapOf, IrFunctionSymbol>() + fun getUnaryOperator(name: Name, receiverType: KotlinType): IrFunctionSymbol { + val key = name to receiverType + var result = unaryOperatorCache[key] + if (result == null) { + result = symbolTable.referenceFunction(receiverType.memberScope.getContributedFunctions(name, NoLookupLocation.FROM_BACKEND) + .single { it.valueParameters.isEmpty() } + ) + unaryOperatorCache[key] = result + } + return result + } } \ No newline at end of file diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/ForLoopsLowering.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/ForLoopsLowering.kt index 85d83730d0b..322257e5752 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/ForLoopsLowering.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/ForLoopsLowering.kt @@ -329,7 +329,6 @@ private class ForLoopsTransformer(val context: Context) : IrElementTransformerVo val forLoopInfo = iteratorToLoopInfo[irIteratorAccess.symbol] ?: return null // If we didn't lower a corresponding header. val builder = context.createIrBuilder(scopeOwnerSymbol, initializer.startOffset, initializer.endOffset) - // TODO: Cache it. val plusOperator = symbols.getBinaryOperator( OperatorNameConventions.PLUS, forLoopInfo.inductionVariable.descriptor.type, diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index 16968964da3..3b4459c7acc 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -309,7 +309,7 @@ task sum_3const(type: RunKonanTest) { task codegen_basics_for_loops(type: RunKonanTest) { source = "codegen/basics/for_loops.kt" - goldValue = "01234\n0123\n43210\n\n024\n02\n420\n\n036\n03\n630\n\n" + goldValue = "01234\n0123\n43210\n\n024\n02\n420\n\n036\n03\n630\n\n024\n02\n420\n\n" } task codegen_basics_for_loops_types(type: RunKonanTest) { diff --git a/backend.native/tests/codegen/basics/for_loops.kt b/backend.native/tests/codegen/basics/for_loops.kt index cc8d1c2dee3..3b424afc533 100644 --- a/backend.native/tests/codegen/basics/for_loops.kt +++ b/backend.native/tests/codegen/basics/for_loops.kt @@ -51,4 +51,24 @@ fun main(args: Array) { } println() println() + + // Without constants + val a = 0 + val b = 4 + val s = 2 + for (i in a..b step s) { + print(i) + } + println() + + for (i in a until b step s) { + print(i) + } + println() + + for (i in b downTo a step s) { + print(i) + } + println() + println() } \ No newline at end of file