From 56d1881f180b2cbcce1f9307ddc144ccb9b245bd Mon Sep 17 00:00:00 2001 From: Vasily Levchenko Date: Fri, 12 Jan 2018 14:19:49 +0300 Subject: [PATCH] [codegen][debug info] debug info generation do not touch llvm builder in the most cases. --- .../backend/konan/llvm/CodeGenerator.kt | 34 +++++++++---------- .../kotlin/backend/konan/llvm/IrToBitcode.kt | 22 ++++++------ llvmDebugInfoC/src/main/cpp/DebugInfoC.cpp | 17 +++++++--- llvmDebugInfoC/src/main/include/DebugInfoC.h | 4 ++- 4 files changed, 42 insertions(+), 35 deletions(-) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt index 36a2b8c1321..6aa53311689 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt @@ -77,6 +77,11 @@ internal class CodeGenerator(override val context: Context) : ContextUtils { } return typeInfoValue(descriptorForTypeInfo) } + + fun generateLocationInfo(locationInfo: LocationInfo): DILocationRef? { + return LLVMCreateLocation(LLVMGetModuleContext(context.llvmModule), locationInfo.line, locationInfo.line, locationInfo.scope) + } + } internal sealed class ExceptionHandler { @@ -507,11 +512,9 @@ internal class FunctionGenerationContext(val function: LLVMValueRef, internal fun debugLocation(locationInfo: LocationInfo): DILocationRef? { if (!context.shouldContainDebugInfo()) return null update(currentBlock, locationInfo) - return LLVMBuilderSetDebugLocation( - builder, - locationInfo.line, - locationInfo.column, - locationInfo.scope) + val debugLocation = codegen.generateLocationInfo(locationInfo) + currentPositionHolder.setBuilderDebugLocation(debugLocation) + return debugLocation } fun indirectBr(address: LLVMValueRef, destinations: Collection): LLVMValueRef? { @@ -624,8 +627,7 @@ internal class FunctionGenerationContext(val function: LLVMValueRef, fun resetDebugLocation() { if (!context.shouldContainDebugInfo()) return - if (!currentPositionHolder.isAfterTerminator) - LLVMBuilderResetDebugLocation(builder) + currentPositionHolder.resetBuilderDebugLocation() } private fun position() = basicBlockToLastLocation[currentBlock] @@ -791,7 +793,7 @@ internal class FunctionGenerationContext(val function: LLVMValueRef, fun positionAtEnd(block: LLVMBasicBlockRef) { LLVMPositionBuilderAtEnd(builder, block) - basicBlockToLastLocation[block]?.let(this@PositionHolder::debugLocation) + basicBlockToLastLocation[block]?.let{ debugLocation(it) } val lastInstr = LLVMGetLastInstruction(block) isAfterTerminator = lastInstr != null && (LLVMIsATerminatorInst(lastInstr) != null) } @@ -800,19 +802,15 @@ internal class FunctionGenerationContext(val function: LLVMValueRef, LLVMDisposeBuilder(builder) } - fun debugLocation(locationInfo: LocationInfo): DILocationRef? { - if (!context.shouldContainDebugInfo()) return null - return LLVMBuilderSetDebugLocation( - builder, - locationInfo.line, - locationInfo.column, - locationInfo.scope) - } - - fun resetDebugLocation() { + fun resetBuilderDebugLocation() { if (!context.shouldContainDebugInfo()) return LLVMBuilderResetDebugLocation(builder) } + + fun setBuilderDebugLocation(debugLocation: DILocationRef?) { + if (!context.shouldContainDebugInfo()) return + LLVMBuilderSetDebugLocation(builder, debugLocation) + } } private var currentPositionHolder: PositionHolder = PositionHolder() diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt index afea1bdb7c9..61c85439f54 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt @@ -683,7 +683,7 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map return evaluateTypeOperator (value) is IrCall -> return evaluateCall (value) @@ -1101,12 +1101,12 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map debugInfoLocalVariableLocation( builder = context.debugInfo.builder, @@ -1114,7 +1114,7 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map debugInfoParameterLocation( builder = context.debugInfo.builder, @@ -1123,7 +1123,7 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map throw Error("Unsupported element type: ${ir2string(element)}") } @@ -1616,7 +1616,7 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map return delegatingConstructorCall(value.descriptor, args) @@ -1630,7 +1630,7 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: MapgetContext(), line, col, sp, nullptr); - llvmBuilder->SetCurrentDebugLocation(location); + auto location = llvm::DILocation::get(*llvm::unwrap(contextRef), line, col, llvm::unwrap(scope), nullptr); return llvm::wrap(location); } +DILocationRef LLVMCreateLocationInlinedAt(LLVMContextRef contextRef, unsigned line, + unsigned col, DIScopeOpaqueRef scope, DILocationRef refLocationInlinedAt) { + auto location = llvm::DILocation::get(*llvm::unwrap(contextRef), line, col, llvm::unwrap(scope), llvm::unwrap(refLocationInlinedAt)); + return llvm::wrap(location); +} + +void LLVMBuilderSetDebugLocation(LLVMBuilderRef builder, DILocationRef refLocation) { + llvm::unwrap(builder)->SetCurrentDebugLocation(llvm::unwrap(refLocation)); +} + void LLVMBuilderResetDebugLocation(LLVMBuilderRef builder) { llvm::unwrap(builder)->SetCurrentDebugLocation(nullptr); } diff --git a/llvmDebugInfoC/src/main/include/DebugInfoC.h b/llvmDebugInfoC/src/main/include/DebugInfoC.h index 45248e205dc..f1b4edf5a57 100644 --- a/llvmDebugInfoC/src/main/include/DebugInfoC.h +++ b/llvmDebugInfoC/src/main/include/DebugInfoC.h @@ -99,7 +99,9 @@ DILocalVariableRef DICreateParameterVariable(DIBuilderRef builder, DIScopeOpaque void DIInsertDeclaration(DIBuilderRef builder, LLVMValueRef value, DILocalVariableRef localVariable, DILocationRef location, LLVMBasicBlockRef bb, int64_t *expr, uint64_t exprCount); DIExpressionRef DICreateEmptyExpression(DIBuilderRef builder); void DIFunctionAddSubprogram(LLVMValueRef fn, DISubprogramRef sp); -DILocationRef LLVMBuilderSetDebugLocation(LLVMBuilderRef builder, unsigned line, unsigned col, DIScopeOpaqueRef scope); +DILocationRef LLVMCreateLocation(LLVMContextRef contextRef, unsigned line, unsigned col, DIScopeOpaqueRef scope); +DILocationRef LLVMCreateLocationInlinedAt(LLVMContextRef contextRef, unsigned line, unsigned col, DIScopeOpaqueRef scope, DILocationRef refLocation); +void LLVMBuilderSetDebugLocation(LLVMBuilderRef builder, DILocationRef refLocation); void LLVMBuilderResetDebugLocation(LLVMBuilderRef builder); const char* LLVMBuilderGetCurrentBbName(LLVMBuilderRef builder); const char *DIGetSubprogramLinkName(DISubprogramRef sp);