From f562c127af5cd22d57b1ad58e52b69f48988e952 Mon Sep 17 00:00:00 2001 From: Pavel Kunyavskiy Date: Thu, 9 Jun 2022 11:38:40 +0200 Subject: [PATCH] [K/N] Improve consistency of using setConst vs override RuntimeGlobals --- .../kotlin/backend/konan/KonanConfig.kt | 18 +++++-- .../kotlin/backend/konan/llvm/IrToBitcode.kt | 8 +-- .../src/main/cpp/CompilerConstants.cpp | 32 ++++++------ .../src/main/cpp/CompilerConstants.hpp | 50 +++++++++++-------- .../test_support/cpp/CompilerGenerated.cpp | 6 ++- 5 files changed, 68 insertions(+), 46 deletions(-) diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfig.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfig.kt index 5e053ffa0b5..114ca77c28e 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfig.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfig.kt @@ -138,11 +138,13 @@ class KonanConfig(val project: Project, val configuration: CompilerConfiguration ?: SourceInfoType.CORESYMBOLICATION.takeIf { debug && target.supportsCoreSymbolication() } ?: SourceInfoType.NOOP + val defaultGCSchedulerType get() = when { + !target.supportsThreads() -> GCSchedulerType.ON_SAFE_POINTS + else -> GCSchedulerType.WITH_TIMER + } + val gcSchedulerType: GCSchedulerType by lazy { - configuration.get(BinaryOptions.gcSchedulerType) ?: when { - !target.supportsThreads() -> GCSchedulerType.ON_SAFE_POINTS - else -> GCSchedulerType.WITH_TIMER - } + configuration.get(BinaryOptions.gcSchedulerType) ?: defaultGCSchedulerType } val needVerifyIr: Boolean @@ -338,11 +340,17 @@ class KonanConfig(val project: Project, val configuration: CompilerConfiguration val ignoreCacheReason = when { optimizationsEnabled -> "for optimized compilation" memoryModel != defaultMemoryModel -> "with ${memoryModel.name.lowercase()} memory model" - propertyLazyInitialization != defaultPropertyLazyInitialization -> "with${if (propertyLazyInitialization) "" else "out"} lazy top levels initialization" + propertyLazyInitialization != defaultPropertyLazyInitialization -> { + "with${if (propertyLazyInitialization) "" else "out"} lazy top levels initialization" + } useDebugInfoInNativeLibs -> "with native libs debug info" allocationMode != defaultAllocationMode -> "with ${allocationMode.name.lowercase()} allocator" memoryModel == MemoryModel.EXPERIMENTAL && gc != defaultGC -> "with ${gc.name.lowercase()} garbage collector" + memoryModel == MemoryModel.EXPERIMENTAL && gcSchedulerType != defaultGCSchedulerType -> { + "with ${gcSchedulerType.name.lowercase()} garbage collector scheduler" + } freezing != defaultFreezing -> "with ${freezing.name.replaceFirstChar { it.lowercase() }} freezing mode" + runtimeAssertsMode != RuntimeAssertsMode.IGNORE -> "with runtime assertions" else -> null } CacheSupport( diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt index 80bd0a417a3..b1ac4208b7f 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt @@ -2733,12 +2733,15 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map null SourceInfoType.LIBBACKTRACE -> "Kotlin_getSourceInfo_libbacktrace" diff --git a/kotlin-native/runtime/src/main/cpp/CompilerConstants.cpp b/kotlin-native/runtime/src/main/cpp/CompilerConstants.cpp index 64ec7e16e8f..3149b87ed78 100644 --- a/kotlin-native/runtime/src/main/cpp/CompilerConstants.cpp +++ b/kotlin-native/runtime/src/main/cpp/CompilerConstants.cpp @@ -10,14 +10,18 @@ using namespace kotlin; -// These are defined by overrideRuntimeGlobals in IrToBitcode.kt +using Kotlin_getSourceInfo_FunctionType = int(*)(void * /*addr*/, SourceInfo* /*result*/, int /*result_size*/); + +/** + * There are two ways, how compiler can define variables for runtime usage. This one, and the other one with details in header file. + * + * This is one is variables defined by overrideRuntimeGlobals in IrToBitcode.kt. They are *not* eligible for runtime optimizations, + * but can be changed after compiling caches. So use this way for variables, which will be rarely accessed. + */ RUNTIME_WEAK int32_t Kotlin_destroyRuntimeMode = 1; -RUNTIME_WEAK int32_t Kotlin_gcSchedulerType = 2; RUNTIME_WEAK int32_t Kotlin_workerExceptionHandling = 0; -RUNTIME_WEAK int32_t Kotlin_freezingEnabled = 1; -RUNTIME_WEAK int32_t Kotlin_freezingChecksEnabled = 1; RUNTIME_WEAK int32_t Kotlin_suspendFunctionsFromAnyThreadFromObjC = 0; -RUNTIME_WEAK const Kotlin_getSourceInfo_FunctionType Kotlin_getSourceInfo_Function = nullptr; +RUNTIME_WEAK Kotlin_getSourceInfo_FunctionType Kotlin_getSourceInfo_Function = nullptr; #ifdef KONAN_ANDROID RUNTIME_WEAK int32_t Kotlin_printToAndroidLogcat = 1; #endif @@ -30,24 +34,22 @@ ALWAYS_INLINE compiler::WorkerExceptionHandling compiler::workerExceptionHandlin return static_cast(Kotlin_workerExceptionHandling); } -ALWAYS_INLINE bool compiler::freezingEnabled() noexcept { - return Kotlin_freezingEnabled != 0; -} - -ALWAYS_INLINE bool compiler::freezingChecksEnabled() noexcept { - return Kotlin_freezingChecksEnabled != 0; -} ALWAYS_INLINE bool compiler::suspendFunctionsFromAnyThreadFromObjCEnabled() noexcept { return Kotlin_suspendFunctionsFromAnyThreadFromObjC != 0; } -ALWAYS_INLINE compiler::GCSchedulerType compiler::getGCSchedulerType() noexcept { - return static_cast(Kotlin_gcSchedulerType); -} #ifdef KONAN_ANDROID ALWAYS_INLINE bool compiler::printToAndroidLogcat() noexcept { return Kotlin_printToAndroidLogcat != 0; } #endif + +ALWAYS_INLINE int compiler::getSourceInfo(void* addr, SourceInfo *result, int result_size) noexcept { + if (Kotlin_getSourceInfo_Function == nullptr) { + return 0; + } else { + return Kotlin_getSourceInfo_Function(addr, result, result_size); + } +} \ No newline at end of file diff --git a/kotlin-native/runtime/src/main/cpp/CompilerConstants.hpp b/kotlin-native/runtime/src/main/cpp/CompilerConstants.hpp index 441beeb817e..d4b0bdf6470 100644 --- a/kotlin-native/runtime/src/main/cpp/CompilerConstants.hpp +++ b/kotlin-native/runtime/src/main/cpp/CompilerConstants.hpp @@ -24,13 +24,23 @@ using string_view = std::experimental::string_view; // Prefer to use getter functions below. These constants are exposed to simplify the job of the inliner. -// These are defined by setRuntimeConstGlobals in IrToBitcode.kt -extern "C" const int32_t KonanNeedDebugInfo; +/** + * There are two ways, how compiler can define variables for runtime usage. This one, and the other one with details in source file. + * + * This is one is variables defined by setRuntimeConstGlobals in IrToBitcode.kt. They are eligible for runtime optimizations, + * and fixed at the point of compiling caches. So use this way for variables, which are heavily used on performance-critical passes or + * will significantly increase code size, if not eliminated. + * + * Don't forget to adjust cache disabling rules and add value to CompilerGenerated.cpp for tests, when adding a new variable. + */ +extern "C" const int32_t Kotlin_needDebugInfo; extern "C" const int32_t Kotlin_runtimeAssertsMode; extern "C" const char* const Kotlin_runtimeLogs; +extern "C" const int32_t Kotlin_gcSchedulerType; +extern "C" const int32_t Kotlin_freezingEnabled; +extern "C" const int32_t Kotlin_freezingChecksEnabled; + class SourceInfo; -using Kotlin_getSourceInfo_FunctionType = int(*)(void * /*addr*/, SourceInfo* /*result*/, int /*result_size*/); -extern "C" const Kotlin_getSourceInfo_FunctionType Kotlin_getSourceInfo_Function; namespace kotlin { namespace compiler { @@ -62,36 +72,36 @@ enum class GCSchedulerType { kAggressive = 3, }; -DestroyRuntimeMode destroyRuntimeMode() noexcept; ALWAYS_INLINE inline bool shouldContainDebugInfo() noexcept { - return KonanNeedDebugInfo != 0; + return Kotlin_needDebugInfo != 0; } ALWAYS_INLINE inline RuntimeAssertsMode runtimeAssertsMode() noexcept { return static_cast(Kotlin_runtimeAssertsMode); } -WorkerExceptionHandling workerExceptionHandling() noexcept; - ALWAYS_INLINE inline std::string_view runtimeLogs() noexcept { return Kotlin_runtimeLogs == nullptr ? std::string_view() : std::string_view(Kotlin_runtimeLogs); } -bool freezingEnabled() noexcept; -bool freezingChecksEnabled() noexcept; -bool suspendFunctionsFromAnyThreadFromObjCEnabled() noexcept; - - -ALWAYS_INLINE inline int getSourceInfo(void* addr, SourceInfo *result, int result_size) { - if (Kotlin_getSourceInfo_Function == nullptr) { - return 0; - } else { - return Kotlin_getSourceInfo_Function(addr, result, result_size); - } +ALWAYS_INLINE inline bool freezingEnabled() noexcept { + return Kotlin_freezingEnabled != 0; } -compiler::GCSchedulerType getGCSchedulerType() noexcept; +ALWAYS_INLINE inline bool freezingChecksEnabled() noexcept { + return Kotlin_freezingChecksEnabled != 0; +} + +ALWAYS_INLINE inline GCSchedulerType getGCSchedulerType() noexcept { + return static_cast(Kotlin_gcSchedulerType); +} + + +WorkerExceptionHandling workerExceptionHandling() noexcept; +DestroyRuntimeMode destroyRuntimeMode() noexcept; +bool suspendFunctionsFromAnyThreadFromObjCEnabled() noexcept; +int getSourceInfo(void* addr, SourceInfo *result, int result_size) noexcept; #ifdef KONAN_ANDROID bool printToAndroidLogcat() noexcept; diff --git a/kotlin-native/runtime/src/test_support/cpp/CompilerGenerated.cpp b/kotlin-native/runtime/src/test_support/cpp/CompilerGenerated.cpp index 81188ed8543..3b9a4301359 100644 --- a/kotlin-native/runtime/src/test_support/cpp/CompilerGenerated.cpp +++ b/kotlin-native/runtime/src/test_support/cpp/CompilerGenerated.cpp @@ -63,10 +63,12 @@ struct KBox { extern "C" { -extern const int32_t KonanNeedDebugInfo = 1; +extern const int32_t Kotlin_needDebugInfo = 1; extern const int32_t Kotlin_runtimeAssertsMode = static_cast(kotlin::compiler::RuntimeAssertsMode::kPanic); extern const char* const Kotlin_runtimeLogs = nullptr; -int32_t Kotlin_gcSchedulerType = 0; // kDisabled +extern const int32_t Kotlin_gcSchedulerType = static_cast(kotlin::compiler::GCSchedulerType::kDisabled); +extern const int32_t Kotlin_freezingChecksEnabled = 1; +extern const int32_t Kotlin_freezingEnabled = 1; extern const TypeInfo* theAnyTypeInfo = theAnyTypeInfoHolder.typeInfo(); extern const TypeInfo* theArrayTypeInfo = theArrayTypeInfoHolder.typeInfo();