JVM_IR: perform file lowerings in parallel

Selected by -Xir-run-lowerings-in-paralled compiler flag.
This commit is contained in:
Georgy Bronnikov
2020-10-20 18:28:03 +03:00
parent c06b345f3c
commit bea5d955d4
5 changed files with 49 additions and 1 deletions
@@ -114,6 +114,12 @@ class K2JVMCompilerArguments : CommonCompilerArguments() {
)
var doNotClearBindingContext: Boolean by FreezableVar(false)
@Argument(
value = "-Xir-run-lowerings-in-parallel",
description = "When using the IR backend, run lowerings for each file in parallel"
)
var runLoweringsInParallel: Boolean by FreezableVar(false)
@Argument(value = "-Xmodule-path", valueDescription = "<path>", description = "Paths where to find Java 9+ modules")
var javaModulePath: String? by NullableStringFreezableVar(null)
@@ -291,6 +291,8 @@ fun CompilerConfiguration.configureAdvancedJvmOptions(arguments: K2JVMCompilerAr
}
arguments.declarationsOutputPath?.let { put(JVMConfigurationKeys.DECLARATIONS_JSON_PATH, it) }
put(CommonConfigurationKeys.RUN_LOWERINGS_IN_PARALLEL, arguments.runLoweringsInParallel)
}
fun CompilerConfiguration.configureKlibPaths(arguments: K2JVMCompilerArguments) {
@@ -50,6 +50,10 @@ object CommonConfigurationKeys {
@JvmField
val USE_FIR_EXTENDED_CHECKERS = CompilerConfigurationKey.create<Boolean>("fir extended checkers")
@JvmField
val RUN_LOWERINGS_IN_PARALLEL =
CompilerConfigurationKey.create<Boolean>("When using the IR backend, run lowerings for each file in parallel")
}
var CompilerConfiguration.languageVersionSettings: LanguageVersionSettings
@@ -9,9 +9,11 @@ import org.jetbrains.kotlin.backend.common.CodegenUtil
import org.jetbrains.kotlin.backend.common.CommonBackendContext
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.lower
import org.jetbrains.kotlin.config.CommonConfigurationKeys
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
import kotlin.concurrent.thread
// Phase composition.
private class CompositePhase<Context : CommonBackendContext, Input, Output>(
@@ -113,12 +115,23 @@ private class PerformByIrFilePhase<Context : CommonBackendContext>(
private val lower: List<CompilerPhase<Context, IrFile, IrFile>>
) : SameTypeCompilerPhase<Context, IrModuleFragment> {
override fun invoke(
phaseConfig: PhaseConfig,
phaserState: PhaserState<IrModuleFragment>,
context: Context,
input: IrModuleFragment
): IrModuleFragment = if (context.configuration.getBoolean(CommonConfigurationKeys.RUN_LOWERINGS_IN_PARALLEL))
invokeParallel(phaseConfig, phaserState, context, input)
else
invokeSequential(phaseConfig, phaserState, context, input)
private fun invokeSequential(
phaseConfig: PhaseConfig, phaserState: PhaserState<IrModuleFragment>, context: Context, input: IrModuleFragment
): IrModuleFragment {
for (irFile in input.files) {
try {
val filePhaserState = phaserState.changeType<IrModuleFragment, IrFile>()
for (phase in lower) {
phase.invoke(phaseConfig, phaserState.changeType(), context, irFile)
phase.invoke(phaseConfig, filePhaserState, context, irFile)
}
} catch (e: Throwable) {
CodegenUtil.reportBackendException(e, "IR lowering", irFile.fileEntry.name)
@@ -129,6 +142,28 @@ private class PerformByIrFilePhase<Context : CommonBackendContext>(
return input
}
private fun invokeParallel(
phaseConfig: PhaseConfig, phaserState: PhaserState<IrModuleFragment>, context: Context, input: IrModuleFragment
): IrModuleFragment {
val threads = input.files.map { irFile ->
thread {
try {
val filePhaserState = state.changeType<IrModuleFragment, IrFile>()
for (phase in lower) {
phase.invoke(phaseConfig, filePhaserState, context, irFile)
}
} catch (e: Throwable) {
CodegenUtil.reportBackendException(e, "IR lowering", irFile.fileEntry.name)
}
}
}
threads.forEach { it.join() }
// TODO: no guarantee that module identity is preserved by `lower`
return input
}
override fun getNamedSubphases(startDepth: Int): List<Pair<Int, NamedCompilerPhase<Context, *>>> =
lower.flatMap { it.getNamedSubphases(startDepth) }
}
+1
View File
@@ -99,6 +99,7 @@ where advanced options include:
-Xsam-conversions={class|indy} Select code generation scheme for SAM conversions.
-Xsam-conversions=indy Generate SAM conversions using `invokedynamic` with `LambdaMetafactory.metafactory`. Requires `-jvm-target 1.8` or greater.
-Xsam-conversions=class Generate SAM conversions as explicit classes
-Xir-run-lowerings-in-parallel When using the IR backend, run lowerings for each file in parallel
-Xsanitize-parentheses Transform '(' and ')' in method names to some other character sequence.
This mode can BREAK BINARY COMPATIBILITY and is only supposed to be used to workaround
problems with parentheses in identifiers on certain platforms