Workers draft (#655)
This commit is contained in:
+7
@@ -90,6 +90,13 @@ internal class InteropBuiltIns(builtIns: KonanBuiltIns) {
|
||||
|
||||
val staticCFunction = packageScope.getContributedFunctions("staticCFunction").toSet()
|
||||
|
||||
val workerPackageScope = builtIns.builtInsModule.getPackage(FqName("konan.worker")).memberScope
|
||||
|
||||
val scheduleFunction = (workerPackageScope.getContributedClassifier("Worker") as ClassDescriptor).
|
||||
unsubstitutedMemberScope.getContributedFunctions("schedule").single()
|
||||
|
||||
val scheduleImplFunction = workerPackageScope.getContributedFunctions("scheduleImpl").single()
|
||||
|
||||
val signExtend = packageScope.getContributedFunctions("signExtend").single()
|
||||
|
||||
val narrow = packageScope.getContributedFunctions("narrow").single()
|
||||
|
||||
+1
@@ -64,4 +64,5 @@ internal class KonanSymbols(context: Context, val symbolTable: SymbolTable): Sym
|
||||
}.toMap()
|
||||
|
||||
|
||||
val scheduleImpl = symbolTable.referenceSimpleFunction(context.interopBuiltIns.scheduleImplFunction)
|
||||
}
|
||||
+52
-9
@@ -26,6 +26,9 @@ import org.jetbrains.kotlin.backend.common.lower.*
|
||||
import org.jetbrains.kotlin.backend.konan.Context
|
||||
import org.jetbrains.kotlin.backend.konan.descriptors.synthesizedName
|
||||
import org.jetbrains.kotlin.backend.common.ir.createFakeOverrideDescriptor
|
||||
import org.jetbrains.kotlin.backend.common.ir.ir2string
|
||||
import org.jetbrains.kotlin.backend.common.pop
|
||||
import org.jetbrains.kotlin.backend.common.push
|
||||
import org.jetbrains.kotlin.backend.konan.llvm.functionName
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.descriptors.annotations.Annotations
|
||||
@@ -33,15 +36,14 @@ import org.jetbrains.kotlin.descriptors.impl.ClassConstructorDescriptorImpl
|
||||
import org.jetbrains.kotlin.descriptors.impl.ClassDescriptorImpl
|
||||
import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl
|
||||
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
|
||||
import org.jetbrains.kotlin.ir.IrElement
|
||||
import org.jetbrains.kotlin.ir.IrStatement
|
||||
import org.jetbrains.kotlin.ir.builders.*
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.IrClassImpl
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.IrConstructorImpl
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl
|
||||
import org.jetbrains.kotlin.ir.expressions.IrCall
|
||||
import org.jetbrains.kotlin.ir.expressions.IrConstKind
|
||||
import org.jetbrains.kotlin.ir.expressions.IrExpression
|
||||
import org.jetbrains.kotlin.ir.expressions.IrFunctionReference
|
||||
import org.jetbrains.kotlin.ir.expressions.*
|
||||
import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl
|
||||
import org.jetbrains.kotlin.ir.expressions.impl.IrDelegatingConstructorCallImpl
|
||||
import org.jetbrains.kotlin.ir.expressions.impl.IrInstanceInitializerCallImpl
|
||||
@@ -52,6 +54,7 @@ import org.jetbrains.kotlin.ir.symbols.IrValueParameterSymbol
|
||||
import org.jetbrains.kotlin.ir.symbols.impl.IrConstructorSymbolImpl
|
||||
import org.jetbrains.kotlin.ir.symbols.impl.IrSimpleFunctionSymbolImpl
|
||||
import org.jetbrains.kotlin.ir.util.*
|
||||
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
|
||||
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
@@ -68,16 +71,56 @@ internal class CallableReferenceLowering(val context: Context): FileLoweringPass
|
||||
override fun lower(irFile: IrFile) {
|
||||
irFile.transformChildrenVoid(object: IrElementTransformerVoidWithContext() {
|
||||
|
||||
override fun visitCall(expression: IrCall): IrExpression {
|
||||
if (expression.descriptor.original in context.interopBuiltIns.staticCFunction) {
|
||||
return expression
|
||||
}
|
||||
return super.visitCall(expression)
|
||||
private val stack = mutableListOf<IrElement>()
|
||||
|
||||
override fun visitElement(element: IrElement): IrElement {
|
||||
stack.push(element)
|
||||
val result = super.visitElement(element)
|
||||
stack.pop()
|
||||
return result
|
||||
}
|
||||
|
||||
override fun visitExpression(expression: IrExpression): IrExpression {
|
||||
stack.push(expression)
|
||||
val result = super.visitExpression(expression)
|
||||
stack.pop()
|
||||
return result
|
||||
}
|
||||
|
||||
override fun visitDeclaration(declaration: IrDeclaration): IrStatement {
|
||||
stack.push(declaration)
|
||||
val result = super.visitDeclaration(declaration)
|
||||
stack.pop()
|
||||
return result
|
||||
}
|
||||
|
||||
override fun visitSpreadElement(spread: IrSpreadElement): IrSpreadElement {
|
||||
stack.push(spread)
|
||||
val result = super.visitSpreadElement(spread)
|
||||
stack.pop()
|
||||
return result
|
||||
}
|
||||
|
||||
override fun visitFunctionReference(expression: IrFunctionReference): IrExpression {
|
||||
expression.transformChildrenVoid(this)
|
||||
|
||||
for (i in stack.size - 1 downTo 0) {
|
||||
val cur = stack[i]
|
||||
if (cur is IrBlock)
|
||||
continue
|
||||
if (cur !is IrCall)
|
||||
break
|
||||
val argument = if (i < stack.size - 1) stack[i + 1] else expression
|
||||
val descriptor = cur.descriptor
|
||||
val argumentDescriptor = descriptor.valueParameters.singleOrNull {
|
||||
cur.getValueArgument(it.index) == argument
|
||||
}
|
||||
if (argumentDescriptor != null && argumentDescriptor.annotations.findAnnotation(FqName("konan.VolatileLambda")) != null) {
|
||||
return expression
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
if (!expression.type.isFunctionOrKFunctionType) {
|
||||
// Not a subject of this lowering.
|
||||
return expression
|
||||
|
||||
+26
@@ -179,6 +179,32 @@ private class InteropTransformer(val context: Context, val irFile: IrFile) : IrB
|
||||
typeArguments = null)
|
||||
}
|
||||
|
||||
interop.scheduleFunction -> {
|
||||
val irCallableReference = unwrapStaticFunctionArgument(expression.getValueArgument(2)!!)
|
||||
|
||||
if (irCallableReference == null || irCallableReference.getArguments().isNotEmpty()) {
|
||||
context.reportCompilationError(
|
||||
"${descriptor.fqNameSafe} must take an unbound, non-capturing function or lambda",
|
||||
irFile, expression
|
||||
)
|
||||
}
|
||||
|
||||
val targetSymbol = irCallableReference.symbol
|
||||
val target = targetSymbol.descriptor
|
||||
val jobPointer = IrFunctionReferenceImpl(
|
||||
builder.startOffset, builder.endOffset,
|
||||
interop.cPointer.defaultType,
|
||||
targetSymbol, target,
|
||||
typeArguments = null)
|
||||
|
||||
builder.irCall(symbols.scheduleImpl).apply {
|
||||
putValueArgument(0, expression.dispatchReceiver)
|
||||
putValueArgument(1, expression.getValueArgument(0))
|
||||
putValueArgument(2, expression.getValueArgument(1))
|
||||
putValueArgument(3, jobPointer)
|
||||
}
|
||||
}
|
||||
|
||||
interop.signExtend, interop.narrow -> {
|
||||
|
||||
val integerTypePredicates = arrayOf(
|
||||
|
||||
@@ -425,6 +425,21 @@ task empty_substring(type: RunKonanTest) {
|
||||
source = "runtime/basic/empty_substring.kt"
|
||||
}
|
||||
|
||||
task worker0(type: RunKonanTest) {
|
||||
goldValue = "Got Input processed\nOK\n"
|
||||
source = "runtime/workers/worker0.kt"
|
||||
}
|
||||
|
||||
task worker1(type: RunKonanTest) {
|
||||
goldValue = "OK\n"
|
||||
source = "runtime/workers/worker1.kt"
|
||||
}
|
||||
|
||||
task worker2(type: RunKonanTest) {
|
||||
goldValue = "OK\n"
|
||||
source = "runtime/workers/worker2.kt"
|
||||
}
|
||||
|
||||
task superFunCall(type: RunKonanTest) {
|
||||
goldValue = "<fun:C><fun:C1>\n<fun:C><fun:C3>\n"
|
||||
source = "codegen/basics/superFunCall.kt"
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import konan.worker.*
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val worker = startWorker()
|
||||
val future = worker.schedule(TransferMode.CHECKED, { "Input".shallowCopy()}) {
|
||||
input -> input + " processed"
|
||||
}
|
||||
future.consume {
|
||||
result -> println("Got $result")
|
||||
}
|
||||
worker.requestTermination().consume { _ -> }
|
||||
println("OK")
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import konan.worker.*
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val COUNT = 5
|
||||
val workers = Array(COUNT, { _ -> startWorker()})
|
||||
|
||||
for (attempt in 1 .. 3) {
|
||||
val futures = Array(workers.size,
|
||||
{ i -> workers[i].schedule(TransferMode.CHECKED, { "$attempt: Input $i".shallowCopy() })
|
||||
{ input -> input + " processed" }
|
||||
})
|
||||
futures.forEachIndexed { index, future ->
|
||||
future.consume {
|
||||
result ->
|
||||
if ("$attempt: Input $index processed" != result) {
|
||||
println("Got unexpected $result")
|
||||
throw Error(result)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
workers.forEach {
|
||||
it.requestTermination().consume { _ -> }
|
||||
}
|
||||
println("OK")
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import konan.worker.*
|
||||
|
||||
data class WorkerArgument(val intParam: Int, val stringParam: String)
|
||||
data class WorkerResult(val intResult: Int, val stringResult: String)
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val COUNT = 5
|
||||
val workers = Array(COUNT, { _ -> startWorker()})
|
||||
|
||||
for (attempt in 1 .. 3) {
|
||||
val futures = Array(workers.size, { workerIndex -> workers[workerIndex].schedule(TransferMode.CHECKED, {
|
||||
WorkerArgument(workerIndex, "attempt $attempt") }) { input ->
|
||||
var sum = 0
|
||||
for (i in 0..input.intParam * 1000) {
|
||||
sum += i
|
||||
}
|
||||
WorkerResult(sum, input.stringParam + " result")
|
||||
}
|
||||
})
|
||||
val futureSet = futures.toSet()
|
||||
var consumed = 0
|
||||
while (consumed < futureSet.size) {
|
||||
val ready = futureSet.waitForMultipleFutures(10000)
|
||||
ready.forEach {
|
||||
it.consume { result ->
|
||||
if (result.stringResult != "attempt $attempt result") throw Error("Unexpected $result")
|
||||
consumed++ }
|
||||
}
|
||||
}
|
||||
}
|
||||
workers.forEach {
|
||||
it.requestTermination().consume { _ -> }
|
||||
}
|
||||
println("OK")
|
||||
}
|
||||
Reference in New Issue
Block a user