JVM_IR Spill stack on array constructor call

KT-42932
This commit is contained in:
Dmitry Petrov
2021-03-11 18:03:37 +03:00
parent dbfe45993a
commit 44e6483090
19 changed files with 326 additions and 10 deletions
@@ -846,6 +846,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/arrays/kt4118.kt");
}
@Test
@TestMetadata("kt42932.kt")
public void testKt42932() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt42932.kt");
}
@Test
@TestMetadata("kt4348.kt")
public void testKt4348() throws Exception {
@@ -858,6 +864,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/arrays/kt4357.kt");
}
@Test
@TestMetadata("kt45410.kt")
public void testKt45410() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt45410.kt");
}
@Test
@TestMetadata("kt503.kt")
public void testKt503() throws Exception {
@@ -4998,6 +4998,12 @@ public class FirBytecodeTextTestGenerated extends AbstractFirBytecodeTextTest {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/bytecodeText/storeStackBeforeInline"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Test
@TestMetadata("arrayConstructor.kt")
public void testArrayConstructor() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/storeStackBeforeInline/arrayConstructor.kt");
}
@Test
@TestMetadata("differentTypes.kt")
public void testDifferentTypes() throws Exception {
@@ -33,7 +33,7 @@ class ArrayConstructorLowering(val context: CommonBackendContext) : IrElementTra
}
}
private class ArrayConstructorTransformer(
open class ArrayConstructorTransformer(
val context: CommonBackendContext,
val container: IrSymbolOwner
) : IrElementTransformerVoidWithContext() {
@@ -73,11 +73,12 @@ private class ArrayConstructorTransformer(
val invokable = expression.getValueArgument(1)!!.transform(this, null)
val scope = (currentScope ?: createScope(container)).scope
return context.createIrBuilder(scope.scopeOwnerSymbol).irBlock(expression.startOffset, expression.endOffset) {
beforeArrayConstructorBody(this)
val index = createTmpVariable(irInt(0), isMutable = true)
val sizeVar = createTmpVariable(size)
val result = createTmpVariable(irCall(sizeConstructor, expression.type).apply {
copyTypeArgumentsFrom(expression)
copyTypeArgumentsFrom(expression)
putValueArgument(0, irGet(sizeVar))
})
@@ -91,12 +92,9 @@ private class ArrayConstructorTransformer(
}
body = irBlock {
val tempIndex = createTmpVariable(irGet(index))
val value = lambda?.inline(parent, listOf(tempIndex))?.patchDeclarationParents(scope.getLocalDeclarationParent()) ?: irCallOp(
invoke.symbol,
invoke.returnType,
irGet(invokableVar!!),
irGet(tempIndex)
)
val value =
lambda?.run { inline(parent, listOf(tempIndex)).patchDeclarationParents(scope.getLocalDeclarationParent()) }
?: irCallOp(invoke.symbol, invoke.returnType, irGet(invokableVar!!), irGet(tempIndex))
+irCall(result.type.getClass()!!.functions.single { it.name == OperatorNameConventions.SET }).apply {
dispatchReceiver = irGet(result)
putValueArgument(0, irGet(tempIndex))
@@ -106,7 +104,13 @@ private class ArrayConstructorTransformer(
+irSet(index.symbol, irCallOp(inc.symbol, index.type, irGet(index)))
}
}
afterArrayConstructorBody(this)
+irGet(result)
}
}
protected open fun beforeArrayConstructorBody(builder: IrBlockBuilder) {}
protected open fun afterArrayConstructorBody(builder: IrBlockBuilder) {}
}
@@ -65,7 +65,7 @@ private val provisionalFunctionExpressionPhase = makeIrFilePhase<CommonBackendCo
)
private val arrayConstructorPhase = makeIrFilePhase(
::ArrayConstructorLowering,
::JvmArrayConstructorLowering,
name = "ArrayConstructor",
description = "Transform `Array(size) { index -> value }` into a loop"
)
@@ -865,6 +865,16 @@ class JvmSymbols(
val runSuspendFunction: IrSimpleFunctionSymbol =
kotlinCoroutinesJvmInternalRunSuspendKt.functionByName("runSuspend")
private val inlineMarkerClass: IrClassSymbol = createClass(
JvmClassName.byInternalName("kotlin/jvm/internal/InlineMarker").fqNameForClassNameWithoutDollars
) { irClass ->
irClass.addFunction("beforeInlineCall", irBuiltIns.unitType, isStatic = true)
irClass.addFunction("afterInlineCall", irBuiltIns.unitType, isStatic = true)
}
val beforeInlineCall = inlineMarkerClass.functionByName("beforeInlineCall")
val afterInlineCall = inlineMarkerClass.functionByName("afterInlineCall")
companion object {
val FLEXIBLE_NULLABILITY_ANNOTATION_FQ_NAME =
IrBuiltIns.KOTLIN_INTERNAL_IR_FQN.child(Name.identifier("FlexibleNullability"))
@@ -0,0 +1,42 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.backend.jvm.lower
import org.jetbrains.kotlin.backend.common.BodyLoweringPass
import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext
import org.jetbrains.kotlin.backend.common.lower.ArrayConstructorTransformer
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
import org.jetbrains.kotlin.ir.builders.IrBlockBuilder
import org.jetbrains.kotlin.ir.builders.irCall
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.declarations.IrSymbolOwner
import org.jetbrains.kotlin.ir.expressions.IrBody
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
class JvmArrayConstructorLowering(val context: JvmBackendContext) : IrElementTransformerVoidWithContext(), BodyLoweringPass {
override fun lower(irBody: IrBody, container: IrDeclaration) {
irBody.transformChildrenVoid(JvmArrayConstructorTransformer(context, container as IrSymbolOwner))
}
}
class JvmArrayConstructorTransformer(
private val jvmContext: JvmBackendContext,
container: IrSymbolOwner
) : ArrayConstructorTransformer(jvmContext, container) {
override fun beforeArrayConstructorBody(builder: IrBlockBuilder) {
with(builder) {
+irCall(jvmContext.ir.symbols.beforeInlineCall)
}
}
override fun afterArrayConstructorBody(builder: IrBlockBuilder) {
with(builder) {
+irCall(jvmContext.ir.symbols.afterInlineCall)
}
}
}
+16
View File
@@ -0,0 +1,16 @@
// WITH_RUNTIME
fun box(): String {
val i = test()
return if (i != 0)
"Failed: $i"
else
"OK"
}
private fun test(): Int {
// JS tests fail for array size '1000000000',
// however, we don't really care much about performance here,
// but rather check that we generate correct code.
return "123".indexOfAny(CharArray(100) { '1' })
}
+115
View File
@@ -0,0 +1,115 @@
// WITH_RUNTIME
private const val MOD = 998244353
private fun mul(a: Int, b: Int) = (a.toLong() * b % MOD).toInt()
fun box(): String {
val n = 400
val d = Array(n) { IntArray(n) { Int.MAX_VALUE / 2 } }
for (i in 0 until n) {
d[i][i] = 0
}
val m = n - 1
val g = Graph(n, 2 * m)
repeat(m) {
val a = it
val b = it + 1
d[a][b] = 1
d[b][a] = 1
g.add(a, b)
g.add(b, a)
}
for (k in 0 until n) {
for (i in 0 until n) {
for (j in 0 until n) {
val s = d[i][k] + d[k][j]
if (s < d[i][j]) d[i][j] = s
}
}
}
for (x in 0 until n) {
val row = IntArray(n) { y ->
var prod = 1
val dx = d[x]
val xy = dx[y]
for (k in 0 until n) if (k != x) {
val dy = d[y]
val xk = dx[k]
val yk = dy[k]
var cnt = 0
var cntMid = 0
g.from(k) { t ->
val xt = dx[t]
val yt = dy[t]
if (xt == xk - 1) when (yt) {
yk - 1 -> {
cnt++
}
yk + 1 -> {
if (xk + yk == xy) {
cntMid++
cnt++
}
}
}
}
if (cntMid > 1 || cnt == 0) {
prod = 0
break
} else {
prod = mul(prod, cnt)
}
}
prod
}
for (i in 0 until n) {
if (row[i] != 1) throw AssertionError("x: $x; row[$i]: ${row[i]}")
}
}
return "OK"
}
class Graph(vCap: Int = 16, eCap: Int = vCap * 2) {
var vCnt = 0
var eCnt = 0
var vHead = IntArray(vCap) { -1 }
var eVert = IntArray(eCap)
var eNext = IntArray(eCap)
fun add(v: Int, u: Int, e: Int = eCnt++) {
ensureVCap(maxOf(v, u) + 1)
ensureECap(e + 1)
eVert[e] = u
eNext[e] = vHead[v]
vHead[v] = e
}
inline fun from(v: Int, action: (u: Int) -> Unit) {
var e = vHead[v]
while (e >= 0) {
action(eVert[e])
e = eNext[e]
}
}
private fun ensureVCap(vCap: Int) {
if (vCap <= vCnt) return
vCnt = vCap
if (vCap > vHead.size) {
val newSize = maxOf(2 * vHead.size, vCap)
vHead = vHead.copyOf(newSize)
}
}
private fun ensureECap(eCap: Int) {
if (eCap <= eCnt) return
eCnt = eCap
if (eCap > eVert.size) {
val newSize = maxOf(2 * eVert.size, eCap)
eVert = eVert.copyOf(newSize)
eNext = eNext.copyOf(newSize)
}
}
}
@@ -0,0 +1,15 @@
// WITH_RUNTIME
fun test(): Int = "123".indexOfAny(CharArray(1000) { '1' })
// JVM_TEMPLATES:
// 5 ALOAD
// 5 ASTORE
// 7 ILOAD
// 6 ISTORE
// JVM_IR_TEMPLATES:
// 3 ALOAD
// 3 ASTORE
// 4 ILOAD
// 3 ISTORE
@@ -846,6 +846,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/arrays/kt4118.kt");
}
@Test
@TestMetadata("kt42932.kt")
public void testKt42932() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt42932.kt");
}
@Test
@TestMetadata("kt4348.kt")
public void testKt4348() throws Exception {
@@ -858,6 +864,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/arrays/kt4357.kt");
}
@Test
@TestMetadata("kt45410.kt")
public void testKt45410() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt45410.kt");
}
@Test
@TestMetadata("kt503.kt")
public void testKt503() throws Exception {
@@ -4866,6 +4866,12 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/bytecodeText/storeStackBeforeInline"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true);
}
@Test
@TestMetadata("arrayConstructor.kt")
public void testArrayConstructor() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/storeStackBeforeInline/arrayConstructor.kt");
}
@Test
@TestMetadata("differentTypes.kt")
public void testDifferentTypes() throws Exception {
@@ -846,6 +846,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/arrays/kt4118.kt");
}
@Test
@TestMetadata("kt42932.kt")
public void testKt42932() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt42932.kt");
}
@Test
@TestMetadata("kt4348.kt")
public void testKt4348() throws Exception {
@@ -858,6 +864,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/arrays/kt4357.kt");
}
@Test
@TestMetadata("kt45410.kt")
public void testKt45410() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt45410.kt");
}
@Test
@TestMetadata("kt503.kt")
public void testKt503() throws Exception {
@@ -4998,6 +4998,12 @@ public class IrBytecodeTextTestGenerated extends AbstractIrBytecodeTextTest {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/bytecodeText/storeStackBeforeInline"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Test
@TestMetadata("arrayConstructor.kt")
public void testArrayConstructor() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/storeStackBeforeInline/arrayConstructor.kt");
}
@Test
@TestMetadata("differentTypes.kt")
public void testDifferentTypes() throws Exception {
@@ -742,6 +742,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/arrays/kt4118.kt");
}
@TestMetadata("kt42932.kt")
public void testKt42932() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt42932.kt");
}
@TestMetadata("kt4348.kt")
public void testKt4348() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt4348.kt");
@@ -752,6 +757,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/arrays/kt4357.kt");
}
@TestMetadata("kt45410.kt")
public void testKt45410() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt45410.kt");
}
@TestMetadata("kt503.kt")
public void testKt503() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt503.kt");
@@ -422,6 +422,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
runTest("compiler/testData/codegen/box/arrays/kt4118.kt");
}
@TestMetadata("kt42932.kt")
public void testKt42932() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt42932.kt");
}
@TestMetadata("kt4348.kt")
public void testKt4348() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt4348.kt");
@@ -432,6 +437,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
runTest("compiler/testData/codegen/box/arrays/kt4357.kt");
}
@TestMetadata("kt45410.kt")
public void testKt45410() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt45410.kt");
}
@TestMetadata("kt503.kt")
public void testKt503() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt503.kt");
@@ -422,6 +422,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/arrays/kt4118.kt");
}
@TestMetadata("kt42932.kt")
public void testKt42932() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt42932.kt");
}
@TestMetadata("kt4348.kt")
public void testKt4348() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt4348.kt");
@@ -432,6 +437,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/arrays/kt4357.kt");
}
@TestMetadata("kt45410.kt")
public void testKt45410() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt45410.kt");
}
@TestMetadata("kt503.kt")
public void testKt503() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt503.kt");
@@ -422,6 +422,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/arrays/kt4118.kt");
}
@TestMetadata("kt42932.kt")
public void testKt42932() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt42932.kt");
}
@TestMetadata("kt4348.kt")
public void testKt4348() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt4348.kt");
@@ -432,6 +437,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/arrays/kt4357.kt");
}
@TestMetadata("kt45410.kt")
public void testKt45410() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt45410.kt");
}
@TestMetadata("kt503.kt")
public void testKt503() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt503.kt");
@@ -250,6 +250,11 @@ public class JsLegacyPrimitiveArraysBoxTestGenerated extends AbstractJsLegacyPri
runTest("compiler/testData/codegen/box/arrays/kt4118.kt");
}
@TestMetadata("kt42932.kt")
public void testKt42932() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt42932.kt");
}
@TestMetadata("kt4348.kt")
public void testKt4348() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt4348.kt");
@@ -260,6 +265,11 @@ public class JsLegacyPrimitiveArraysBoxTestGenerated extends AbstractJsLegacyPri
runTest("compiler/testData/codegen/box/arrays/kt4357.kt");
}
@TestMetadata("kt45410.kt")
public void testKt45410() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt45410.kt");
}
@TestMetadata("kt503.kt")
public void testKt503() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt503.kt");
@@ -372,6 +372,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
runTest("compiler/testData/codegen/box/arrays/kt4118.kt");
}
@TestMetadata("kt42932.kt")
public void testKt42932() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt42932.kt");
}
@TestMetadata("kt4348.kt")
public void testKt4348() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt4348.kt");
@@ -382,6 +387,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
runTest("compiler/testData/codegen/box/arrays/kt4357.kt");
}
@TestMetadata("kt45410.kt")
public void testKt45410() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt45410.kt");
}
@TestMetadata("kt7009.kt")
public void testKt7009() throws Exception {
runTest("compiler/testData/codegen/box/arrays/kt7009.kt");