From b2fa9df6297316008d64e934b598b16b437aff8f Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Wed, 17 Oct 2018 08:42:45 +0300 Subject: [PATCH] Disable asserts, avoid line numbers in runtime checks. (#2216) * Disable asserts, avoid line numbers in runtime checks. * Rework assertion enabling. * Review feedback. --- .../kotlin/backend/konan/llvm/IrToBitcode.kt | 20 +++++++++++++------ runtime/src/main/cpp/KAssert.cpp | 5 ++++- runtime/src/main/cpp/KAssert.h | 16 +++++++++------ 3 files changed, 28 insertions(+), 13 deletions(-) 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 bcf6e4aede1..8998ecef8c7 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 @@ -385,6 +385,7 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map) { if (args.isEmpty()) return - memScoped { - val argsCasted = args.map { it -> constPointer(it).bitcast(int8TypePtr) } - val llvmUsedGlobal = + val argsCasted = args.map { it -> constPointer(it).bitcast(int8TypePtr) } + val llvmUsedGlobal = context.llvm.staticData.placeGlobalArray(name, int8TypePtr, argsCasted) - LLVMSetLinkage(llvmUsedGlobal.llvmGlobal, LLVMLinkage.LLVMAppendingLinkage) - LLVMSetSection(llvmUsedGlobal.llvmGlobal, "llvm.metadata") - } + LLVMSetLinkage(llvmUsedGlobal.llvmGlobal, LLVMLinkage.LLVMAppendingLinkage) + LLVMSetSection(llvmUsedGlobal.llvmGlobal, "llvm.metadata") + } + + private fun appendDebugSelector() { + if (!context.config.produce.isNativeBinary) return + val llvmDebugSelector = + context.llvm.staticData.placeGlobal("KonanNeedDebugInfo", + Int32(if (context.shouldContainDebugInfo()) 1 else 0)) + llvmDebugSelector.setConstant(true) + llvmDebugSelector.setLinkage(LLVMLinkage.LLVMExternalLinkage) } //-------------------------------------------------------------------------// diff --git a/runtime/src/main/cpp/KAssert.cpp b/runtime/src/main/cpp/KAssert.cpp index 997f38f3f3b..ee3ab1fdeaa 100644 --- a/runtime/src/main/cpp/KAssert.cpp +++ b/runtime/src/main/cpp/KAssert.cpp @@ -20,7 +20,10 @@ void RuntimeAssertFailed(const char* location, const char* message) { // TODO: produce stacktrace and such. char buf[1024]; - konan::snprintf(buf, sizeof(buf), "%s: runtime assert: %s\n", location, message); + if (location != nullptr) + konan::snprintf(buf, sizeof(buf), "%s: runtime assert: %s\n", location, message); + else + konan::snprintf(buf, sizeof(buf), "runtime assert: %s\n", message); konan::consoleErrorUtf8(buf, konan::strnlen(buf, sizeof(buf))); konan::abort(); } diff --git a/runtime/src/main/cpp/KAssert.h b/runtime/src/main/cpp/KAssert.h index 50e88dfce57..c111c1783fa 100644 --- a/runtime/src/main/cpp/KAssert.h +++ b/runtime/src/main/cpp/KAssert.h @@ -27,21 +27,25 @@ RUNTIME_NORETURN void RuntimeAssertFailed(const char* location, const char* message); +// During codegeneration we set this constant to 1 or 0 to allow bitcode optimizer +// to get rid of code behind condition. +extern "C" const int KonanNeedDebugInfo; + #if KONAN_ENABLE_ASSERT // Use RuntimeAssert() in internal state checks, which could be ignored in production. -#define RuntimeAssert(condition, message) \ - if (!(condition)) { \ +#define RuntimeAssert(condition, message) \ +if (KonanNeedDebugInfo && (!(condition))) { \ RuntimeAssertFailed( __FILE__ ":" TOSTRING(__LINE__), message); \ - } +} #else #define RuntimeAssert(condition, message) #endif // Use RuntimeCheck() in runtime checks that could fail due to external condition and shall lead // to program termination. Never compiled out. -#define RuntimeCheck(condition, message) \ - if (!(condition)) { \ - RuntimeAssertFailed( __FILE__ ":" TOSTRING(__LINE__), message); \ +#define RuntimeCheck(condition, message) \ + if (!(condition)) { \ + RuntimeAssertFailed(nullptr, message); \ } #endif // RUNTIME_ASSERT_H