Skip to content

Commit 2ca1f0b

Browse files
mcpiromanSpace Team
authored andcommitted
[Klib] Deduplicate entries in IrLibrary about two IR dirs
KT-80999
1 parent e40b387 commit 2ca1f0b

File tree

15 files changed

+214
-255
lines changed

15 files changed

+214
-255
lines changed

compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/ic/KotlinLibraryHeader.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,17 @@ internal class KotlinLoadedLibraryHeader(
4646
}
4747

4848
override val sourceFileDeserializers: Map<KotlinSourceFile, IdSignatureDeserializer> by lazy(LazyThreadSafetyMode.NONE) {
49+
val ir = library.mainIr
4950
buildMapUntil(sourceFiles.size) {
5051
val deserializer = IdSignatureDeserializer(IrLibraryFileFromBytes(object : IrLibraryBytesSource() {
5152
private fun err(): Nothing = icError("Not supported")
5253
override fun irDeclaration(index: Int): ByteArray = err()
5354
override fun type(index: Int): ByteArray = err()
54-
override fun signature(index: Int): ByteArray = library.signature(index, it)
55-
override fun string(index: Int): ByteArray = library.string(index, it)
55+
override fun signature(index: Int): ByteArray = ir.signature(index, it)
56+
override fun string(index: Int): ByteArray = ir.string(index, it)
5657
override fun body(index: Int): ByteArray = err()
5758
override fun debugInfo(index: Int): ByteArray? = null
58-
override fun fileEntry(index: Int): ByteArray? = library.fileEntry(index, it)
59+
override fun fileEntry(index: Int): ByteArray? = ir.fileEntry(index, it)
5960
}), null, irInterner)
6061

6162
put(sourceFiles[it], deserializer)
@@ -73,9 +74,10 @@ internal class KotlinLoadedLibraryHeader(
7374

7475
private val sourceFiles by lazy(LazyThreadSafetyMode.NONE) {
7576
val extReg = ExtensionRegistryLite.newInstance()
76-
val sources = (0 until library.fileCount()).map {
77-
val fileProto = IrFile.parseFrom(library.file(it).codedInputStream, extReg)
78-
val fileReader = IrLibraryFileFromBytes(IrKlibBytesSource(library, it))
77+
val mainIr = library.mainIr
78+
val sources = (0 until mainIr.fileCount()).map {
79+
val fileProto = IrFile.parseFrom(mainIr.file(it).codedInputStream, extReg)
80+
val fileReader = IrLibraryFileFromBytes(IrKlibBytesSource(mainIr, it))
7981
val fileEntry = fileReader.fileEntry(fileProto)
8082
fileReader.deserializeFileEntryName(fileEntry)
8183
}

compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/BasicIrModuleDeserializer.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,14 @@ abstract class BasicIrModuleDeserializer(
5555
}
5656

5757
override fun init(delegate: IrModuleDeserializer) {
58-
val fileCount = klib.fileCount()
59-
58+
val mainIr = klib.mainIr
59+
val fileCount = mainIr.fileCount()
6060
fileDeserializationStates = buildList {
6161
for (i in 0 until fileCount) {
62-
val fileStream = klib.file(i).codedInputStream
62+
val fileStream = mainIr.file(i).codedInputStream
6363
val fileProto = ProtoFile.parseFrom(fileStream, ExtensionRegistryLite.newInstance())
64-
val fileReader = IrLibraryFileFromBytes(IrKlibBytesSource(klib, i))
65-
val file = fileReader.
66-
createFile(moduleFragment, fileProto, linker.irInterner)
64+
val fileReader = IrLibraryFileFromBytes(IrKlibBytesSource(mainIr, i))
65+
val file = fileReader.createFile(moduleFragment, fileProto, linker.irInterner)
6766

6867
this += deserializeIrFile(fileProto, file, fileReader, i, delegate, allowErrorNodes)
6968

compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/FileFingerprints.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package org.jetbrains.kotlin.backend.common.serialization
77

8+
import org.jetbrains.kotlin.library.IrLibrary
89
import org.jetbrains.kotlin.library.KotlinLibrary
910
import org.jetbrains.kotlin.library.SerializedIrFile
1011
import java.io.File
@@ -57,20 +58,20 @@ value class SerializedIrFileFingerprint private constructor(val fileFingerprint:
5758
return FingerprintHash(cityHash128WithSeed(withFileEntriesHash, file.declarations))
5859
}
5960

60-
private fun calculateFileFingerprint(lib: KotlinLibrary, fileIndex: Int): FingerprintHash {
61-
val fileDataHash = cityHash128(lib.file(fileIndex))
62-
val withTypesHash = cityHash128WithSeed(fileDataHash, lib.types(fileIndex))
63-
val withSignaturesHash = cityHash128WithSeed(withTypesHash, lib.signatures(fileIndex))
64-
val withStringsHash = cityHash128WithSeed(withSignaturesHash, lib.strings(fileIndex))
65-
val withBodiesHash = cityHash128WithSeed(withStringsHash, lib.bodies(fileIndex))
66-
val withFileEntriesHash = lib.fileEntries(fileIndex)?.let { cityHash128WithSeed(withBodiesHash, it) } ?: withBodiesHash
67-
return FingerprintHash(cityHash128WithSeed(withFileEntriesHash, lib.declarations(fileIndex)))
61+
private fun calculateFileFingerprint(ir: IrLibrary.IrDirectory, fileIndex: Int): FingerprintHash {
62+
val fileDataHash = cityHash128(ir.file(fileIndex))
63+
val withTypesHash = cityHash128WithSeed(fileDataHash, ir.types(fileIndex))
64+
val withSignaturesHash = cityHash128WithSeed(withTypesHash, ir.signatures(fileIndex))
65+
val withStringsHash = cityHash128WithSeed(withSignaturesHash, ir.strings(fileIndex))
66+
val withBodiesHash = cityHash128WithSeed(withStringsHash, ir.bodies(fileIndex))
67+
val withFileEntriesHash = ir.fileEntries(fileIndex)?.let { cityHash128WithSeed(withBodiesHash, it) } ?: withBodiesHash
68+
return FingerprintHash(cityHash128WithSeed(withFileEntriesHash, ir.declarations(fileIndex)))
6869
}
6970
}
7071

7172
constructor(file: SerializedIrFile) : this(calculateFileFingerprint(file))
7273

73-
constructor(lib: KotlinLibrary, fileIndex: Int) : this(calculateFileFingerprint(lib, fileIndex))
74+
constructor(lib: KotlinLibrary, fileIndex: Int) : this(calculateFileFingerprint(lib.mainIr, fileIndex))
7475

7576
override fun toString(): String {
7677
return fileFingerprint.toString()

compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/IncrementalCompilationSupport.kt

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ import org.jetbrains.kotlin.utils.addToStdlib.shouldNotBeCalled
2727
class ICData(val icData: List<SerializedIrFile>)
2828

2929
class ICKotlinLibrary(private val icData: List<SerializedIrFile>) : IrLibrary {
30-
override val hasMainIr get() = true
31-
override val hasFileEntriesTable get() = true
32-
3330
private inline fun Array<DeclarationIdTableReader?>.itemBytes(fileIndex: Int, key: DeclarationId, factory: () -> DeclarationIdTableReader): ByteArray {
3431
val reader = this[fileIndex] ?: factory().also { this[fileIndex] = it }
3532

@@ -56,67 +53,81 @@ class ICKotlinLibrary(private val icData: List<SerializedIrFile>) : IrLibrary {
5653
private val indexedBodies = arrayOfNulls<IrArrayReader>(icData.size)
5754
private val indexedFileEntries = arrayOfNulls<IrArrayReader>(icData.size)
5855

59-
override fun irDeclaration(index: Int, fileIndex: Int): ByteArray =
60-
indexedDeclarations.itemBytes(fileIndex, DeclarationId(index)) {
61-
DeclarationIdTableReader(icData[fileIndex].declarations)
62-
}
56+
override val hasMainIr get() = true
57+
override val mainIr: IrLibrary.IrDirectory = object : IrLibrary.IrDirectory {
58+
override val hasFileEntriesTable get() = true
59+
override fun irDeclaration(index: Int, fileIndex: Int): ByteArray =
60+
indexedDeclarations.itemBytes(fileIndex, DeclarationId(index)) {
61+
DeclarationIdTableReader(icData[fileIndex].declarations)
62+
}
6363

64-
override fun type(index: Int, fileIndex: Int): ByteArray =
65-
indexedTypes.itemBytes(fileIndex, index) {
66-
IrArrayReader(icData[fileIndex].types)
67-
}
64+
override fun type(index: Int, fileIndex: Int): ByteArray =
65+
indexedTypes.itemBytes(fileIndex, index) {
66+
IrArrayReader(icData[fileIndex].types)
67+
}
6868

69-
override fun signature(index: Int, fileIndex: Int): ByteArray =
70-
indexedSignatures.itemBytes(fileIndex, index) {
71-
IrArrayReader(icData[fileIndex].signatures)
72-
}
69+
override fun signature(index: Int, fileIndex: Int): ByteArray =
70+
indexedSignatures.itemBytes(fileIndex, index) {
71+
IrArrayReader(icData[fileIndex].signatures)
72+
}
7373

74-
override fun string(index: Int, fileIndex: Int): ByteArray =
75-
indexedStrings.itemBytes(fileIndex, index) {
76-
IrArrayReader(icData[fileIndex].strings)
77-
}
74+
override fun string(index: Int, fileIndex: Int): ByteArray =
75+
indexedStrings.itemBytes(fileIndex, index) {
76+
IrArrayReader(icData[fileIndex].strings)
77+
}
7878

79-
override fun body(index: Int, fileIndex: Int): ByteArray =
80-
indexedBodies.itemBytes(fileIndex, index) {
81-
IrArrayReader(icData[fileIndex].bodies)
82-
}
79+
override fun body(index: Int, fileIndex: Int): ByteArray =
80+
indexedBodies.itemBytes(fileIndex, index) {
81+
IrArrayReader(icData[fileIndex].bodies)
82+
}
8383

84-
override fun debugInfo(index: Int, fileIndex: Int): ByteArray? =
85-
indexedDebugInfos.itemNullableBytes(fileIndex, index) {
86-
icData[fileIndex].debugInfo?.let { IrArrayReader(it) }
87-
}
84+
override fun debugInfo(index: Int, fileIndex: Int): ByteArray? =
85+
indexedDebugInfos.itemNullableBytes(fileIndex, index) {
86+
icData[fileIndex].debugInfo?.let { IrArrayReader(it) }
87+
}
8888

89-
override fun fileEntry(index: Int, fileIndex: Int): ByteArray? =
90-
indexedFileEntries.itemNullableBytes(fileIndex, index) {
91-
icData[fileIndex].fileEntries?.let { IrArrayReader(it) }
92-
}
89+
override fun fileEntry(index: Int, fileIndex: Int): ByteArray? =
90+
indexedFileEntries.itemNullableBytes(fileIndex, index) {
91+
icData[fileIndex].fileEntries?.let { IrArrayReader(it) }
92+
}
9393

94-
override fun file(index: Int): ByteArray = icData[index].fileData
94+
override fun file(index: Int): ByteArray = icData[index].fileData
9595

96-
override fun fileCount(): Int = icData.size
96+
override fun fileCount(): Int = icData.size
9797

98-
override fun types(fileIndex: Int): ByteArray = icData[fileIndex].types
98+
override fun types(fileIndex: Int): ByteArray = icData[fileIndex].types
9999

100-
override fun signatures(fileIndex: Int): ByteArray = icData[fileIndex].signatures
100+
override fun signatures(fileIndex: Int): ByteArray = icData[fileIndex].signatures
101101

102-
override fun strings(fileIndex: Int): ByteArray = icData[fileIndex].strings
102+
override fun strings(fileIndex: Int): ByteArray = icData[fileIndex].strings
103103

104-
override fun declarations(fileIndex: Int): ByteArray = icData[fileIndex].declarations
104+
override fun declarations(fileIndex: Int): ByteArray = icData[fileIndex].declarations
105105

106-
override fun bodies(fileIndex: Int): ByteArray = icData[fileIndex].bodies
106+
override fun bodies(fileIndex: Int): ByteArray = icData[fileIndex].bodies
107107

108-
override fun fileEntries(fileIndex: Int): ByteArray? = icData[fileIndex].fileEntries
108+
override fun fileEntries(fileIndex: Int): ByteArray? = icData[fileIndex].fileEntries
109+
}
109110

110111
// This class is not used by the K2 compiler, so the first stage inlining feature is not supported.
111112
override val hasInlinableFunsIr: Boolean get() = false
112-
override fun irFileOfInlineableFuns(): ByteArray = shouldNotBeCalled()
113-
override fun irDeclarationOfInlineableFuns(index: Int): ByteArray = shouldNotBeCalled()
114-
override fun typeOfInlineableFuns(index: Int): ByteArray = shouldNotBeCalled()
115-
override fun signatureOfInlineableFuns(index: Int): ByteArray = shouldNotBeCalled()
116-
override fun stringOfInlineableFuns(index: Int): ByteArray = shouldNotBeCalled()
117-
override fun bodyOfInlineableFuns(index: Int): ByteArray = shouldNotBeCalled()
118-
override fun debugInfoOfInlineableFuns(index: Int): ByteArray? = shouldNotBeCalled()
119-
override fun fileEntryOfInlineableFuns(index: Int): ByteArray = shouldNotBeCalled()
113+
override val inlinableFunsIr = object : IrLibrary.IrDirectory {
114+
override val hasFileEntriesTable: Boolean get() = shouldNotBeCalled()
115+
override fun irDeclaration(index: Int, fileIndex: Int): ByteArray = shouldNotBeCalled()
116+
override fun type(index: Int, fileIndex: Int): ByteArray = shouldNotBeCalled()
117+
override fun signature(index: Int, fileIndex: Int): ByteArray = shouldNotBeCalled()
118+
override fun string(index: Int, fileIndex: Int): ByteArray = shouldNotBeCalled()
119+
override fun body(index: Int, fileIndex: Int): ByteArray = shouldNotBeCalled()
120+
override fun debugInfo(index: Int, fileIndex: Int): ByteArray = shouldNotBeCalled()
121+
override fun fileEntry(index: Int, fileIndex: Int): ByteArray = shouldNotBeCalled()
122+
override fun file(index: Int): ByteArray = shouldNotBeCalled()
123+
override fun fileCount(): Int = shouldNotBeCalled()
124+
override fun types(fileIndex: Int): ByteArray = shouldNotBeCalled()
125+
override fun signatures(fileIndex: Int): ByteArray = shouldNotBeCalled()
126+
override fun strings(fileIndex: Int): ByteArray = shouldNotBeCalled()
127+
override fun declarations(fileIndex: Int): ByteArray = shouldNotBeCalled()
128+
override fun bodies(fileIndex: Int): ByteArray = shouldNotBeCalled()
129+
override fun fileEntries(fileIndex: Int): ByteArray? = shouldNotBeCalled()
130+
}
120131
}
121132

122133
class CurrentModuleWithICDeserializer(

compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/IrFileDeserializer.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -236,14 +236,14 @@ class IrLibraryFileFromBytes(private val bytesSource: IrLibraryBytesSource) : Ir
236236
}
237237
}
238238

239-
class IrKlibBytesSource(private val klib: IrLibrary, private val fileIndex: Int) : IrLibraryBytesSource() {
240-
override fun irDeclaration(index: Int): ByteArray = klib.irDeclaration(index, fileIndex)
241-
override fun type(index: Int): ByteArray = klib.type(index, fileIndex)
242-
override fun signature(index: Int): ByteArray = klib.signature(index, fileIndex)
243-
override fun string(index: Int): ByteArray = klib.string(index, fileIndex)
244-
override fun body(index: Int): ByteArray = klib.body(index, fileIndex)
245-
override fun debugInfo(index: Int): ByteArray? = klib.debugInfo(index, fileIndex)
246-
override fun fileEntry(index: Int): ByteArray? = klib.fileEntry(index, fileIndex)
239+
class IrKlibBytesSource(private val ir: IrLibrary.IrDirectory, private val fileIndex: Int) : IrLibraryBytesSource() {
240+
override fun irDeclaration(index: Int): ByteArray = ir.irDeclaration(index, fileIndex)
241+
override fun type(index: Int): ByteArray = ir.type(index, fileIndex)
242+
override fun signature(index: Int): ByteArray = ir.signature(index, fileIndex)
243+
override fun string(index: Int): ByteArray = ir.string(index, fileIndex)
244+
override fun body(index: Int): ByteArray = ir.body(index, fileIndex)
245+
override fun debugInfo(index: Int): ByteArray? = ir.debugInfo(index, fileIndex)
246+
override fun fileEntry(index: Int): ByteArray? = ir.fileEntry(index, fileIndex)
247247
}
248248

249249
fun IrLibraryFile.deserializeFqName(fqn: List<Int>): String =
@@ -292,7 +292,7 @@ fun IrLibraryFile.fileEntry(protoFile: ProtoFile): FileEntry =
292292
protoFile.fileEntry
293293
}
294294

295-
fun IrLibrary.fileEntry(protoFile: ProtoFile, fileIndex: Int): FileEntry =
295+
fun IrLibrary.IrDirectory.fileEntry(protoFile: ProtoFile, fileIndex: Int): FileEntry =
296296
if (protoFile.hasFileEntryId() && hasFileEntriesTable) {
297297
val fileEntry = fileEntry(protoFile.fileEntryId, fileIndex) ?: error("Invalid KLib: cannot read file entry by its index")
298298
ProtoFileEntry.parseFrom(fileEntry)

compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/NonLinkingIrInlineFunctionDeserializer.kt

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class NonLinkingIrInlineFunctionDeserializer(
8888
private val irInterner: IrInterningService,
8989
irBuiltIns: IrBuiltIns,
9090
) {
91-
private val fileReader = IrLibraryFileFromBytes(InlinableFunsFileIrKlibBytesSource(library))
91+
private val fileReader = IrLibraryFileFromBytes(IrKlibBytesSource(library.inlinableFunsIr, 0))
9292

9393
private val dummyFileSymbol = IrFileImpl(
9494
fileEntry = object : IrFileEntry {
@@ -143,7 +143,7 @@ class NonLinkingIrInlineFunctionDeserializer(
143143
* if the declaration happens to have multiple inline functions.
144144
*/
145145
val reversedSignatureIndex: Map<IdSignature, Int> = run {
146-
val fileStream = library.irFileOfInlineableFuns().codedInputStream
146+
val fileStream = library.inlinableFunsIr.file(0).codedInputStream
147147
val fileProto = ProtoFile.parseFrom(fileStream, ExtensionRegistryLite.newInstance())
148148
fileProto.declarationIdList.associateBy { symbolDeserializer.deserializeIdSignature(it) }
149149
}
@@ -170,14 +170,4 @@ class NonLinkingIrInlineFunctionDeserializer(
170170
function
171171
}
172172
}
173-
174-
class InlinableFunsFileIrKlibBytesSource(private val klib: IrLibrary) : IrLibraryBytesSource() {
175-
override fun irDeclaration(index: Int): ByteArray = klib.irDeclarationOfInlineableFuns(index)
176-
override fun type(index: Int): ByteArray = klib.typeOfInlineableFuns(index)
177-
override fun signature(index: Int): ByteArray = klib.signatureOfInlineableFuns(index)
178-
override fun string(index: Int): ByteArray = klib.stringOfInlineableFuns(index)
179-
override fun body(index: Int): ByteArray = klib.bodyOfInlineableFuns(index)
180-
override fun debugInfo(index: Int): ByteArray? = klib.debugInfoOfInlineableFuns(index)
181-
override fun fileEntry(index: Int): ByteArray = klib.fileEntryOfInlineableFuns(index)
182-
}
183173
}

compiler/tests-integration/tests/org/jetbrains/kotlin/klib/FilePathsInKlibTest.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,16 @@ class FilePathsInKlibTest : KtUsefulTestCase() {
4545
}.load().apply { assertFalse(hasProblems) }.librariesStdlibFirst
4646

4747
val lib = libs.last()
48-
val fileSize = lib.fileCount()
48+
val mainIr = lib.mainIr
49+
val fileSize = mainIr.fileCount()
4950
val extReg = ExtensionRegistryLite.newInstance()
5051

5152
val result = ArrayList<String>(fileSize)
5253

5354
for (i in 0 until fileSize) {
54-
val fileStream = lib.file(i).codedInputStream
55+
val fileStream = mainIr.file(i).codedInputStream
5556
val fileProto = IrFile.parseFrom(fileStream, extReg)
56-
val fileReader = IrLibraryFileFromBytes(IrKlibBytesSource(lib, i))
57+
val fileReader = IrLibraryFileFromBytes(IrKlibBytesSource(mainIr, i))
5758
val fileEntry = fileReader.fileEntry(fileProto)
5859
val fileName = fileReader.deserializeFileEntryName(fileEntry)
5960

0 commit comments

Comments
 (0)