From 20b76d414980350b743dd6179482cd8e1f8627af Mon Sep 17 00:00:00 2001 From: Georgy Bronnikov Date: Mon, 7 Jun 2021 17:52:17 +0300 Subject: [PATCH] JVM_IR: reorganize initialization of JvmGeneratorExtensionsImpl CachedFields are now created at initialization. Therefore we need CompilerConfiguration as a constructor parameter. --- .../compiler/KotlinToJVMBytecodeCompiler.kt | 5 +--- .../backend/jvm/JvmGeneratorExtensionsImpl.kt | 25 ++++++------------- .../kotlin/backend/jvm/JvmIrCodegenFactory.kt | 4 ++- .../kotlin/backend/jvm/JvmBackendContext.kt | 2 +- .../backend/jvm/JvmGeneratorExtensions.kt | 2 +- .../classic/ClassicFrontend2IrConverter.kt | 7 +----- .../frontend/fir/Fir2IrResultsConverter.kt | 10 ++++---- .../kotlin/ir/AbstractIrGeneratorTestCase.kt | 2 +- .../kotlin/codegen/GenerationUtils.kt | 13 +++++++--- .../idea/internal/KotlinBytecodeToolWindow.kt | 2 +- .../plugin/impl/KJvmReplCompilerBase.kt | 3 ++- .../plugin/impl/ScriptJvmCompilerImpls.kt | 3 ++- 12 files changed, 36 insertions(+), 42 deletions(-) diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt index 84f3d2530c7..a0fba83314d 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt @@ -306,10 +306,7 @@ object KotlinToJVMBytecodeCompiler { ) .codegenFactory( if (configuration.getBoolean(JVMConfigurationKeys.IR)) JvmIrCodegenFactory( - configuration.get(CLIConfigurationKeys.PHASE_CONFIG) ?: PhaseConfig(jvmPhases), - jvmGeneratorExtensions = JvmGeneratorExtensionsImpl( - irDeserializationEnabled = configuration.getBoolean(JVMConfigurationKeys.SERIALIZE_IR) - ) + configuration, configuration.get(CLIConfigurationKeys.PHASE_CONFIG) ?: PhaseConfig(jvmPhases) ) else DefaultCodegenFactory ) .withModule(module) diff --git a/compiler/ir/backend.jvm/entrypoint/src/org/jetbrains/kotlin/backend/jvm/JvmGeneratorExtensionsImpl.kt b/compiler/ir/backend.jvm/entrypoint/src/org/jetbrains/kotlin/backend/jvm/JvmGeneratorExtensionsImpl.kt index 24b4c4abba7..c96053d3e47 100644 --- a/compiler/ir/backend.jvm/entrypoint/src/org/jetbrains/kotlin/backend/jvm/JvmGeneratorExtensionsImpl.kt +++ b/compiler/ir/backend.jvm/entrypoint/src/org/jetbrains/kotlin/backend/jvm/JvmGeneratorExtensionsImpl.kt @@ -11,7 +11,9 @@ import org.jetbrains.kotlin.backend.common.serialization.signature.IdSignatureSe import org.jetbrains.kotlin.backend.jvm.lower.SingletonObjectJvmStaticTransformer import org.jetbrains.kotlin.backend.jvm.serialization.deserializeClassFromByteArray import org.jetbrains.kotlin.backend.jvm.serialization.deserializeIrFileFromByteArray -import org.jetbrains.kotlin.config.LanguageVersionSettings +import org.jetbrains.kotlin.config.CompilerConfiguration +import org.jetbrains.kotlin.config.JVMConfigurationKeys +import org.jetbrains.kotlin.config.languageVersionSettings import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.annotations.FilteredAnnotations import org.jetbrains.kotlin.incremental.components.NoLookupLocation @@ -28,7 +30,6 @@ import org.jetbrains.kotlin.ir.expressions.IrDelegatingConstructorCall import org.jetbrains.kotlin.ir.expressions.impl.IrDelegatingConstructorCallImpl import org.jetbrains.kotlin.ir.symbols.impl.DescriptorlessExternalPackageFragmentSymbol import org.jetbrains.kotlin.ir.util.DeclarationStubGenerator -import org.jetbrains.kotlin.ir.util.ExternalDependenciesGenerator import org.jetbrains.kotlin.ir.util.SymbolTable import org.jetbrains.kotlin.ir.util.constructors import org.jetbrains.kotlin.load.java.JvmAnnotationNames @@ -60,11 +61,15 @@ import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.typeUtil.replaceAnnotations open class JvmGeneratorExtensionsImpl( + configuration: CompilerConfiguration, private val generateFacades: Boolean = true, - override val irDeserializationEnabled: Boolean = false, ) : GeneratorExtensions(), JvmGeneratorExtensions { override val classNameOverride: MutableMap = mutableMapOf() + override val irDeserializationEnabled: Boolean = configuration.getBoolean(JVMConfigurationKeys.SERIALIZE_IR) + + override val cachedFields = CachedFieldsForObjectInstances(IrFactoryImpl, configuration.languageVersionSettings) + override val samConversion: SamConversion get() = JvmSamConversion @@ -106,18 +111,6 @@ open class JvmGeneratorExtensionsImpl( } } - @Volatile - private var cachedFields: CachedFieldsForObjectInstances? = null - - override fun getCachedFields(irFactory: IrFactory, languageVersionSettings: LanguageVersionSettings): CachedFieldsForObjectInstances { - cachedFields?.let { return it } - synchronized(this) { - cachedFields?.let { return it } - cachedFields = CachedFieldsForObjectInstances(irFactory, languageVersionSettings) - return cachedFields!! - } - } - override fun deserializeLazyClass( irClass: IrLazyClass, stubGenerator: DeclarationStubGenerator, @@ -126,7 +119,6 @@ open class JvmGeneratorExtensionsImpl( ): Boolean { val serializedIr = (irClass.source as? KotlinJvmBinarySourceElement)?.binaryClass?.classHeader?.serializedIr ?: return false deserializeClassFromByteArray(serializedIr, stubGenerator, irClass, allowErrorNodes) - val cachedFields = getCachedFields(stubGenerator.irBuiltIns.irFactory, stubGenerator.irBuiltIns.languageVersionSettings) irClass.transform(SingletonObjectJvmStaticTransformer(stubGenerator.irBuiltIns, cachedFields), null) return true } @@ -139,7 +131,6 @@ open class JvmGeneratorExtensionsImpl( ): Boolean { val serializedIr = (irClass.source as? JvmPackagePartSource)?.knownJvmBinaryClass?.classHeader?.serializedIr ?: return false deserializeIrFileFromByteArray(serializedIr, stubGenerator, irClass, allowErrorNodes) - val cachedFields = getCachedFields(stubGenerator.irBuiltIns.irFactory, stubGenerator.irBuiltIns.languageVersionSettings) irClass.transform(SingletonObjectJvmStaticTransformer(stubGenerator.irBuiltIns, cachedFields), null) return true } diff --git a/compiler/ir/backend.jvm/entrypoint/src/org/jetbrains/kotlin/backend/jvm/JvmIrCodegenFactory.kt b/compiler/ir/backend.jvm/entrypoint/src/org/jetbrains/kotlin/backend/jvm/JvmIrCodegenFactory.kt index 1d29e9e8ef5..8854413c185 100644 --- a/compiler/ir/backend.jvm/entrypoint/src/org/jetbrains/kotlin/backend/jvm/JvmIrCodegenFactory.kt +++ b/compiler/ir/backend.jvm/entrypoint/src/org/jetbrains/kotlin/backend/jvm/JvmIrCodegenFactory.kt @@ -15,6 +15,7 @@ import org.jetbrains.kotlin.backend.jvm.ir.getKtFile import org.jetbrains.kotlin.backend.jvm.serialization.JvmIdSignatureDescriptor 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 import org.jetbrains.kotlin.descriptors.ModuleDescriptor import org.jetbrains.kotlin.descriptors.konan.DeserializedKlibModuleOrigin @@ -39,10 +40,11 @@ import org.jetbrains.kotlin.psi2ir.preprocessing.SourceDeclarationsPreprocessor import org.jetbrains.kotlin.resolve.CleanableBindingContext open class JvmIrCodegenFactory( + configuration: CompilerConfiguration, private val phaseConfig: PhaseConfig, private val externalMangler: JvmDescriptorMangler? = null, private val externalSymbolTable: SymbolTable? = null, - private val jvmGeneratorExtensions: JvmGeneratorExtensionsImpl = JvmGeneratorExtensionsImpl() + private val jvmGeneratorExtensions: JvmGeneratorExtensionsImpl = JvmGeneratorExtensionsImpl(configuration), ) : CodegenFactory { data class JvmIrBackendInput( val state: GenerationState, diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendContext.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendContext.kt index b72c23df165..58e4f41da9c 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendContext.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendContext.kt @@ -67,7 +67,7 @@ class JvmBackendContext( internal val innerClassesSupport = JvmInnerClassesSupport(irFactory) internal val cachedDeclarations = JvmCachedDeclarations( - this, generatorExtensions.getCachedFields(irFactory, state.languageVersionSettings) + this, generatorExtensions.cachedFields ) override val mapping: Mapping = DefaultMapping() diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmGeneratorExtensions.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmGeneratorExtensions.kt index d59897bc4d7..ffe2bf6b66b 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmGeneratorExtensions.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmGeneratorExtensions.kt @@ -16,5 +16,5 @@ interface JvmGeneratorExtensions { val rawTypeAnnotationConstructor: IrConstructor? - fun getCachedFields(irFactory: IrFactory, languageVersionSettings: LanguageVersionSettings): CachedFieldsForObjectInstances + val cachedFields: CachedFieldsForObjectInstances } diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/classic/ClassicFrontend2IrConverter.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/classic/ClassicFrontend2IrConverter.kt index 06ddb31115b..97a712e5c76 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/classic/ClassicFrontend2IrConverter.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/classic/ClassicFrontend2IrConverter.kt @@ -6,13 +6,11 @@ package org.jetbrains.kotlin.test.frontend.classic import org.jetbrains.kotlin.backend.common.phaser.PhaseConfig -import org.jetbrains.kotlin.backend.jvm.JvmGeneratorExtensionsImpl import org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory import org.jetbrains.kotlin.backend.jvm.jvmPhases import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys import org.jetbrains.kotlin.codegen.ClassBuilderFactories import org.jetbrains.kotlin.codegen.state.GenerationState -import org.jetbrains.kotlin.config.JVMConfigurationKeys import org.jetbrains.kotlin.test.backend.ir.IrBackendInput import org.jetbrains.kotlin.test.directives.CodegenTestDirectives import org.jetbrains.kotlin.test.model.BackendKinds @@ -39,10 +37,7 @@ class ClassicFrontend2IrConverter( val files = psiFiles.values.toList() val phaseConfig = configuration.get(CLIConfigurationKeys.PHASE_CONFIG) ?: PhaseConfig(jvmPhases) - val codegenFactory = JvmIrCodegenFactory( - phaseConfig, - jvmGeneratorExtensions = JvmGeneratorExtensionsImpl(irDeserializationEnabled = configuration.getBoolean(JVMConfigurationKeys.SERIALIZE_IR)) - ) + val codegenFactory = JvmIrCodegenFactory(configuration, phaseConfig) val state = GenerationState.Builder( project, ClassBuilderFactories.TEST, analysisResult.moduleDescriptor, analysisResult.bindingContext, files, configuration diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/fir/Fir2IrResultsConverter.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/fir/Fir2IrResultsConverter.kt index 1314d46c86d..708e8e750a6 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/fir/Fir2IrResultsConverter.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/fir/Fir2IrResultsConverter.kt @@ -41,15 +41,15 @@ class Fir2IrResultsConverter( module: TestModule, inputArtifact: FirOutputArtifact ): IrBackendInput { - val extensions = JvmGeneratorExtensionsImpl() + val compilerConfigurationProvider = testServices.compilerConfigurationProvider + val configuration = compilerConfigurationProvider.getCompilerConfiguration(module) + val extensions = JvmGeneratorExtensionsImpl(configuration) + val (irModuleFragment, symbolTable, components) = inputArtifact.firAnalyzerFacade.convertToIr(extensions) val dummyBindingContext = NoScopeRecordCliBindingTrace().bindingContext - val compilerConfigurationProvider = testServices.compilerConfigurationProvider - val configuration = compilerConfigurationProvider.getCompilerConfiguration(module) - val phaseConfig = configuration.get(CLIConfigurationKeys.PHASE_CONFIG) ?: PhaseConfig(jvmPhases) - val codegenFactory = JvmIrCodegenFactory(phaseConfig) + val codegenFactory = JvmIrCodegenFactory(configuration, phaseConfig, jvmGeneratorExtensions = extensions) // TODO: handle fir from light tree val ktFiles = inputArtifact.firFiles.values.mapNotNull { it.psi as KtFile? } diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/ir/AbstractIrGeneratorTestCase.kt b/compiler/tests-common/tests/org/jetbrains/kotlin/ir/AbstractIrGeneratorTestCase.kt index 7bcf111be10..dec3f503a20 100644 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/ir/AbstractIrGeneratorTestCase.kt +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/ir/AbstractIrGeneratorTestCase.kt @@ -141,7 +141,7 @@ abstract class AbstractIrGeneratorTestCase : CodegenTestCase() { ): IrModuleFragment { return generateIrModule( JvmResolveUtil.analyze(ktFilesToAnalyze, environment), psi2ir, ktFilesToAnalyze, - JvmGeneratorExtensionsImpl(generateFacades = false), + JvmGeneratorExtensionsImpl(environment.configuration, generateFacades = false), createIdSignatureComposer = { bindingContext -> JvmIdSignatureDescriptor(JvmDescriptorMangler(MainFunctionDetector(bindingContext, languageVersionSettings))) } diff --git a/compiler/tests-compiler-utils/tests/org/jetbrains/kotlin/codegen/GenerationUtils.kt b/compiler/tests-compiler-utils/tests/org/jetbrains/kotlin/codegen/GenerationUtils.kt index cdfcf64ed61..7a80565e6aa 100644 --- a/compiler/tests-compiler-utils/tests/org/jetbrains/kotlin/codegen/GenerationUtils.kt +++ b/compiler/tests-compiler-utils/tests/org/jetbrains/kotlin/codegen/GenerationUtils.kt @@ -113,11 +113,15 @@ object GenerationUtils { // TODO: add running checkers and check that it's safe to compile val firAnalyzerFacade = FirAnalyzerFacade(session, configuration.languageVersionSettings, files) - val extensions = JvmGeneratorExtensionsImpl() + val extensions = JvmGeneratorExtensionsImpl(configuration) val (moduleFragment, symbolTable, components) = firAnalyzerFacade.convertToIr(extensions) val dummyBindingContext = NoScopeRecordCliBindingTrace().bindingContext - val codegenFactory = JvmIrCodegenFactory(configuration.get(CLIConfigurationKeys.PHASE_CONFIG) ?: PhaseConfig(jvmPhases)) + val codegenFactory = JvmIrCodegenFactory( + configuration, + configuration.get(CLIConfigurationKeys.PHASE_CONFIG) ?: PhaseConfig(jvmPhases), + jvmGeneratorExtensions = extensions + ) // Create and initialize the test module and its dependencies val container = TopDownAnalyzerFacadeForJVM.createContainer( @@ -194,7 +198,10 @@ object GenerationUtils { files, configuration ).codegenFactory( if (isIrBackend) - JvmIrCodegenFactory(configuration.get(CLIConfigurationKeys.PHASE_CONFIG) ?: PhaseConfig(jvmPhases)) + JvmIrCodegenFactory( + configuration, + configuration.get(CLIConfigurationKeys.PHASE_CONFIG) ?: PhaseConfig(jvmPhases), + ) else DefaultCodegenFactory ).isIrBackend(isIrBackend).apply(configureGenerationState).build() if (analysisResult.shouldGenerateCode) { diff --git a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/internal/KotlinBytecodeToolWindow.kt b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/internal/KotlinBytecodeToolWindow.kt index ef89ad2391a..a7686d7e4dc 100644 --- a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/internal/KotlinBytecodeToolWindow.kt +++ b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/internal/KotlinBytecodeToolWindow.kt @@ -330,7 +330,7 @@ class KotlinBytecodeToolWindow(private val myProject: Project, private val toolW .generateDeclaredClassFilter(generateClassFilter) .codegenFactory( if (configuration.getBoolean(JVMConfigurationKeys.IR)) - JvmIrCodegenFactory(PhaseConfig(jvmPhases)) + JvmIrCodegenFactory(configuration, PhaseConfig(jvmPhases)) else DefaultCodegenFactory ) diff --git a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/KJvmReplCompilerBase.kt b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/KJvmReplCompilerBase.kt index f76351e6f6f..d330e75966b 100644 --- a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/KJvmReplCompilerBase.kt +++ b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/KJvmReplCompilerBase.kt @@ -198,10 +198,11 @@ open class KJvmReplCompilerBase protected cons snippetKtFile: KtFile, sourceFiles: List ): GenerationState { - val generatorExtensions = object : JvmGeneratorExtensionsImpl() { + val generatorExtensions = object : JvmGeneratorExtensionsImpl(compilationState.environment.configuration) { override fun getPreviousScripts() = history.map { compilationState.symbolTable.referenceScript(it.item) } } val codegenFactory = JvmIrCodegenFactory( + compilationState.environment.configuration, compilationState.environment.configuration.get(CLIConfigurationKeys.PHASE_CONFIG) ?: PhaseConfig(jvmPhases), compilationState.mangler, compilationState.symbolTable, generatorExtensions ) diff --git a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/ScriptJvmCompilerImpls.kt b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/ScriptJvmCompilerImpls.kt index 2404a322cdc..5a6c4a3bf34 100644 --- a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/ScriptJvmCompilerImpls.kt +++ b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/impl/ScriptJvmCompilerImpls.kt @@ -238,7 +238,8 @@ private fun generate( ).codegenFactory( if (kotlinCompilerConfiguration.getBoolean(JVMConfigurationKeys.IR)) JvmIrCodegenFactory( - kotlinCompilerConfiguration.get(CLIConfigurationKeys.PHASE_CONFIG) ?: PhaseConfig(jvmPhases) + kotlinCompilerConfiguration, + kotlinCompilerConfiguration.get(CLIConfigurationKeys.PHASE_CONFIG) ?: PhaseConfig(jvmPhases), ) else DefaultCodegenFactory ).build().also { KotlinCodegenFacade.compileCorrectFiles(it)