[KLIB] Implement new linker based on IdSignature

- Remove klib dependency on metadata and uniqID
 - Refactored proto format to make it more effective and compact
  -- Use special encoding for some types of data (coordinates, flags, types)
  -- Remove symbols table
  -- Use packed proto list if it is possible
 - Remove extension from metadata
 - Remove special ids for function interfaces
 - Fix klib IO
 - Fix incremental cache
 - General code clean up
This commit is contained in:
Roman Artemev
2020-01-27 15:58:52 +03:00
committed by romanart
parent 31d73c5d79
commit 6a37955a36
211 changed files with 6616 additions and 78716 deletions
@@ -39,9 +39,9 @@ interface MetadataLibrary {
interface IrLibrary {
val dataFlowGraph: ByteArray?
fun irDeclaration(index: Long, fileIndex: Int): ByteArray
fun symbol(index: Int, fileIndex: Int): ByteArray
fun irDeclaration(index: Int, fileIndex: Int): ByteArray
fun type(index: Int, fileIndex: Int): ByteArray
fun signature(index: Int, fileIndex: Int): ByteArray
fun string(index: Int, fileIndex: Int): ByteArray
fun body(index: Int, fileIndex: Int): ByteArray
fun file(index: Int): ByteArray
@@ -17,7 +17,6 @@
package org.jetbrains.kotlin.library
import org.jetbrains.kotlin.konan.file.File
import org.jetbrains.kotlin.library.KLIB_METADATA_FILE_EXTENSION_WITH_DOT
const val KLIB_MANIFEST_FILE_NAME = "manifest"
const val KLIB_MODULE_METADATA_FILE_NAME = "module"
@@ -59,10 +58,10 @@ interface IrKotlinLibraryLayout : KotlinLibraryLayout {
get() = File(componentDir, KLIB_IR_FOLDER_NAME)
val irDeclarations
get() = File(irDir, "irDeclarations.knd")
val irSymbols
get() = File(irDir, "symbols.knt")
val irTypes
get() = File(irDir, "types.knt")
val irSignatures
get() = File(irDir, "signatures.knt")
val irStrings
get() = File(irDir, "strings.knt")
val irBodies
@@ -73,8 +72,8 @@ interface IrKotlinLibraryLayout : KotlinLibraryLayout {
get() = File(irDir, "module_data_flow_graph")
fun irDeclarations(file: File): File = File(file, "irCombined.knd")
fun irSymbols(file: File): File = File(file, "symbols.knt")
fun irTypes(file: File): File = File(file, "types.knt")
fun irSignatures(file: File): File = File(file, "signatures.knt")
fun irStrings(file: File): File = File(file, "strings.knt")
fun irBodies(file: File): File = File(file, "body.knb")
fun irFile(file: File): File = File(file, "file.knf")
@@ -33,22 +33,20 @@ class SerializedMetadata(
)
sealed class SerializedDeclaration {
abstract val id: Long
abstract val local: Int
abstract val id: Int
abstract val size: Int
abstract val bytes: ByteArray
abstract val declarationName: String
}
class TopLevelDeclaration(override val id: Long, isLocal: Boolean, override val declarationName: String, override val bytes: ByteArray) : SerializedDeclaration() {
override val local = if (isLocal) 1 else 0
class TopLevelDeclaration(override val id: Int, override val declarationName: String, override val bytes: ByteArray) :
SerializedDeclaration() {
override val size = bytes.size
}
object SkippedDeclaration : SerializedDeclaration() {
override val id = -1L
override val local = -1
override val id = -1
override val size = 0
override val bytes = ByteArray(0)
override val declarationName: String = "<SKIPPED>"
@@ -58,8 +56,8 @@ class SerializedIrFile(
val fileData: ByteArray,
val fqName: String,
val path: String,
val symbols: ByteArray,
val types: ByteArray,
val signatures: ByteArray,
val strings: ByteArray,
val bodies: ByteArray,
val declarations: ByteArray
@@ -169,7 +169,7 @@ abstract class IrTableReader<K>(file: File, keyReader: ByteBuffer.() -> K) {
class IndexIrTableReader(file: File) : IrTableReader<Long>(file, { long })
data class DeclarationId(val id: Long)
data class DeclarationId(val id: Int)
class DeclarationIrTableReader(file: File) : IrTableReader<DeclarationId>(file, { DeclarationId(long) })
class DeclarationIrMultiTableReader(file: File) : IrMultiTableReader<DeclarationId>(file, { DeclarationId(long) })
class DeclarationIrTableReader(file: File) : IrTableReader<DeclarationId>(file, { DeclarationId(int) })
class DeclarationIrMultiTableReader(file: File) : IrMultiTableReader<DeclarationId>(file, { DeclarationId(int) })
@@ -91,7 +91,7 @@ class IrTableWriter(private val data: List<Pair<Long, ByteArray>>) : IrFileWrite
class IrDeclarationWriter(private val declarations: List<SerializedDeclaration>) : IrFileWriter() {
private val SINGLE_INDEX_RECORD_SIZE = Long.SIZE_BYTES + 2 * Int.SIZE_BYTES
private val SINGLE_INDEX_RECORD_SIZE = 3 * Int.SIZE_BYTES
private val INDEX_HEADER_SIZE = Int.SIZE_BYTES
override fun writeData(dataOutput: DataOutput) {
@@ -100,7 +100,7 @@ class IrDeclarationWriter(private val declarations: List<SerializedDeclaration>)
var dataOffset = INDEX_HEADER_SIZE + SINGLE_INDEX_RECORD_SIZE * declarations.size
for (d in declarations) {
dataOutput.writeLong(d.id)
dataOutput.writeInt(d.id)
dataOutput.writeInt(dataOffset)
dataOutput.writeInt(d.size)
dataOffset += d.size
@@ -115,7 +115,7 @@ class IrDeclarationWriter(private val declarations: List<SerializedDeclaration>)
class IrMemoryDeclarationWriter(private val declarations: List<SerializedDeclaration>) : IrMemoryWriter() {
private val SINGLE_INDEX_RECORD_SIZE = Long.SIZE_BYTES + 2 * Int.SIZE_BYTES
private val SINGLE_INDEX_RECORD_SIZE = 3 * Int.SIZE_BYTES
private val INDEX_HEADER_SIZE = Int.SIZE_BYTES
override fun writeData(dataOutput: DataOutput) {
@@ -124,7 +124,7 @@ class IrMemoryDeclarationWriter(private val declarations: List<SerializedDeclara
var dataOffset = INDEX_HEADER_SIZE + SINGLE_INDEX_RECORD_SIZE * declarations.size
for (d in declarations) {
dataOutput.writeLong(d.id)
dataOutput.writeInt(d.id)
dataOutput.writeInt(dataOffset)
dataOutput.writeInt(d.size)
dataOffset += d.size
@@ -23,8 +23,8 @@ class IrMonoliticWriterImpl(irLayout: IrKotlinLibraryLayout) : IrWriterImpl(irLa
with(ir.files.sortedBy { it.path }) {
IrArrayWriter(map { it.fileData }).writeIntoFile(irLayout.irFiles.absolutePath)
IrArrayWriter(map { it.declarations }).writeIntoFile(irLayout.irDeclarations.absolutePath)
IrArrayWriter(map { it.symbols }).writeIntoFile(irLayout.irSymbols.absolutePath)
IrArrayWriter(map { it.types }).writeIntoFile(irLayout.irTypes.absolutePath)
IrArrayWriter(map { it.signatures }).writeIntoFile(irLayout.irSignatures.absolutePath)
IrArrayWriter(map { it.strings }).writeIntoFile(irLayout.irStrings.absolutePath)
IrArrayWriter(map { it.bodies }).writeIntoFile(irLayout.irBodies.absolutePath)
}
@@ -50,8 +50,8 @@ class IrPerFileWriterImpl(irLayout: IrKotlinLibraryLayout) : IrWriterImpl(irLayo
irLayout.irFile(fileDir).writeBytes(file.fileData)
irLayout.irDeclarations(fileDir).writeBytes(file.declarations)
irLayout.irSymbols(fileDir).writeBytes(file.symbols)
irLayout.irTypes(fileDir).writeBytes(file.types)
irLayout.irSignatures(fileDir).writeBytes(file.signatures)
irLayout.irStrings(fileDir).writeBytes(file.strings)
irLayout.irBodies(fileDir).writeBytes(file.bodies)
}
@@ -97,19 +97,19 @@ abstract class IrLibraryImpl(
class IrMonoliticLibraryImpl(_access: IrLibraryAccess<IrKotlinLibraryLayout>) : IrLibraryImpl(_access) {
override fun fileCount(): Int = files.entryCount()
override fun irDeclaration(index: Long, fileIndex: Int) = loadIrDeclaration(index, fileIndex)
override fun symbol(index: Int, fileIndex: Int) = symbols.tableItemBytes(fileIndex, index)
override fun irDeclaration(index: Int, fileIndex: Int) = loadIrDeclaration(index, fileIndex)
override fun type(index: Int, fileIndex: Int) = types.tableItemBytes(fileIndex, index)
override fun signature(index: Int, fileIndex: Int) = signatures.tableItemBytes(fileIndex, index)
override fun string(index: Int, fileIndex: Int) = strings.tableItemBytes(fileIndex, index)
override fun body(index: Int, fileIndex: Int) = bodies.tableItemBytes(fileIndex, index)
override fun file(index: Int) = files.tableItemBytes(index)
private fun loadIrDeclaration(index: Long, fileIndex: Int) =
private fun loadIrDeclaration(index: Int, fileIndex: Int) =
combinedDeclarations.tableItemBytes(fileIndex, DeclarationId(index))
private val combinedDeclarations: DeclarationIrMultiTableReader by lazy {
@@ -118,18 +118,18 @@ class IrMonoliticLibraryImpl(_access: IrLibraryAccess<IrKotlinLibraryLayout>) :
})
}
private val symbols: IrMultiArrayReader by lazy {
IrMultiArrayReader(access.realFiles {
it.irSymbols
})
}
private val types: IrMultiArrayReader by lazy {
IrMultiArrayReader(access.realFiles {
it.irTypes
})
}
private val signatures: IrMultiArrayReader by lazy {
IrMultiArrayReader(access.realFiles {
it.irSignatures
})
}
private val strings: IrMultiArrayReader by lazy {
IrMultiArrayReader(access.realFiles {
it.irStrings
@@ -158,7 +158,7 @@ class IrPerFileLibraryImpl(_access: IrLibraryAccess<IrKotlinLibraryLayout>) : Ir
}
private val fileToDeclarationMap = mutableMapOf<Int, DeclarationIrTableReader>()
override fun irDeclaration(index: Long, fileIndex: Int): ByteArray {
override fun irDeclaration(index: Int, fileIndex: Int): ByteArray {
val dataReader = fileToDeclarationMap.getOrPut(fileIndex) {
val fileDirectory = directories[fileIndex]
DeclarationIrTableReader(access.realFiles {
@@ -168,17 +168,6 @@ class IrPerFileLibraryImpl(_access: IrLibraryAccess<IrKotlinLibraryLayout>) : Ir
return dataReader.tableItemBytes(DeclarationId(index))
}
private val fileToSymbolMap = mutableMapOf<Int, IrArrayReader>()
override fun symbol(index: Int, fileIndex: Int): ByteArray {
val dataReader = fileToSymbolMap.getOrPut(fileIndex) {
val fileDirectory = directories[fileIndex]
IrArrayReader(access.realFiles {
it.irSymbols(fileDirectory)
})
}
return dataReader.tableItemBytes(index)
}
private val fileToTypeMap = mutableMapOf<Int, IrArrayReader>()
override fun type(index: Int, fileIndex: Int): ByteArray {
val dataReader = fileToTypeMap.getOrPut(fileIndex) {
@@ -190,6 +179,16 @@ class IrPerFileLibraryImpl(_access: IrLibraryAccess<IrKotlinLibraryLayout>) : Ir
return dataReader.tableItemBytes(index)
}
override fun signature(index: Int, fileIndex: Int): ByteArray {
val dataReader = fileToTypeMap.getOrPut(fileIndex) {
val fileDirectory = directories[fileIndex]
IrArrayReader(access.realFiles {
it.irSignatures(fileDirectory)
})
}
return dataReader.tableItemBytes(index)
}
private val fileToStringMap = mutableMapOf<Int, IrArrayReader>()
override fun string(index: Int, fileIndex: Int): ByteArray {
val dataReader = fileToStringMap.getOrPut(fileIndex) {
@@ -136,8 +136,6 @@ class ExtractingIrLibraryImpl(val zipped: IrLibraryLayoutImpl) :
override val irDeclarations: File by lazy { zipped.extract(zipped.irDeclarations) }
override val irSymbols: File by lazy { zipped.extract(zipped.irSymbols) }
override val irTypes: File by lazy { zipped.extract(zipped.irTypes) }
override val irStrings: File by lazy { zipped.extract(zipped.irStrings) }