[Wasm] Renovated Ir declarations dump by adding a type entry

This commit is contained in:
Sergei Kharitontcev-Beglov
2023-06-30 10:38:17 +02:00
committed by Space Team
parent 3b52b452a3
commit bc0afc36a2
@@ -29,10 +29,12 @@ internal fun IrDeclaration.fqNameForDceDump(): String {
return (fqn + signature + synthetic)
}
private data class IrDeclarationDumpInfo(val fqName: String, val type: String, val size: Int)
fun dumpDeclarationIrSizesIfNeed(path: String?, allModules: List<IrModuleFragment>) {
if (path == null) return
val declarations = linkedSetOf<IrDeclaration>()
val declarations = linkedSetOf<IrDeclarationDumpInfo>()
allModules.forEach {
it.acceptChildrenVoid(object : IrElementVisitorVoid {
@@ -41,15 +43,21 @@ fun dumpDeclarationIrSizesIfNeed(path: String?, allModules: List<IrModuleFragmen
}
override fun visitDeclaration(declaration: IrDeclarationBase) {
when (declaration) {
is IrFunction,
is IrProperty,
is IrField,
is IrAnonymousInitializer,
is IrClass,
-> {
declarations.add(declaration)
}
val type = when (declaration) {
is IrFunction -> "function"
is IrProperty -> "property"
is IrField -> "field"
is IrAnonymousInitializer -> "anonymous initializer"
else -> null
}
type?.let {
declarations.add(
IrDeclarationDumpInfo(
fqName = declaration.fqNameForDceDump().removeQuotes(),
type = it,
size = declaration.dumpKotlinLike().length
)
)
}
super.visitDeclaration(declaration)
@@ -65,21 +73,17 @@ fun dumpDeclarationIrSizesIfNeed(path: String?, allModules: List<IrModuleFragmen
}
val value = declarations
.groupBy({ it.fqNameForDceDump() }, { it })
.map { (k, v) -> k to v.maxBy { it.dumpKotlinLike().length } }
.joinToString(separator, prefix, postfix) { (fqn, element) ->
val size = element.dumpKotlinLike().length
val type = when (element) {
is IrFunction -> "function"
is IrProperty -> "property"
is IrField -> "field"
is IrAnonymousInitializer -> "anonymousInitializer"
is IrClass -> "class"
else -> "unknown"
// TODO: introduce a way to assign unique names to each declaration and use it for both reachability and size infos
.groupBy { it.fqName }
.flatMap { (_, v) ->
v.mapIndexed { index: Int, declaration: IrDeclarationDumpInfo ->
if (index == 0) declaration
else declaration.copy(fqName = "${declaration.fqName} ($index)")
}
"""$indent"${fqn.removeQuotes()}": {
|$indent$indent"size": $size,
|$indent$indent"type": "$type"
}.joinToString(separator, prefix, postfix) { declaration ->
"""$indent"${declaration.fqName}": {
|$indent$indent"size": ${declaration.size},
|$indent$indent"type": "${declaration.type}"
|$indent}
""".trimMargin()
}