From da1fde24777eb809e767550c91ffe1f729ad97d9 Mon Sep 17 00:00:00 2001 From: Alexander Shabalin Date: Thu, 11 May 2023 17:30:49 +0200 Subject: [PATCH] [K/N] Enable custom allocator by default ^KT-55364 --- buildSrc/src/main/kotlin/nativeTest.kt | 2 ++ .../org/jetbrains/kotlin/backend/konan/KonanConfig.kt | 9 +++++++++ kotlin-native/backend.native/tests/build.gradle | 1 + .../blackboxtest/support/ConfigurationProperties.kt | 1 + .../konan/blackboxtest/support/NativeTestSupport.kt | 6 ++++++ .../blackboxtest/support/compilation/TestCompilation.kt | 4 ++++ .../blackboxtest/support/settings/TestProcessSettings.kt | 9 +++++++++ 7 files changed, 32 insertions(+) diff --git a/buildSrc/src/main/kotlin/nativeTest.kt b/buildSrc/src/main/kotlin/nativeTest.kt index 4d535986b31..5ffaa58d46f 100644 --- a/buildSrc/src/main/kotlin/nativeTest.kt +++ b/buildSrc/src/main/kotlin/nativeTest.kt @@ -21,6 +21,7 @@ private enum class TestProperty(shortName: String) { USE_THREAD_STATE_CHECKER("useThreadStateChecker"), GC_TYPE("gcType"), GC_SCHEDULER("gcScheduler"), + ALLOCATOR("alloc"), CACHE_MODE("cacheMode"), EXECUTION_TIMEOUT("executionTimeout"), SANITIZER("sanitizer"), @@ -168,6 +169,7 @@ fun Project.nativeTest( compute(USE_THREAD_STATE_CHECKER) compute(GC_TYPE) compute(GC_SCHEDULER) + compute(ALLOCATOR) compute(CACHE_MODE) compute(EXECUTION_TIMEOUT) compute(SANITIZER) 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 39bea711bf1..4c77daa4c65 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 @@ -229,6 +229,9 @@ class KonanConfig(val project: Project, val configuration: CompilerConfiguration private val shouldCoverLibraries = !configuration.getList(KonanConfigKeys.LIBRARIES_TO_COVER).isNullOrEmpty() private val defaultAllocationMode get() = when { + gc == GC.PARALLEL_MARK_CONCURRENT_SWEEP && sanitizer == null -> { + AllocationMode.CUSTOM + } target.supportsMimallocAllocator() && sanitizer == null -> { AllocationMode.MIMALLOC } @@ -240,6 +243,9 @@ class KonanConfig(val project: Project, val configuration: CompilerConfiguration null -> defaultAllocationMode AllocationMode.STD -> AllocationMode.STD AllocationMode.MIMALLOC -> { + if (sanitizer != null) { + configuration.report(CompilerMessageSeverity.STRONG_WARNING, "Sanitizers are useful only with the std allocator") + } if (target.supportsMimallocAllocator()) { AllocationMode.MIMALLOC } else { @@ -249,6 +255,9 @@ class KonanConfig(val project: Project, val configuration: CompilerConfiguration } } AllocationMode.CUSTOM -> { + if (sanitizer != null) { + configuration.report(CompilerMessageSeverity.STRONG_WARNING, "Sanitizers are useful only with the std allocator") + } if (gc == GC.PARALLEL_MARK_CONCURRENT_SWEEP) { AllocationMode.CUSTOM } else { diff --git a/kotlin-native/backend.native/tests/build.gradle b/kotlin-native/backend.native/tests/build.gradle index bf477f82379..1c0351fbca9 100644 --- a/kotlin-native/backend.native/tests/build.gradle +++ b/kotlin-native/backend.native/tests/build.gradle @@ -3135,6 +3135,7 @@ standaloneTest("stress_gc_allocations") { (project.testTarget != "watchos_simulator_arm64") && !isNoopGC && !isAggressiveGC && // TODO: Investigate why too slow + !runtimeAssertionsPanic && // New allocator with assertions makes this test very slow (project.testTarget != "mingw_x64") // TODO: Fix on mingw. source = "runtime/memory/stress_gc_allocations.kt" flags = ['-tr', '-opt-in=kotlin.native.internal.InternalForKotlinNative'] diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/ConfigurationProperties.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/ConfigurationProperties.kt index f17de550e19..ad794c7770b 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/ConfigurationProperties.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/ConfigurationProperties.kt @@ -63,6 +63,7 @@ internal enum class ClassLevelProperty(shortName: String) { USE_THREAD_STATE_CHECKER("useThreadStateChecker"), GC_TYPE("gcType"), GC_SCHEDULER("gcScheduler"), + ALLOCATOR("alloc"), CACHE_MODE("cacheMode"), EXECUTION_TIMEOUT("executionTimeout"), SANITIZER("sanitizer"), diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/NativeTestSupport.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/NativeTestSupport.kt index cad2bc5df7d..4bd485b1079 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/NativeTestSupport.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/NativeTestSupport.kt @@ -163,6 +163,8 @@ private object NativeTestSupport { val gcScheduler = computeGCScheduler(enforcedProperties) + val allocator = computeAllocator(enforcedProperties) + val nativeHome = getOrCreateTestProcessSettings().get() val distribution = Distribution(nativeHome.dir.path) @@ -183,6 +185,7 @@ private object NativeTestSupport { output += threadStateChecker output += gcType output += gcScheduler + output += allocator output += nativeTargets output += sanitizer output += CacheMode::class to cacheMode @@ -228,6 +231,9 @@ private object NativeTestSupport { private fun computeGCScheduler(enforcedProperties: EnforcedProperties): GCScheduler = ClassLevelProperty.GC_SCHEDULER.readValue(enforcedProperties, GCScheduler.values(), default = GCScheduler.UNSPECIFIED) + private fun computeAllocator(enforcedProperties: EnforcedProperties): Allocator = + ClassLevelProperty.ALLOCATOR.readValue(enforcedProperties, Allocator.values(), default = Allocator.UNSPECIFIED) + private fun computeNativeTargets(enforcedProperties: EnforcedProperties, hostManager: HostManager): KotlinNativeTargets { val hostTarget = HostManager.host return KotlinNativeTargets( diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/compilation/TestCompilation.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/compilation/TestCompilation.kt index bb20fb16b38..da3cd471afe 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/compilation/TestCompilation.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/compilation/TestCompilation.kt @@ -132,6 +132,7 @@ internal abstract class SourceBasedCompilation( private val sanitizer: Sanitizer, private val gcType: GCType, private val gcScheduler: GCScheduler, + private val allocator: Allocator, private val pipelineType: PipelineType, freeCompilerArgs: TestCompilerArgs, override val sourceModules: Collection, @@ -193,6 +194,7 @@ internal class LibraryCompilation( sanitizer = settings.get(), gcType = settings.get(), gcScheduler = settings.get(), + allocator = settings.get(), pipelineType = settings.get(), freeCompilerArgs = freeCompilerArgs, sourceModules = sourceModules, @@ -226,6 +228,7 @@ internal class ObjCFrameworkCompilation( sanitizer = settings.get(), gcType = settings.get(), gcScheduler = settings.get(), + allocator = settings.get(), pipelineType = settings.getStageDependentPipelineType(), freeCompilerArgs = freeCompilerArgs, sourceModules = sourceModules, @@ -310,6 +313,7 @@ internal class ExecutableCompilation( sanitizer = settings.get(), gcType = settings.get(), gcScheduler = settings.get(), + allocator = settings.get(), pipelineType = settings.getStageDependentPipelineType(), freeCompilerArgs = freeCompilerArgs, sourceModules = sourceModules, diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/settings/TestProcessSettings.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/settings/TestProcessSettings.kt index 00be59c5e49..13f0c856cc0 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/settings/TestProcessSettings.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/settings/TestProcessSettings.kt @@ -166,6 +166,15 @@ internal enum class GCScheduler(val compilerFlag: String?) { override fun toString() = compilerFlag?.let { "($it)" }.orEmpty() } +internal enum class Allocator(val compilerFlag: String?) { + UNSPECIFIED(null), + STD("-Xallocator=std"), + MIMALLOC("-Xallocator=mimalloc"), + CUSTOM("-Xallocator=custom"); + + override fun toString() = compilerFlag?.let { "($it)" }.orEmpty() +} + /** * Current project's directories. */