[flang-commits] [flang] Get the BoxType from the RHS instead of LHS for polymorphic pointer assignment inside FORALL. (PR #164279)

via flang-commits flang-commits at lists.llvm.org
Tue Oct 21 00:57:52 PDT 2025


================
@@ -4730,13 +4730,14 @@ class FirConverter : public Fortran::lower::AbstractConverter {
     if (Fortran::evaluate::UnwrapExpr<Fortran::evaluate::NullPointer>(
             assign.rhs))
       return fir::factory::createUnallocatedBox(*builder, loc, lhsBoxType, {});
-    hlfir::Entity rhs = Fortran::lower::convertExprToHLFIR(
-        loc, *this, assign.rhs, localSymbols, rhsContext);
+    hlfir::Entity rhs{fir::getBase(genExprBox(loc, assign.rhs, rhsContext))};
+    auto rhsBoxType =
+        llvm::cast<fir::BaseBoxType>(fir::unwrapRefType(rhs.getType()));
----------------
jeanPerier wrote:

I see why you are doing this, HLFIR is lacking a helper here.

Could you add a new method to the Entity class definition in HLFIRTools.h after getElementOrSequenceType def:
```
  /// Return the fir.class or fir.box type needed to describe this entity.
  fir::BaseBoxType getBoxType() const {
    if (isBoxAddressOrValue())
      return llvm::cast<fir::BaseBoxType>(fir::unwrapRefType(getType()));
    const bool isVolatile = fir::isa_volatile_type(getType());
    mlir::Type boxType =
    return fir::BoxType::get(getElementOrSequenceType(), isVolatile);
  } 
```

That way, you can keep convertExprToHLFIR and use it to get the rhsBoxType.
Then, you should replace `rhsBoxType.getBoxTypeWithNewShape(rhs.getRank())` with `rhsBoxType.getBoxTypeWithNewAttr(fir::BaseBoxType::Attribute::Pointer)` in the `hlfir::genVariableBox` to get the right attribute set via embox/rebox in the runtime descriptor that will be stored.

https://github.com/llvm/llvm-project/pull/164279


More information about the flang-commits mailing list