diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/LlvmAttributes.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/LlvmAttributes.kt index 4cd156f89d8..44fcea358dd 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/LlvmAttributes.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/LlvmAttributes.kt @@ -22,6 +22,7 @@ internal fun addLlvmFunctionWithDefaultAttributes( type: LLVMTypeRef ): LLVMValueRef = LLVMAddFunction(module, name, type)!!.also { addDefaultLlvmFunctionAttributes(context, it) + addTargetCpuAndFeaturesAttributes(context, it) } /** @@ -35,6 +36,18 @@ private fun addDefaultLlvmFunctionAttributes(context: Context, llvmFunction: LLV } } +/** + * Set target cpu and its features to make LLVM generate correct machine code. + */ +private fun addTargetCpuAndFeaturesAttributes(context: Context, llvmFunction: LLVMValueRef) { + context.config.platform.targetCpu?.let { + LLVMAddTargetDependentFunctionAttr(llvmFunction, "target-cpu", it) + } + context.config.platform.targetCpuFeatures?.let { + LLVMAddTargetDependentFunctionAttr(llvmFunction, "target-features", it) + } +} + internal fun addLlvmAttributesForKotlinFunction(context: Context, irFunction: IrFunction, llvmFunction: LLVMValueRef) { if (irFunction.returnType.isNothing()) { setFunctionNoReturn(llvmFunction) diff --git a/kotlin-native/konan/konan.properties b/kotlin-native/konan/konan.properties index 039e04511a2..c02b1b5af37 100644 --- a/kotlin-native/konan/konan.properties +++ b/kotlin-native/konan/konan.properties @@ -109,7 +109,7 @@ targetTriple.macos_x64 = x86_64-apple-macos targetSysRoot.macos_x64 = target-sysroot-xcode_12_5-macosx targetCpu.macos_x64 = core2 targetCpuFeatures.macos_x64=+cx16,+cx8,+fxsr,+mmx,+sahf,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87 -clangFlags.macos_x64 = -cc1 -emit-obj -disable-llvm-passes -x ir -target-cpu $targetCpu.macos_x64 +clangFlags.macos_x64 = -cc1 -emit-obj -disable-llvm-passes -x ir clangNooptFlags.macos_x64 = -O1 clangOptFlags.macos_x64 = -O3 clangDebugFlags.macos_x64 = -O0 @@ -147,7 +147,7 @@ targetTriple.macos_arm64 = arm64-apple-macos targetSysRoot.macos_arm64 = target-sysroot-xcode_12_5-macosx # TODO: Check Clang behaviour. targetCpu.macos_arm64 = cyclone -clangFlags.macos_arm64 = -cc1 -emit-obj -disable-llvm-passes -x ir -target-cpu $targetCpu.macos_arm64 +clangFlags.macos_arm64 = -cc1 -emit-obj -disable-llvm-passes -x ir clangNooptFlags.macos_arm64 = -O1 clangOptFlags.macos_arm64 = -O3 # See clangDebugFlags.ios_arm64 @@ -188,7 +188,7 @@ targetTriple.ios_arm32 = armv7-apple-ios # Shared with 64-bit version. targetSysRoot.ios_arm32 = target-sysroot-xcode_12_5-iphoneos targetCpu.ios_arm32 = generic -clangFlags.ios_arm32 = -cc1 -emit-obj -disable-llvm-optzns -x ir -target-cpu $targetCpu.ios_arm32 +clangFlags.ios_arm32 = -cc1 -emit-obj -disable-llvm-optzns -x ir clangNooptFlags.ios_arm32 = -O1 clangOptFlags.ios_arm32 = -O3 clangDebugFlags.ios_arm32 = -O0 @@ -218,7 +218,7 @@ target-sysroot-xcode_12_5-iphoneos.default = \ targetTriple.ios_arm64 = arm64-apple-ios targetSysRoot.ios_arm64 = target-sysroot-xcode_12_5-iphoneos targetCpu.ios_arm64 = cyclone -clangFlags.ios_arm64 = -cc1 -emit-obj -disable-llvm-passes -x ir -target-cpu $targetCpu.ios_arm64 +clangFlags.ios_arm64 = -cc1 -emit-obj -disable-llvm-passes -x ir clangNooptFlags.ios_arm64 = -O1 clangOptFlags.ios_arm64 = -O3 # We force LLVM to use SelectionDAG because fast isel may miscompile sign extension. @@ -247,7 +247,7 @@ target-sysroot-xcode_12_5-iphonesimulator.default = \ targetTriple.ios_x64 = x86_64-apple-ios-simulator targetSysRoot.ios_x64 = target-sysroot-xcode_12_5-iphonesimulator targetCpu.ios_x64 = core2 -clangFlags.ios_x64 = -cc1 -emit-obj -disable-llvm-passes -x ir -target-cpu $targetCpu.ios_x64 +clangFlags.ios_x64 = -cc1 -emit-obj -disable-llvm-passes -x ir clangNooptFlags.ios_x64 = -O1 clangOptFlags.ios_x64 = -O3 clangDebugFlags.ios_x64 = -O0 @@ -273,7 +273,7 @@ target-sysroot-xcode_12_5-ios_simulator_arm64.default = \ targetTriple.ios_simulator_arm64 = arm64-apple-ios-simulator targetSysRoot.ios_simulator_arm64 = target-sysroot-xcode_12_5-iphonesimulator targetCpu.ios_simulator_arm64 = cyclone -clangFlags.ios_simulator_arm64 = -cc1 -emit-obj -disable-llvm-passes -x ir -target-cpu $targetCpu.ios_simulator_arm64 +clangFlags.ios_simulator_arm64 = -cc1 -emit-obj -disable-llvm-passes -x ir clangNooptFlags.ios_simulator_arm64 = -O1 clangOptFlags.ios_simulator_arm64 = -O3 clangDebugFlags.ios_simulator_arm64 = -O0 -mllvm -fast-isel=false -mllvm -global-isel=false @@ -298,7 +298,7 @@ target-sysroot-xcode_12_5-appletvsimulator.default = \ targetTriple.tvos_x64 = x86_64-apple-tvos-simulator targetSysRoot.tvos_x64 = target-sysroot-xcode_12_5-appletvsimulator targetCpu.tvos_x64 = core2 -clangFlags.tvos_x64 = -cc1 -emit-obj -disable-llvm-passes -x ir -target-cpu $targetCpu.tvos_x64 +clangFlags.tvos_x64 = -cc1 -emit-obj -disable-llvm-passes -x ir clangNooptFlags.tvos_x64 = -O1 clangOptFlags.tvos_x64 = -O3 clangDebugFlags.tvos_x64 = -O0 @@ -323,7 +323,7 @@ target-sysroot-xcode_12_5-tvos_simulator_arm64.default = \ targetTriple.tvos_simulator_arm64 = arm64-apple-tvos-simulator targetSysRoot.tvos_simulator_arm64 = target-sysroot-xcode_12_5-appletvsimulator targetCpu.tvos_simulator_arm64 = cyclone -clangFlags.tvos_simulator_arm64 = -cc1 -emit-obj -disable-llvm-passes -x ir -target-cpu $targetCpu.tvos_simulator_arm64 +clangFlags.tvos_simulator_arm64 = -cc1 -emit-obj -disable-llvm-passes -x ir clangNooptFlags.tvos_simulator_arm64 = -O1 clangOptFlags.tvos_simulator_arm64 = -O3 clangDebugFlags.tvos_simulator_arm64 = -O0 -mllvm -fast-isel=false -mllvm -global-isel=false @@ -348,7 +348,7 @@ target-sysroot-xcode_12_5-appletvos.default = \ targetTriple.tvos_arm64 = arm64-apple-tvos targetSysRoot.tvos_arm64 = target-sysroot-xcode_12_5-appletvos targetCpu.tvos_arm64 = cyclone -clangFlags.tvos_arm64 = -cc1 -emit-obj -disable-llvm-passes -x ir -target-cpu $targetCpu.tvos_arm64 +clangFlags.tvos_arm64 = -cc1 -emit-obj -disable-llvm-passes -x ir clangNooptFlags.tvos_arm64 = -O1 clangOptFlags.tvos_arm64 = -O3 clangDebugFlags.tvos_arm64 = -O0 -mllvm -fast-isel=false -mllvm -global-isel=false @@ -373,7 +373,7 @@ target-sysroot-xcode_12_5-watchos.default = \ targetTriple.watchos_arm32 = armv7k-apple-watchos targetSysRoot.watchos_arm32 = target-sysroot-xcode_12_5-watchos targetCpu.watchos_arm32 = cortex-a7 -clangFlags.watchos_arm32 = -cc1 -emit-obj -disable-llvm-passes -x ir -target-cpu $targetCpu.watchos_arm32 +clangFlags.watchos_arm32 = -cc1 -emit-obj -disable-llvm-passes -x ir clangNooptFlags.watchos_arm32 = -O1 clangOptFlags.watchos_arm32 = -O3 clangDebugFlags.watchos_arm32 = -O0 @@ -396,9 +396,8 @@ targetToolchain.macos_arm64-watchos_arm64 = target-toolchain-xcode_12_5 targetTriple.watchos_arm64 = arm64_32-apple-watchos targetSysRoot.watchos_arm64 = target-sysroot-xcode_12_5-watchos +# TODO: Use -target-cpu=apple-s4 when we start generating bitcode for arm64_32. targetCpu.watchos_arm64 = cortex-a7 -# TODO: Use -target-cpu=apple-s4 after LLVM update. -# Note that we can't use $targetCpu.watchos_arm64 because bitcode is generated for watchos_arm32. clangFlags.watchos_arm64 = -cc1 -emit-obj -disable-llvm-passes -x ir -mllvm -aarch64-watch-bitcode-compatibility \ -mllvm -arm-bitcode-compatibility -mllvm -fast-isel=false -mllvm -global-isel=false clangNooptFlags.watchos_arm64 = -O1 @@ -426,12 +425,9 @@ target-sysroot-xcode_12_5-watchsimulator.default = \ targetTriple.watchos_x86 = i386-apple-watchos-simulator targetSysRoot.watchos_x86 = target-sysroot-xcode_12_5-watchsimulator -# -target-cpu pentium4 makes sure that code-generator knows which CPU flavour to emit code for. -# Can be seen on optimized build and FP tests, where value is passed on stack -# by Kotlin and taken from SSE registers by C code. -# TODO: once this information is available in function attributes, we can remove this flag. +# Make sure that SSE2 is available by setting target-cpu to pentium4 targetCpu.watchos_x86 = pentium4 -clangFlags.watchos_x86 = -cc1 -emit-obj -disable-llvm-passes -x ir -target-cpu $targetCpu.watchos_x86 +clangFlags.watchos_x86 = -cc1 -emit-obj -disable-llvm-passes -x ir clangNooptFlags.watchos_x86 = -O1 clangOptFlags.watchos_x86 = -O3 clangDebugFlags.watchos_x86 = -O0 @@ -456,7 +452,7 @@ target-sysroot-xcode_12_5-watchos_x64.default = \ targetTriple.watchos_x64 = x86_64-apple-watchos-simulator targetSysRoot.watchos_x64 = target-sysroot-xcode_12_5-watchsimulator targetCpu.watchos_x64 = core2 -clangFlags.watchos_x64 = -cc1 -emit-obj -disable-llvm-passes -x ir -target-cpu $targetCpu.watchos_x64 +clangFlags.watchos_x64 = -cc1 -emit-obj -disable-llvm-passes -x ir clangNooptFlags.watchos_x64 = -O1 clangOptFlags.watchos_x64 = -O3 clangDebugFlags.watchos_x64 = -O0 @@ -481,7 +477,7 @@ target-sysroot-xcode_12_5-watchos_simulator_arm64.default = \ targetTriple.watchos_simulator_arm64 = arm64-apple-watchos-simulator targetSysRoot.watchos_simulator_arm64 = target-sysroot-xcode_12_5-watchsimulator targetCpu.watchos_simulator_arm64 = cyclone -clangFlags.watchos_simulator_arm64 = -cc1 -emit-obj -disable-llvm-passes -x ir -target-cpu $targetCpu.watchos_simulator_arm64 +clangFlags.watchos_simulator_arm64 = -cc1 -emit-obj -disable-llvm-passes -x ir clangNooptFlags.watchos_simulator_arm64 = -O1 clangOptFlags.watchos_simulator_arm64 = -O3 clangDebugFlags.watchos_simulator_arm64 = -O0 -mllvm -fast-isel=false -mllvm -global-isel=false @@ -526,7 +522,7 @@ targetSysRoot.linux_x64 = $gccToolchain.linux_x64/x86_64-unknown-linux-gnu/sysro libGcc.linux_x64 = ../../lib/gcc/x86_64-unknown-linux-gnu/8.3.0 targetCpu.linux_x64 = x86-64 targetCpuFeatures.linux_x64 = +cx8,+fxsr,+mmx,+sse,+sse2,+x87 -clangFlags.linux_x64 = -cc1 -target-cpu $targetCpu.linux_x64 -emit-obj -disable-llvm-optzns -x ir \ +clangFlags.linux_x64 = -cc1 -emit-obj -disable-llvm-optzns -x ir \ -ffunction-sections -fdata-sections clangNooptFlags.linux_x64 = -O1 clangOptFlags.linux_x64 = -O3 @@ -593,7 +589,7 @@ targetCpu.linux_arm32_hfp = arm1136jf-s # Taken from clang-produced bitcode. # TODO: Not sure if we need so many features. Revise. targetCpuFeatures.linux_arm32_hfp = +armv6kz,+dsp,+fp64,+strict-align,+vfp2,+vfp2sp,-crypto,-d32,-fp-armv8,-fp-armv8d16,-fp-armv8d16sp,-fp-armv8sp,-fp16,-fp16fml,-fullfp16,-neon,-thumb-mode,-vfp3,-vfp3d16,-vfp3d16sp,-vfp3sp,-vfp4,-vfp4d16,-vfp4d16sp,-vfp4sp -clangFlags.linux_arm32_hfp = -cc1 -target-cpu $targetCpu.linux_arm32_hfp -mfloat-abi hard -emit-obj -disable-llvm-optzns -x ir +clangFlags.linux_arm32_hfp = -cc1 -mfloat-abi hard -emit-obj -disable-llvm-optzns -x ir clangNooptFlags.linux_arm32_hfp = -O1 clangOptFlags.linux_arm32_hfp = -O3 -ffunction-sections clangDebugFlags.linux_arm32_hfp = -O0 @@ -649,7 +645,7 @@ linkerKonanFlags.linux_arm64 = -Bstatic -lstdc++ -Bdynamic -ldl -lm -lpthread \ # targetSysroot-relative. libGcc.linux_arm64 = ../../lib/gcc/aarch64-unknown-linux-gnu/8.3.0 targetCpu.linux_arm64 = cortex-a57 -clangFlags.linux_arm64 = -cc1 -target-cpu $targetCpu.linux_arm64 -emit-obj -disable-llvm-optzns -x ir +clangFlags.linux_arm64 = -cc1 -emit-obj -disable-llvm-optzns -x ir clangNooptFlags.linux_arm64 = -O1 clangOptFlags.linux_arm64 = -O3 -ffunction-sections clangDebugFlags.linux_arm64 = -O0 @@ -695,7 +691,8 @@ libGcc.linux_mips32 = ../../lib/gcc/mips-unknown-linux-gnu/8.3.0 targetCpu.linux_mips32 = mips32r2 # We generate a single binary with a big GOT. It causes problems for MIPS. # See https://gcc.gnu.org/onlinedocs/gcc/MIPS-Options.html about mxgot (aka -target-feature +xgot). -clangFlags.linux_mips32 = -cc1 -emit-obj -disable-llvm-optzns -x ir -target-cpu $targetCpu.linux_mips32 -target-feature +xgot +targetCpuFeatures.linux_mips32 = +xgot +clangFlags.linux_mips32 = -cc1 -emit-obj -disable-llvm-optzns -x ir clangNooptFlags.linux_mips32 = -O1 clangOptFlags.linux_mips32 = -O3 -ffunction-sections clangDebugFlags.linux_mips32 = -O0 @@ -734,7 +731,8 @@ linkerKonanFlags.linux_mipsel32 = -Bstatic -lstdc++ -Bdynamic -ldl -lm -lpthread # targetSysroot-relative. libGcc.linux_mipsel32 = ../../lib/gcc/mipsel-unknown-linux-gnu/8.3.0 targetCpu.linux_mipsel32 = mips32r2 -clangFlags.linux_mipsel32 = -cc1 -emit-obj -disable-llvm-optzns -x ir -target-cpu $targetCpu.linux_mipsel32 -target-feature +xgot +targetCpuFeatures.linux_mipsel32 = +xgot +clangFlags.linux_mipsel32 = -cc1 -emit-obj -disable-llvm-optzns -x ir clangNooptFlags.linux_mipsel32 = -O1 clangOptFlags.linux_mipsel32 = -O3 -ffunction-sections clangDebugFlags.linux_mipsel32 = -O0 @@ -773,7 +771,7 @@ dependencies.mingw_x64-android_arm32 = \ targetTriple.android_arm32 = arm-unknown-linux-androideabi targetCpu.android_arm32 = arm7tdmi targetCpuFeatures.android_arm32 = +soft-float,+strict-align,-crypto,-neon,-thumb-mode -clangFlags.android_arm32 = -cc1 -target-cpu $targetCpu.android_arm32 -emit-obj -disable-llvm-optzns -x ir -femulated-tls -mrelocation-model pic +clangFlags.android_arm32 = -cc1 -emit-obj -disable-llvm-optzns -x ir -femulated-tls -mrelocation-model pic clangOptFlags.android_arm32 = -O3 -ffunction-sections linkerNoDebugFlags.android_arm32 = -Wl,-S clangNooptFlags.android_arm32 = -O1 @@ -802,7 +800,7 @@ dependencies.mingw_x64-android_arm64 = \ targetTriple.android_arm64 = aarch64-unknown-linux-android targetCpu.android_arm64 = cortex-a57 -clangFlags.android_arm64 = -cc1 -target-cpu $targetCpu.android_arm64 -emit-obj -disable-llvm-passes -x ir -femulated-tls -mrelocation-model pic +clangFlags.android_arm64 = -cc1 -emit-obj -disable-llvm-passes -x ir -femulated-tls -mrelocation-model pic clangOptFlags.android_arm64 = -O3 -ffunction-sections clangNooptFlags.android_arm64 = -O1 targetSysRoot.android_arm64 = target-sysroot-1-android_ndk @@ -830,12 +828,9 @@ dependencies.mingw_x64-android_x86 = \ target-toolchain-2-windows-android_ndk targetTriple.android_x86 = i686-unknown-linux-android -# -target-cpu pentium4 makes sure that cogenerator knows which CPU flavour to emit code for. -# Can be seen on optimized build and FP tests, where value is passed on stack -# by Kotlin and taken from SSE registers by C code. -# TODO: once this information is available in function attributes, we can set target CPU to i686. +# Make sure that SSE2 is available by setting target-cpu to pentium4 targetCpu.android_x86 = pentium4 -clangFlags.android_x86 = -cc1 -target-cpu $targetCpu.android_x86 -emit-obj -disable-llvm-passes -x ir -femulated-tls -mrelocation-model pic +clangFlags.android_x86 = -cc1 -emit-obj -disable-llvm-passes -x ir -femulated-tls -mrelocation-model pic clangOptFlags.android_x86 = -O3 -ffunction-sections clangNooptFlags.android_x86 = -O1 targetSysRoot.android_x86 = target-sysroot-1-android_ndk @@ -864,7 +859,7 @@ dependencies.mingw_x64-android_x64 = \ targetTriple.android_x64 = x86_64-unknown-linux-android targetCpu.android_x64 = x86-64 -clangFlags.android_x64 = -cc1 -target-cpu $targetCpu.android_x64 -emit-obj -disable-llvm-passes -x ir -femulated-tls -mrelocation-model pic +clangFlags.android_x64 = -cc1 -emit-obj -disable-llvm-passes -x ir -femulated-tls -mrelocation-model pic clangOptFlags.android_x64 = -O3 -ffunction-sections clangNooptFlags.android_x64 = -O1 targetSysRoot.android_x64 = target-sysroot-1-android_ndk @@ -902,7 +897,7 @@ targetCpuFeatures.mingw_x64 = +cx8,+fxsr,+mmx,+sse,+sse2,+x87 # For using with Universal Windows Platform (UWP) we need to use this slower option. # See https://youtrack.jetbrains.com/issue/KT-27654. # TODO: remove, once fixed in mingw, check with the bug testcase. -clangFlags.mingw_x64 = -cc1 -emit-obj -disable-llvm-passes -x ir -femulated-tls -target-cpu $targetCpu.mingw_x64 +clangFlags.mingw_x64 = -cc1 -emit-obj -disable-llvm-passes -x ir -femulated-tls clangOptFlags.mingw_x64 = -O3 -ffunction-sections clangNooptFlags.mingw_x64 = -O1 linkerNoDebugFlags.mingw_x64 = -Wl,-S @@ -930,15 +925,12 @@ dependencies.macos_x64-mingw_x86 = \ targetTriple.mingw_x86 = i686-pc-windows-gnu targetSysRoot.mingw_x86 = msys2-mingw-w64-i686-clang-llvm-lld-compiler_rt-8.0.1 +# Make sure that SSE2 is available by setting target-cpu to pentium4 +targetCpu.mingw_x86 = pentium4 # For using with Universal Windows Platform (UWP) we need to use slower -emulated-tls option. # See https://youtrack.jetbrains.com/issue/KT-27654. # TODO: remove, once fixed in mingw, check with the bug testcase. -# -target-cpu pentium4 makes sure that code-generator knows which CPU flavour to emit code for. -# Can be seen on optimized build and FP tests, where value is passed in coprocessor registers -# by Kotlin and taken from SSE registers by C code. -# TODO: once this information is available in function attributes, we can remove this flag. -targetCpu.mingw_x86 = pentium4 -clangFlags.mingw_x86 = -cc1 -emit-obj -disable-llvm-passes -x ir -femulated-tls -target-cpu $targetCpu.mingw_x86 +clangFlags.mingw_x86 = -cc1 -emit-obj -disable-llvm-passes -x ir -femulated-tls clangOptFlags.mingw_x86 = -O3 -ffunction-sections clangNooptFlags.mingw_x86 = -O1 linkerNoDebugFlags.mingw_x86 = -Wl,-S @@ -975,7 +967,7 @@ dependencies.mingw_x64-wasm32 = \ targetTriple.wasm32 = wasm32-unknown-unknown targetSysRoot.wasm32 = target-sysroot-4-embedded targetCpu.wasm32 = generic -clangFlags.wasm32 = -cc1 -emit-obj -disable-llvm-optzns -x ir -target-cpu $targetCpu.wasm32 +clangFlags.wasm32 = -cc1 -emit-obj -disable-llvm-optzns -x ir clangNooptFlags.wasm32 = -O1 clangOptFlags.wasm32 = -O3 clangDebugFlags.wasm32 = -O0