[IR] Do not interact with file system during Ir Serialization
- make `IrModuleSerializer` produce ByteArrays with serialized data - isolate file system access on Klib writer
This commit is contained in:
+14
-38
@@ -25,9 +25,7 @@ import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
|
||||
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
|
||||
import org.jetbrains.kotlin.ir.visitors.acceptVoid
|
||||
import org.jetbrains.kotlin.library.SerializedIr
|
||||
import org.jetbrains.kotlin.library.impl.CombinedIrFileWriter
|
||||
import org.jetbrains.kotlin.library.impl.DeclarationId
|
||||
import org.jetbrains.kotlin.library.impl.SimpleIrTableFileWriter
|
||||
import org.jetbrains.kotlin.library.impl.*
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.types.Variance
|
||||
import org.jetbrains.kotlin.backend.common.serialization.proto.Annotations as ProtoAnnotations
|
||||
@@ -1247,7 +1245,7 @@ open class IrModuleSerializer(
|
||||
open fun backendSpecificExplicitRoot(declaration: IrFunction) = false
|
||||
open fun backendSpecificExplicitRoot(declaration: IrClass) = false
|
||||
|
||||
fun serializeIrFile(file: IrFile): ProtoFile {
|
||||
fun serializeIrFile(file: IrFile, topLevelDeclarations: MutableList<SerializedDeclaration>): ProtoFile {
|
||||
val proto = ProtoFile.newBuilder()
|
||||
.setFileEntry(serializeFileEntry(file.fileEntry))
|
||||
.setFqName(serializeString(file.fqName.toString()))
|
||||
@@ -1255,13 +1253,13 @@ open class IrModuleSerializer(
|
||||
|
||||
file.declarations.forEach {
|
||||
if (it.descriptor.isExpectMember && !it.descriptor.isSerializableExpectClass) {
|
||||
writer.skipDeclaration()
|
||||
topLevelDeclarations.add(SkippedDeclaration)
|
||||
return@forEach
|
||||
}
|
||||
|
||||
val byteArray = serializeDeclaration(it).toByteArray()
|
||||
val uniqId = declarationTable.uniqIdByDeclaration(it)
|
||||
writer.addDeclaration(DeclarationId(uniqId.index, uniqId.isLocal), byteArray)
|
||||
topLevelDeclarations.add(TopLevelDeclaration(uniqId.index, uniqId.isLocal, byteArray))
|
||||
proto.addDeclarationId(protoUniqId(uniqId))
|
||||
}
|
||||
|
||||
@@ -1298,46 +1296,24 @@ open class IrModuleSerializer(
|
||||
return proto.build()
|
||||
}
|
||||
|
||||
lateinit var writer: CombinedIrFileWriter
|
||||
lateinit var symbolTableWriter: SimpleIrTableFileWriter
|
||||
lateinit var typeTableWriter: SimpleIrTableFileWriter
|
||||
lateinit var stringTableWriter: SimpleIrTableFileWriter
|
||||
|
||||
fun serializeModule(module: IrModuleFragment): ProtoModule {
|
||||
fun serializedIrModule(module: IrModuleFragment): SerializedIr {
|
||||
val proto = ProtoModule.newBuilder()
|
||||
.setName(serializeName(module.name))
|
||||
|
||||
val files = module.files.filter { it.packageFragmentDescriptor !is FunctionInterfacePackageFragment }
|
||||
val topLevelDeclarationsCount = files.sumBy { it.declarations.size }
|
||||
val topLevelDeclarations = mutableListOf<SerializedDeclaration>()
|
||||
|
||||
writer = CombinedIrFileWriter(topLevelDeclarationsCount)
|
||||
|
||||
files.forEach {
|
||||
proto.addFile(serializeIrFile(it))
|
||||
module.files.filter { it.packageFragmentDescriptor !is FunctionInterfacePackageFragment }.forEach {
|
||||
proto.addFile(serializeIrFile(it, topLevelDeclarations))
|
||||
}
|
||||
|
||||
symbolTableWriter = SimpleIrTableFileWriter("symbols", protoSymbolArray.size)
|
||||
for (protoSymbol in protoSymbolArray)
|
||||
symbolTableWriter.addItem(protoSymbol.toByteArray())
|
||||
assert(module.files.sumBy { it.declarations.size } == topLevelDeclarations.size)
|
||||
|
||||
typeTableWriter = SimpleIrTableFileWriter("types", protoTypeArray.size)
|
||||
for (protoSymbol in protoTypeArray)
|
||||
typeTableWriter.addItem(protoSymbol.toByteArray())
|
||||
val symbols = protoSymbolArray.map { it.toByteArray() }
|
||||
val types = protoTypeArray.map { it.toByteArray() }
|
||||
val strings = protoStringArray.map { it.toByteArray() }
|
||||
|
||||
stringTableWriter = SimpleIrTableFileWriter("strings", protoStringArray.size)
|
||||
for (protoSymbol in protoStringArray)
|
||||
stringTableWriter.addItem(protoSymbol.toByteArray())
|
||||
val moduleBytes = proto.build().toByteArray()
|
||||
|
||||
return proto.build()
|
||||
}
|
||||
|
||||
fun serializedIrModule(module: IrModuleFragment): SerializedIr {
|
||||
val moduleHeader = serializeModule(module).toByteArray()
|
||||
return SerializedIr(
|
||||
moduleHeader, symbolTableWriter.finishWriting().absolutePath,
|
||||
typeTableWriter.finishWriting().absolutePath,
|
||||
stringTableWriter.finishWriting().absolutePath,
|
||||
writer.finishWriting().absolutePath
|
||||
)
|
||||
return SerializedIr(moduleBytes, symbols, types, strings, topLevelDeclarations)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user