Skip to content

Commit

Permalink
JVM Codegen: Step into a function from default arguments
Browse files Browse the repository at this point in the history
Exception default lambdas, since they are inlined at call site, there
are no `ASTORE` instruction for lambda instances in caller.

Fixes: KT-64731
  • Loading branch information
scaventz committed Mar 7, 2024
1 parent 27a4cb6 commit 5cb27f2
Show file tree
Hide file tree
Showing 28 changed files with 145 additions and 231 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ private fun extractDefaultLambdasInfo(
return conditions.map {
val varAssignmentInstruction = it.varInsNode!!
var instanceInstuction = varAssignmentInstruction.previous

if (instanceInstuction is LineNumberNode) {
instanceInstuction = instanceInstuction.previous.previous
}
if (instanceInstuction is TypeInsnNode && instanceInstuction.opcode == Opcodes.CHECKCAST) {
instanceInstuction = instanceInstuction.previous
}
Expand Down Expand Up @@ -156,7 +160,7 @@ private fun extractDefaultLambdasInfo(
}

is FieldInsnNode -> {
toDelete.addAll(InsnSequence(instanceInstuction, varAssignmentInstruction.next).toList())
toDelete.addAll(InsnSequence(instanceInstuction.previous, varAssignmentInstruction.next).toList())

val needReification =
instanceInstuction.previous.takeIf { isNeedClassReificationMarker(it) }?.let { toDelete.add(it) } != null
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.jetbrains.kotlin.backend.common.descriptors.synthesizedString
import org.jetbrains.kotlin.backend.common.ir.ValueRemapper
import org.jetbrains.kotlin.backend.common.lower.isMovedReceiver
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.builders.*
import org.jetbrains.kotlin.ir.builders.declarations.addValueParameter
Expand Down Expand Up @@ -58,7 +59,7 @@ open class DefaultArgumentStubGenerator<TContext : CommonBackendContext>(
log { "$originalDeclaration -> $newIrFunction" }

return context.irFactory.createBlockBody(UNDEFINED_OFFSET, UNDEFINED_OFFSET) {
statements += builder.irBlockBody(newIrFunction) {
statements += builder.irBlockBody(UNDEFINED_OFFSET) {
val params = mutableListOf<IrValueDeclaration>()
val variables = mutableMapOf<IrValueSymbol, IrValueSymbol>()

Expand Down Expand Up @@ -191,7 +192,10 @@ open class DefaultArgumentStubGenerator<TContext : CommonBackendContext>(
//
// This control flow limits us to an if-then (without an else), and this together with the
// restriction on loading the parameter in the default case means we cannot create any temporaries.
+irIfThen(irNotEquals(defaultFlag, irInt(0)), irSet(parameter.symbol, default))
val iSet = irBlockBody(parameter.startOffset, parameter.endOffset) {
+irSet(parameter.symbol, default)
}.statements.first() as IrExpression
+irIfThen(irNotEquals(defaultFlag, irInt(0)), iSet)
return parameter
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,12 +483,6 @@ class ExpressionCodegen(

lineNumberMapper.buildSmapFor(inlinedBlock, inlinedBlock.buildOrGetClassSMAP(info), info)

if (inlineCall.usesDefaultArguments()) {
// $default function has first LN pointing to original callee
callee?.markLineNumber(startOffset = true)
mv.nop()
}

// 2. Evaluate DEFAULT arguments from inline function call
inlinedBlock.getDefaultAdditionalStatementsFromInlinedBlock().forEach { exp ->
exp.accept(this, info).discard()
Expand Down Expand Up @@ -922,13 +916,7 @@ class ExpressionCodegen(
override fun visitSetValue(expression: IrSetValue, data: BlockInfo): PromisedValue {
expression.value.markLineNumber(startOffset = true)
expression.value.accept(this, data).materializeAt(expression.symbol.owner.type)
// We set the value of parameters only for default values. The inliner accepts only
// a very specific bytecode pattern for default arguments and does not tolerate a
// line number on the store. Therefore, if we are storing to a parameter, we do not
// output a line number for the store.
if (expression.symbol !is IrValueParameterSymbol) {
expression.markLineNumber(startOffset = true)
}
expression.markLineNumber(startOffset = true)
mv.store(findLocalIndex(expression.symbol), expression.symbol.owner.asmType)
return unitValue
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// SKIP_INLINE_CHECK_IN: inlineFun$default
// WITH_STDLIB
// JVM_ABI_K1_K2_DIFF: KT-62464

// FILE: 1.kt
// TARGET_BACKEND: JVM
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// JVM_ABI_K1_K2_DIFF: KT-62464

// FILE: 1.kt
package test
Expand Down
45 changes: 0 additions & 45 deletions compiler/testData/codegen/boxInline/jvmName/simple.jvm_abi.txt

This file was deleted.

1 change: 0 additions & 1 deletion compiler/testData/codegen/boxInline/jvmName/simple.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// TARGET_BACKEND: JVM
// WITH_STDLIB
// JVM_ABI_K1_K2_DIFF: KT-62464

// FILE: 1.kt
package test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ test/_1Kt
test/_1Kt$lParams$1
*L
1#1,47:1
34#2,5:48
35#3:53
37#2,2:48
35#3:50
*S KotlinDebug
*F
+ 1 2.kt
_2Kt
*L
45#1:48,5
45#1:53
45#1:48,2
45#1:50
*E

Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ test/_1Kt
test/_1Kt$lParams$1
*L
1#1,47:1
34#2,5:48
35#3:53
37#2,2:48
35#3:50
*S KotlinDebug
*F
+ 1 2.kt
_2Kt
*L
45#1:48,5
45#1:53
45#1:48,2
45#1:50
*E

SMAP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ test/_1Kt
test/_1Kt$lParams$1
*L
1#1,47:1
34#2,5:48
32#2:54
35#3:53
37#2,2:48
32#2:51
35#3:50
*S KotlinDebug
*F
+ 1 2.kt
_2Kt
*L
45#1:48,5
45#1:54
45#1:53
45#1:48,2
45#1:51
45#1:50
*E

Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@ test/_1Kt
test/_1Kt$lParams$1
*L
1#1,47:1
34#2,5:48
32#2:54
35#3:53
37#2,2:48
32#2:51
35#3:50
*S KotlinDebug
*F
+ 1 2.kt
_2Kt
*L
45#1:48,5
45#1:54
45#1:53
45#1:48,2
45#1:51
45#1:50
*E

SMAP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ test/_1Kt
test/_1Kt$lParams$1
*L
1#1,49:1
34#2,5:50
32#2:56
35#3:55
37#2,2:50
32#2:53
35#3:52
*S KotlinDebug
*F
+ 1 2.kt
_2Kt
*L
46#1:50,5
46#1:56
46#1:55
46#1:50,2
46#1:53
46#1:52
*E

0 comments on commit 5cb27f2

Please sign in to comment.