[zephyr] Update zephyr configurables file and fix sample.

This commit is contained in:
Sergey Bogolepov
2019-10-01 17:13:44 +03:00
committed by Sergey Bogolepov
parent dc14efebdd
commit eb0e6a8f11
11 changed files with 60 additions and 68 deletions
@@ -44,18 +44,6 @@ internal class BitcodeCompiler(val context: Context) {
runTool(absoluteToolName, *arg)
}
private fun optAndLlc(configurables: ZephyrConfigurables, file: BitcodeFile): String {
val optimizedBc = temporary("optimized", ".bc")
val optFlags = llvmProfilingFlags() + listOf("-O3", "-internalize", "-globaldce")
hostLlvmTool("opt", file, "-o=$optimizedBc", *optFlags.toTypedArray())
val combinedO = temporary("combined", ".o")
val llcFlags = llvmProfilingFlags() + listOf("-function-sections", "-data-sections")
hostLlvmTool("llc", optimizedBc, "-filetype=obj", "-o", combinedO, *llcFlags.toTypedArray())
return combinedO
}
private fun clang(configurables: ClangFlags, file: BitcodeFile): ObjectFile {
val objectFile = temporary("result", ".o")
@@ -73,6 +61,9 @@ internal class BitcodeCompiler(val context: Context) {
val flags = mutableListOf<String>().apply {
addNonEmpty(configurables.clangFlags)
addNonEmpty(listOf("-triple", targetTriple))
if (configurables is ZephyrConfigurables) {
addNonEmpty(configurables.constructClangCC1Args())
}
addNonEmpty(when {
optimize -> configurables.clangOptFlags
debug -> configurables.clangDebugFlags
@@ -92,8 +83,6 @@ internal class BitcodeCompiler(val context: Context) {
return objectFile
}
// llvm-lto, opt and llc share same profiling flags, so we can
// reuse this function.
private fun llvmProfilingFlags(): List<String> {
val flags = mutableListOf<String>()
if (context.shouldProfilePhases()) {
@@ -107,11 +96,7 @@ internal class BitcodeCompiler(val context: Context) {
fun makeObjectFiles(bitcodeFile: BitcodeFile): List<ObjectFile> =
listOf(when (val configurables = platform.configurables) {
is ClangFlags ->
clang(configurables, bitcodeFile)
is ZephyrConfigurables ->
optAndLlc(configurables, bitcodeFile)
else ->
error("Unsupported configurables kind: ${configurables::class.simpleName}!")
is ClangFlags -> clang(configurables, bitcodeFile)
else -> error("Unsupported configurables kind: ${configurables::class.simpleName}!")
})
}
@@ -74,19 +74,6 @@ private fun linkAllDependencies(context: Context, generatedBitcodeFiles: List<St
}
}
private fun shouldOptimizeWithLlvmApi(context: Context) =
context.config.target.family != Family.ZEPHYR
private fun shoudRunClosedWorldCleanUp(context: Context) =
// GlobalDCE will kill coverage-related globals.
!context.coverage.enabled
private fun runLlvmPipeline(context: Context) = when {
shouldOptimizeWithLlvmApi(context) -> runLlvmOptimizationPipeline(context)
shoudRunClosedWorldCleanUp(context) -> runClosedWorldCleanup(context)
else -> {}
}
private fun insertAliasToEntryPoint(context: Context) {
val nomain = context.config.configuration.get(KonanConfigKeys.NOMAIN) ?: false
if (context.config.produce != CompilerOutputKind.PROGRAM || nomain)
@@ -124,7 +111,7 @@ internal fun produceOutput(context: Context) {
embedAppleLinkerOptionsToBitcode(context.llvm, context.config)
}
linkAllDependencies(context, generatedBitcodeFiles)
runLlvmPipeline(context)
runLlvmOptimizationPipeline(context)
// Insert `_main` after pipeline so we won't worry about optimizations
// corrupting entry point.
insertAliasToEntryPoint(context)
@@ -5,7 +5,9 @@ import kotlinx.cinterop.memScoped
import kotlinx.cinterop.ptr
import kotlinx.cinterop.value
import llvm.*
import org.jetbrains.kotlin.konan.target.Configurables
import org.jetbrains.kotlin.konan.target.KonanTarget
import org.jetbrains.kotlin.konan.target.ZephyrConfigurables
private fun initializeLlvmGlobalPassRegistry() {
val passRegistry = LLVMGetGlobalPassRegistry()
@@ -38,6 +40,7 @@ internal fun runLateBitcodePasses(context: Context, llvmModule: LLVMModuleRef) {
private class LlvmPipelineConfiguration(context: Context) {
private val target = context.config.target
private val configurables: Configurables = context.config.platform.configurables
val targetTriple: String = context.llvm.targetTriple
@@ -69,8 +72,11 @@ private class LlvmPipelineConfiguration(context: Context) {
KonanTarget.ANDROID_X86 -> "i686"
KonanTarget.LINUX_MIPS32 -> "mips32r2"
KonanTarget.LINUX_MIPSEL32 -> "mips32r2"
KonanTarget.WASM32 -> "wasm32"
is KonanTarget.ZEPHYR -> error("There is no support for ${target.name} target yet.")
KonanTarget.WASM32 -> "generic"
is KonanTarget.ZEPHYR -> (configurables as ZephyrConfigurables).targetCpu ?: run {
context.reportCompilationWarning("targetCpu for target $target was not set. Targeting `generic` cpu.")
"generic"
}
}
val cpuFeatures: String = when (target) {
@@ -92,6 +98,8 @@ private class LlvmPipelineConfiguration(context: Context) {
}
val sizeLevel: Int = when {
target is KonanTarget.ZEPHYR ||
target == KonanTarget.WASM32 -> 3
context.shouldOptimize() -> 0
context.shouldContainDebugInfo() -> 0
else -> 0
@@ -108,20 +116,6 @@ private class LlvmPipelineConfiguration(context: Context) {
val codeModel: LLVMCodeModel = LLVMCodeModel.LLVMCodeModelDefault
}
// Since we are in a "closed world" internalization and global dce
// can be safely used to reduce size of a bitcode.
internal fun runClosedWorldCleanup(context: Context) {
initializeLlvmGlobalPassRegistry()
val llvmModule = context.llvmModule!!
val modulePasses = LLVMCreatePassManager()
if (context.llvmModuleSpecification.isFinal) {
LLVMAddInternalizePass(modulePasses, 0)
}
LLVMAddGlobalDCEPass(modulePasses)
LLVMRunPassManager(modulePasses, llvmModule)
LLVMDisposePassManager(modulePasses)
}
internal fun runLlvmOptimizationPipeline(context: Context) {
val llvmModule = context.llvmModule!!
val config = LlvmPipelineConfiguration(context)
+10 -8
View File
@@ -18,28 +18,30 @@
# STM32 F4 Discovery
quadruple.zephyr_stm32f4_disco = armv7m-none-eabi
targetSysRoot.zephyr_stm32f4_disco = target-sysroot-2-wasm
entrySelector.zephyr_stm32f4_disco = --defsym main=Konan_main
targetSysRoot.zephyr_stm32f4_disco = target-sysroot-3-wasm
boardSpecificClangFlags.zephyr_stm32f4_disco = -mabi=aapcs -mthumb -mcpu=cortex-m4
boardSpecificClangFlags.zephyr_stm32f4_disco = -mthumb
targetCpu.zephyr_stm32f4_disco = cortex-m4
targetAbi.zephyr_stm32f4_disco = aapcs
clangFlags = -Os
targetToolchain.linux_x64-zephyr_stm32f4_disco = gcc-arm-none-eabi-7-2017-q4-major-linux/arm-none-eabi
dependencies.linux_x64-zephyr_stm32f4_disco = \
libffi-3.2.1-2-linux-x86-64 \
clang-llvm-6.0.1-linux-x86-64 \
clang-llvm-8.0.0-linux-x86-64 \
gcc-arm-none-eabi-7-2017-q4-major-linux \
target-sysroot-2-wasm
target-sysroot-3-wasm
targetToolchain.macos_x64-zephyr_stm32f4_disco = gcc-arm-none-eabi-7-2017-q4-major-mac/arm-none-eabi
dependencies.macos_x64-zephyr_stm32f4_disco = \
gcc-arm-none-eabi-7-2017-q4-major-mac \
libffi-3.2.1-3-darwin-macos \
clang-llvm-6.0.1-darwin-macos \
target-sysroot-2-wasm
clang-llvm-apple-8.0.0-darwin-macos \
target-sysroot-3-wasm
targetToolchain.mingw_x64-zephyr_stm32f4_disco = gcc-arm-none-eabi-7-2017-q4-major-win32/arm-none-eabi
dependencies.mingw_x64-zephyr_stm32f4_disco = \
libffi-3.2.1-mingw-w64-x86-64 \
msys2-mingw-w64-x86_64-clang-llvm-lld-compiler_rt-8.0.1 \
gcc-arm-none-eabi-7-2017-q4-major-win32 \
target-sysroot-2-wasm
target-sysroot-3-wasm
+1 -1
View File
@@ -1558,7 +1558,7 @@ void garbageCollect(MemoryState* state, bool force) {
size_t beforeDecrements = state->toRelease->size();
decrementStack(state);
size_t afterDecrements = state->toRelease->size();
ssize_t stackReferences = afterDecrements - beforeDecrements;
long stackReferences = afterDecrements - beforeDecrements;
if (state->gcErgonomics && stackReferences * 5 > state->gcThreshold) {
increaseGcThreshold(state);
GC_LOG("||| GC: too many stack references, increased threshold to \n", state->gcThreshold);
+2
View File
@@ -2,6 +2,8 @@
BOARD=stm32f4_disco
export ZEPHYR_BASE="PLEASE_SET_ZEPHYR_BASE"
# By default `modules` directory is installed alongside ZEPHYR_BASE
export ZEPHYR_MODULES_DIR="PLEASE_SET_ZEPHYR_MODULES_TOO"
if [ "$ZEPHYR_BASE" == "PLEASE_SET_ZEPHYR_BASE" ] ; then
echo "Please set ZEPHYR_BASE in this build.sh."
@@ -26,9 +26,9 @@ cinterop -def $DIR/c_interop/platforms/$BOARD.def \
-compilerOpts -I$ZEPHYR_BASE/include \
-compilerOpts -I$ZEPHYR_BASE/include/drivers \
-compilerOpts -I$ZEPHYR_BASE/ext/hal/cmsis/Include \
-compilerOpts -I$ZEPHYR_BASE/ext/hal/st/stm32cube/stm32f4xx/soc \
-compilerOpts -I$ZEPHYR_BASE/ext/hal/st/stm32cube/stm32f4xx/drivers/include \
-compilerOpts -I$ZEPHYR_BASE/ext/hal/st/stm32cube/stm32f4xx/drivers/include/Legacy \
-compilerOpts -IZEPHYR_MODULES_DIR/hal/stm32/stm32cube/stm32f4xx/soc \
-compilerOpts -IZEPHYR_MODULES_DIR/hal/stm32/stm32cube/stm32f4xx/drivers/include \
-compilerOpts -IZEPHYR_MODULES_DIR/hal/stm32/stm32cube/stm32f4xx/drivers/include/Legacy \
-compilerOpts -I$ZEPHYR_BASE/drivers \
-compilerOpts -I$DIR/build/zephyr/include/generated \
-compilerOpts -I$DIR/build/zephyr/include/generated/syscalls \
@@ -151,7 +151,7 @@ class ClangArgs(private val configurables: Configurables) : Configurables by con
"-isystem$absoluteTargetSysRoot/include/libcxx",
"-isystem$absoluteTargetSysRoot/include/libc"
) +
(configurables as ZephyrConfigurables).boardSpecificClangFlags
(configurables as ZephyrConfigurables).constructClangArgs()
}
return result
}
@@ -39,7 +39,6 @@ interface Configurables : TargetableExternalStorage {
val libffiDir get() = hostString("libffiDir")
// TODO: Delegate to a map?
val entrySelector get() = targetList("entrySelector")
val linkerOptimizationFlags get() = targetList("linkerOptimizationFlags")
val linkerKonanFlags get() = targetList("linkerKonanFlags")
val linkerNoDebugFlags get() = targetList("linkerNoDebugFlags")
@@ -87,8 +86,8 @@ interface AndroidConfigurables : TargetableConfigurables, ClangFlags
interface WasmConfigurables : TargetableConfigurables, ClangFlags, LldFlags
// Codegen for Zephyr calls opt and llc with predefined set of flags
// so there is no need for OptFlags or LlcFlags.
interface ZephyrConfigurables : TargetableConfigurables {
interface ZephyrConfigurables : TargetableConfigurables, ClangFlags {
val boardSpecificClangFlags get() = targetList("boardSpecificClangFlags")
val targetCpu get() = targetString("targetCpu")
val targetAbi get() = targetString("targetAbi")
}
@@ -0,0 +1,23 @@
package org.jetbrains.kotlin.konan.target
fun ZephyrConfigurables.constructClangArgs(): List<String> = mutableListOf<String>().apply {
targetCpu?.let {
add("-mcpu=$it")
}
targetAbi?.let {
add("-mabi=$it")
}
addAll(boardSpecificClangFlags)
}
fun ZephyrConfigurables.constructClangCC1Args(): List<String> = mutableListOf<String>().apply {
addAll("-cc1 -emit-obj -disable-llvm-optzns -x ir -fdata-sections -ffunction-sections".split(" "))
targetCpu?.let {
addAll(listOf("-target-abi", it))
}
targetAbi?.let {
addAll(listOf("-target-cpu", it))
}
addAll(boardSpecificClangFlags)
}
@@ -37,7 +37,7 @@ class ZephyrConfigurablesImpl(target: KonanTarget, properties: Properties, baseD
: ZephyrConfigurables, KonanPropertiesLoader(target, properties, baseDir)
fun loadConfigurables(target: KonanTarget, properties: Properties, baseDir: String?) = when (target) {
fun loadConfigurables(target: KonanTarget, properties: Properties, baseDir: String?): Configurables = when (target) {
KonanTarget.LINUX_X64, KonanTarget.LINUX_ARM32_HFP, KonanTarget.LINUX_ARM64 ->
LinuxConfigurablesImpl(target, properties, baseDir)