From 6b01886334dad665a1e3acc4b8fbfc30ddfa209e Mon Sep 17 00:00:00 2001 From: Mads Ager Date: Tue, 20 Dec 2022 16:16:40 +0100 Subject: [PATCH] [K/N] Avoid string copying when setting up bridge debug info. The name of the function was copied at least 4 times. This reduces a 56 seconds codegen phase in a non-optimized build by 2 seconds. --- .../kotlin/backend/konan/llvm/DebugUtils.kt | 9 ++---- .../src/main/cpp/DebugInfoC.cpp | 30 +++++++++++++++---- .../src/main/include/DebugInfoC.h | 6 ++++ 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/DebugUtils.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/DebugUtils.kt index ec96f5856b2..026b35c332c 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/DebugUtils.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/DebugUtils.kt @@ -275,20 +275,17 @@ internal fun setupBridgeDebugInfo(generationState: NativeGenerationState, functi val file = debugInfo.compilerGeneratedFile // TODO: can we share the scope among all bridges? - val scope: DIScopeOpaqueRef = DICreateFunction( + val scope: DIScopeOpaqueRef = DICreateBridgeFunction( builder = debugInfo.builder, scope = file.reinterpret(), - name = function.name, - linkageName = function.name, + function = function, file = file, lineNo = 0, type = debugInfo.subroutineType(generationState.runtime.targetData, emptyList()), // TODO: use proper type. isLocal = 0, isDefinition = 1, scopeLine = 0 - )!!.also { - DIFunctionAddSubprogram(function, it) - }.reinterpret() + )!!.reinterpret() return LocationInfo(scope, 1, 0) } diff --git a/kotlin-native/llvmDebugInfoC/src/main/cpp/DebugInfoC.cpp b/kotlin-native/llvmDebugInfoC/src/main/cpp/DebugInfoC.cpp index 82d040f9656..080ba7f574e 100644 --- a/kotlin-native/llvmDebugInfoC/src/main/cpp/DebugInfoC.cpp +++ b/kotlin-native/llvmDebugInfoC/src/main/cpp/DebugInfoC.cpp @@ -81,11 +81,11 @@ DIModuleRef DICreateModule(DIBuilderRef builder, DIScopeOpaqueRef scope, return llvm::wrap(llvm::unwrap(builder)->createModule(llvm::unwrap(scope), name, configurationMacro, includePath, iSysRoot)); } -DISubprogramRef DICreateFunction(DIBuilderRef builderRef, DIScopeOpaqueRef scope, - const char* name, const char *linkageName, - DIFileRef file, unsigned lineNo, - DISubroutineTypeRef type, int isLocal, - int isDefinition, unsigned scopeLine) { +static DISubprogramRef DICreateFunctionShared(DIBuilderRef builderRef, DIScopeOpaqueRef scope, + llvm::StringRef name, llvm::StringRef linkageName, + DIFileRef file, unsigned lineNo, + DISubroutineTypeRef type, int isLocal, + int isDefinition, unsigned scopeLine) { auto builder = llvm::unwrap(builderRef); auto subprogram = builder->createFunction(llvm::unwrap(scope), name, @@ -102,6 +102,26 @@ DISubprogramRef DICreateFunction(DIBuilderRef builderRef, DIScopeOpaqueRef scope return llvm::wrap(subprogram); } +DISubprogramRef DICreateFunction(DIBuilderRef builderRef, DIScopeOpaqueRef scope, + const char* name, const char *linkageName, + DIFileRef file, unsigned lineNo, + DISubroutineTypeRef type, int isLocal, + int isDefinition, unsigned scopeLine) { + return DICreateFunctionShared(builderRef, scope, name, linkageName, file, lineNo, type, isLocal, isDefinition, scopeLine); +} + +DISubprogramRef DICreateBridgeFunction(DIBuilderRef builderRef, DIScopeOpaqueRef scope, + LLVMValueRef function, + DIFileRef file, unsigned lineNo, + DISubroutineTypeRef type, int isLocal, + int isDefinition, unsigned scopeLine) { + auto fn = llvm::cast(llvm::unwrap(function)); + auto name = fn->getName(); + auto subprogram = DICreateFunctionShared(builderRef, scope, name, name, file, lineNo, type, isLocal, isDefinition, scopeLine); + fn->setSubprogram(llvm::unwrap(subprogram)); + return subprogram; +} + DIScopeOpaqueRef DICreateLexicalBlockFile(DIBuilderRef builderRef, DIScopeOpaqueRef scopeRef, DIFileRef fileRef) { return llvm::wrap(llvm::unwrap(builderRef)->createLexicalBlockFile(llvm::unwrap(scopeRef), llvm::unwrap(fileRef))); } diff --git a/kotlin-native/llvmDebugInfoC/src/main/include/DebugInfoC.h b/kotlin-native/llvmDebugInfoC/src/main/include/DebugInfoC.h index 6d17d6aeb42..f20f20af2f9 100644 --- a/kotlin-native/llvmDebugInfoC/src/main/include/DebugInfoC.h +++ b/kotlin-native/llvmDebugInfoC/src/main/include/DebugInfoC.h @@ -93,6 +93,12 @@ DISubprogramRef DICreateFunction(DIBuilderRef builder, DIScopeOpaqueRef scope, DISubroutineTypeRef type, int isLocal, int isDefinition, unsigned scopeLine); +DISubprogramRef DICreateBridgeFunction(DIBuilderRef builder, DIScopeOpaqueRef scope, + LLVMValueRef function, + DIFileRef file, unsigned lineNo, + DISubroutineTypeRef type, int isLocal, + int isDefinition, unsigned scopeLine); + DISubroutineTypeRef DICreateSubroutineType(DIBuilderRef builder, DITypeOpaqueRef* types, unsigned typesCount);