[K/N] Intrinsics for atomic get/set of volatile properties.
These intrinsics are equivalent to KMutableProperty0.get/set invocation and used internally to optimize allocation of a property reference. Merge-request: KT-MR-11233 Merged-by: Maria Sokolova <maria.sokolova@jetbrains.com>
This commit is contained in:
+4
@@ -94,6 +94,8 @@ internal enum class IntrinsicType {
|
||||
// Worker
|
||||
WORKER_EXECUTE,
|
||||
// Atomics
|
||||
ATOMIC_GET_FIELD,
|
||||
ATOMIC_SET_FIELD,
|
||||
COMPARE_AND_SET_FIELD,
|
||||
COMPARE_AND_EXCHANGE_FIELD,
|
||||
GET_AND_SET_FIELD,
|
||||
@@ -263,6 +265,8 @@ internal class IntrinsicGenerator(private val environment: IntrinsicGeneratorEnv
|
||||
IntrinsicType.INTEROP_MEMORY_COPY -> emitMemoryCopy(callSite, args)
|
||||
IntrinsicType.IS_EXPERIMENTAL_MM -> emitIsExperimentalMM()
|
||||
IntrinsicType.THE_UNIT_INSTANCE -> theUnitInstanceRef.llvm
|
||||
IntrinsicType.ATOMIC_GET_FIELD -> reportNonLoweredIntrinsic(intrinsicType)
|
||||
IntrinsicType.ATOMIC_SET_FIELD -> reportNonLoweredIntrinsic(intrinsicType)
|
||||
IntrinsicType.COMPARE_AND_SET -> emitCompareAndSet(callSite, args)
|
||||
IntrinsicType.COMPARE_AND_EXCHANGE -> emitCompareAndSwap(callSite, args, resultSlot)
|
||||
IntrinsicType.GET_AND_SET -> emitGetAndSet(callSite, args, resultSlot)
|
||||
|
||||
+12
-5
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.ir.builders.declarations.buildFun
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
import org.jetbrains.kotlin.ir.expressions.*
|
||||
import org.jetbrains.kotlin.ir.symbols.IrFieldSymbol
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl
|
||||
import org.jetbrains.kotlin.ir.symbols.IrReturnableBlockSymbol
|
||||
import org.jetbrains.kotlin.ir.types.IrType
|
||||
import org.jetbrains.kotlin.ir.util.*
|
||||
@@ -28,6 +29,7 @@ import org.jetbrains.kotlin.ir.util.irCall
|
||||
import org.jetbrains.kotlin.ir.visitors.*
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.util.capitalizeDecapitalize.*
|
||||
import org.jetbrains.kotlin.ir.util.*
|
||||
|
||||
object IR_DECLARATION_ORIGIN_VOLATILE : IrDeclarationOriginImpl("VOLATILE")
|
||||
|
||||
@@ -219,7 +221,9 @@ internal class VolatileFieldsLowering(val context: Context) : FileLoweringPass {
|
||||
|
||||
override fun visitCall(expression: IrCall): IrExpression {
|
||||
expression.transformChildrenVoid(this)
|
||||
val intrinsicType = tryGetIntrinsicType(expression).takeIf { it in intrinsicMap } ?: return expression
|
||||
val intrinsicType = tryGetIntrinsicType(expression).takeIf {
|
||||
it in intrinsicMap || it == IntrinsicType.ATOMIC_GET_FIELD || it == IntrinsicType.ATOMIC_SET_FIELD
|
||||
} ?: return expression
|
||||
builder.at(expression)
|
||||
val reference = getConstPropertyReference(expression.extensionReceiver, null)
|
||||
?: return unsupported("Only compile-time known IrProperties supported for $intrinsicType")
|
||||
@@ -231,12 +235,15 @@ internal class VolatileFieldsLowering(val context: Context) : FileLoweringPass {
|
||||
if (backingField?.hasAnnotation(KonanFqNames.volatile) != true) {
|
||||
return unsupported("Only volatile properties are supported for $intrinsicType")
|
||||
}
|
||||
val function = intrinsicMap[intrinsicType]!!(backingField)
|
||||
val function = when(intrinsicType) {
|
||||
IntrinsicType.ATOMIC_GET_FIELD -> property.getter ?: error("Getter is not defined for the property: ${property.render()}")
|
||||
IntrinsicType.ATOMIC_SET_FIELD -> property.setter ?: error("Setter is not defined for the property: ${property.render()}")
|
||||
else -> intrinsicMap[intrinsicType]!!(backingField)
|
||||
}
|
||||
return builder.irCall(function).apply {
|
||||
dispatchReceiver = reference.dispatchReceiver
|
||||
putValueArgument(0, expression.getValueArgument(0))
|
||||
if (intrinsicType == IntrinsicType.COMPARE_AND_SET_FIELD || intrinsicType == IntrinsicType.COMPARE_AND_EXCHANGE_FIELD) {
|
||||
putValueArgument(1, expression.getValueArgument(1))
|
||||
for (index in 0 until expression.valueArgumentsCount) {
|
||||
putValueArgument(index, expression.getValueArgument(index))
|
||||
}
|
||||
}.let {
|
||||
if (backingField.requiresBooleanConversion()) {
|
||||
|
||||
@@ -282,49 +282,88 @@ private fun debugString(value: Any?): String {
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares the value of the field referenced by [this] to [expectedValue], and if they are equal,
|
||||
* atomically replaces it with [newValue].
|
||||
* Atomically gets the value of the field referenced by [this].
|
||||
*
|
||||
* Provides sequential consistent ordering guarantees.
|
||||
*
|
||||
* This is equivalent to KMutableProperty0#get() invocation and used internally to optimize allocation of a property reference.
|
||||
*
|
||||
* For now, it can be used only within the same file, where property is defined.
|
||||
* Check https://youtrack.jetbrains.com/issue/KT-55426 for details.
|
||||
*
|
||||
* Comparison is done by reference or value depending on field representation.
|
||||
*
|
||||
* If [this] is not a compile-time known reference to the property with [Volatile] annotation [IllegalArgumentException]
|
||||
* would be thrown.
|
||||
*
|
||||
* If property referenced by [this] has nontrivial setter it will not be called.
|
||||
*/
|
||||
@PublishedApi
|
||||
@TypedIntrinsic(IntrinsicType.ATOMIC_GET_FIELD)
|
||||
internal external fun <T> KMutableProperty0<T>.atomicGetField(): T
|
||||
|
||||
/**
|
||||
* Atomically sets the value of the field referenced by [this] to the [new value][newValue].
|
||||
*
|
||||
* Returns true if the actual field value matched [expectedValue]
|
||||
* Provides sequential consistent ordering guarantees.
|
||||
*
|
||||
* This is equivalent to KMutableProperty0#set(value: T) invocation and used internally to optimize allocation of a property reference.
|
||||
*
|
||||
* For now, it can be used only within the same file, where property is defined.
|
||||
* Check https://youtrack.jetbrains.com/issue/KT-55426 for details.
|
||||
*
|
||||
* If [this] is not a compile-time known reference to the property with [Volatile] annotation [IllegalArgumentException]
|
||||
* would be thrown.
|
||||
*
|
||||
* If property referenced by [this] has nontrivial setter it will not be called.
|
||||
*/
|
||||
@PublishedApi
|
||||
@TypedIntrinsic(IntrinsicType.ATOMIC_SET_FIELD)
|
||||
internal external fun <T> KMutableProperty0<T>.atomicSetField(newValue: T)
|
||||
|
||||
/**
|
||||
* Atomically sets the value of the field referenced by [this] to the [new value][newValue]
|
||||
* if the current value equals the [expected value][expectedValue].
|
||||
* Returns true if the operation was successful and false only if the current value of the field was not equal to the expected value.
|
||||
*
|
||||
* Comparison is done by reference or value depending on field representation.
|
||||
*
|
||||
* Provides sequential consistent ordering guarantees and never fails spuriously.
|
||||
*
|
||||
* For now, it can be used only within the same file, where property is defined.
|
||||
* Check https://youtrack.jetbrains.com/issue/KT-55426 for details.
|
||||
*
|
||||
* If [this] is not a compile-time known reference to the property with [Volatile] annotation [IllegalArgumentException]
|
||||
* would be thrown.
|
||||
*
|
||||
* If property referenced by [this] has nontrivial setter it will not be called.
|
||||
*/
|
||||
@PublishedApi
|
||||
@TypedIntrinsic(IntrinsicType.COMPARE_AND_SET_FIELD)
|
||||
internal external fun <T> KMutableProperty0<T>.compareAndSetField(expectedValue: T, newValue: T): Boolean
|
||||
|
||||
/**
|
||||
* Compares the value of the field referenced by [this] to [expectedValue], and if they are equal,
|
||||
* atomically replaces it with [newValue].
|
||||
* Atomically sets the value of the field referenced by [this] to the [new value][newValue]
|
||||
* if the current value equals the [expected value][expectedValue] and returns the old value of the field in any case.
|
||||
*
|
||||
* Comparison is done by reference or value depending on field representation.
|
||||
*
|
||||
* Provides sequential consistent ordering guarantees and never fails spuriously.
|
||||
*
|
||||
* For now, it can be used only within the same file, where property is defined.
|
||||
* Check https://youtrack.jetbrains.com/issue/KT-55426 for details.
|
||||
*
|
||||
* Comparison is done by reference or value depending on field representation.
|
||||
*
|
||||
* If [this] is not a compile-time known reference to the property with [Volatile] annotation [IllegalArgumentException]
|
||||
* would be thrown.
|
||||
*
|
||||
* If property referenced by [this] has nontrivial setter it will not be called.
|
||||
*
|
||||
* Returns the field value before operation.
|
||||
*
|
||||
*/
|
||||
@PublishedApi
|
||||
@TypedIntrinsic(IntrinsicType.COMPARE_AND_EXCHANGE_FIELD)
|
||||
internal external fun <T> KMutableProperty0<T>.compareAndExchangeField(expectedValue: T, newValue: T): T
|
||||
|
||||
/**
|
||||
* Atomically sets value of the field referenced by [this] to [newValue] and returns old field value.
|
||||
* Atomically sets the value of the field referenced by [this] to the [new value][newValue] and returns the old value of the field.
|
||||
*
|
||||
* Provides sequential consistent ordering guarantees.
|
||||
*
|
||||
* For now, it can be used only within the same file, where property is defined.
|
||||
* Check https://youtrack.jetbrains.com/issue/KT-55426 for details.
|
||||
@@ -338,9 +377,10 @@ internal external fun <T> KMutableProperty0<T>.compareAndExchangeField(expectedV
|
||||
@TypedIntrinsic(IntrinsicType.GET_AND_SET_FIELD)
|
||||
internal external fun <T> KMutableProperty0<T>.getAndSetField(newValue: T): T
|
||||
|
||||
|
||||
/**
|
||||
* Atomically increments value of the field referenced by [this] by [delta] and returns old field value.
|
||||
* Atomically adds the given [delta] to the value of the field referenced by [this] and returns the old value of the field.
|
||||
*
|
||||
* Provides sequential consistent ordering guarantees.
|
||||
*
|
||||
* For now, it can be used only within the same file, where property is defined.
|
||||
* Check https://youtrack.jetbrains.com/issue/KT-55426 for details.
|
||||
@@ -355,7 +395,9 @@ internal external fun <T> KMutableProperty0<T>.getAndSetField(newValue: T): T
|
||||
internal external fun KMutableProperty0<Short>.getAndAddField(delta: Short): Short
|
||||
|
||||
/**
|
||||
* Atomically increments value of the field referenced by [this] by [delta] and returns old field value.
|
||||
* Atomically adds the given [delta] to the value of the field referenced by [this] and returns the old value of the field.
|
||||
*
|
||||
* Provides sequential consistent ordering guarantees.
|
||||
*
|
||||
* For now, it can be used only within the same file, where property is defined.
|
||||
* Check https://youtrack.jetbrains.com/issue/KT-55426 for details.
|
||||
@@ -370,7 +412,9 @@ internal external fun KMutableProperty0<Short>.getAndAddField(delta: Short): Sho
|
||||
internal external fun KMutableProperty0<Int>.getAndAddField(newValue: Int): Int
|
||||
|
||||
/**
|
||||
* Atomically increments value of the field referenced by [this] by [delta] and returns old field value.
|
||||
* Atomically adds the given [delta] to the value of the field referenced by [this] and returns the old value of the field.
|
||||
*
|
||||
* Provides sequential consistent ordering guarantees.
|
||||
*
|
||||
* For now, it can be used only within the same file, where property is defined.
|
||||
* Check https://youtrack.jetbrains.com/issue/KT-55426 for details.
|
||||
@@ -385,7 +429,9 @@ internal external fun KMutableProperty0<Int>.getAndAddField(newValue: Int): Int
|
||||
internal external fun KMutableProperty0<Long>.getAndAddField(newValue: Long): Long
|
||||
|
||||
/**
|
||||
* Atomically increments value of the field referenced by [this] by [delta] and returns old field value.
|
||||
* Atomically adds the given [delta] to the value of the field referenced by [this] and returns the old value of the field.
|
||||
*
|
||||
* Provides sequential consistent ordering guarantees.
|
||||
*
|
||||
* For now, it can be used only within the same file, where property is defined.
|
||||
* Check https://youtrack.jetbrains.com/issue/KT-55426 for details.
|
||||
|
||||
@@ -83,6 +83,8 @@ internal class IntrinsicType {
|
||||
const val WORKER_EXECUTE = "WORKER_EXECUTE"
|
||||
|
||||
// Atomic
|
||||
const val ATOMIC_GET_FIELD = "ATOMIC_GET_FIELD"
|
||||
const val ATOMIC_SET_FIELD = "ATOMIC_SET_FIELD"
|
||||
const val COMPARE_AND_SET_FIELD = "COMPARE_AND_SET_FIELD"
|
||||
const val COMPARE_AND_EXCHANGE_FIELD = "COMPARE_AND_EXCHANGE_FIELD"
|
||||
const val GET_AND_SET_FIELD = "GET_AND_SET_FIELD"
|
||||
|
||||
Reference in New Issue
Block a user