backend: Add caching for unary and binary operation symbols
This commit is contained in:
+24
-8
@@ -207,13 +207,29 @@ open class Symbols<out T: CommonBackendContext>(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<Triple<Name, KotlinType, KotlinType>, 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<Pair<Name, KotlinType>, 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
|
||||
}
|
||||
}
|
||||
-1
@@ -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,
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -51,4 +51,24 @@ fun main(args: Array<String>) {
|
||||
}
|
||||
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()
|
||||
}
|
||||
Reference in New Issue
Block a user