Skip to content
This repository has been archived by the owner on Aug 10, 2021. It is now read-only.

Commit

Permalink
Mimalloc allocator integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Elena Lepilkina authored and Elena Lepilkina committed Dec 25, 2019
1 parent 01a719e commit 3726c06
Show file tree
Hide file tree
Showing 20 changed files with 225 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class K2Native : CLICompiler<K2NativeCompilerArguments>() {
put(LIGHT_DEBUG, arguments.lightDebug)
put(STATIC_FRAMEWORK, selectFrameworkType(configuration, arguments, outputKind))
put(OVERRIDE_CLANG_OPTIONS, arguments.clangOptions.toNonNullList())
put(ALLOCATION_MODE, arguments.allocator)

put(PRINT_IR, arguments.printIr)
put(PRINT_IR_WITH_DESCRIPTORS, arguments.printIrWithDescriptors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ class K2NativeCompilerArguments : CommonCompilerArguments() {
@Argument(value="-Xoverride-clang-options", valueDescription = "<arg1,arg2,...>", description = "Explicit list of Clang options")
var clangOptions: Array<String>? = null

@Argument(value="-Xallocator", valueDescription = "std | mimalloc", description = "Allocator used in runtime")
var allocator: String = "std"

override fun configureAnalysisFlags(collector: MessageCollector): MutableMap<AnalysisFlag<*>, Any> =
super.configureAnalysisFlags(collector).also {
val useExperimental = it[AnalysisFlags.useExperimental] as List<*>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,13 @@ import org.jetbrains.kotlin.config.CommonConfigurationKeys
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.konan.CURRENT
import org.jetbrains.kotlin.konan.CompilerVersion
import org.jetbrains.kotlin.konan.MetaVersion
import org.jetbrains.kotlin.konan.TempFiles
import org.jetbrains.kotlin.konan.file.File
import org.jetbrains.kotlin.konan.library.KonanLibrary
import org.jetbrains.kotlin.konan.properties.loadProperties
import org.jetbrains.kotlin.konan.target.Distribution
import org.jetbrains.kotlin.konan.target.HostManager
import org.jetbrains.kotlin.konan.target.KonanTarget
import org.jetbrains.kotlin.konan.target.PlatformManager
import org.jetbrains.kotlin.konan.target.*
import org.jetbrains.kotlin.util.Logger
import kotlin.system.exitProcess
import org.jetbrains.kotlin.library.toUnresolvedLibraries
import org.jetbrains.kotlin.konan.CompilerVersion
import org.jetbrains.kotlin.library.KotlinLibrary
import org.jetbrains.kotlin.library.resolver.TopologicalLibraryOrder

Expand Down Expand Up @@ -116,6 +109,18 @@ class KonanConfig(val project: Project, val configuration: CompilerConfiguration
add(if (debug) "debug.bc" else "release.bc")
add(if (memoryModel == MemoryModel.STRICT) "strict.bc" else "relaxed.bc")
if (shouldCoverLibraries || shouldCoverSources) add("profileRuntime.bc")
if (configuration.get(KonanConfigKeys.ALLOCATION_MODE) == "mimalloc") {
if (!target.supportsMimallocAllocator()) {
configuration.report(CompilerMessageSeverity.STRONG_WARNING,
"Mimalloc allocator isn't supported on target ${target.name}. Used standard mode.")
add("std_alloc.bc")
} else {
add("opt_alloc.bc")
add("mimalloc.bc")
}
} else {
add("std_alloc.bc")
}
}.map {
File(distribution.defaultNatives(target)).child(it).absolutePath
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class KonanConfigKeys {
= CompilerConfigurationKey.create("program or library name")
val OVERRIDE_CLANG_OPTIONS: CompilerConfigurationKey<List<String>>
= CompilerConfigurationKey.create("arguments for clang")
val ALLOCATION_MODE: CompilerConfigurationKey<String>
= CompilerConfigurationKey.create("allocation mode")
val PRINT_BITCODE: CompilerConfigurationKey<Boolean>
= CompilerConfigurationKey.create("print bitcode")
val PRINT_DESCRIPTORS: CompilerConfigurationKey<Boolean>
Expand Down
4 changes: 2 additions & 2 deletions backend.native/tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,8 @@ def createTestTasks(File testRoot, Class<Task> taskType, Closure taskConfigurati

void dependsOnPlatformLibs(Task t) {
if (!useCustomDist) {
def testTarget = project.testTarget
if (testTarget != null && testTarget != project.hostName) {
def testTarget = project.target
if (testTarget != project.platformManager.Companion.host) {
t.dependsOn(":${testTarget}PlatformLibs")
} else {
t.dependsOn(':distPlatformLibs')
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jetbrains.kotlin

import org.gradle.api.Action
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import org.jetbrains.kotlin.konan.target.Family
import org.jetbrains.kotlin.konan.target.HostManager
import org.jetbrains.kotlin.konan.target.KonanTarget

import java.io.File
import javax.inject.Inject

open class CompileToBitcode @Inject constructor(@InputDirectory val srcRoot: File,
val folderName: String,
val target: String) : DefaultTask() {
enum class Language {
C, CPP
}

val compilerArgs = mutableListOf<String>()
val linkerArgs = mutableListOf<String>()
val excludeFiles = mutableListOf<String>()
var srcDir = File(srcRoot, "cpp")
var headersDir = File(srcRoot, "headers")
var skipLinkagePhase = false
var excludedTargets = mutableListOf<String>()
var language = Language.CPP

private val targetDir by lazy { File(project.buildDir, target) }

private val objDir by lazy { File(targetDir, folderName) }

private val KonanTarget.isMINGW
get() = this.family == Family.MINGW

@OutputFile
val outFile = File(targetDir, "${folderName}.bc")

@TaskAction
fun compile() {
if (target in excludedTargets) return
objDir.mkdirs()
val plugin = project.convention.getPlugin(ExecClang::class.java)
val commonFlags = listOf("-c", "-emit-llvm", "-I$headersDir")
val (executable, defaultFlags, srcFilesPatterns) =
when (language) {
Language.C -> Triple("clang",
// Used flags provided by original build of allocator C code.
commonFlags + listOf("-std=gnu11", "-O3", "-Wall", "-Wextra", "-Wno-unknown-pragmas",
"-ftls-model=initial-exec"),
listOf("**/*.c"))
Language.CPP -> Triple("clang++",
commonFlags + listOfNotNull("-std=c++14", "-Werror", "-O2",
"-fPIC".takeIf { !HostManager().targetByName(target).isMINGW }),
listOf("**/*.cpp", "**/*.mm"))
}

plugin.execKonanClang(target, Action {
it.workingDir = objDir
it.executable = executable
it.args = defaultFlags + compilerArgs +
project.fileTree(srcDir) {
it.include(srcFilesPatterns)
it.exclude(excludeFiles)
}.files.map { it.absolutePath }
})

if (!skipLinkagePhase) {
project.exec {
val llvmDir = project.findProperty("llvmDir")
it.executable = "$llvmDir/bin/llvm-link"
it.args = listOf("-o", outFile.absolutePath) + linkerArgs +
project.fileTree(objDir) {
it.include("**/*.bc")
}.files.map { it.absolutePath }
}
}
}
}
5 changes: 4 additions & 1 deletion build-tools/src/main/kotlin/org/jetbrains/kotlin/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,7 @@ fun compileSwift(project: Project, target: KonanTarget, sources: List<String>, o
""".trimMargin())
check(exitCode == 0, { "Compilation failed" })
check(output.toFile().exists(), { "Compiler swiftc hasn't produced an output file: $output" })
}
}

fun targetSupportsMimallocAllocator(targetName: String) =
HostManager().targetByName(targetName).supportsMimallocAllocator()
12 changes: 3 additions & 9 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@
* Copyright 2010-2018 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.
*/
import org.jetbrains.kotlin.CompileCppToBitcode
import org.jetbrains.kotlin.CompileToBitcode

// TODO: consider using some Gradle plugins to build and test

targetList.each { targetName ->
task ("${targetName}Hash", type: CompileCppToBitcode) {
name 'hash'
target targetName
}
tasks.create("${targetName}Hash", CompileToBitcode, file("src/hash"), 'hash', targetName)
}

targetList.each { targetName ->
task ("${targetName}Files", type: CompileCppToBitcode) {
name 'files'
target targetName
}
tasks.create("${targetName}Files", CompileToBitcode, file("src/files"), 'files', targetName)
}

task build {
Expand Down

0 comments on commit 3726c06

Please sign in to comment.