Skip to content

Commit

Permalink
CLI: extract BackendInputForMultiModuleChunk.runBackend
Browse files Browse the repository at this point in the history
  • Loading branch information
mglukhikh authored and qodana-bot committed Sep 12, 2024
1 parent d02e9a3 commit aeb58a8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.jetbrains.kotlin.cli.common.checkKotlinPackageUsageForPsi
import org.jetbrains.kotlin.cli.common.config.addKotlinSourceRoot
import org.jetbrains.kotlin.cli.common.fir.FirDiagnosticsCompilerResultsReporter
import org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.cli.common.messages.toLogger
import org.jetbrains.kotlin.cli.jvm.compiler.pipeline.compileSingleModuleUsingFrontendIrAndPsi
import org.jetbrains.kotlin.cli.jvm.compiler.pipeline.runFrontendAndGenerateIrForMultiModuleChunkUsingFrontendIRAndPsi
Expand All @@ -38,6 +39,7 @@ import org.jetbrains.kotlin.fir.backend.jvm.FirJvmBackendClassResolver
import org.jetbrains.kotlin.fir.backend.jvm.FirJvmBackendExtension
import org.jetbrains.kotlin.fir.pipeline.Fir2IrActualizedResult
import org.jetbrains.kotlin.ir.backend.jvm.jvmResolveLibraries
import org.jetbrains.kotlin.ir.declarations.impl.IrModuleFragmentImpl
import org.jetbrains.kotlin.load.kotlin.ModuleVisibilityManager
import org.jetbrains.kotlin.modules.Module
import org.jetbrains.kotlin.name.FqName
Expand Down Expand Up @@ -71,7 +73,7 @@ object KotlinToJVMBytecodeCompiler {

val useFrontendIR = compilerConfiguration.getBoolean(CommonConfigurationKeys.USE_FIR)
val messageCollector = environment.messageCollector
val (codegenFactory, wholeBackendInput, moduleDescriptor, bindingContext, firJvmBackendResolver, firJvmBackendExtension, mainClassFqName) = if (useFrontendIR) {
val backendInputForMultiModuleChunk = if (useFrontendIR) {
// K2/PSI: base checks
val projectEnvironment =
VfsBasedProjectEnvironment(
Expand Down Expand Up @@ -99,29 +101,60 @@ object KotlinToJVMBytecodeCompiler {
} else {
runFrontendAndGenerateIrUsingClassicFrontend(environment, compilerConfiguration, chunk)
} ?: return true
// K1/K2 common multi-chunk part
val localFileSystem = VirtualFileManager.getInstance().getFileSystem(StandardFileSystems.FILE_PROTOCOL)

val diagnosticsReporter = DiagnosticReporterFactory.createReporter(messageCollector)
return backendInputForMultiModuleChunk.runBackend(
project,
chunk,
compilerConfiguration,
messageCollector,
diagnosticsReporter,
buildFile,
allSourceFiles = environment.getSourceFiles(),
)
}

internal fun BackendInputForMultiModuleChunk.runBackend(
project: Project,
chunk: List<Module>,
compilerConfiguration: CompilerConfiguration,
messageCollector: MessageCollector,
diagnosticsReporter: BaseDiagnosticsCollector,
buildFile: File?,
allSourceFiles: List<KtFile>?,
): Boolean {
val localFileSystem = VirtualFileManager.getInstance().getFileSystem(StandardFileSystems.FILE_PROTOCOL)
val codegenInputs = ArrayList<CodegenFactory.CodegenInput>(chunk.size)

for (module in chunk) {
ProgressIndicatorAndCompilationCanceledStatus.checkCanceled()

val ktFiles = module.getSourceFiles(environment.getSourceFiles(), localFileSystem, chunk.size > 1, buildFile)
if (!checkKotlinPackageUsageForPsi(compilerConfiguration, ktFiles)) return false
val moduleConfiguration = compilerConfiguration.applyModuleProperties(module, buildFile)
val ktFiles = if (allSourceFiles != null) {
val sourceFiles = module.getSourceFiles(allSourceFiles, localFileSystem, chunk.size > 1, buildFile)
if (!checkKotlinPackageUsageForPsi(compilerConfiguration, sourceFiles)) return false
sourceFiles
} else null

val backendInput = codegenFactory.getModuleChunkBackendInput(wholeBackendInput, ktFiles).let {
val moduleConfiguration = compilerConfiguration.applyModuleProperties(module, buildFile)
val backendInput = (if (ktFiles != null) {
codegenFactory.getModuleChunkBackendInput(backendInput, ktFiles)
} else {
// K2/LT only, backend is always JVM-based
val wholeModule = (backendInput as JvmIrCodegenFactory.JvmIrBackendInput).irModuleFragment
val moduleCopy = IrModuleFragmentImpl(wholeModule.descriptor, wholeModule.irBuiltins)
wholeModule.files.filterTo(moduleCopy.files) { file ->
file.fileEntry.name in module.getSourceFiles()
}
backendInput.copy(moduleCopy)
}).let {
if (it is JvmIrCodegenFactory.JvmIrBackendInput && firJvmBackendExtension != null) {
it.copy(backendExtension = firJvmBackendExtension)
} else it
}
// Lowerings (per module)
codegenInputs += runLowerings(
environment.project, moduleConfiguration, moduleDescriptor, bindingContext,
ktFiles, module, codegenFactory, backendInput, diagnosticsReporter, firJvmBackendResolver
project, moduleConfiguration, moduleDescriptor, bindingContext,
sourceFiles = null, module, codegenFactory, backendInput, diagnosticsReporter,
firJvmBackendClassResolver
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package org.jetbrains.kotlin.cli.jvm.compiler.pipeline
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.KtSourceFile
import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
import org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory
import org.jetbrains.kotlin.backend.jvm.JvmIrDeserializerImpl
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
import org.jetbrains.kotlin.cli.common.CommonCompilerPerformanceManager
Expand All @@ -25,15 +24,13 @@ import org.jetbrains.kotlin.cli.common.prepareJvmSessions
import org.jetbrains.kotlin.cli.jvm.compiler.FirKotlinToJvmBytecodeCompiler.createPendingReporter
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.BackendInputForMultiModuleChunk
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.codegenFactoryWithJvmIrBackendInput
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.runCodegen
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.runLowerings
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.runBackend
import org.jetbrains.kotlin.cli.jvm.compiler.NoScopeRecordCliBindingTrace
import org.jetbrains.kotlin.cli.jvm.compiler.VfsBasedProjectEnvironment
import org.jetbrains.kotlin.cli.jvm.compiler.applyModuleProperties
import org.jetbrains.kotlin.cli.jvm.compiler.createLibraryListForJvm
import org.jetbrains.kotlin.cli.jvm.compiler.findMainClass
import org.jetbrains.kotlin.cli.jvm.compiler.writeOutputsIfNeeded
import org.jetbrains.kotlin.codegen.CodegenFactory
import org.jetbrains.kotlin.codegen.state.GenerationState
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.JVMConfigurationKeys
Expand All @@ -50,7 +47,6 @@ import org.jetbrains.kotlin.fir.pipeline.buildResolveAndCheckFirViaLightTree
import org.jetbrains.kotlin.fir.pipeline.runPlatformCheckers
import org.jetbrains.kotlin.fir.resolve.providers.FirSymbolProvider
import org.jetbrains.kotlin.fir.session.environment.AbstractProjectFileSearchScope
import org.jetbrains.kotlin.ir.declarations.impl.IrModuleFragmentImpl
import org.jetbrains.kotlin.load.kotlin.ModuleVisibilityManager
import org.jetbrains.kotlin.load.kotlin.incremental.components.IncrementalCompilationComponents
import org.jetbrains.kotlin.modules.Module
Expand Down Expand Up @@ -191,7 +187,7 @@ private fun compileMultiModuleChunkUsingFrontendIrAndLightTree(
firResult.convertToIrAndActualizeForJvm(fir2IrExtensions, compilerConfiguration, diagnosticsReporter, irGenerationExtensions)
val (factory, input) = fir2IrAndIrActualizerResult.codegenFactoryWithJvmIrBackendInput(compilerConfiguration)

val (codegenFactory, wholeBackendInput, moduleDescriptor, bindingContext, firJvmBackendResolver, firJvmBackendExtension, mainClassFqName) = BackendInputForMultiModuleChunk(
val backendInputForMultiModuleChunk = BackendInputForMultiModuleChunk(
factory,
input,
fir2IrAndIrActualizerResult.irModuleFragment.descriptor,
Expand All @@ -203,46 +199,18 @@ private fun compileMultiModuleChunkUsingFrontendIrAndLightTree(
)
)

/**
* This part is similar to [org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules],
* see "K1/K2 common multi-chunk part"
*/
val codegenInputs = ArrayList<CodegenFactory.CodegenInput>(chunk.size)
val firFiles = firResult.outputs.flatMap { it.fir }
if (!checkKotlinPackageUsageForLightTree(compilerConfiguration, firFiles)) return false
for (module in chunk) {
ProgressIndicatorAndCompilationCanceledStatus.checkCanceled()
val moduleConfiguration = compilerConfiguration.applyModuleProperties(module, buildFile)
val wholeModule = (wholeBackendInput as JvmIrCodegenFactory.JvmIrBackendInput).irModuleFragment
val moduleCopy = IrModuleFragmentImpl(wholeModule.descriptor, wholeModule.irBuiltins)
wholeModule.files.filterTo(moduleCopy.files) { file ->
file.fileEntry.name in module.getSourceFiles()
}
val backendInput = wholeBackendInput.copy(moduleCopy).let {
if (firJvmBackendExtension != null) {
it.copy(backendExtension = firJvmBackendExtension)
} else it
}
// Lowerings (per module)
codegenInputs += runLowerings(
project, moduleConfiguration, moduleDescriptor, bindingContext,
sourceFiles = null, module, codegenFactory, backendInput, diagnosticsReporter,
firJvmBackendResolver
)
}

val outputs = ArrayList<GenerationState>(chunk.size)

for (input in codegenInputs) {
// Codegen (per module)
outputs += runCodegen(input, input.state, codegenFactory, diagnosticsReporter, compilerConfiguration)
}

diagnosticsReporter.reportToMessageCollector(
messageCollector, compilerConfiguration.getBoolean(CLIConfigurationKeys.RENDER_DIAGNOSTIC_INTERNAL_NAME)
return backendInputForMultiModuleChunk.runBackend(
project,
chunk,
compilerConfiguration,
messageCollector,
diagnosticsReporter,
buildFile,
allSourceFiles = null,
)

return writeOutputsIfNeeded(project, compilerConfiguration, messageCollector, outputs, mainClassFqName)
}

private fun compileSingleModuleUsingFrontendIrAndLightTree(
Expand Down

0 comments on commit aeb58a8

Please sign in to comment.