Skip to content

Commit 4830b44

Browse files
committed
Merge branch 'in-progress-temporary' into 2024
2 parents 3ab45bf + c847f9d commit 4830b44

File tree

4 files changed

+96
-2
lines changed

4 files changed

+96
-2
lines changed

build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ tasks {
8888
compileKotlin {
8989
dependsOn(project(":compiler-plugin").tasks.jar)
9090
// force recompile
91-
// outputs.upToDateWhen { false }
92-
// outputs.cacheIf { false }
91+
outputs.upToDateWhen { false }
92+
outputs.cacheIf { false }
9393
}
9494

9595
val benchmark by registering(JavaExec::class) {

compiler-plugin/src/main/kotlin/PluginRegistrar.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class PluginRegistrar : CompilerPluginRegistrar() {
1818
IrGenerationExtension.registerExtension(object : IrGenerationExtension {
1919
override fun generate(moduleFragment: IrModuleFragment, pluginContext: IrPluginContext) {
2020
moduleFragment.transform(Memoizer(pluginContext), null)
21+
moduleFragment.transform(SumInlining(pluginContext, configuration), null)
2122
moduleFragment.transform(OverflowChecker(pluginContext, configuration), null)
2223
moduleFragment.transform(IrDestructuringFinder(pluginContext, configuration), null)
2324
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.sschr15.aoc.compiler.internal
2+
3+
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
4+
import org.jetbrains.kotlin.backend.common.ir.inline
5+
import org.jetbrains.kotlin.config.CompilerConfiguration
6+
import org.jetbrains.kotlin.ir.IrStatement
7+
import org.jetbrains.kotlin.ir.builders.declarations.buildVariable
8+
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
9+
import org.jetbrains.kotlin.ir.declarations.IrFunction
10+
import org.jetbrains.kotlin.ir.declarations.lazy.IrLazyFunction
11+
import org.jetbrains.kotlin.ir.declarations.lazy.IrLazyFunctionBase
12+
import org.jetbrains.kotlin.ir.expressions.IrCall
13+
import org.jetbrains.kotlin.ir.expressions.IrExpression
14+
import org.jetbrains.kotlin.ir.expressions.IrSyntheticBody
15+
import org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI
16+
import org.jetbrains.kotlin.ir.types.isArray
17+
import org.jetbrains.kotlin.ir.types.isSubtypeOfClass
18+
import org.jetbrains.kotlin.ir.types.typeWith
19+
import org.jetbrains.kotlin.ir.util.getPackageFragment
20+
import org.jetbrains.kotlin.ir.util.isAnnotationWithEqualFqName
21+
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
22+
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
23+
import org.jetbrains.kotlin.name.FqName
24+
25+
@OptIn(UnsafeDuringIrConstructionAPI::class)
26+
class SumInlining(val context: IrPluginContext, val config: CompilerConfiguration) : IrElementTransformerVoid() {
27+
val skipCheckAnnotation = FqName("com.sschr15.aoc.annotations.SkipOverflowChecks")
28+
29+
override fun visitFunction(declaration: IrFunction): IrStatement {
30+
if (declaration.annotations.any { it.isAnnotationWithEqualFqName(skipCheckAnnotation) }) {
31+
return declaration // Skip this function
32+
}
33+
34+
val body = declaration.body ?: return super.visitFunction(declaration) // No body to transform
35+
if (body is IrSyntheticBody) return super.visitFunction(declaration) // Synthetic body has no statements
36+
37+
// val statements = body.statements.toMutableList()
38+
// statements.transformFlat { statement ->
39+
// if (statement !is IrCall) return@transformFlat (statement.transform(this, null) as? IrStatement)?.let(::listOf)
40+
// val function = statement.symbol.owner
41+
// config.report(CompilerMessageSeverity.WARNING, "Attempting function call: ${function.name}")
42+
// if (function.getPackageFragment().packageFqName != FqName("kotlin.collections"))
43+
// return@transformFlat listOf(statement.transform(this, null))
44+
// if (function.name.asString() !in listOf("sum", "sumBy", "sumOf")) return@transformFlat listOf(statement.transform(this, null))
45+
//// config.report(CompilerMessageSeverity.WARNING, "Attempting sum function call: ${function.name}")
46+
// if (statement.extensionReceiver == null) return@transformFlat listOf(statement.transform(this, null))
47+
// val receiver = statement.extensionReceiver!!
48+
// val receiverType = receiver.type
49+
// if (!receiverType.isArray() && !receiverType.isSubtypeOfClass(context.irBuiltIns.iterableClass)) return@transformFlat listOf(statement.transform(this, null))
50+
//
51+
// // Narrowed it to a summation call on a collection, time to forcefully inline it!
52+
// if (function.valueParameters.isEmpty()) {
53+
// // sum()
54+
// listOf(function.inline(declaration))
55+
// } else {
56+
// // sumBy { ... } or sumOf { ... }
57+
// val lambda = buildVariable(declaration, function.startOffset, function.endOffset, IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA, function.name, context.irBuiltIns.functionN(1).typeWith(receiverType))
58+
// lambda.initializer = statement.getValueArgument(0)
59+
// listOf(
60+
// lambda,
61+
// function.inline(declaration, listOf(lambda))
62+
// )
63+
// }
64+
// }
65+
66+
declaration.transformChildrenVoid(object : IrElementTransformerVoid() {
67+
override fun visitCall(expression: IrCall): IrExpression {
68+
val function = expression.symbol.owner
69+
if (function.getPackageFragment().packageFqName != FqName("kotlin.collections"))
70+
return super.visitCall(expression)
71+
if (function.name.asString() !in listOf("sum", "sumBy", "sumOf")) return super.visitCall(expression)
72+
if (expression.extensionReceiver == null) return super.visitCall(expression)
73+
val receiver = expression.extensionReceiver!!
74+
val receiverType = receiver.type
75+
if (!receiverType.isArray() && !receiverType.isSubtypeOfClass(context.irBuiltIns.iterableClass)) return super.visitCall(expression)
76+
77+
if (function.valueParameters.isEmpty()) {
78+
// sum()
79+
return function.inline(declaration)
80+
} else {
81+
// sumBy { ... } or sumOf { ... }
82+
val lambda = buildVariable(declaration, function.startOffset, function.endOffset, IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA, function.name, context.irBuiltIns.functionN(1).typeWith(receiverType))
83+
lambda.initializer = expression.getValueArgument(0)
84+
85+
return function.inline(declaration, listOf(lambda))
86+
}
87+
}
88+
})
89+
90+
return declaration
91+
}
92+
}

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
kotlin.code.style=official
2+
kotlin.compiler.execution.strategy=in-process

0 commit comments

Comments
 (0)