[Native] Move remaining usages for LLVMBuildLoad to LLVMBuildLoad2.

Step towards LLVM opaque pointers.
This commit is contained in:
Mads Ager
2023-10-12 15:16:10 +02:00
committed by Space Team
parent 5b9a4b289f
commit bdfb3c3ac9
7 changed files with 33 additions and 18 deletions
@@ -128,7 +128,7 @@ private class CallsChecker(generationState: NativeGenerationState, goodFunctions
calledName = null
val superStruct = LLVMGetArgOperand(call, 0)
val superClassPtrPtr = LLVMBuildGEP(builder, superStruct, listOf(llvm.int32(0), llvm.int32(1)).toCValues(), 2, "")
val superClassPtr = LLVMBuildLoad(builder, superClassPtrPtr, "")!!
val superClassPtr = LLVMBuildLoad2(builder, llvm.int8PtrType, superClassPtrPtr, "")!!
val classPtr = getSuperClass.buildCall(builder, listOf(superClassPtr))
val calledPtrLlvmFunPtr = getMethodImpl.buildCall(builder, listOf(classPtr, LLVMGetArgOperand(call, 1)!!))
calledPtrLlvm = LLVMBuildBitCast(builder, calledPtrLlvmFunPtr, llvm.int8PtrType, "")!!
@@ -725,13 +725,20 @@ internal abstract class FunctionGenerationContext(
return applyMemoryOrderAndAlignment(LLVMBuildLoad2(builder, type, address, name)!!, memoryOrder, alignment)
}
fun loadSlot(address: LLVMValueRef, isVar: Boolean, resultSlot: LLVMValueRef? = null, name: String = "",
memoryOrder: LLVMAtomicOrdering? = null, alignment: Int? = null): LLVMValueRef {
val value = LLVMBuildLoad(builder, address, name)!!
fun loadSlot(
type: LLVMTypeRef,
address: LLVMValueRef,
isVar: Boolean,
resultSlot: LLVMValueRef? = null,
name: String = "",
memoryOrder: LLVMAtomicOrdering? = null,
alignment: Int? = null
): LLVMValueRef {
val value = LLVMBuildLoad2(builder, type, address, name)!!
memoryOrder?.let { LLVMSetOrdering(value, it) }
alignment?.let { LLVMSetAlignment(value, it) }
if (isObjectRef(value) && isVar) {
val slot = resultSlot ?: alloca(LLVMTypeOf(value), variableLocation = null)
if (isObjectType(type) && isVar) {
val slot = resultSlot ?: alloca(type, variableLocation = null)
storeStackRef(value, slot)
}
return value
@@ -419,7 +419,7 @@ internal class IntrinsicGenerator(private val environment: IntrinsicGeneratorEnv
private fun FunctionGenerationContext.emitAtomicGetArrayElement(callSite: IrCall, args: List<LLVMValueRef>, resultSlot: LLVMValueRef?): LLVMValueRef {
require(args.size == 2) { "The call to ${callSite.symbol.owner.name.asString()} expects 2 value arguments." }
val address = arrayGetElementAddress(callSite, args[0], args[1])
return loadSlot(address, isVar = true, resultSlot, memoryOrder = LLVMAtomicOrdering.LLVMAtomicOrderingSequentiallyConsistent)
return loadSlot(callSite.llvmReturnType, address, isVar = true, resultSlot, memoryOrder = LLVMAtomicOrdering.LLVMAtomicOrderingSequentiallyConsistent)
}
private fun FunctionGenerationContext.transformArgsForAtomicArray(callSite: IrCall, args: List<LLVMValueRef>): List<LLVMValueRef> {
@@ -1785,7 +1785,10 @@ internal class CodeGeneratorVisitor(
}
}
return functionGenerationContext.loadSlot(
fieldAddress, !value.symbol.owner.isFinal, resultSlot,
value.type.toLLVMType(llvm),
fieldAddress,
!value.symbol.owner.isFinal,
resultSlot,
memoryOrder = order,
alignment = alignment
)
@@ -2938,7 +2941,7 @@ internal class CodeGeneratorVisitor(
val bbNeedInit = basicBlock("need_init", null)
val value = LLVMBuildLoad(builder, initGuard, "")!!
val value = LLVMBuildLoad2(builder, llvm.int32Type, initGuard, "")!!
condBr(icmpEq(value, llvm.kImmInt32Zero), bbNeedInit, bbInited)
appendingTo(bbInited) {
@@ -23,8 +23,9 @@ internal class VariableManager(val functionGenerationContext: FunctionGeneration
fun address() : LLVMValueRef
}
inner class SlotRecord(val address: LLVMValueRef, val refSlot: Boolean, val isVar: Boolean) : Record {
override fun load(resultSlot: LLVMValueRef?) : LLVMValueRef = functionGenerationContext.loadSlot(address, isVar, resultSlot)
inner class SlotRecord(val address: LLVMValueRef, val type: LLVMTypeRef, val isVar: Boolean) : Record {
val refSlot = functionGenerationContext.isObjectType(type)
override fun load(resultSlot: LLVMValueRef?) : LLVMValueRef = functionGenerationContext.loadSlot(type, address, isVar, resultSlot)
override fun store(value: LLVMValueRef) {
functionGenerationContext.storeAny(value, address, true)
}
@@ -32,8 +33,9 @@ internal class VariableManager(val functionGenerationContext: FunctionGeneration
override fun toString() = (if (refSlot) "refslot" else "slot") + " for ${address}"
}
inner class ParameterRecord(val address: LLVMValueRef, val refSlot: Boolean) : Record {
override fun load(resultSlot: LLVMValueRef?): LLVMValueRef = functionGenerationContext.loadSlot(address, false, resultSlot)
inner class ParameterRecord(val address: LLVMValueRef, val type: LLVMTypeRef) : Record {
val refSlot = functionGenerationContext.isObjectType(type)
override fun load(resultSlot: LLVMValueRef?): LLVMValueRef = functionGenerationContext.loadSlot(type, address, false, resultSlot)
override fun store(value: LLVMValueRef) = functionGenerationContext.store(value, address)
override fun address() : LLVMValueRef = this.address
override fun toString() = (if (refSlot) "refslot" else "slot") + " for ${address}"
@@ -79,7 +81,7 @@ internal class VariableManager(val functionGenerationContext: FunctionGeneration
val slot = functionGenerationContext.alloca(type, valueDeclaration.name.asString(), variableLocation)
if (value != null)
functionGenerationContext.storeAny(value, slot, true)
variables.add(SlotRecord(slot, functionGenerationContext.isObjectType(type), isVar))
variables.add(SlotRecord(slot, type, isVar))
contextVariablesToIndex[valueDeclaration] = index
return index
}
@@ -92,7 +94,7 @@ internal class VariableManager(val functionGenerationContext: FunctionGeneration
val slot = functionGenerationContext.alloca(
type, "p-${valueDeclaration.name.asString()}", variableLocation)
val isObject = functionGenerationContext.isObjectType(type)
variables.add(ParameterRecord(slot, isObject))
variables.add(ParameterRecord(slot, type))
contextVariablesToIndex[valueDeclaration] = index
if (isObject)
skipSlots++
@@ -114,7 +116,7 @@ internal class VariableManager(val functionGenerationContext: FunctionGeneration
val slot = functionGenerationContext.alloca(type, variableLocation = null)
if (value != null)
functionGenerationContext.storeAny(value, slot, true)
variables.add(SlotRecord(slot, functionGenerationContext.isObjectType(type), true))
variables.add(SlotRecord(slot, type, true))
return index
}
@@ -41,7 +41,7 @@ internal fun ObjCExportCodeGeneratorBase.generateBlockToKotlinFunctionConverter(
val thisRef = param(0)
val associatedObjectHolder = if (useSeparateHolder) {
val bodyPtr = bitcast(pointerType(bodyType), thisRef)
loadSlot(structGep(bodyPtr, 1), isVar = false)
loadSlot(codegen.kObjHeaderPtr, structGep(bodyPtr, 1), isVar = false)
} else {
thisRef
}
+4 -1
View File
@@ -76,6 +76,8 @@ excludedFunctions.mingw = LLVMDumpType
# Functions from LLVMIntPtrType to LLVMModuleCreateWithName are excluded because they work with the GlobalContext.
# This might not be safe if the compiler is called from a daemon process.
#
# Also exclude the functions that rely on typed pointers as we get rid of them from the code generator.
excludedFunctions = LLVMInitializeAllAsmParsers LLVMInitializeAllAsmPrinters LLVMInitializeAllDisassemblers \
LLVMInitializeAllTargetInfos LLVMInitializeAllTargetMCs LLVMInitializeAllTargets LLVMInitializeNativeTarget \
LLVMInitializeNativeAsmParser LLVMInitializeNativeAsmPrinter LLVMInitializeNativeDisassembler \
@@ -83,6 +85,7 @@ excludedFunctions = LLVMInitializeAllAsmParsers LLVMInitializeAllAsmPrinters LLV
LLVMInt16Type LLVMInt32Type LLVMInt64Type LLVMInt128Type LLVMIntType LLVMHalfType LLVMFloatType LLVMDoubleType \
LLVMX86FP80Type LLVMFP128Type LLVMPPCFP128Type LLVMX86MMXType LLVMStructType LLVMVoidType LLVMLabelType \
LLVMMDString LLVMMDNode LLVMConstString LLVMConstStruct LLVMAppendBasicBlock LLVMInsertBasicBlock LLVMCreateBuilder \
LLVMParseBitcode LLVMParseBitcode2 LLVMGetBitcodeModule LLVMGetBitcodeModule2 LLVMGetGlobalContext LLVMModuleCreateWithName
LLVMParseBitcode LLVMParseBitcode2 LLVMGetBitcodeModule LLVMGetBitcodeModule2 LLVMGetGlobalContext LLVMModuleCreateWithName \
LLVMBuildLoad
strictEnums = LLVMIntPredicate LLVMOpcode LLVMDLLStorageClass LLVMCallConv LLVMThreadLocalMode LLVMAtomicOrdering