[kotlin compiler][update][fixup] adoption SharedVariablesLowering to new Shared Variable Manager
This commit is contained in:
+24
-27
@@ -17,18 +17,16 @@
|
|||||||
package org.jetbrains.kotlin.backend.konan.lower
|
package org.jetbrains.kotlin.backend.konan.lower
|
||||||
|
|
||||||
import org.jetbrains.kotlin.backend.common.*
|
import org.jetbrains.kotlin.backend.common.*
|
||||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
|
||||||
import org.jetbrains.kotlin.descriptors.ValueDescriptor
|
|
||||||
import org.jetbrains.kotlin.descriptors.VariableDescriptor
|
|
||||||
import org.jetbrains.kotlin.ir.IrElement
|
import org.jetbrains.kotlin.ir.IrElement
|
||||||
import org.jetbrains.kotlin.ir.IrStatement
|
import org.jetbrains.kotlin.ir.IrStatement
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
|
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrFunction
|
import org.jetbrains.kotlin.ir.declarations.IrFunction
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrVariable
|
import org.jetbrains.kotlin.ir.declarations.IrVariable
|
||||||
import org.jetbrains.kotlin.ir.expressions.IrExpression
|
import org.jetbrains.kotlin.ir.expressions.IrExpression
|
||||||
import org.jetbrains.kotlin.ir.expressions.IrGetValue
|
import org.jetbrains.kotlin.ir.expressions.IrGetValue
|
||||||
import org.jetbrains.kotlin.ir.expressions.IrSetVariable
|
import org.jetbrains.kotlin.ir.expressions.IrSetVariable
|
||||||
import org.jetbrains.kotlin.ir.expressions.IrValueAccessExpression
|
import org.jetbrains.kotlin.ir.expressions.IrValueAccessExpression
|
||||||
|
import org.jetbrains.kotlin.ir.symbols.IrValueSymbol
|
||||||
|
import org.jetbrains.kotlin.ir.symbols.IrVariableSymbol
|
||||||
import org.jetbrains.kotlin.ir.visitors.*
|
import org.jetbrains.kotlin.ir.visitors.*
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
@@ -39,7 +37,7 @@ class SharedVariablesLowering(val context: BackendContext) : FunctionLoweringPas
|
|||||||
}
|
}
|
||||||
|
|
||||||
private inner class SharedVariablesTransformer(val irFunction: IrFunction) {
|
private inner class SharedVariablesTransformer(val irFunction: IrFunction) {
|
||||||
val sharedVariables = mutableSetOf<ValueDescriptor>()
|
val sharedVariables = mutableSetOf<IrVariable>()
|
||||||
|
|
||||||
fun lowerSharedVariables() {
|
fun lowerSharedVariables() {
|
||||||
collectSharedVariables()
|
collectSharedVariables()
|
||||||
@@ -50,8 +48,8 @@ class SharedVariablesLowering(val context: BackendContext) : FunctionLoweringPas
|
|||||||
|
|
||||||
private fun collectSharedVariables() {
|
private fun collectSharedVariables() {
|
||||||
irFunction.acceptVoid(object : IrElementVisitorVoid {
|
irFunction.acceptVoid(object : IrElementVisitorVoid {
|
||||||
val relevantVars = mutableSetOf<VariableDescriptor>()
|
val relevantVars = mutableSetOf<IrVariable>()
|
||||||
val functionsVariables = mutableListOf<MutableSet<VariableDescriptor>>()
|
val functionsVariables = mutableListOf<MutableSet<IrVariable>>()
|
||||||
|
|
||||||
override fun visitElement(element: IrElement) {
|
override fun visitElement(element: IrElement) {
|
||||||
element.acceptChildrenVoid(this)
|
element.acceptChildrenVoid(this)
|
||||||
@@ -66,39 +64,38 @@ class SharedVariablesLowering(val context: BackendContext) : FunctionLoweringPas
|
|||||||
override fun visitVariable(declaration: IrVariable) {
|
override fun visitVariable(declaration: IrVariable) {
|
||||||
declaration.acceptChildrenVoid(this)
|
declaration.acceptChildrenVoid(this)
|
||||||
|
|
||||||
val variableDescriptor = declaration.descriptor
|
if (declaration.isVar) {
|
||||||
if (variableDescriptor.isVar) {
|
relevantVars.add(declaration)
|
||||||
relevantVars.add(variableDescriptor)
|
functionsVariables.peek()!!.add(declaration)
|
||||||
functionsVariables.peek()!!.add(variableDescriptor)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun visitVariableAccess(expression: IrValueAccessExpression) {
|
override fun visitVariableAccess(expression: IrValueAccessExpression) {
|
||||||
expression.acceptChildrenVoid(this)
|
expression.acceptChildrenVoid(this)
|
||||||
|
|
||||||
val descriptor = expression.descriptor
|
val value = expression.symbol.owner
|
||||||
//if (descriptor in relevantVars && descriptor.containingDeclaration != currentDeclaration) {
|
//if (descriptor in relevantVars && descriptor.containingDeclaration != currentDeclaration) {
|
||||||
if (descriptor in relevantVars && !functionsVariables.peek()!!.contains(descriptor)) {
|
// TODO: fix lowerings to match check `(value as IrVariable).parent != currentDeclaration`
|
||||||
sharedVariables.add(descriptor)
|
if (value in relevantVars && !functionsVariables.peek()!!.contains(value)) {
|
||||||
|
sharedVariables.add(value as IrVariable)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun rewriteSharedVariables() {
|
private fun rewriteSharedVariables() {
|
||||||
val transformedDescriptors = HashMap<ValueDescriptor, VariableDescriptor>()
|
val transformedVariableSymbols = HashMap<IrValueSymbol, IrVariableSymbol>()
|
||||||
|
|
||||||
irFunction.transformChildrenVoid(object : IrElementTransformerVoid() {
|
irFunction.transformChildrenVoid(object : IrElementTransformerVoid() {
|
||||||
override fun visitVariable(declaration: IrVariable): IrStatement {
|
override fun visitVariable(declaration: IrVariable): IrStatement {
|
||||||
declaration.transformChildrenVoid(this)
|
declaration.transformChildrenVoid(this)
|
||||||
|
|
||||||
val oldDescriptor = declaration.descriptor
|
if (declaration !in sharedVariables) return declaration
|
||||||
if (oldDescriptor !in sharedVariables) return declaration
|
|
||||||
|
|
||||||
val newDescriptor = context.sharedVariablesManager.createSharedVariableDescriptor(oldDescriptor)
|
val newDeclaration = context.sharedVariablesManager.declareSharedVariable(declaration)
|
||||||
transformedDescriptors[oldDescriptor] = newDescriptor
|
transformedVariableSymbols[declaration.symbol] = newDeclaration.symbol
|
||||||
|
|
||||||
return context.sharedVariablesManager.defineSharedValue(newDescriptor, declaration)
|
return context.sharedVariablesManager.defineSharedValue(declaration, newDeclaration)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -106,23 +103,23 @@ class SharedVariablesLowering(val context: BackendContext) : FunctionLoweringPas
|
|||||||
override fun visitGetValue(expression: IrGetValue): IrExpression {
|
override fun visitGetValue(expression: IrGetValue): IrExpression {
|
||||||
expression.transformChildrenVoid(this)
|
expression.transformChildrenVoid(this)
|
||||||
|
|
||||||
val newDescriptor = getTransformedDescriptor(expression.descriptor) ?: return expression
|
val newSymbol = getTransformedSymbol(expression.symbol) ?: return expression
|
||||||
|
|
||||||
return context.sharedVariablesManager.getSharedValue(newDescriptor, expression)
|
return context.sharedVariablesManager.getSharedValue(newSymbol, expression)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun visitSetVariable(expression: IrSetVariable): IrExpression {
|
override fun visitSetVariable(expression: IrSetVariable): IrExpression {
|
||||||
expression.transformChildrenVoid(this)
|
expression.transformChildrenVoid(this)
|
||||||
|
|
||||||
val newDescriptor = getTransformedDescriptor(expression.descriptor) ?: return expression
|
val newDescriptor = getTransformedSymbol(expression.symbol) ?: return expression
|
||||||
|
|
||||||
return context.sharedVariablesManager.setSharedValue(newDescriptor, expression)
|
return context.sharedVariablesManager.setSharedValue(newDescriptor, expression)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getTransformedDescriptor(oldDescriptor: ValueDescriptor): VariableDescriptor? =
|
private fun getTransformedSymbol(oldSymbol: IrValueSymbol): IrVariableSymbol? =
|
||||||
transformedDescriptors.getOrElse(oldDescriptor) {
|
transformedVariableSymbols.getOrElse(oldSymbol) {
|
||||||
assert(oldDescriptor !in sharedVariables) {
|
assert(oldSymbol.owner !in sharedVariables) {
|
||||||
"Shared variable is not transformed: $oldDescriptor"
|
"Shared variable is not transformed: $oldSymbol"
|
||||||
}
|
}
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user