WASM: Lower expression bodies into block bodies

This commit is contained in:
Igor Laevsky
2021-09-02 15:48:43 +03:00
committed by TeamCityServer
parent b79719d6f5
commit 37874b1937
3 changed files with 49 additions and 22 deletions
@@ -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.common.lower
import org.jetbrains.kotlin.backend.common.CommonBackendContext
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.builders.irReturn
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.expressions.IrExpressionBody
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
import org.jetbrains.kotlin.ir.visitors.acceptVoid
// Replaces IrExpressionBody with IrBlockBody returning this expression.
// Taken from kotlin/native ReturnsInsertionLowering.kt
class ExpressionBodyTransformer(val context: CommonBackendContext) : FileLoweringPass {
override fun lower(irFile: IrFile) {
irFile.acceptVoid(object : IrElementVisitorVoid {
override fun visitElement(element: IrElement) {
element.acceptChildrenVoid(this)
}
override fun visitFunction(declaration: IrFunction) {
declaration.acceptChildrenVoid(this)
context.createIrBuilder(declaration.symbol, declaration.endOffset, declaration.endOffset).run {
val body = declaration.body
if (body is IrExpressionBody)
declaration.body = context.irFactory.createBlockBody(body.startOffset, body.endOffset) {
statements += irReturn(body.expression)
}
}
}
})
}
}
@@ -419,6 +419,12 @@ private val propertyAccessorInlinerLoweringPhase = makeWasmModulePhase(
description = "[Optimization] Inline property accessors"
)
private val expressionBodyTransformer = makeWasmModulePhase(
::ExpressionBodyTransformer,
name = "ExpressionBodyTransformer",
description = "Replace IrExpressionBody with IrBlockBody"
)
val wasmPhases = NamedCompilerPhase(
name = "IrModuleLowering",
description = "IR module lowering",
@@ -490,6 +496,7 @@ val wasmPhases = NamedCompilerPhase(
objectDeclarationLoweringPhase then
fieldInitializersLoweringPhase then
genericReturnTypeLowering then
expressionBodyTransformer then
// Replace builtins before autoboxing
builtInsLoweringPhase0 then
@@ -22,28 +22,6 @@ import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
import org.jetbrains.kotlin.ir.visitors.acceptVoid
internal class ExpressionBodyTransformer(val context: Context) : FileLoweringPass {
override fun lower(irFile: IrFile) {
irFile.acceptVoid(object : IrElementVisitorVoid {
override fun visitElement(element: IrElement) {
element.acceptChildrenVoid(this)
}
override fun visitFunction(declaration: IrFunction) {
declaration.acceptChildrenVoid(this)
context.createIrBuilder(declaration.symbol, declaration.endOffset, declaration.endOffset).run {
val body = declaration.body
if (body is IrExpressionBody)
declaration.body = IrBlockBodyImpl(body.startOffset, body.endOffset) {
statements += irReturn(body.expression)
}
}
}
})
}
}
internal class ReturnsInsertionLowering(val context: Context) : FileLoweringPass {
private val symbols = context.ir.symbols