[K/N] Extracted asserts removal to a separate lowering

This commit is contained in:
Igor Chevdar
2023-07-20 13:53:22 +03:00
committed by Space Team
parent e8ca45378b
commit 8f3b9694f7
4 changed files with 40 additions and 14 deletions
@@ -63,6 +63,7 @@ class KonanConfig(val project: Project, val configuration: CompilerConfiguration
?: target.family.isAppleFamily // Default is true for Apple targets.
val generateDebugTrampoline = debug && configuration.get(KonanConfigKeys.GENERATE_DEBUG_TRAMPOLINE) ?: false
val optimizationsEnabled = configuration.getBoolean(KonanConfigKeys.OPTIMIZATION)
val assertsEnabled = configuration.getBoolean(KonanConfigKeys.ENABLE_ASSERTIONS)
val sanitizer = configuration.get(BinaryOptions.sanitizer)?.takeIf {
when {
it != SanitizerKind.THREAD -> "${it.name} sanitizer is not supported yet"
@@ -487,6 +487,12 @@ private val objectClassesPhase = createFileLoweringPhase(
description = "Object classes lowering"
)
private val assertsRemovalPhase = createFileLoweringPhase(
lowering = ::AssertRemovalLowering,
name = "AssertsRemoval",
description = "Asserts removal"
)
private val constEvaluationPhase = createFileLoweringPhase(
lowering = { context: Context ->
val configuration = IrInterpreterConfiguration(printOnlyExceptionMessage = true)
@@ -509,6 +515,7 @@ private fun PhaseEngine<NativeGenerationState>.getAllLowerings() = listOfNotNull
inlinePhase,
removeExpectDeclarationsPhase,
stripTypeAliasDeclarationsPhase,
assertsRemovalPhase.takeUnless { context.config.assertsEnabled },
constEvaluationPhase,
provisionalFunctionExpressionPhase,
postInlinePhase,
@@ -0,0 +1,32 @@
/*
* Copyright 2010-2023 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.konan.lower
import org.jetbrains.kotlin.backend.common.BodyLoweringPass
import org.jetbrains.kotlin.backend.konan.Context
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.expressions.IrBlock
import org.jetbrains.kotlin.ir.expressions.IrBody
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrReturnableBlock
import org.jetbrains.kotlin.ir.expressions.impl.IrCompositeImpl
import org.jetbrains.kotlin.ir.util.inlineFunction
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
internal class AssertRemovalLowering(val context: Context) : BodyLoweringPass {
private val asserts = context.ir.symbols.asserts.toSet()
override fun lower(irBody: IrBody, container: IrDeclaration) {
irBody.transformChildrenVoid(object : IrElementTransformerVoid() {
override fun visitBlock(expression: IrBlock): IrExpression {
if ((expression as? IrReturnableBlock)?.inlineFunction?.let { it.symbol in asserts } == true)
return IrCompositeImpl(expression.startOffset, expression.endOffset, expression.type)
return super.visitBlock(expression)
}
})
}
}
@@ -10,7 +10,6 @@ import org.jetbrains.kotlin.backend.common.ir.Symbols
import org.jetbrains.kotlin.backend.common.lower.at
import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
import org.jetbrains.kotlin.backend.konan.Context
import org.jetbrains.kotlin.backend.konan.KonanConfigKeys
import org.jetbrains.kotlin.ir.builders.IrBuilderWithScope
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.declarations.IrDeclarationBase
@@ -19,8 +18,6 @@ import org.jetbrains.kotlin.ir.declarations.IrSymbolOwner
import org.jetbrains.kotlin.ir.expressions.IrBody
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.impl.IrCompositeImpl
import org.jetbrains.kotlin.ir.types.isUnit
import org.jetbrains.kotlin.ir.util.file
import org.jetbrains.kotlin.ir.visitors.IrElementTransformer
@@ -30,12 +27,6 @@ import org.jetbrains.kotlin.ir.visitors.IrElementTransformer
* - First phase of typeOf intrinsic lowering.
*/
internal class PreInlineLowering(val context: Context) : BodyLoweringPass {
private val symbols get() = context.ir.symbols
private val asserts = symbols.asserts.toSet()
private val enableAssertions = context.config.configuration.getBoolean(KonanConfigKeys.ENABLE_ASSERTIONS)
override fun lower(irBody: IrBody, container: IrDeclaration) = lower(irBody, container, container.file)
fun lower(irBody: IrBody, container: IrDeclaration, irFile: IrFile) {
@@ -50,11 +41,6 @@ internal class PreInlineLowering(val context: Context) : BodyLoweringPass {
expression.transformChildren(this, data)
return when {
!enableAssertions && expression.symbol in asserts -> {
// Replace assert() call with an empty composite if assertions are not enabled.
require(expression.type.isUnit())
IrCompositeImpl(expression.startOffset, expression.endOffset, expression.type)
}
Symbols.isTypeOfIntrinsic(expression.symbol) -> {
with (KTypeGenerator(context, irFile, expression, needExactTypeParameters = true)) {
data.at(expression).irKType(expression.getTypeArgument(0)!!, leaveReifiedForLater = true)