Add a separate compiler switch for runtime asserts

This commit is contained in:
Alexander Shabalin
2021-07-28 07:43:08 +00:00
committed by Space
parent c3515cc338
commit 48a2b23b3a
18 changed files with 199 additions and 104 deletions
@@ -321,6 +321,15 @@ class K2Native : CLICompiler<K2NativeCompilerArguments>() {
true
}
})
put(RUNTIME_ASSERTS_MODE, when (arguments.runtimeAssertsMode) {
"ignore" -> RuntimeAssertsMode.IGNORE
"log" -> RuntimeAssertsMode.LOG
"panic" -> RuntimeAssertsMode.PANIC
else -> {
configuration.report(ERROR, "Unsupported runtime asserts mode ${arguments.runtimeAssertsMode}")
RuntimeAssertsMode.IGNORE
}
})
}
}
}
@@ -321,6 +321,9 @@ class K2NativeCompilerArguments : CommonCompilerArguments() {
)
var checkLldCompatibility: String? = null
@Argument(value="-Xruntime-asserts-mode", valueDescription = "<mode>", description = "Enable asserts in runtime. Possible values: 'ignore', 'log', 'panic'")
var runtimeAssertsMode: String? = "ignore"
override fun configureAnalysisFlags(collector: MessageCollector, languageVersion: LanguageVersion): MutableMap<AnalysisFlag<*>, Any> =
super.configureAnalysisFlags(collector, languageVersion).also {
val useExperimental = it[AnalysisFlags.useExperimental] as List<*>
@@ -4,7 +4,7 @@
*/
package org.jetbrains.kotlin.backend.konan
// Must match `DestroyRuntimeMode` in Runtime.h
// Must match `DestroyRuntimeMode` in CompilerConstants.hpp
enum class DestroyRuntimeMode(val value: Int) {
LEGACY(0),
ON_SHUTDOWN(1),
@@ -49,6 +49,7 @@ class KonanConfig(val project: Project, val configuration: CompilerConfiguration
val destroyRuntimeMode: DestroyRuntimeMode get() = configuration.get(KonanConfigKeys.DESTROY_RUNTIME_MODE)!!
val gc: GC get() = configuration.get(KonanConfigKeys.GARBAGE_COLLECTOR)!!
val gcAggressive: Boolean get() = configuration.get(KonanConfigKeys.GARBAGE_COLLECTOR_AGRESSIVE)!!
val runtimeAssertsMode: RuntimeAssertsMode get() = configuration.get(KonanConfigKeys.RUNTIME_ASSERTS_MODE)!!
val needVerifyIr: Boolean
get() = configuration.get(KonanConfigKeys.VERIFY_IR) == true
@@ -159,6 +159,7 @@ class KonanConfigKeys {
val GARBAGE_COLLECTOR: CompilerConfigurationKey<GC> = CompilerConfigurationKey.create("gc")
val GARBAGE_COLLECTOR_AGRESSIVE: CompilerConfigurationKey<Boolean> = CompilerConfigurationKey.create("turn on agressive GC mode")
val CHECK_LLD_COMPATIBILITY: CompilerConfigurationKey<Boolean> = CompilerConfigurationKey.create("check compatibility with LLD")
val RUNTIME_ASSERTS_MODE: CompilerConfigurationKey<RuntimeAssertsMode> = CompilerConfigurationKey.create("enable runtime asserts")
}
}
@@ -0,0 +1,12 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package org.jetbrains.kotlin.backend.konan
// Must match `RuntimeAssertsMode` in CompilerConstants.hpp
enum class RuntimeAssertsMode(val value: Int) {
IGNORE(0),
LOG(1),
PANIC(2),
}
@@ -358,7 +358,7 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map<IrE
codegen.objCDataGenerator?.finishModule()
context.coverage.writeRegionInfo()
appendDebugSelector()
setRuntimeConstGlobals()
overrideRuntimeGlobals()
appendLlvmUsed("llvm.used", context.llvm.usedFunctions + context.llvm.usedGlobals)
appendLlvmUsed("llvm.compiler.used", context.llvm.compilerUsedGlobals)
@@ -2417,16 +2417,23 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map<IrE
LLVMSetSection(llvmUsedGlobal.llvmGlobal, "llvm.metadata")
}
// TODO: Consider migrating `KonanNeedDebugInfo` to the `overrideRuntimeGlobal` mechanism from below.
private fun appendDebugSelector() {
if (!context.producedLlvmModuleContainsStdlib) return
val llvmDebugSelector =
context.llvm.staticData.placeGlobal("KonanNeedDebugInfo",
Int32(if (context.shouldContainDebugInfo()) 1 else 0))
llvmDebugSelector.setConstant(true)
llvmDebugSelector.setLinkage(LLVMLinkage.LLVMExternalLinkage)
// Globals set this way will be const, but can only be built into runtime-containing module. Which
// means they are set at stdlib-cache compilation time.
private fun setRuntimeConstGlobal(name: String, value: ConstValue) {
val global = context.llvm.staticData.placeGlobal(name, value)
global.setConstant(true)
global.setLinkage(LLVMLinkage.LLVMExternalLinkage)
}
private fun setRuntimeConstGlobals() {
if (!context.producedLlvmModuleContainsStdlib)
return
setRuntimeConstGlobal("KonanNeedDebugInfo", Int32(if (context.shouldContainDebugInfo()) 1 else 0))
setRuntimeConstGlobal("Kotlin_runtimeAssertsMode", Int32(context.config.runtimeAssertsMode.value))
}
// Globals set this way cannot be const, but are overridable when producing final executable.
private fun overrideRuntimeGlobal(name: String, value: ConstValue) {
// TODO: A similar mechanism is used in `ObjCExportCodeGenerator`. Consider merging them.
if (context.llvmModuleSpecification.importsKotlinDeclarationsFromOtherSharedLibraries()) {
@@ -117,6 +117,17 @@ if (ext.isExperimentalMM) {
}
}
// TODO: It also makes sense to test -g without asserts, and also to test -opt with asserts.
if (project.globalTestArgs.contains("-g")) {
tasks.withType(KonanCompileNativeBinary.class).configureEach {
extraOpts "-Xruntime-asserts-mode=panic"
}
tasks.withType(RunExternalTestGroup.class).configureEach {
flags = (flags ?: []) + "-Xruntime-asserts-mode=panic"
}
}
allprojects {
// Root directories for test output (logs, compiled files, statistics etc). Only single path must be in each set.
// backend.native/tests