[flang-commits] [flang] [llvm] [Flang][OpenMP] Generate correct present checks for implicit maps of optional allocatables (PR #138210)

via flang-commits flang-commits at lists.llvm.org
Tue May 6 06:56:53 PDT 2025


================
@@ -156,9 +156,9 @@ genBoundsOpsFromBox(fir::FirOpBuilder &builder, mlir::Location loc,
         builder.genIfOp(loc, resTypes, info.isPresent, /*withElseRegion=*/true)
             .genThen([&]() {
               mlir::Value box =
-                  !fir::isBoxAddress(info.addr.getType())
+                  !fir::isBoxAddress(info.rawInput.getType())
                       ? info.addr
-                      : builder.create<fir::LoadOp>(loc, info.addr);
+                      : builder.create<fir::LoadOp>(loc, info.rawInput);
----------------
agozillon wrote:

The issue this is trying to solve (which might not be the correct way to do so, so please do feel free to suggest alternatives :-) ) is the generation of external loads to the presence check, which will itself cause a segmentation fault if the Box isn't present (e.g. optional argument not been supplied).

This might only be an issue for OpenMP, but I think it also applies to OpenACC (as it's utilising the same utilities), but when you utilise the getDataOperandBaseAddr we get the AddrAndBoundsInfo which contains our rawInput and addr. In this particular case the rawInput is a hlfir.declare but the addr is a load of the declare (the address), and when we pass the AddrAndBoundsInfo into this function to create the appropriate presence checks before accessing information and utilise the Info.addr directly segment above, we'll skip the load generation inside of the protection of the present check and instead utilise the one generated external to the presence check that getDataOperandBaseAddr created to access the .addr, which at runtime will generate a segfault in certain cases. Swapping it to utilise rawInput ensures the load is generated inside of the presence checks!

That was at least my understanding of the problem, there is likely better ways to fix it, this was the simplest/(seemingly)least intrusive one I came across. 


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


More information about the flang-commits mailing list