-
Notifications
You must be signed in to change notification settings - Fork 9
Implement handling for the experiment annotation. #156
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| #include "marco/Codegen/Lowering/BaseModelica/ModelLowerer.h" | ||
| #include "marco/Dialect/BaseModelica/IR/Ops.h" | ||
|
|
||
| using namespace ::marco; | ||
| using namespace ::marco::codegen; | ||
|
|
@@ -146,6 +147,11 @@ bool ModelLowerer::lower(const ast::bmodelica::Model &model) { | |
| } | ||
| } | ||
|
|
||
| // Lower experiment annotation | ||
| if (!lowerExperimentAnnotation(modelOp, model)) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
|
|
@@ -269,4 +275,84 @@ bool ModelLowerer::lowerVariableAttributes( | |
|
|
||
| return true; | ||
| } | ||
|
|
||
| bool ModelLowerer::lowerExperimentAnnotation( | ||
| mlir::bmodelica::ModelOp modelOp, const ast::bmodelica::Model &model) { | ||
| if (!model.hasAnnotation()) { | ||
| return true; | ||
| } | ||
|
|
||
| const auto *const annotation = model.getAnnotation(); | ||
| const auto *const classModification = annotation->getProperties(); | ||
|
|
||
| std::optional<double> startTime, stopTime; | ||
| for (const auto &argumentNode : classModification->getArguments()) { | ||
| const auto *const elementModification = | ||
| argumentNode->cast<ast::bmodelica::Argument>() | ||
| ->dyn_cast<ast::bmodelica::ElementModification>(); | ||
|
|
||
| if (!elementModification) { | ||
| continue; | ||
| } | ||
| if (elementModification->getName() != "experiment") { | ||
| continue; | ||
| } | ||
| if (!elementModification->hasModification()) { | ||
| continue; | ||
| } | ||
|
|
||
| const auto *const experimentModification = | ||
| elementModification->getModification(); | ||
| if (!experimentModification->hasClassModification()) { | ||
| continue; | ||
| } | ||
| const auto *const experimentClassModification = | ||
| experimentModification->getClassModification(); | ||
|
|
||
| for (const auto &experimentArgumentNode : | ||
| experimentClassModification->getArguments()) { | ||
| const auto *const argument = | ||
| experimentArgumentNode->cast<ast::bmodelica::Argument>() | ||
| ->dyn_cast<ast::bmodelica::ElementModification>(); | ||
|
|
||
| const auto argumentName = argument->getName(); | ||
| if (argumentName != "StartTime" && argumentName != "StopTime") { | ||
| continue; | ||
| } | ||
|
|
||
| if (!argument->hasModification()) { | ||
| continue; | ||
| } | ||
| const auto *const argumentModification = argument->getModification(); | ||
|
|
||
| if (!argumentModification->hasExpression()) { | ||
| continue; | ||
| } | ||
| const auto *const constant = argumentModification->getExpression() | ||
| ->dyn_cast<ast::bmodelica::Constant>(); | ||
| if (!constant) { | ||
| continue; | ||
| } | ||
|
|
||
| const auto value = constant->as<double>(); | ||
| if (argumentName == "StartTime") { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe emit a warning in case of case-sensitive inequality but case-insensitive match? (not sure you can do it with the current infrastructure, but at least add it as a todo). |
||
| startTime = value; | ||
| } else if (argumentName == "StopTime") { | ||
| stopTime = value; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (startTime.has_value()) { | ||
| modelOp->setAttr(modelOp.getExperimentStartTimeAttrName(), | ||
| builder().getF64FloatAttr(startTime.value())); | ||
| } | ||
| if (stopTime.has_value()) { | ||
| modelOp->setAttr(modelOp.getExperimentEndTimeAttrName(), | ||
| builder().getF64FloatAttr(stopTime.value())); | ||
| } | ||
|
|
||
| // TODO: actually handle errors; right now we are *very* permissive | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't be too restrictive on this. The specification of the language is quite permissive by nature. If we have a match on the expected annotations tree and value types, then we emit the attribute. Otherwise (e.g., a string given as start time), just emit a warning rather than an error. |
||
| return true; | ||
| } | ||
| } // namespace marco::codegen::lowering::bmodelica | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // RUN: modelica-opt %s --split-input-file --convert-bmodelica-to-runtime | FileCheck %s | ||
|
|
||
| // COM: Model with both start and end time annotations. | ||
|
|
||
| // CHECK-LABEL: module { | ||
| // CHECK: runtime.start_time 0.000000e+00 | ||
| // CHECK: runtime.end_time 1.000000e+00 | ||
|
|
||
| bmodelica.model @Test attributes {experiment.startTime = 0.000000e+00 : f64, experiment.endTime = 1.000000e+00 : f64} { | ||
|
|
||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| // COM: Model with only start time annotation. | ||
|
|
||
| // CHECK-LABEL: module { | ||
| // CHECK: runtime.start_time 5.000000e-01 | ||
| // CHECK: runtime.end_time | ||
|
|
||
| bmodelica.model @Test2 attributes {experiment.startTime = 5.000000e-01 : f64} { | ||
|
|
||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| // COM: Model with only end time annotation. | ||
|
|
||
| // CHECK-LABEL: module { | ||
| // CHECK: runtime.start_time | ||
| // CHECK: runtime.end_time 2.000000e+00 | ||
|
|
||
| bmodelica.model @Test3 attributes {experiment.endTime = 2.000000e+00 : f64} { | ||
|
|
||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| // COM: Model without experiment annotations. | ||
|
|
||
| // CHECK-LABEL: module { | ||
| // CHECK: runtime.start_time | ||
| // CHECK: runtime.end_time | ||
|
|
||
| bmodelica.model @Test4 { | ||
|
|
||
| } |
Uh oh!
There was an error while loading. Please reload this page.