IR: make copying optional in performByIrFile

For codegen phases, input files are readonly data. There is no need to
copy them.
This commit is contained in:
Georgy Bronnikov
2021-01-27 11:03:54 +03:00
parent 1383c31caf
commit c7feaee8ae
2 changed files with 18 additions and 9 deletions
@@ -30,15 +30,17 @@ import java.util.concurrent.atomic.AtomicReference
fun <Context : CommonBackendContext> performByIrFile(
name: String = "PerformByIrFile",
description: String = "Perform phases by IrFile",
lower: List<CompilerPhase<Context, IrFile, IrFile>>
copyBeforeLowering: Boolean = true,
lower: List<CompilerPhase<Context, IrFile, IrFile>>,
): NamedCompilerPhase<Context, IrModuleFragment> =
NamedCompilerPhase(
name, description, emptySet(), PerformByIrFilePhase(lower), emptySet(), emptySet(), emptySet(),
name, description, emptySet(), PerformByIrFilePhase(lower, copyBeforeLowering), emptySet(), emptySet(), emptySet(),
setOf(defaultDumper), nlevels = 1,
)
private class PerformByIrFilePhase<Context : CommonBackendContext>(
private val lower: List<CompilerPhase<Context, IrFile, IrFile>>
private val lower: List<CompilerPhase<Context, IrFile, IrFile>>,
private val copyBeforeLowering: Boolean,
) : SameTypeCompilerPhase<Context, IrModuleFragment> {
override fun invoke(
phaseConfig: PhaseConfig,
@@ -85,7 +87,10 @@ private class PerformByIrFilePhase<Context : CommonBackendContext>(
// Each thread needs its own copy of phaserState.alreadyDone
val filesAndStates = input.files.map {
it.copySavingMappings(remappedFiles, remappedFunctions, remappedClasses) to phaserState.copyOf()
if (copyBeforeLowering)
it.copySavingMappings(remappedFiles, remappedFunctions, remappedClasses) to phaserState.copyOf()
else
it to phaserState.copyOf()
}
val executor = Executors.newFixedThreadPool(nThreads)
@@ -111,12 +116,15 @@ private class PerformByIrFilePhase<Context : CommonBackendContext>(
// Presumably each thread has run through the same list of phases.
phaserState.alreadyDone.addAll(filesAndStates[0].second.alreadyDone)
input.files.clear()
input.files.addAll(filesAndStates.map { (irFile, _) -> irFile }.toMutableList())
// Repair after working on copied files.
if (copyBeforeLowering) {
input.files.clear()
input.files.addAll(filesAndStates.map { (irFile, _) -> irFile }.toMutableList())
adjustDefaultArgumentStubs(context, remappedFunctions)
context.handleDeepCopy(remappedFiles, remappedClasses, remappedFunctions)
input.transformChildrenVoid(CrossFileCallAdjuster(remappedFunctions))
adjustDefaultArgumentStubs(context, remappedFunctions)
context.handleDeepCopy(remappedFiles, remappedClasses, remappedFunctions)
input.transformChildrenVoid(CrossFileCallAdjuster(remappedFunctions))
}
// TODO: no guarantee that module identity is preserved by `lower`
return input
@@ -287,6 +287,7 @@ private fun codegenPhase(generateMultifileFacade: Boolean): NamedCompilerPhase<J
return performByIrFile(
name = "CodegenByIrFile$suffix",
description = "Code generation by IrFile$descriptionSuffix",
copyBeforeLowering = false,
lower = listOf(
makeIrFilePhase(
{ context ->