diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.kt index 9252dd0ba04..8428405e9f1 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.kt @@ -124,7 +124,7 @@ class K2JVMCompilerArguments : CommonCompilerArguments() { "0 means use a thread per processor core.\n" + "Default value is 1" ) - var parallelBackendThreads: String by FreezableVar("1") + var backendThreads: String by FreezableVar("1") @Argument(value = "-Xmodule-path", valueDescription = "", description = "Paths where to find Java 9+ modules") var javaModulePath: String? by NullableStringFreezableVar(null) @@ -505,7 +505,7 @@ Also sets `-jvm-target` value equal to the selected JDK version""" @Argument( value = "-Xlink-via-signatures", - description = "Link JVM IR symbols via signatures, instead of descriptors. \n" + + description = "Link JVM IR symbols via signatures, instead of descriptors.\n" + "This mode is slower, but can be useful in troubleshooting problems with the JVM IR backend" ) var linkViaSignatures: Boolean by FreezableVar(false) diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/jvmArguments.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/jvmArguments.kt index 96334961c11..66eaf0b7d26 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/jvmArguments.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/jvmArguments.kt @@ -321,11 +321,27 @@ fun CompilerConfiguration.configureAdvancedJvmOptions(arguments: K2JVMCompilerAr arguments.declarationsOutputPath?.let { put(JVMConfigurationKeys.DECLARATIONS_JSON_PATH, it) } - val nThreadsRaw = arguments.parallelBackendThreads.toIntOrNull() ?: 1 + val nThreadsRaw = parseBackendThreads(arguments.backendThreads, messageCollector) val nThreads = if (nThreadsRaw == 0) Runtime.getRuntime().availableProcessors() else nThreadsRaw + if (nThreads > 1) { + messageCollector.report(LOGGING, "Running backend in parallel with $nThreads threads") + } put(CommonConfigurationKeys.PARALLEL_BACKEND_THREADS, nThreads) } +private fun parseBackendThreads(stringValue: String, messageCollector: MessageCollector): Int { + val value = stringValue.toIntOrNull() + if (value == null) { + messageCollector.report(ERROR, "Cannot parse -Xbackend-threads value: \"$stringValue\". Please use an integer number") + return 1 + } + if (value < 0) { + messageCollector.report(ERROR, "-Xbackend-threads value cannot be negative") + return 1 + } + return value +} + fun CompilerConfiguration.configureKlibPaths(arguments: K2JVMCompilerArguments) { val libraries = arguments.klibLibraries ?: return assert(arguments.useIR && !arguments.useOldBackend) { "Klib libraries can only be used with IR backend" } diff --git a/compiler/testData/cli/jvm/extraHelp.out b/compiler/testData/cli/jvm/extraHelp.out index 79293ed46d0..c5fb0eeaae0 100644 --- a/compiler/testData/cli/jvm/extraHelp.out +++ b/compiler/testData/cli/jvm/extraHelp.out @@ -16,6 +16,9 @@ where advanced options include: -Xassertions=jvm: enable, depend on jvm assertion settings; -Xassertions=legacy: calculate condition on each call, check depends on jvm assertion settings in the kotlin package; default: legacy + -Xbackend-threads= When using the IR backend, run lowerings by file in N parallel threads. + 0 means use a thread per processor core. + Default value is 1 -Xbuild-file= Path to the .xml build file to compile -Xcompile-java Reuse javac analysis and compile Java source files -Xdump-declarations-to= Path to JSON file to dump Java to Kotlin declaration mappings @@ -99,9 +102,6 @@ where advanced options include: * ignore * strict * warn (report a warning) - -Xbackend-threads= When using the IR backend, run lowerings by file in N parallel threads. - 0 means use a thread per processor core. - Default value is 1 -Xprofile= Debug option: Run compiler with async profiler and save snapshots to `outputDir`; `command` is passed to async-profiler on start. `profilerPath` is a path to libasyncProfiler.so; async-profiler.jar should be on the compiler classpath. diff --git a/compiler/testData/cli/jvm/kt51846_backendThreadsValidation1.args b/compiler/testData/cli/jvm/kt51846_backendThreadsValidation1.args new file mode 100644 index 00000000000..95c0fc1327e --- /dev/null +++ b/compiler/testData/cli/jvm/kt51846_backendThreadsValidation1.args @@ -0,0 +1,4 @@ +$TESTDATA_DIR$/simple.kt +-d +$TEMP_DIR$ +-Xbackend-threads=2 diff --git a/compiler/testData/cli/jvm/kt51846_backendThreadsValidation1.out b/compiler/testData/cli/jvm/kt51846_backendThreadsValidation1.out new file mode 100644 index 00000000000..d86bac9de59 --- /dev/null +++ b/compiler/testData/cli/jvm/kt51846_backendThreadsValidation1.out @@ -0,0 +1 @@ +OK diff --git a/compiler/testData/cli/jvm/kt51846_backendThreadsValidation2.args b/compiler/testData/cli/jvm/kt51846_backendThreadsValidation2.args new file mode 100644 index 00000000000..14670c7b437 --- /dev/null +++ b/compiler/testData/cli/jvm/kt51846_backendThreadsValidation2.args @@ -0,0 +1,4 @@ +$TESTDATA_DIR$/simple.kt +-d +$TEMP_DIR$ +-Xbackend-threads=abcd diff --git a/compiler/testData/cli/jvm/kt51846_backendThreadsValidation2.out b/compiler/testData/cli/jvm/kt51846_backendThreadsValidation2.out new file mode 100644 index 00000000000..4e34b349d2e --- /dev/null +++ b/compiler/testData/cli/jvm/kt51846_backendThreadsValidation2.out @@ -0,0 +1,2 @@ +error: cannot parse -Xbackend-threads value: "abcd". Please use an integer number +COMPILATION_ERROR diff --git a/compiler/testData/cli/jvm/kt51846_backendThreadsValidation3.args b/compiler/testData/cli/jvm/kt51846_backendThreadsValidation3.args new file mode 100644 index 00000000000..77ea2eb473c --- /dev/null +++ b/compiler/testData/cli/jvm/kt51846_backendThreadsValidation3.args @@ -0,0 +1,4 @@ +$TESTDATA_DIR$/simple.kt +-d +$TEMP_DIR$ +-Xbackend-threads=-3 diff --git a/compiler/testData/cli/jvm/kt51846_backendThreadsValidation3.out b/compiler/testData/cli/jvm/kt51846_backendThreadsValidation3.out new file mode 100644 index 00000000000..df5973cdf45 --- /dev/null +++ b/compiler/testData/cli/jvm/kt51846_backendThreadsValidation3.out @@ -0,0 +1,2 @@ +error: -Xbackend-threads value cannot be negative +COMPILATION_ERROR diff --git a/compiler/tests-gen/org/jetbrains/kotlin/cli/CliTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/cli/CliTestGenerated.java index e7565c1a1de..396a8be9af7 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/cli/CliTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/cli/CliTestGenerated.java @@ -689,6 +689,21 @@ public class CliTestGenerated extends AbstractCliTest { runTest("compiler/testData/cli/jvm/kt49209.args"); } + @TestMetadata("kt51846_backendThreadsValidation1.args") + public void testKt51846_backendThreadsValidation1() throws Exception { + runTest("compiler/testData/cli/jvm/kt51846_backendThreadsValidation1.args"); + } + + @TestMetadata("kt51846_backendThreadsValidation2.args") + public void testKt51846_backendThreadsValidation2() throws Exception { + runTest("compiler/testData/cli/jvm/kt51846_backendThreadsValidation2.args"); + } + + @TestMetadata("kt51846_backendThreadsValidation3.args") + public void testKt51846_backendThreadsValidation3() throws Exception { + runTest("compiler/testData/cli/jvm/kt51846_backendThreadsValidation3.args"); + } + @TestMetadata("languageVersion.args") public void testLanguageVersion() throws Exception { runTest("compiler/testData/cli/jvm/languageVersion.args");