Skip to content

Commit 4fe4c6a

Browse files
ivandev0Space Team
authored andcommitted
[IR] Store preprocessed functions as private declarations
It is important to modify preprocessed inline functions to be private. These functions are not supposed to be used anywhere. Private declarations have a separate type of signatures. If the signature remains public, then we can have clashes with original functions. For example 1. Type parameter clash. If a type argument is used in the `typeOf` call, then we handle it specially and keep it as is (instead of erasure). This type argument must point to the type parameter of the original function. 2. Reference to the inline function. If inside a preprocessed function we have a reference to another inline function, then this reference must point to the original one. #KT-81070 #KT-81470
1 parent 88ebb6e commit 4fe4c6a

File tree

36 files changed

+117
-124
lines changed

36 files changed

+117
-124
lines changed

compiler/ir/ir.inline/src/org/jetbrains/kotlin/ir/inline/InlineFunctionSerializationPreProcessing.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class InlineFunctionSerializationPreProcessing(
3838

3939
val preprocessed = declaration
4040
.copyAndEraseTypeParameters()
41-
.convertToPublicTopLevel()
41+
.convertToPrivateTopLevel()
4242
.erasePrivateSymbols()
4343
.applyCrossModuleFunctionInlining()
4444

@@ -53,8 +53,8 @@ class InlineFunctionSerializationPreProcessing(
5353
.preprocess(this) as IrSimpleFunction
5454
}
5555

56-
private fun IrSimpleFunction.convertToPublicTopLevel(): IrSimpleFunction {
57-
visibility = DescriptorVisibilities.PUBLIC
56+
private fun IrSimpleFunction.convertToPrivateTopLevel(): IrSimpleFunction {
57+
visibility = DescriptorVisibilities.PRIVATE
5858
correspondingPropertySymbol = null
5959
parent = file
6060

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

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,7 @@ open class IrFileSerializer(
316316
?: error("Given symbol is unbound and have no signature: $symbol")
317317
symbol is IrFileSymbol -> IdSignature.FileSignature(symbol) // TODO: special signature for files?
318318
else -> {
319-
var symbolOwner = symbol.owner
320-
321-
// Prefer a real inline function over its prepared copy - the latter is only used store inlinable body,
322-
// and only the former has the correct declaration shape (such as parameters) allowing to compute
323-
// a valid signature of the function.
324-
(symbolOwner as? IrSimpleFunction)?.originalOfPreparedInlineFunctionCopy?.let {
325-
symbolOwner = it
326-
}
319+
val symbolOwner = symbol.owner
327320

328321
// Compute the signature:
329322
when {
@@ -1641,15 +1634,17 @@ open class IrFileSerializer(
16411634

16421635
fun serializeIrFileWithPreparedInlineFunctions(preparedFunctions: List<IrSimpleFunction>): SerializedIrFile {
16431636
val topLevelDeclarations = preparedFunctions.map { function ->
1644-
val byteArray = serializeDeclaration(function).toByteArray()
1645-
val idSig = declarationTable.signatureByDeclaration(
1646-
function.originalOfPreparedInlineFunctionCopy!!,
1647-
compatibleMode = false,
1648-
recordInSignatureClashDetector = false
1649-
)
1650-
val sigIndex = idSignatureSerializer.protoIdSignature(idSig)
1651-
1652-
SerializedDeclaration(sigIndex, byteArray)
1637+
inFile(function.file) {
1638+
val byteArray = serializeDeclaration(function).toByteArray()
1639+
val idSig = declarationTable.signatureByDeclaration(
1640+
function.originalOfPreparedInlineFunctionCopy!!,
1641+
compatibleMode = false,
1642+
recordInSignatureClashDetector = false
1643+
)
1644+
val sigIndex = idSignatureSerializer.protoIdSignature(idSig)
1645+
1646+
SerializedDeclaration(sigIndex, byteArray)
1647+
}
16531648
}
16541649

16551650
// Memoize all preprocessed functions in `ProtoFile.declarationIdList`.

compiler/testData/codegen/box/inline/crossinlineLambdaAllocation.preprocessed.ir.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
Module: main
22
// CallableId=/runTwice
3-
FUN name:runTwice visibility:public modality:FINAL returnType:<root>.Pair<kotlin.Any?> [inline]
3+
FUN name:runTwice visibility:private modality:FINAL returnType:<root>.Pair<kotlin.Any?> [inline]
44
TYPE_PARAMETER name:T index:0 variance: superTypes:[kotlin.Any?] reified:false
55
VALUE_PARAMETER kind:Regular name:f index:0 type:kotlin.Function0<kotlin.Any?>
66
BLOCK_BODY
7-
RETURN type=kotlin.Nothing from='public final fun runTwice <T> (f: kotlin.Function0<kotlin.Any?>): <root>.Pair<kotlin.Any?> [inline] declared in <root>'
7+
RETURN type=kotlin.Nothing from='private final fun runTwice <T> (f: kotlin.Function0<kotlin.Any?>): <root>.Pair<kotlin.Any?> [inline] declared in <root>'
88
CONSTRUCTOR_CALL 'public constructor <init> (first: T of <root>.Pair, second: T of <root>.Pair) [primary] declared in <root>.Pair' type=<root>.Pair<kotlin.Any?> origin=null
99
TYPE_ARG (of class) T: kotlin.Any?
1010
ARG first: CALL 'public abstract fun invoke (): R of kotlin.Function0 [operator] declared in kotlin.Function0' type=kotlin.Any? origin=INVOKE
1111
ARG <this>: GET_VAR 'f: kotlin.Function0<kotlin.Any?> declared in <root>.runTwice' type=kotlin.Function0<kotlin.Any?> origin=VARIABLE_AS_FUNCTION
1212
ARG second: CALL 'public abstract fun invoke (): R of kotlin.Function0 [operator] declared in kotlin.Function0' type=kotlin.Any? origin=INVOKE
1313
ARG <this>: GET_VAR 'f: kotlin.Function0<kotlin.Any?> declared in <root>.runTwice' type=kotlin.Function0<kotlin.Any?> origin=VARIABLE_AS_FUNCTION
1414
// CallableId=/bar
15-
FUN name:bar visibility:public modality:FINAL returnType:kotlin.Int [inline]
15+
FUN name:bar visibility:private modality:FINAL returnType:kotlin.Int [inline]
1616
VALUE_PARAMETER kind:Regular name:test index:0 type:kotlin.Function0<kotlin.Int> [crossinline]
1717
BLOCK_BODY
1818
VAR name:outsideCounter type:kotlin.internal.SharedVariableBox<kotlin.Int> [val]
@@ -145,7 +145,7 @@ FUN name:bar visibility:public modality:FINAL returnType:kotlin.Int [inline]
145145
ARG <this>: CALL 'public final fun <get-globalCounter> (): kotlin.Int declared in <root>' type=kotlin.Int origin=PLUSEQ
146146
ARG other: CALL 'public final fun <get-element> (): T of kotlin.internal.SharedVariableBox declared in kotlin.internal.SharedVariableBox' type=kotlin.Int origin=null
147147
ARG <this>: GET_VAR 'val outsideCounter: kotlin.internal.SharedVariableBox<kotlin.Int> [val] declared in <root>.bar' type=kotlin.internal.SharedVariableBox<kotlin.Int> origin=null
148-
RETURN type=kotlin.Nothing from='public final fun bar (test: kotlin.Function0<kotlin.Int>): kotlin.Int [inline] declared in <root>'
148+
RETURN type=kotlin.Nothing from='private final fun bar (test: kotlin.Function0<kotlin.Int>): kotlin.Int [inline] declared in <root>'
149149
CALL 'public final fun plus (other: kotlin.Int): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=PLUS
150150
ARG <this>: CALL 'public final fun foo (): kotlin.Int declared in <root>.bar.<no name provided>' type=kotlin.Int origin=null
151151
ARG <this>: CALL 'public final fun <get-first> (): T of <root>.Pair declared in <root>.Pair' type=<root>.bar.<no name provided> origin=GET_PROPERTY

compiler/testData/codegen/box/reflection/typeOf/nonReifiedTypeParameters/typeParameterFlags.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// IGNORE_BACKEND_K2_MULTI_MODULE: JS_IR, WASM_JS
2-
// ^^^ KT-81070: IrClassSymbolImpl is unbound. Signature: kotlin/Any|null[0]
31
// WITH_REFLECT
42
// WITH_STDLIB
53

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
Module: main
22
// CallableId=/foo
3-
FUN name:foo visibility:public modality:FINAL returnType:kotlin.reflect.KType [inline]
3+
FUN name:foo visibility:private modality:FINAL returnType:kotlin.reflect.KType [inline]
44
TYPE_PARAMETER name:T index:0 variance: superTypes:[kotlin.Any?] reified:true
55
VALUE_PARAMETER kind:Regular name:a index:0 type:T of <root>.foo
66
BLOCK_BODY
7-
RETURN type=kotlin.Nothing from='public final fun foo <T> (a: T of <root>.foo): kotlin.reflect.KType [inline] declared in <root>'
7+
RETURN type=kotlin.Nothing from='private final fun foo <T> (a: T of <root>.foo): kotlin.reflect.KType [inline] declared in <root>'
88
CALL 'public final fun typeOf <T> (): kotlin.reflect.KType [inline] declared in kotlin.reflect' type=kotlin.reflect.KType origin=null
99
TYPE_ARG T: T of <root>.foo
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
Module: main
22
// CallableId=/foo
3-
FUN name:foo visibility:public modality:FINAL returnType:kotlin.reflect.KType [inline]
3+
FUN name:foo visibility:private modality:FINAL returnType:kotlin.reflect.KType [inline]
44
TYPE_PARAMETER name:T index:0 variance: superTypes:[kotlin.CharSequence] reified:true
55
VALUE_PARAMETER kind:Regular name:c index:0 type:kotlin.Int
66
VALUE_PARAMETER kind:Regular name:a index:1 type:T of <root>.foo
77
BLOCK_BODY
8-
RETURN type=kotlin.Nothing from='public final fun foo <T> (c: kotlin.Int, a: T of <root>.foo): kotlin.reflect.KType [inline] declared in <root>'
8+
RETURN type=kotlin.Nothing from='private final fun foo <T> (c: kotlin.Int, a: T of <root>.foo): kotlin.reflect.KType [inline] declared in <root>'
99
CALL 'public final fun typeOf <T> (): kotlin.reflect.KType [inline] declared in kotlin.reflect' type=kotlin.reflect.KType origin=null
1010
TYPE_ARG T: T of <root>.foo
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
Module: main
22
// CallableId=/foo
3-
FUN name:foo visibility:public modality:FINAL returnType:kotlin.reflect.KType [inline]
3+
FUN name:foo visibility:private modality:FINAL returnType:kotlin.reflect.KType [inline]
44
TYPE_PARAMETER name:T index:0 variance: superTypes:[kotlin.CharSequence] reified:true
55
VALUE_PARAMETER kind:Regular name:<this> index:0 type:kotlin.Int
66
VALUE_PARAMETER kind:Regular name:a index:1 type:T of <root>.foo
77
BLOCK_BODY
8-
RETURN type=kotlin.Nothing from='public final fun foo <T> (<this>: kotlin.Int, a: T of <root>.foo): kotlin.reflect.KType [inline] declared in <root>'
8+
RETURN type=kotlin.Nothing from='private final fun foo <T> (<this>: kotlin.Int, a: T of <root>.foo): kotlin.reflect.KType [inline] declared in <root>'
99
CALL 'public final fun typeOf <T> (): kotlin.reflect.KType [inline] declared in kotlin.reflect' type=kotlin.reflect.KType origin=null
1010
TYPE_ARG T: T of <root>.foo
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
Module: main
22
// CallableId=/foo
3-
FUN name:foo visibility:public modality:FINAL returnType:kotlin.reflect.KType [inline]
3+
FUN name:foo visibility:private modality:FINAL returnType:kotlin.reflect.KType [inline]
44
TYPE_PARAMETER name:T index:0 variance: superTypes:[kotlin.CharSequence] reified:true
55
VALUE_PARAMETER kind:Regular name:c index:0 type:kotlin.Int
66
VALUE_PARAMETER kind:Regular name:<this> index:1 type:kotlin.Int
77
VALUE_PARAMETER kind:Regular name:a index:2 type:T of <root>.foo
88
BLOCK_BODY
9-
RETURN type=kotlin.Nothing from='public final fun foo <T> (c: kotlin.Int, <this>: kotlin.Int, a: T of <root>.foo): kotlin.reflect.KType [inline] declared in <root>'
9+
RETURN type=kotlin.Nothing from='private final fun foo <T> (c: kotlin.Int, <this>: kotlin.Int, a: T of <root>.foo): kotlin.reflect.KType [inline] declared in <root>'
1010
CALL 'public final fun typeOf <T> (): kotlin.reflect.KType [inline] declared in kotlin.reflect' type=kotlin.reflect.KType origin=null
1111
TYPE_ARG T: T of <root>.foo
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
Module: main
22
// CallableId=/foo
3-
FUN name:foo visibility:public modality:FINAL returnType:kotlin.reflect.KType [inline]
3+
FUN name:foo visibility:private modality:FINAL returnType:kotlin.reflect.KType [inline]
44
TYPE_PARAMETER name:T index:0 variance: superTypes:[kotlin.CharSequence] reified:true
55
VALUE_PARAMETER kind:Regular name:<this> index:0 type:<root>.A
66
VALUE_PARAMETER kind:Regular name:c index:1 type:kotlin.Int
77
VALUE_PARAMETER kind:Regular name:<this> index:2 type:<root>.A
88
VALUE_PARAMETER kind:Regular name:a index:3 type:T of <root>.foo
99
BLOCK_BODY
10-
RETURN type=kotlin.Nothing from='public final fun foo <T> (<this>: <root>.A, c: kotlin.Int, <this>: <root>.A, a: T of <root>.foo): kotlin.reflect.KType [inline] declared in <root>'
10+
RETURN type=kotlin.Nothing from='private final fun foo <T> (<this>: <root>.A, c: kotlin.Int, <this>: <root>.A, a: T of <root>.foo): kotlin.reflect.KType [inline] declared in <root>'
1111
CALL 'public final fun typeOf <T> (): kotlin.reflect.KType [inline] declared in kotlin.reflect' type=kotlin.reflect.KType origin=null
1212
TYPE_ARG T: T of <root>.foo

compiler/testData/codegen/box/typeErasure/funWithReifiedTypeParameterInlinesFunWithThreeTypeParameters.preprocessed.ir.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
Module: main
22
// CallableId=/typeOfX
3-
FUN name:typeOfX visibility:public modality:FINAL returnType:kotlin.reflect.KType [inline]
3+
FUN name:typeOfX visibility:private modality:FINAL returnType:kotlin.reflect.KType [inline]
44
TYPE_PARAMETER name:T1 index:0 variance: superTypes:[kotlin.Any?] reified:true
55
VALUE_PARAMETER kind:Regular name:x index:0 type:T1 of <root>.typeOfX
66
BLOCK_BODY
7-
RETURN type=kotlin.Nothing from='public final fun typeOfX <T1> (x: T1 of <root>.typeOfX): kotlin.reflect.KType [inline] declared in <root>'
7+
RETURN type=kotlin.Nothing from='private final fun typeOfX <T1> (x: T1 of <root>.typeOfX): kotlin.reflect.KType [inline] declared in <root>'
88
CALL 'public final fun typeOf <T> (): kotlin.reflect.KType [inline] declared in kotlin.reflect' type=kotlin.reflect.KType origin=null
99
TYPE_ARG T: T1 of <root>.typeOfX
1010
// CallableId=/typeOfTriple1
11-
FUN name:typeOfTriple1 visibility:public modality:FINAL returnType:kotlin.reflect.KType [inline]
11+
FUN name:typeOfTriple1 visibility:private modality:FINAL returnType:kotlin.reflect.KType [inline]
1212
TYPE_PARAMETER name:T2 index:0 variance: superTypes:[kotlin.Any?] reified:true
1313
TYPE_PARAMETER name:T3 index:1 variance: superTypes:[kotlin.Any?] reified:true
1414
TYPE_PARAMETER name:T4 index:2 variance: superTypes:[kotlin.Any?] reified:false
1515
VALUE_PARAMETER kind:Regular name:x index:0 type:T2 of <root>.typeOfTriple1
1616
VALUE_PARAMETER kind:Regular name:y index:1 type:T3 of <root>.typeOfTriple1
1717
VALUE_PARAMETER kind:Regular name:z index:2 type:kotlin.Any?
1818
BLOCK_BODY
19-
RETURN type=kotlin.Nothing from='public final fun typeOfTriple1 <T2, T3, T4> (x: T2 of <root>.typeOfTriple1, y: T3 of <root>.typeOfTriple1, z: kotlin.Any?): kotlin.reflect.KType [inline] declared in <root>'
19+
RETURN type=kotlin.Nothing from='private final fun typeOfTriple1 <T2, T3, T4> (x: T2 of <root>.typeOfTriple1, y: T3 of <root>.typeOfTriple1, z: kotlin.Any?): kotlin.reflect.KType [inline] declared in <root>'
2020
BLOCK type=kotlin.reflect.KType origin=INLINE_ARGS_CONTAINER
2121
VAR IR_TEMPORARY_VARIABLE name:tmp0 type:<root>.Triple<T2 of <root>.typeOfTriple1, T3 of <root>.typeOfTriple1, kotlin.Any?> [val]
2222
CONSTRUCTOR_CALL 'public constructor <init> (x: A of <root>.Triple, y: B of <root>.Triple, z: C of <root>.Triple) [primary] declared in <root>.Triple' type=<root>.Triple<T2 of <root>.typeOfTriple1, T3 of <root>.typeOfTriple1, kotlin.Any?> origin=null
@@ -36,15 +36,15 @@ FUN name:typeOfTriple1 visibility:public modality:FINAL returnType:kotlin.reflec
3636
CALL 'public final fun typeOf <T> (): kotlin.reflect.KType [inline] declared in kotlin.reflect' type=kotlin.reflect.KType origin=null
3737
TYPE_ARG T: <root>.Triple<T2 of <root>.typeOfTriple1, T3 of <root>.typeOfTriple1, T4 of <root>.typeOfTriple1>
3838
// CallableId=/typeOfTriple2
39-
FUN name:typeOfTriple2 visibility:public modality:FINAL returnType:kotlin.reflect.KType [inline]
39+
FUN name:typeOfTriple2 visibility:private modality:FINAL returnType:kotlin.reflect.KType [inline]
4040
TYPE_PARAMETER name:T2 index:0 variance: superTypes:[kotlin.Any?] reified:true
4141
TYPE_PARAMETER name:T3 index:1 variance: superTypes:[kotlin.Any?] reified:false
4242
TYPE_PARAMETER name:T4 index:2 variance: superTypes:[kotlin.Any?] reified:true
4343
VALUE_PARAMETER kind:Regular name:x index:0 type:T2 of <root>.typeOfTriple2
4444
VALUE_PARAMETER kind:Regular name:y index:1 type:kotlin.Any?
4545
VALUE_PARAMETER kind:Regular name:z index:2 type:T4 of <root>.typeOfTriple2
4646
BLOCK_BODY
47-
RETURN type=kotlin.Nothing from='public final fun typeOfTriple2 <T2, T3, T4> (x: T2 of <root>.typeOfTriple2, y: kotlin.Any?, z: T4 of <root>.typeOfTriple2): kotlin.reflect.KType [inline] declared in <root>'
47+
RETURN type=kotlin.Nothing from='private final fun typeOfTriple2 <T2, T3, T4> (x: T2 of <root>.typeOfTriple2, y: kotlin.Any?, z: T4 of <root>.typeOfTriple2): kotlin.reflect.KType [inline] declared in <root>'
4848
BLOCK type=kotlin.reflect.KType origin=INLINE_ARGS_CONTAINER
4949
VAR IR_TEMPORARY_VARIABLE name:tmp0 type:<root>.Triple<T2 of <root>.typeOfTriple2, kotlin.Any?, T4 of <root>.typeOfTriple2> [val]
5050
CONSTRUCTOR_CALL 'public constructor <init> (x: A of <root>.Triple, y: B of <root>.Triple, z: C of <root>.Triple) [primary] declared in <root>.Triple' type=<root>.Triple<T2 of <root>.typeOfTriple2, kotlin.Any?, T4 of <root>.typeOfTriple2> origin=null

0 commit comments

Comments
 (0)