[K/N] Fix gep for local array header

This corrects the type given to GEP2 when initializing a stacklocal
array. This issue only cause problems when bitcode gets written before
any of the optimization passes, in which case it will fail on LLVM
validation when reading the bitcode back in on a subsequent pass.
This commit is contained in:
Troels Bjerre Lund
2023-12-11 14:59:34 +01:00
committed by Space Cloud
parent fab51ff83c
commit f2cb0f1407
@@ -6,10 +6,7 @@
package org.jetbrains.kotlin.backend.konan.llvm package org.jetbrains.kotlin.backend.konan.llvm
import kotlinx.cinterop.cValuesOf import kotlinx.cinterop.*
import kotlinx.cinterop.memScoped
import kotlinx.cinterop.toCValues
import kotlinx.cinterop.toKString
import llvm.* import llvm.*
import org.jetbrains.kotlin.backend.konan.MemoryModel import org.jetbrains.kotlin.backend.konan.MemoryModel
import org.jetbrains.kotlin.backend.konan.NativeGenerationState import org.jetbrains.kotlin.backend.konan.NativeGenerationState
@@ -424,7 +421,7 @@ internal class StackLocalsManagerImpl(
val objectHeader = structGep(type, stackSlot, 0, "objHeader") val objectHeader = structGep(type, stackSlot, 0, "objHeader")
val typeInfo = codegen.typeInfoForAllocation(irClass) val typeInfo = codegen.typeInfoForAllocation(irClass)
setTypeInfoForLocalObject(objectHeader, typeInfo) setTypeInfoForLocalObject(runtime.objHeaderType, objectHeader, typeInfo)
val gcRootSetSlot = createRootSetSlot() val gcRootSetSlot = createRootSetSlot()
StackLocal(null, irClass, stackSlot, objectHeader, gcRootSetSlot) StackLocal(null, irClass, stackSlot, objectHeader, gcRootSetSlot)
} }
@@ -477,7 +474,7 @@ internal class StackLocalsManagerImpl(
val arraySlot = LLVMBuildAlloca(builder, arrayType, "")!! val arraySlot = LLVMBuildAlloca(builder, arrayType, "")!!
// Set array size in ArrayHeader. // Set array size in ArrayHeader.
val arrayHeaderSlot = structGep(arrayType, arraySlot, 0, "arrayHeader") val arrayHeaderSlot = structGep(arrayType, arraySlot, 0, "arrayHeader")
setTypeInfoForLocalObject(arrayHeaderSlot, typeInfo) setTypeInfoForLocalObject(runtime.arrayHeaderType, arrayHeaderSlot, typeInfo)
val sizeField = structGep(runtime.arrayHeaderType, arrayHeaderSlot, 1, "count_") val sizeField = structGep(runtime.arrayHeaderType, arrayHeaderSlot, 1, "count_")
store(count, sizeField) store(count, sizeField)
@@ -542,8 +539,8 @@ internal class StackLocalsManagerImpl(
} }
} }
private fun setTypeInfoForLocalObject(objectHeader: LLVMValueRef, typeInfoPointer: LLVMValueRef) = with(functionGenerationContext) { private fun setTypeInfoForLocalObject(headerType: LLVMTypeRef, header: LLVMValueRef, typeInfoPointer: LLVMValueRef) = with(functionGenerationContext) {
val typeInfo = structGep(runtime.objHeaderType, objectHeader, 0, "typeInfoOrMeta_") val typeInfo = structGep(headerType, header, 0, "typeInfoOrMeta_")
// Set tag OBJECT_TAG_PERMANENT_CONTAINER | OBJECT_TAG_NONTRIVIAL_CONTAINER. // Set tag OBJECT_TAG_PERMANENT_CONTAINER | OBJECT_TAG_NONTRIVIAL_CONTAINER.
val typeInfoValue = intToPtr(or(ptrToInt(typeInfoPointer, codegen.intPtrType), val typeInfoValue = intToPtr(or(ptrToInt(typeInfoPointer, codegen.intPtrType),
codegen.immThreeIntPtrType), kTypeInfoPtr) codegen.immThreeIntPtrType), kTypeInfoPtr)