-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[Flang] Fix ASSIGN statement #149941
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
[Flang] Fix ASSIGN statement #149941
Conversation
Handle the case where the assigned variable also has a pointer attribute. Fixes llvm#121721
@llvm/pr-subscribers-flang-fir-hlfir Author: Carlos Seo (ceseo) ChangesHandle the case where the assigned variable also has a pointer attribute. Fixes #121721 Full diff: https://github.com/llvm/llvm-project/pull/149941.diff 1 Files Affected:
diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index 7ce397a11861b..243fda63d5e29 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -5506,10 +5506,35 @@ class FirConverter : public Fortran::lower::AbstractConverter {
void genFIR(const Fortran::parser::AssignStmt &stmt) {
const Fortran::semantics::Symbol &symbol =
*std::get<Fortran::parser::Name>(stmt.t).symbol;
+
mlir::Location loc = toLocation();
+ mlir::Type symbolType = genType(symbol);
+ mlir::Value addr = getSymbolAddress(symbol);
+
+ // Handle the case where the assigned variable is declared as a pointer
+ if (auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(symbolType)) {
+ if (auto ptrType = mlir::dyn_cast<fir::PointerType>(eleTy)) {
+ symbolType = ptrType.getEleTy();
+ } else {
+ symbolType = eleTy;
+ }
+ }
+ else if (auto ptrType = mlir::dyn_cast<fir::PointerType>(symbolType)) {
+ symbolType = ptrType.getEleTy();
+ }
+
mlir::Value labelValue = builder->createIntegerConstant(
- loc, genType(symbol), std::get<Fortran::parser::Label>(stmt.t));
- builder->create<fir::StoreOp>(loc, labelValue, getSymbolAddress(symbol));
+ loc, symbolType, std::get<Fortran::parser::Label>(stmt.t));
+
+ // If the address points to a boxed pointer, we need to dereference it
+ if (auto refType = mlir::dyn_cast<fir::ReferenceType>(addr.getType())) {
+ if (auto boxType = mlir::dyn_cast<fir::BoxType>(refType.getEleTy())) {
+ mlir::Value boxValue = builder->create<fir::LoadOp>(loc, addr);
+ addr = builder->create<fir::BoxAddrOp>(loc, boxValue);
+ }
+ }
+
+ builder->create<fir::StoreOp>(loc, labelValue, addr);
}
void genFIR(const Fortran::parser::FormatStmt &) {
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the bugfix. Please could you add a lit test for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks for the fix
Edit: please could this be backported to the LLVM 21 release branch. There are instructions here
Handle the case where the assigned variable also has a pointer attribute.
Fixes #121721