[LLVM][Optimizations] Replace magic numbers with constants.

This commit is contained in:
Sergey Bogolepov
2019-10-08 10:59:06 +03:00
committed by Sergey Bogolepov
parent 767515cdcc
commit 02e53f95f8
@@ -85,24 +85,29 @@ private class LlvmPipelineConfiguration(context: Context) {
else -> "" else -> ""
} }
/**
* Null value means that LLVM should use default inliner params
* for the provided optimization and size level.
*/
val customInlineThreshold: Int? = when { val customInlineThreshold: Int? = when {
context.shouldOptimize() -> 100 context.shouldOptimize() -> INLINE_THRESHOLD_OPT
context.shouldContainDebugInfo() -> null context.shouldContainDebugInfo() -> null
else -> null else -> null
} }
val optimizationLevel: Int = when { val optimizationLevel: LlvmOptimizationLevel = when {
context.shouldOptimize() -> 3 context.shouldOptimize() -> LlvmOptimizationLevel.AGGRESSIVE
context.shouldContainDebugInfo() -> 0 context.shouldContainDebugInfo() -> LlvmOptimizationLevel.NONE
else -> 1 else -> LlvmOptimizationLevel.DEFAULT
} }
val sizeLevel: Int = when { val sizeLevel: LlvmSizeLevel = when {
// We try to optimize code as much as possible on embedded targets.
target is KonanTarget.ZEPHYR || target is KonanTarget.ZEPHYR ||
target == KonanTarget.WASM32 -> 3 target == KonanTarget.WASM32 -> LlvmSizeLevel.AGGRESSIVE
context.shouldOptimize() -> 0 context.shouldOptimize() -> LlvmSizeLevel.NONE
context.shouldContainDebugInfo() -> 0 context.shouldContainDebugInfo() -> LlvmSizeLevel.NONE
else -> 0 else -> LlvmSizeLevel.NONE
} }
val codegenOptimizationLevel: LLVMCodeGenOptLevel = when { val codegenOptimizationLevel: LLVMCodeGenOptLevel = when {
@@ -114,6 +119,27 @@ private class LlvmPipelineConfiguration(context: Context) {
val relocMode: LLVMRelocMode = LLVMRelocMode.LLVMRelocDefault val relocMode: LLVMRelocMode = LLVMRelocMode.LLVMRelocDefault
val codeModel: LLVMCodeModel = LLVMCodeModel.LLVMCodeModelDefault val codeModel: LLVMCodeModel = LLVMCodeModel.LLVMCodeModelDefault
companion object {
// By default LLVM uses 250 for -03 builds.
// We use a smaller value since default value leads to
// unreasonably bloated runtime code without any measurable
// performance benefits.
// This value still has to be tuned for different targets, though.
private const val INLINE_THRESHOLD_OPT = 100
}
enum class LlvmOptimizationLevel(val value: Int) {
NONE(0),
DEFAULT(1),
AGGRESSIVE(3)
}
enum class LlvmSizeLevel(val value: Int) {
NONE(0),
DEFAULT(1),
AGGRESSIVE(2)
}
} }
internal fun runLlvmOptimizationPipeline(context: Context) { internal fun runLlvmOptimizationPipeline(context: Context) {
@@ -126,8 +152,8 @@ internal fun runLlvmOptimizationPipeline(context: Context) {
initializeLlvmGlobalPassRegistry() initializeLlvmGlobalPassRegistry()
val passBuilder = LLVMPassManagerBuilderCreate() val passBuilder = LLVMPassManagerBuilderCreate()
val modulePasses = LLVMCreatePassManager() val modulePasses = LLVMCreatePassManager()
LLVMPassManagerBuilderSetOptLevel(passBuilder, config.optimizationLevel) LLVMPassManagerBuilderSetOptLevel(passBuilder, config.optimizationLevel.value)
LLVMPassManagerBuilderSetSizeLevel(passBuilder, config.sizeLevel) LLVMPassManagerBuilderSetSizeLevel(passBuilder, config.sizeLevel.value)
// TODO: use LLVMGetTargetFromName instead. // TODO: use LLVMGetTargetFromName instead.
val target = alloc<LLVMTargetRefVar>() val target = alloc<LLVMTargetRefVar>()
val foundLlvmTarget = LLVMGetTargetFromTriple(config.targetTriple, target.ptr, null) == 0 val foundLlvmTarget = LLVMGetTargetFromTriple(config.targetTriple, target.ptr, null) == 0