[flang] Only check for embox/rebox if defining op is present
authorValentin Clement <clementval@gmail.com>
Mon, 13 Mar 2023 14:25:30 +0000 (15:25 +0100)
committerValentin Clement <clementval@gmail.com>
Mon, 13 Mar 2023 14:25:56 +0000 (15:25 +0100)
When the base box is a block argument, the defining op is null.
Only try to recover embox/rebox if there is a defining op.

Reviewed By: PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D145910

flang/lib/Lower/ConvertExpr.cpp

index b600356..f6cd761 100644 (file)
@@ -7238,17 +7238,19 @@ updateBoxForParentComponent(Fortran::lower::AbstractConverter &converter,
   mlir::Operation *op = boxBase.getDefiningOp();
   mlir::Type actualTy = converter.genType(expr);
 
-  if (auto embox = mlir::dyn_cast<fir::EmboxOp>(op)) {
-    auto newBox = builder.create<fir::EmboxOp>(
-        loc, fir::BoxType::get(actualTy), embox.getMemref(), embox.getShape(),
-        embox.getSlice(), embox.getTypeparams());
-    return fir::substBase(box, newBox);
-  }
-  if (auto rebox = mlir::dyn_cast<fir::ReboxOp>(op)) {
-    auto newBox = builder.create<fir::ReboxOp>(loc, fir::BoxType::get(actualTy),
-                                               rebox.getBox(), rebox.getShape(),
-                                               rebox.getSlice());
-    return fir::substBase(box, newBox);
+  if (op) {
+    if (auto embox = mlir::dyn_cast<fir::EmboxOp>(op)) {
+      auto newBox = builder.create<fir::EmboxOp>(
+          loc, fir::BoxType::get(actualTy), embox.getMemref(), embox.getShape(),
+          embox.getSlice(), embox.getTypeparams());
+      return fir::substBase(box, newBox);
+    }
+    if (auto rebox = mlir::dyn_cast<fir::ReboxOp>(op)) {
+      auto newBox = builder.create<fir::ReboxOp>(
+          loc, fir::BoxType::get(actualTy), rebox.getBox(), rebox.getShape(),
+          rebox.getSlice());
+      return fir::substBase(box, newBox);
+    }
   }
 
   mlir::Value empty;