Concurrency prototype (#453)

* Concurrent executors

* Review feedback
This commit is contained in:
Nikolay Igotti
2017-04-07 15:16:04 +03:00
committed by GitHub
parent 3efbf3ae27
commit 100e4df05d
20 changed files with 584 additions and 39 deletions
@@ -119,7 +119,7 @@ internal interface ContextUtils : RuntimeAware {
val ClassDescriptor.typeInfoPtr: ConstPointer
get() {
return if (isExternal(this)) {
constPointer(externalGlobal(this.typeInfoSymbolName, runtime.typeInfoType))
constPointer(importGlobal(this.typeInfoSymbolName, runtime.typeInfoType))
} else {
context.llvmDeclarations.forClass(this).typeInfo
}
@@ -1287,7 +1287,8 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
assert (!descriptor.isUnit())
return if (codegen.isExternal(descriptor)) {
val llvmType = codegen.getLLVMType(descriptor.defaultType)
codegen.externalGlobal(descriptor.objectInstanceFieldSymbolName, llvmType)
codegen.importGlobal(descriptor.objectInstanceFieldSymbolName, llvmType,
threadLocal = true)
} else {
context.llvmDeclarations.forSingleton(descriptor).instanceFieldRef
}
@@ -296,11 +296,8 @@ private class DeclarationsGeneratorVisitor(override val context: Context) :
} else {
"kobjref:" + qualifyInternalName(descriptor)
}
val instanceFieldRef = LLVMAddGlobal(
context.llvmModule,
getLLVMType(descriptor.defaultType),
symbolName
)!!
val instanceFieldRef = addGlobal(
symbolName, getLLVMType(descriptor.defaultType), threadLocal = true)
return SingletonLlvmDeclarations(instanceFieldRef)
}
@@ -326,7 +323,8 @@ private class DeclarationsGeneratorVisitor(override val context: Context) :
// Fields are module-private, so we use internal name:
val name = "kvar:" + qualifyInternalName(descriptor)
val storage = LLVMAddGlobal(context.llvmModule, getLLVMType(descriptor.type), name)!!
val storage = addGlobal(
name, getLLVMType(descriptor.type), threadLocal = true)
this.staticFields[descriptor] = StaticFieldLlvmDeclarations(storage)
}
@@ -182,14 +182,29 @@ internal fun getGlobalType(ptrToGlobal: LLVMValueRef): LLVMTypeRef {
return LLVMGetElementType(ptrToGlobal.type)!!
}
internal fun ContextUtils.externalGlobal(name: String, type: LLVMTypeRef): LLVMValueRef {
internal fun ContextUtils.addGlobal(name: String, type: LLVMTypeRef,
threadLocal: Boolean = false): LLVMValueRef {
assert(LLVMGetNamedGlobal(context.llvmModule, name) == null)
val result = LLVMAddGlobal(context.llvmModule, type, name)!!
if (threadLocal)
LLVMSetThreadLocalMode(result, LLVMThreadLocalMode.LLVMLocalExecTLSModel)
return result
}
internal fun ContextUtils.importGlobal(name: String, type: LLVMTypeRef,
threadLocal: Boolean = false): LLVMValueRef {
val found = LLVMGetNamedGlobal(context.llvmModule, name)
if (found != null) {
assert (getGlobalType(found) == type)
assert (LLVMGetInitializer(found) == null)
if (threadLocal)
assert(LLVMGetThreadLocalMode(found) == LLVMThreadLocalMode.LLVMLocalExecTLSModel)
return found
} else {
return LLVMAddGlobal(context.llvmModule, type, name)!!
val result = LLVMAddGlobal(context.llvmModule, type, name)!!
if (threadLocal)
LLVMSetThreadLocalMode(result, LLVMThreadLocalMode.LLVMLocalExecTLSModel)
return result
}
}
@@ -72,7 +72,7 @@ internal class RTTIGenerator(override val context: Context) : ContextUtils {
if (annot != null) {
val nameValue = annot.allValueArguments.values.single() as StringValue
// TODO: use LLVMAddAlias?
val global = LLVMAddGlobal(context.llvmModule, pointerType(runtime.typeInfoType), nameValue.value)
val global = addGlobal(nameValue.value, pointerType(runtime.typeInfoType))
LLVMSetInitializer(global, typeInfoGlobal)
}
}
@@ -42,6 +42,7 @@ internal class StaticData(override val context: Context): ContextUtils {
throw IllegalArgumentException("Global '$name' already exists")
}
// Globals created with this API are *not* thread local.
val llvmGlobal = LLVMAddGlobal(module, type, name)!!
if (!isExported) {
@@ -158,7 +158,7 @@ internal val ContextUtils.theUnitInstanceRef: ConstPointer
get() {
val unitDescriptor = context.builtIns.unit
return if (isExternal(unitDescriptor)) {
constPointer(externalGlobal(theUnitInstanceName, context.llvm.runtime.objHeaderType))
constPointer(importGlobal(theUnitInstanceName, context.llvm.runtime.objHeaderType))
} else {
context.llvmDeclarations.getUnitInstanceRef()
}