Detect inline cycles faster

E.g. in the following code

    fun x() {}
    inline fun f() { x(); g() }
    inline fun g() { x(); f() }

the old implementation of inline cycle detection bailed out after
generating 3 calls of x() in each function, while the new one stops
after 2. In other words, code generation for a single function is no
longer reentered.
This commit is contained in:
pyos
2020-03-17 16:48:58 +01:00
committed by max-kammerer
parent 39372c06cf
commit 72b80ef158
17 changed files with 122 additions and 132 deletions
@@ -81,7 +81,12 @@ public abstract class FunctionGenerationStrategy {
@NotNull MemberCodegen<?> parentCodegen
) {
ExpressionCodegen codegen = new ExpressionCodegen(mv, frameMap, signature.getReturnType(), context, state, parentCodegen);
doGenerateBody(codegen, signature);
state.getGlobalInlineContext().enterDeclaration(context.getFunctionDescriptor());
try {
doGenerateBody(codegen, signature);
} finally {
state.getGlobalInlineContext().exitDeclaration();
}
}
@Override
@@ -1,51 +0,0 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.codegen
import org.jetbrains.kotlin.codegen.inline.InlineCall
import org.jetbrains.kotlin.diagnostics.DiagnosticSink
import org.jetbrains.kotlin.diagnostics.Errors
class InlineCycleReporter(private val diagnostics: DiagnosticSink) {
private val processingFunctions = linkedMapOf<Any, InlineCall>()
fun enterIntoInlining(call: InlineCall?): Boolean {
// null call for default method inlining
val id = call?.id
if (id != null) {
if (processingFunctions.contains(id)) {
val cycle = processingFunctions.values.dropWhile { it.id != id }
for (cycleCall in cycle) {
val callPsiElement = cycleCall.callElement
if (callPsiElement != null) {
diagnostics.report(Errors.INLINE_CALL_CYCLE.on(callPsiElement, cycleCall.calleeDescriptor))
}
}
return false
}
processingFunctions[id] = call
}
return true
}
fun exitFromInliningOf(call: InlineCall?) {
if (call != null) {
processingFunctions.remove(call.id)
}
}
}
@@ -5,24 +5,48 @@
package org.jetbrains.kotlin.codegen.inline
import org.jetbrains.kotlin.codegen.InlineCycleReporter
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.diagnostics.DiagnosticSink
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.diagnostics.Errors
import java.util.*
class GlobalInlineContext(diagnostics: DiagnosticSink) {
private val inlineCycleReporter: InlineCycleReporter = InlineCycleReporter(diagnostics)
class GlobalInlineContext(private val diagnostics: DiagnosticSink) {
// Ordered set of declarations and inline calls being generated right now.
// No call in it should point to a declaration that's before it in the stack.
private val inlineCallsAndDeclarations = LinkedList<Any? /* CallableDescriptor | PsiElement? */>()
private val inlineDeclarationSet = mutableSetOf<CallableDescriptor>()
private val typesUsedInInlineFunctions = LinkedList<MutableSet<String>>()
fun enterIntoInlining(call: InlineCall?) =
inlineCycleReporter.enterIntoInlining(call).also {
if (it) typesUsedInInlineFunctions.push(hashSetOf())
}
fun enterDeclaration(descriptor: CallableDescriptor) {
assert(descriptor.original !in inlineDeclarationSet) { "entered inlining cycle on $descriptor" }
inlineDeclarationSet.add(descriptor.original)
inlineCallsAndDeclarations.add(descriptor.original)
}
fun exitFromInliningOf(call: InlineCall?) {
inlineCycleReporter.exitFromInliningOf(call)
fun exitDeclaration() {
inlineDeclarationSet.remove(inlineCallsAndDeclarations.removeLast())
}
fun enterIntoInlining(callee: CallableDescriptor?, element: PsiElement?): Boolean {
if (callee != null && callee.original in inlineDeclarationSet) {
element?.let { diagnostics.report(Errors.INLINE_CALL_CYCLE.on(it, callee.original)) }
for ((call, callTarget) in inlineCallsAndDeclarations.dropWhile { it != callee.original }.zipWithNext()) {
// Every call element should be followed by the callee's descriptor.
if (call is PsiElement && callTarget is CallableDescriptor) {
diagnostics.report(Errors.INLINE_CALL_CYCLE.on(call, callTarget))
}
}
return false
}
inlineCallsAndDeclarations.add(element)
typesUsedInInlineFunctions.push(hashSetOf())
return true
}
fun exitFromInlining() {
inlineCallsAndDeclarations.removeLast()
val pop = typesUsedInInlineFunctions.pop()
typesUsedInInlineFunctions.peek()?.addAll(pop)
}
@@ -1,32 +0,0 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.codegen.inline
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
interface InlineCall {
val id: Any
val calleeDescriptor: CallableDescriptor
val callElement: PsiElement?
}
class InlineCallImpl(
override val calleeDescriptor: CallableDescriptor,
override val callElement: PsiElement
) : InlineCall {
override val id: Any
get() = callElement
companion object {
fun of(resolvedCall: ResolvedCall<*>?) =
resolvedCall?.run {
InlineCallImpl(resultingDescriptor.original, call.callElement)
}
}
}
@@ -63,8 +63,7 @@ class PsiInlineCodegen(
callDefault: Boolean,
codegen: ExpressionCodegen
) {
val inlineCall = InlineCallImpl.of(resolvedCall)
if (!state.globalInlineContext.enterIntoInlining(inlineCall)) {
if (!state.globalInlineContext.enterIntoInlining(resolvedCall?.resultingDescriptor, resolvedCall?.call?.callElement)) {
generateStub(resolvedCall, codegen)
return
}
@@ -72,7 +71,7 @@ class PsiInlineCodegen(
val registerLineNumber = registerLineNumberAfterwards(resolvedCall)
performInline(resolvedCall?.typeArguments?.keys?.toList(), callDefault, callDefault, codegen.typeSystem, registerLineNumber)
} finally {
state.globalInlineContext.exitFromInliningOf(inlineCall)
state.globalInlineContext.exitFromInlining()
}
}