[flang-commits] [flang] [llvm] [flang] Enhance show_descriptor intrinsic to avoid extra copies and extra descriptor creation (PR #173461)

via flang-commits flang-commits at lists.llvm.org
Mon Jan 5 06:34:55 PST 2026


================
@@ -7893,9 +7893,29 @@ void IntrinsicLibrary::genShowDescriptor(
   assert(args.size() == 1 && "expected single argument for show_descriptor");
   const mlir::Value descriptor = fir::getBase(args[0]);
 
-  assert(fir::isa_box_type(descriptor.getType()) &&
-         "argument must have been lowered to box type");
-  fir::runtime::genShowDescriptor(builder, loc, descriptor);
+  // Use consistent !fir.ref<!fir.box<none>> argument type
+  auto targetType = fir::BoxType::get(builder.getNoneType());
+  auto targetRefType = fir::ReferenceType::get(targetType);
+
+  // If it's already a reference to a box, convert it to correct type and
+  // pass it directly
+  if (fir::isa_ref_type(descriptor.getType()) &&
+      fir::isa_box_type(fir::unwrapRefType(descriptor.getType()))) {
+    fir::runtime::genShowDescriptor(
+        builder, loc, builder.createConvert(loc, targetRefType, descriptor));
+    return;
+  }
+
+  mlir::Value descrAddr = nullptr;
+  if (fir::isa_box_type(descriptor.getType())) {
+    // Spill it to the stack
+    descrAddr = builder.createTemporary(loc, targetType);
+    builder.createStoreWithConvert(loc, descriptor, descrAddr);
+  } else {
+    // If argument is not a box type (and not ref<box>), pass fir.absent.
+    descrAddr = builder.genAbsentOp(loc, targetRefType);
----------------
jeanPerier wrote:

In practice, I think it is likely more useful to build a descriptor here so that the runtime can print all the information about the variable.

Outside of POINTER/ALLOCATABLE where there is single descriptor associated to the symbol, whether a variable is tracked via a descriptor is a bit of a compiler detail and I expect that most people using show_descriptor will mostly care about info about the variable (and the descriptor address matters for pointer/allocatable, so your change is relevant for those).

I would suggest calling createBox at that point instead of using creating a nullptr. 

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


More information about the flang-commits mailing list