JVM IR: Handle NonInlinedConst
This commit is contained in:
committed by
max-kammerer
parent
e40636f6ab
commit
0b76f60b63
+7
-5
@@ -427,11 +427,13 @@ class ExpressionCodegen(
|
|||||||
override fun visitFieldAccess(expression: IrFieldAccessExpression, data: BlockInfo): PromisedValue {
|
override fun visitFieldAccess(expression: IrFieldAccessExpression, data: BlockInfo): PromisedValue {
|
||||||
val callee = expression.symbol.owner
|
val callee = expression.symbol.owner
|
||||||
callee.constantValue()?.let {
|
callee.constantValue()?.let {
|
||||||
// Handling const reads before codegen is important for constant folding.
|
if (context.state.shouldInlineConstVals) {
|
||||||
assert(expression is IrSetField) { "read of const val ${callee.name} not inlined by ConstLowering" }
|
// Handling const reads before codegen is important for constant folding.
|
||||||
// This can only be the field's initializer; JVM implementations are required
|
assert(expression is IrSetField) { "read of const val ${callee.name} not inlined by ConstLowering" }
|
||||||
// to generate those for ConstantValue-marked fields automatically, so this is redundant.
|
// This can only be the field's initializer; JVM implementations are required
|
||||||
return defaultValue(expression.type)
|
// to generate those for ConstantValue-marked fields automatically, so this is redundant.
|
||||||
|
return defaultValue(expression.type)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val realField = callee.resolveFakeOverride()!!
|
val realField = callee.resolveFakeOverride()!!
|
||||||
|
|||||||
+15
-8
@@ -5,9 +5,9 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.backend.jvm.lower
|
package org.jetbrains.kotlin.backend.jvm.lower
|
||||||
|
|
||||||
import org.jetbrains.kotlin.backend.common.CommonBackendContext
|
|
||||||
import org.jetbrains.kotlin.backend.common.FileLoweringPass
|
import org.jetbrains.kotlin.backend.common.FileLoweringPass
|
||||||
import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase
|
import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase
|
||||||
|
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
|
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrField
|
import org.jetbrains.kotlin.ir.declarations.IrField
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrFile
|
import org.jetbrains.kotlin.ir.declarations.IrFile
|
||||||
@@ -16,9 +16,8 @@ import org.jetbrains.kotlin.ir.expressions.IrCall
|
|||||||
import org.jetbrains.kotlin.ir.expressions.IrConst
|
import org.jetbrains.kotlin.ir.expressions.IrConst
|
||||||
import org.jetbrains.kotlin.ir.expressions.IrExpression
|
import org.jetbrains.kotlin.ir.expressions.IrExpression
|
||||||
import org.jetbrains.kotlin.ir.expressions.IrGetField
|
import org.jetbrains.kotlin.ir.expressions.IrGetField
|
||||||
import org.jetbrains.kotlin.ir.util.deepCopyWithSymbols
|
import org.jetbrains.kotlin.ir.expressions.impl.IrGetFieldImpl
|
||||||
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
|
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
|
||||||
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
|
|
||||||
|
|
||||||
internal val constPhase = makeIrFilePhase(
|
internal val constPhase = makeIrFilePhase(
|
||||||
::ConstLowering,
|
::ConstLowering,
|
||||||
@@ -34,9 +33,17 @@ fun IrField.constantValue(implicitConst: Boolean = false): IrConst<*>? {
|
|||||||
return if (inline) (initializer?.expression as? IrConst<*>)?.copy() else null
|
return if (inline) (initializer?.expression as? IrConst<*>)?.copy() else null
|
||||||
}
|
}
|
||||||
|
|
||||||
class ConstLowering(val context: CommonBackendContext) : IrElementTransformerVoid(), FileLoweringPass {
|
class ConstLowering(val context: JvmBackendContext) : IrElementTransformerVoid(), FileLoweringPass {
|
||||||
override fun lower(irFile: IrFile) {
|
override fun lower(irFile: IrFile) = irFile.transformChildrenVoid()
|
||||||
irFile.transformChildrenVoid(this)
|
|
||||||
|
private fun IrExpression.lowerConstRead(field: IrField?): IrExpression? {
|
||||||
|
val value = field?.constantValue()
|
||||||
|
?: return null
|
||||||
|
|
||||||
|
return if (context.state.shouldInlineConstVals)
|
||||||
|
value
|
||||||
|
else
|
||||||
|
IrGetFieldImpl(startOffset, endOffset, field.symbol, field.type)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun visitCall(expression: IrCall): IrExpression {
|
override fun visitCall(expression: IrCall): IrExpression {
|
||||||
@@ -44,9 +51,9 @@ class ConstLowering(val context: CommonBackendContext) : IrElementTransformerVoi
|
|||||||
val property = function.correspondingPropertySymbol?.owner ?: return super.visitCall(expression)
|
val property = function.correspondingPropertySymbol?.owner ?: return super.visitCall(expression)
|
||||||
if (function != property.getter)
|
if (function != property.getter)
|
||||||
return super.visitCall(expression)
|
return super.visitCall(expression)
|
||||||
return property.backingField?.constantValue() ?: super.visitCall(expression)
|
return expression.lowerConstRead(property.backingField) ?: super.visitCall(expression)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun visitGetField(expression: IrGetField): IrExpression =
|
override fun visitGetField(expression: IrGetField): IrExpression =
|
||||||
expression.symbol.owner.constantValue() ?: super.visitGetField(expression)
|
expression.lowerConstRead(expression.symbol.owner) ?: super.visitGetField(expression)
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
// !LANGUAGE: -InlineConstVals
|
// !LANGUAGE: -InlineConstVals
|
||||||
// IGNORE_BACKEND: JVM_IR, JS_IR, NATIVE
|
// IGNORE_BACKEND: JS_IR, NATIVE
|
||||||
// TODO: muted automatically, investigate should it be ran for JS or not
|
// TODO: muted automatically, investigate should it be ran for JS or not
|
||||||
// IGNORE_BACKEND: JS
|
// IGNORE_BACKEND: JS
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
// !LANGUAGE: -InlineConstVals
|
// !LANGUAGE: -InlineConstVals
|
||||||
// IGNORE_BACKEND: JVM_IR
|
|
||||||
|
|
||||||
const val z = 0
|
const val z = 0
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
// !LANGUAGE: -InlineConstVals
|
// !LANGUAGE: -InlineConstVals
|
||||||
// IGNORE_BACKEND: JVM_IR
|
|
||||||
|
|
||||||
const val z = 0
|
const val z = 0
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
// !LANGUAGE: -InlineConstVals
|
// !LANGUAGE: -InlineConstVals
|
||||||
// IGNORE_BACKEND: JVM_IR
|
|
||||||
|
|
||||||
const val one = 1
|
const val one = 1
|
||||||
const val two = 2
|
const val two = 2
|
||||||
|
|||||||
-1
@@ -1,5 +1,4 @@
|
|||||||
// !LANGUAGE: -InlineConstVals
|
// !LANGUAGE: -InlineConstVals
|
||||||
// IGNORE_BACKEND: JVM_IR
|
|
||||||
|
|
||||||
const val y = "cde"
|
const val y = "cde"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user