Skip to content

Commit fc0a978

Browse files
authored
[Flang] Fix ASSIGN statement (#149941)
Handle the case where the assigned variable also has a pointer attribute. Fixes #121721
1 parent 594b6f7 commit fc0a978

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

flang/lib/Lower/Bridge.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5509,10 +5509,34 @@ class FirConverter : public Fortran::lower::AbstractConverter {
55095509
void genFIR(const Fortran::parser::AssignStmt &stmt) {
55105510
const Fortran::semantics::Symbol &symbol =
55115511
*std::get<Fortran::parser::Name>(stmt.t).symbol;
5512+
55125513
mlir::Location loc = toLocation();
5514+
mlir::Type symbolType = genType(symbol);
5515+
mlir::Value addr = getSymbolAddress(symbol);
5516+
5517+
// Handle the case where the assigned variable is declared as a pointer
5518+
if (auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(symbolType)) {
5519+
if (auto ptrType = mlir::dyn_cast<fir::PointerType>(eleTy)) {
5520+
symbolType = ptrType.getEleTy();
5521+
} else {
5522+
symbolType = eleTy;
5523+
}
5524+
} else if (auto ptrType = mlir::dyn_cast<fir::PointerType>(symbolType)) {
5525+
symbolType = ptrType.getEleTy();
5526+
}
5527+
55135528
mlir::Value labelValue = builder->createIntegerConstant(
5514-
loc, genType(symbol), std::get<Fortran::parser::Label>(stmt.t));
5515-
builder->create<fir::StoreOp>(loc, labelValue, getSymbolAddress(symbol));
5529+
loc, symbolType, std::get<Fortran::parser::Label>(stmt.t));
5530+
5531+
// If the address points to a boxed pointer, we need to dereference it
5532+
if (auto refType = mlir::dyn_cast<fir::ReferenceType>(addr.getType())) {
5533+
if (auto boxType = mlir::dyn_cast<fir::BoxType>(refType.getEleTy())) {
5534+
mlir::Value boxValue = builder->create<fir::LoadOp>(loc, addr);
5535+
addr = builder->create<fir::BoxAddrOp>(loc, boxValue);
5536+
}
5537+
}
5538+
5539+
builder->create<fir::StoreOp>(loc, labelValue, addr);
55165540
}
55175541

55185542
void genFIR(const Fortran::parser::FormatStmt &) {

flang/test/Lower/assign-statement.f90

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
! RUN: bbc -emit-fir -o - %s | FileCheck %s
2+
3+
! CHECK-LABEL: func @_QQmain
4+
program main
5+
integer :: ip
6+
pointer :: ip
7+
8+
allocate(ip)
9+
assign 10 to ip
10+
! CHECK: fir.store %c10_i32 to %11 : !fir.ptr<i32>
11+
10 return
12+
end program main

0 commit comments

Comments
 (0)