@@ -439,20 +439,28 @@ static ModRefResult getCallModRef(fir::CallOp call, mlir::Value var) {
439439 // An example of something currently problematic is the allocmem generated for
440440 // ALLOCATE of allocatable target. It currently does not have the target
441441 // attribute, which would lead this analysis to believe it cannot escape.
442- if (!varSrc.isFortranUserVariable () || !isCallToFortranUserProcedure (call))
442+ if (!varSrc.isFortranUserVariable () || !isCallToFortranUserProcedure (call)) {
443+ LLVM_DEBUG (llvm::dbgs () << " ModAndRef because not a user variable or not a "
444+ " call to user procedure.\n " );
443445 return ModRefResult::getModAndRef ();
446+ }
444447 // Pointer and target may have been captured.
445- if (varSrc.isTargetOrPointer ())
448+ if (varSrc.isTargetOrPointer ()) {
449+ LLVM_DEBUG (llvm::dbgs () << " ModAndRef because target or pointer.\n " );
446450 return ModRefResult::getModAndRef ();
451+ }
447452 // Host associated variables may be addressed indirectly via an internal
448453 // function call, whether the call is in the parent or an internal procedure.
449454 // Note that the host associated/internal procedure may be referenced
450455 // indirectly inside calls to non internal procedure. This is because internal
451456 // procedures may be captured or passed. As this is tricky to analyze, always
452457 // consider such variables may be accessed in any calls.
453458 if (varSrc.kind == fir::AliasAnalysis::SourceKind::HostAssoc ||
454- varSrc.isCapturedInInternalProcedure )
459+ varSrc.isCapturedInInternalProcedure ) {
460+ LLVM_DEBUG (llvm::dbgs () << " ModAndRef because var is host associated or "
461+ " captured in internal procedure.\n " );
455462 return ModRefResult::getModAndRef ();
463+ }
456464 // At that stage, it has been ruled out that local (including the saved ones)
457465 // and dummy cannot be indirectly accessed in the call.
458466 if (varSrc.kind != fir::AliasAnalysis::SourceKind::Allocate &&
@@ -485,6 +493,8 @@ ModRefResult AliasAnalysis::getModRef(Operation *op, Value location) {
485493 if (!interface) {
486494 if (auto call = llvm::dyn_cast<fir::CallOp>(op))
487495 return getCallModRef (call, location);
496+
497+ LLVM_DEBUG (llvm::dbgs () << " ModAndRef because no interface or call.\n " );
488498 return ModRefResult::getModAndRef ();
489499 }
490500
@@ -621,38 +631,33 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v,
621631 if (mlir::isa<fir::PointerType>(boxTy.getEleTy ()))
622632 attributes.set (Attribute::Pointer);
623633
624- auto def = getOriginalDef (op.getMemref (), attributes,
625- isCapturedInInternalProcedure,
626- approximateSource);
627- if (auto addrOfOp = def.template getDefiningOp <fir::AddrOfOp>()) {
628- global = addrOfOp.getSymbol ();
629-
630- if (hasGlobalOpTargetAttr (def, addrOfOp))
631- attributes.set (Attribute::Target);
634+ auto boxSrc = getSource (op.getMemref ());
635+ attributes |= boxSrc.attributes ;
636+ approximateSource |= boxSrc.approximateSource ;
637+ isCapturedInInternalProcedure |= boxSrc.isCapturedInInternalProcedure ;
632638
639+ global = llvm::dyn_cast<mlir::SymbolRefAttr>(boxSrc.origin .u );
640+ if (global) {
633641 type = SourceKind::Global;
634- }
635- // TODO: Add support to fir.allocmem
636- else if (auto allocOp =
637- def.template getDefiningOp <fir::AllocaOp>()) {
638- v = def;
639- defOp = v.getDefiningOp ();
640- type = SourceKind::Allocate;
641- } else if (isDummyArgument (def)) {
642- defOp = nullptr ;
643- v = def;
644642 } else {
645- type = SourceKind::Indirect;
643+ auto def = llvm::cast<mlir::Value>(boxSrc.origin .u );
644+ // TODO: Add support to fir.allocmem
645+ if (auto allocOp = def.template getDefiningOp <fir::AllocaOp>()) {
646+ v = def;
647+ defOp = v.getDefiningOp ();
648+ type = SourceKind::Allocate;
649+ } else if (isDummyArgument (def)) {
650+ defOp = nullptr ;
651+ v = def;
652+ } else {
653+ type = SourceKind::Indirect;
654+ }
646655 }
647- // TODO: This assignment is redundant but somehow works around an
648- // apparent MSVC bug reporting "undeclared identifier" at the next
649- // "breakFromLoop = true;". See
650- // <https://github.com/llvm/llvm-project/pull/127845#issuecomment-2669829610>.
651656 breakFromLoop = true ;
652- } else {
653- // No further tracking for addresses loaded from memory for now.
654- type = SourceKind::Indirect;
657+ return ;
655658 }
659+ // No further tracking for addresses loaded from memory for now.
660+ type = SourceKind::Indirect;
656661 breakFromLoop = true ;
657662 })
658663 .Case <fir::AddrOfOp>([&](auto op) {
0 commit comments