[flang-commits] [flang] [llvm] [Flang][OpenMP] Improve use_device_addr code generation (PR #221265)
Akash Banerjee via flang-commits
flang-commits at lists.llvm.org
Thu Sep 17 07:49:05 PDT 2026
================
@@ -1212,6 +1231,57 @@ class MapInfoFinalizationPass
argIface.getUseDevicePtrBlockArgs());
}
+ /// Check if we can optimize the descriptor mappings for use_device_addr
+ bool canOptimizeUseDeviceAddrMapping(fir::FirOpBuilder &builder,
+ mlir::Value descriptor,
+ mlir::omp::MapInfoOp op,
+ mlir::Operation *target) {
+ // optimize only temporary descriptors (i.e. allocated on function stack)
+ if (!isAllocaOp(descriptor))
+ return false;
+ // check if given descriptor is mapped as use_device_addr argument
+ if (getUseDeviceAddrBlockArg(op, *target) == nullptr)
+ return false;
+ auto module = builder.getModule();
+ // check if the OpenMP offload target device is specified
+ auto iface =
+ llvm::cast<mlir::omp::OffloadModuleInterface>(module.getOperation());
+ if (iface.getTargetTriples().empty())
+ return false;
+
+ // Only optimize descriptors whose element size is known at compile time.
+ // Excluded:
+ // - polymorphic entities (!fir.class): elem_len is a runtime property,
+ // since the dynamic type may extend the declared type.
+ // - deferred-length characters (!fir.char<k,?>) and parameterized
+ // derived types: LEN parameters are runtime values.
+ // - assumed-rank arrays (!fir.array<*:T>): descriptor layout depends on
+ // a rank that is not known here.
+ //
+ // TODO: The restrictions can be lifted if fir.create_box operation supports
+ // creation of box with dynamical element size.
+ bool isArray = false;
+ bool knownRanks = false;
+ fir::BaseBoxType baseBoxTy = mlir::dyn_cast<fir::BaseBoxType>(
+ fir::unwrapRefType(descriptor.getType()));
+ if (baseBoxTy) {
+ isArray = baseBoxTy.isArray();
+ knownRanks = !baseBoxTy.isAssumedRank();
+ }
+ if (!isArray)
+ return false;
+ if (!knownRanks)
+ return false;
+ auto eleTy = baseBoxTy.unwrapInnerType();
+ if (fir::hasDynamicSize(eleTy))
+ return false;
+ if (fir::isPolymorphicType(eleTy))
----------------
TIFitis wrote:
```suggestion
if (fir::isPolymorphicType(baseBoxTy))
```
I think we should pass `baseBoxTy` to `fir::isPolymorphicType` here. `unwrapInnerType()` returns the element type, losing the `!fir.class` wrapper. Checking `eleTy` therefore misses `class(t)` arrays and still allows them into the `fir.create_box` path, which does not support polymorphic descriptors.
https://github.com/llvm/llvm-project/pull/221265
More information about the flang-commits
mailing list