Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for threading compiler flag to Rust target #1098

Merged
merged 6 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion org.lflang/src/org/lflang/generator/rust/RustGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class RustGenerator(

Files.createDirectories(fileConfig.srcGenPath)

val gen = RustModelBuilder.makeGenerationInfo(targetConfig, reactors)
val gen = RustModelBuilder.makeGenerationInfo(targetConfig, reactors, errorReporter)
val codeMaps: Map<Path, CodeMap> = RustEmitter.generateRustProject(fileConfig, gen)

if (targetConfig.noCompile || errorsOccurred()) {
Expand Down
40 changes: 29 additions & 11 deletions org.lflang/src/org/lflang/generator/rust/RustModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import java.nio.file.Path
import java.util.*

private typealias Ident = String
const val PARALLEL_RT_FEATURE = "parallel-runtime"

/** Root model class for the entire generation. */
data class GenerationInfo(
Expand Down Expand Up @@ -416,15 +417,15 @@ object RustModelBuilder {
/**
* Given the input to the generator, produce the model classes.
*/
fun makeGenerationInfo(targetConfig: TargetConfig, reactors: List<Reactor>): GenerationInfo {
fun makeGenerationInfo(targetConfig: TargetConfig, reactors: List<Reactor>, errorReporter: ErrorReporter): GenerationInfo {
val reactorsInfos = makeReactorInfos(reactors)
// todo how do we pick the main reactor? it seems like super.doGenerate sets that field...
val mainReactor = reactorsInfos.lastOrNull { it.isMain } ?: reactorsInfos.last()


val dependencies = targetConfig.rust.cargoDependencies.toMutableMap()
dependencies.compute(RustEmitterBase.runtimeCrateFullName) { _, spec ->
computeDefaultRuntimeConfiguration(spec, targetConfig)
computeDefaultRuntimeConfiguration(spec, targetConfig, errorReporter)
}

return GenerationInfo(
Expand Down Expand Up @@ -452,20 +453,28 @@ object RustModelBuilder {
private fun computeDefaultRuntimeConfiguration(
userSpec: CargoDependencySpec?,
targetConfig: TargetConfig,
errorReporter: ErrorReporter
): CargoDependencySpec {

val userRtVersion: String? = targetConfig.runtimeVersion

if (userSpec == null) {
// default configuration for the runtime crate
return if (targetConfig.externalRuntimePath != null) newCargoSpec(
gitTag = userRtVersion?.let { "v$it" },
localPath = targetConfig.externalRuntimePath,
) else newCargoSpec(
gitRepo = RustEmitterBase.runtimeGitUrl,

val userRtVersion: String? = targetConfig.runtimeVersion
// enable parallel feature if asked
val parallelFeature = listOf(PARALLEL_RT_FEATURE).takeIf { targetConfig.threading }

val spec = newCargoSpec(
gitTag = userRtVersion?.let { "v$it" },
rev = runtimeGitRevision.takeIf { userRtVersion == null },
features = parallelFeature,
)

if (targetConfig.externalRuntimePath != null) {
spec.localPath = targetConfig.externalRuntimePath
} else {
spec.gitRepo = RustEmitterBase.runtimeGitUrl
spec.rev = runtimeGitRevision.takeIf { userRtVersion == null }
}

return spec
} else {
if (userSpec.localPath == null && userSpec.gitRepo == null) {
// default the location
Expand All @@ -481,6 +490,15 @@ object RustModelBuilder {
userSpec.localPath = targetConfig.externalRuntimePath
}

// enable parallel feature if asked
if (targetConfig.threading && PARALLEL_RT_FEATURE !in userSpec.features) {
userSpec.features += PARALLEL_RT_FEATURE
}

if (!targetConfig.threading && PARALLEL_RT_FEATURE in userSpec.features) {
errorReporter.reportWarning("Threading cannot be disabled as it was enabled manually as a runtime feature.")
}

return userSpec
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
f35bcc79a5422eae256e9082314a05e9b4530bc4
0100686e1d0fcf648f7979e0fca2017e6b33a9a6
2 changes: 1 addition & 1 deletion test/Rust/src-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# If it causes problems, we can throw it away or hide it.
[workspace]
members = ["*"]
exclude = ["target", "multiport", "generics"]
exclude = ["concurrent", "target", "multiport", "generics"]

[profile.release-with-min-size] # use `build-type: MinSizeRel`
inherits = "release"
Expand Down
10 changes: 10 additions & 0 deletions test/Rust/src/concurrent/Workers.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
target Rust { workers: 16 };
main reactor {
reaction(startup) {=
if (ctx.num_workers() != 16) {
panic!("Expected to have 16 workers.");
} else {
println!("Using 16 workers.");
}
=}
}