Skip to content

Commit

Permalink
[Wasm] Move all WasmIR optimizer and macro handling into emit stage
Browse files Browse the repository at this point in the history
  • Loading branch information
igoriakovlev authored and qodana-bot committed Sep 21, 2024
1 parent 1cf1852 commit 894b9da
Show file tree
Hide file tree
Showing 11 changed files with 320 additions and 241 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ class DeclarationGenerator(
val wasmType = wasmModuleTypeTransformer.transformType(declaration.type)

val initBody = mutableListOf<WasmInstr>()
val wasmExpressionGenerator = WasmIrExpressionBuilder(initBody)
val wasmExpressionGenerator = WasmExpressionBuilder(initBody)

val initValue: IrExpression? = declaration.initializer?.expression
if (initValue != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ class WasmCompiledModuleFragment(
createFieldInitializerFunction()

masterInitFunction.instructions.clear()
with(WasmIrExpressionBuilder(masterInitFunction.instructions)) {
with(WasmExpressionBuilder(masterInitFunction.instructions)) {
buildCall(WasmSymbol(fieldInitializerFunction), serviceCodeLocation)
wasmCompiledFileFragments.forEach { fragment ->
fragment.mainFunctionWrappers.forEach { signature ->
Expand All @@ -373,7 +373,7 @@ class WasmCompiledModuleFragment(
wasmCompiledFileFragments.forEach { allDefinedFunctions.putAll(it.functions.defined) }

tryGetAssociatedObject.instructions.clear()
with(WasmIrExpressionBuilder(tryGetAssociatedObject.instructions)) {
with(WasmExpressionBuilder(tryGetAssociatedObject.instructions)) {
wasmCompiledFileFragments.forEach { fragment ->
for ((klass, associatedObjectsInstanceGetters) in fragment.classAssociatedObjectsInstanceGetters) {
val klassId = typeIds[klass]!!
Expand Down Expand Up @@ -404,7 +404,7 @@ class WasmCompiledModuleFragment(

private fun createStartUnitTestsFunction() {
startUnitTestsFunction.instructions.clear()
with(WasmIrExpressionBuilder(startUnitTestsFunction.instructions)) {
with(WasmExpressionBuilder(startUnitTestsFunction.instructions)) {
wasmCompiledFileFragments.forEach { fragment ->
val signature = fragment.testFun
if (signature != null) {
Expand All @@ -417,7 +417,7 @@ class WasmCompiledModuleFragment(

private fun createFieldInitializerFunction() {
fieldInitializerFunction.instructions.clear()
with(WasmIrExpressionBuilder(fieldInitializerFunction.instructions)) {
with(WasmExpressionBuilder(fieldInitializerFunction.instructions)) {
var stringPoolInitializer: Pair<FieldInitializer, WasmSymbol<WasmGlobal>>? = null
wasmCompiledFileFragments.forEach { fragment ->
fragment.fieldInitializers.forEach { initializer ->
Expand Down Expand Up @@ -446,7 +446,7 @@ class WasmCompiledModuleFragment(
wasmCompiledFileFragments.forEach { fragment ->
fragment.typeInfo.forEach { (referenceKey, typeInfo) ->
val instructions = mutableListOf<WasmInstr>()
WasmIrExpressionBuilder(instructions).buildConstI32(
WasmExpressionBuilder(instructions).buildConstI32(
typeIds.getValue(referenceKey),
SourceLocation.NoLocation("Compile time data per class")
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class WasmFunctionCodegenContext(
private val wasmModuleTypeTransformer: WasmModuleTypeTransformer,
) {
val bodyGen: WasmExpressionBuilder =
WasmIrExpressionBuilder(wasmFunction.instructions)
WasmExpressionBuilder(wasmFunction.instructions)

private val wasmLocals = LinkedHashMap<IrValueSymbol, WasmLocal>()
private val wasmSyntheticLocals = LinkedHashMap<SyntheticLocalType, WasmLocal>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ sealed class WasmDataMode {
val offset: MutableList<WasmInstr>
) : WasmDataMode() {
constructor(memoryIdx: Int, offset: Int) : this(memoryIdx, mutableListOf<WasmInstr>().also<MutableList<WasmInstr>> {
WasmIrExpressionBuilder(it).buildConstI32(offset, SourceLocation.NoLocation("Offset value for WasmDataMode.Active "))
WasmExpressionBuilder(it).buildConstI32(offset, SourceLocation.NoLocation("Offset value for WasmDataMode.Active "))
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ import org.jetbrains.kotlin.wasm.ir.source.location.SourceLocation
* - at least, an API user has to think about what to pass a location
* - it's not taken from some context-like thing implicitly, so you will not get it implicitly from a wrong context/scope.
*/
abstract class WasmExpressionBuilder {
abstract fun buildInstr(op: WasmOp, location: SourceLocation, vararg immediates: WasmImmediate)
class WasmExpressionBuilder(val expression: MutableList<WasmInstr>) {
fun buildInstr(op: WasmOp, location: SourceLocation, vararg immediates: WasmImmediate) {
expression += WasmInstrWithLocation(op, immediates.toList(), location)
}

abstract var numberOfNestedBlocks: Int
var numberOfNestedBlocks: Int = 0
set(value) {
assert(value >= 0) { "end without matching block" }
field = value
}

fun buildConstI32(value: Int, location: SourceLocation) {
buildInstr(WasmOp.I32_CONST, location, WasmImmediate.ConstI32(value))
Expand Down Expand Up @@ -282,3 +288,9 @@ abstract class WasmExpressionBuilder {
buildInstr(WasmOp.PSEUDO_COMMENT_GROUP_END, SourceLocation.NoLocation("Pseudo-instruction"))
}
}

inline fun buildWasmExpression(body: WasmExpressionBuilder.() -> Unit): MutableList<WasmInstr> {
val res = mutableListOf<WasmInstr>()
WasmExpressionBuilder(res).body()
return res
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright 2010-2024 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.wasm.ir.convertors

import org.jetbrains.kotlin.wasm.ir.WasmInstr

internal fun processInstructionsFlow(input: Sequence<WasmInstr>): Sequence<WasmInstr> {
val macroTableHandled = handleMacroTable(input)
val macroIfHandled = handleMacroIf(macroTableHandled)
val removedUnreachableCode = removeUnreachableInstructions(macroIfHandled)
val mergedWithDrop = removeInstructionPriorDrop(removedUnreachableCode)
val mergedWithUnreachable = removeInstructionPriorUnreachable(mergedWithDrop)
val mergedWithTee = mergeSetAndGetIntoTee(mergedWithUnreachable)
return mergedWithTee
}
Loading

0 comments on commit 894b9da

Please sign in to comment.