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

Fix compilation with Runge-Kutta #77

Merged
merged 4 commits into from
Dec 21, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,29 @@ struct TensorExtractOpInterface
}
};

struct TensorInsertOpInterface
: public EquationExpressionOpInterface::ExternalModel<
::TensorInsertOpInterface, TensorInsertOp> {
void printExpression(
mlir::Operation *op, llvm::raw_ostream &os,
const llvm::DenseMap<mlir::Value, int64_t> &inductions) const {
auto castedOp = mlir::cast<TensorInsertOp>(op);

::printExpression(os, castedOp.getValue(), inductions);
os << " into ";
::printExpression(os, castedOp.getDestination(), inductions);
os << "[";

llvm::interleaveComma(castedOp.getIndices(), os, [&](mlir::Value exp) {
::printExpression(os, exp, inductions);
});

os << "]";
}

DEFINE_DEFAULT_IS_EQUIVALENT(TensorInsertOp)
};

struct ArrayFromElementsOpInterface
: public EquationExpressionOpInterface::ExternalModel<
::ArrayFromElementsOpInterface, ArrayFromElementsOp> {
Expand Down Expand Up @@ -1980,6 +2003,7 @@ void registerEquationExpressionOpInterfaceExternalModels(
TensorBroadcastOp::attachInterface<::TensorBroadcastOpInterface>(*context);
TensorViewOp::attachInterface<::TensorViewOpInterface>(*context);
TensorExtractOp::attachInterface<::TensorExtractOpInterface>(*context);
TensorInsertOp::attachInterface<::TensorInsertOpInterface>(*context);

// Array operations.
ArrayFromElementsOp::attachInterface<::ArrayFromElementsOpInterface>(*context);
Expand Down
19 changes: 16 additions & 3 deletions lib/Dialect/BaseModelica/Transforms/RungeKutta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,8 @@ VariableOp declareSlopeVariable(mlir::OpBuilder &builder, VariableOp variableOp,
"__rk_k" + std::to_string(order) + "_" + variableOp.getSymName().str();

auto variableType =
VariableType::get(std::nullopt, RealType::get(builder.getContext()),
VariableType::get(variableOp.getVariableType().getShape(),
RealType::get(builder.getContext()),
VariabilityProperty::none, IOProperty::none);

return builder.create<VariableOp>(variableOp.getLoc(), name, variableType);
Expand All @@ -806,7 +807,8 @@ VariableOp declareErrorVariable(mlir::OpBuilder &builder,
std::string name = "__rk_e_" + variableOp.getSymName().str();

auto variableType =
VariableType::get(std::nullopt, RealType::get(builder.getContext()),
VariableType::get(variableOp.getVariableType().getShape(),
RealType::get(builder.getContext()),
VariabilityProperty::none, IOProperty::none);

return builder.create<VariableOp>(variableOp.getLoc(), name, variableType);
Expand Down Expand Up @@ -934,6 +936,7 @@ FunctionOp RungeKuttaPass::createEquationFunction(
auto functionOp =
rewriter.create<FunctionOp>(explicitEquationOp.getLoc(), "rk_eq");

symbolTableCollection.getSymbolTable(moduleOp).insert(functionOp);
rewriter.createBlock(&functionOp.getBodyRegion());

// Declare the variables.
Expand Down Expand Up @@ -974,16 +977,18 @@ FunctionOp RungeKuttaPass::createEquationFunction(
rewriter.setInsertionPointToStart(algorithmOp.getBody());

mlir::IRMapping mapping;
llvm::DenseSet<VariableGetOp> mappedInductions;

// Get the values of the induction variables.
auto originalInductions = explicitEquationOp.getInductionVariables();

for (size_t i = 0, e = originalInductions.size(); i < e; ++i) {
mlir::Value mappedInduction = rewriter.create<VariableGetOp>(
auto mappedInduction = rewriter.create<VariableGetOp>(
inductionVariablesOps[i].getLoc(),
inductionVariablesOps[i].getVariableType().unwrap(),
inductionVariablesOps[i].getSymName());

mappedInductions.insert(mappedInduction);
mapping.map(originalInductions[i], mappedInduction);
}

Expand Down Expand Up @@ -1050,6 +1055,12 @@ FunctionOp RungeKuttaPass::createEquationFunction(
}

for (VariableGetOp variableGetOp : variableGetOps) {
if (mappedInductions.contains(variableGetOp)) {
// Skip the variables that have been introduced to map the original
// inductions.
continue;
}

rewriter.setInsertionPoint(variableGetOp);

if (variableGetOp.getVariable() == mappedStateVariableOp.getSymName()) {
Expand All @@ -1059,6 +1070,7 @@ FunctionOp RungeKuttaPass::createEquationFunction(
VariableOp variableOp = symbolTableCollection.lookupSymbolIn<VariableOp>(
modelOp, variableGetOp.getVariableAttr());

assert(variableOp && "Variable not found");
auto futureVariableIt = futureVariables.find(variableOp);

if (futureVariableIt == futureVariables.end()) {
Expand Down Expand Up @@ -1283,6 +1295,7 @@ mlir::LogicalResult createSlopeEquation(
}

callArgs.push_back(secondArg);
llvm::append_range(callArgs, templateOp.getInductionVariables());

auto callOp = rewriter.create<CallOp>(loc, eqRhsFunc.functionOp, callArgs);
mlir::Value rhs = callOp.getResult(0);
Expand Down
Loading