Skip to content

Commit e43496b

Browse files
ceseotru
authored andcommitted
[Flang] Fix ASSIGN statement (llvm#149941)
Handle the case where the assigned variable also has a pointer attribute. Fixes llvm#121721 (cherry picked from commit fc0a978)
1 parent 4d6604a commit e43496b

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
@@ -5505,10 +5505,34 @@ class FirConverter : public Fortran::lower::AbstractConverter {
55055505
void genFIR(const Fortran::parser::AssignStmt &stmt) {
55065506
const Fortran::semantics::Symbol &symbol =
55075507
*std::get<Fortran::parser::Name>(stmt.t).symbol;
5508+
55085509
mlir::Location loc = toLocation();
5510+
mlir::Type symbolType = genType(symbol);
5511+
mlir::Value addr = getSymbolAddress(symbol);
5512+
5513+
// Handle the case where the assigned variable is declared as a pointer
5514+
if (auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(symbolType)) {
5515+
if (auto ptrType = mlir::dyn_cast<fir::PointerType>(eleTy)) {
5516+
symbolType = ptrType.getEleTy();
5517+
} else {
5518+
symbolType = eleTy;
5519+
}
5520+
} else if (auto ptrType = mlir::dyn_cast<fir::PointerType>(symbolType)) {
5521+
symbolType = ptrType.getEleTy();
5522+
}
5523+
55095524
mlir::Value labelValue = builder->createIntegerConstant(
5510-
loc, genType(symbol), std::get<Fortran::parser::Label>(stmt.t));
5511-
builder->create<fir::StoreOp>(loc, labelValue, getSymbolAddress(symbol));
5525+
loc, symbolType, std::get<Fortran::parser::Label>(stmt.t));
5526+
5527+
// If the address points to a boxed pointer, we need to dereference it
5528+
if (auto refType = mlir::dyn_cast<fir::ReferenceType>(addr.getType())) {
5529+
if (auto boxType = mlir::dyn_cast<fir::BoxType>(refType.getEleTy())) {
5530+
mlir::Value boxValue = builder->create<fir::LoadOp>(loc, addr);
5531+
addr = builder->create<fir::BoxAddrOp>(loc, boxValue);
5532+
}
5533+
}
5534+
5535+
builder->create<fir::StoreOp>(loc, labelValue, addr);
55125536
}
55135537

55145538
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)