@@ -27,6 +27,26 @@ using namespace mlir;
2727
2828#define DEBUG_TYPE " fir-alias-analysis"
2929
30+ // Inspect for value-scoped Allocate effects and determine whether
31+ // 'candidate' is a new allocation. Returns SourceKind::Allocate if a
32+ // MemAlloc effect is attached
33+ static fir::AliasAnalysis::SourceKind
34+ classifyAllocateFromEffects (mlir::Operation *op, mlir::Value candidate) {
35+ if (!op)
36+ return fir::AliasAnalysis::SourceKind::Unknown;
37+ auto interface = llvm::dyn_cast<mlir::MemoryEffectOpInterface>(op);
38+ if (!interface)
39+ return fir::AliasAnalysis::SourceKind::Unknown;
40+ llvm::SmallVector<mlir::MemoryEffects::EffectInstance, 4 > effects;
41+ interface.getEffects (effects);
42+ for (mlir::MemoryEffects::EffectInstance &e : effects) {
43+ if (mlir::isa<mlir::MemoryEffects::Allocate>(e.getEffect ()) &&
44+ e.getValue () && e.getValue () == candidate)
45+ return fir::AliasAnalysis::SourceKind::Allocate;
46+ }
47+ return fir::AliasAnalysis::SourceKind::Unknown;
48+ }
49+
3050// ===----------------------------------------------------------------------===//
3151// AliasAnalysis: alias
3252// ===----------------------------------------------------------------------===//
@@ -535,6 +555,11 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v,
535555 mlir::Operation *instantiationPoint{nullptr };
536556 while (defOp && !breakFromLoop) {
537557 ty = defOp->getResultTypes ()[0 ];
558+ // Value-scoped allocation detection via effects.
559+ if (classifyAllocateFromEffects (defOp, v) == SourceKind::Allocate) {
560+ type = SourceKind::Allocate;
561+ break ;
562+ }
538563 llvm::TypeSwitch<Operation *>(defOp)
539564 .Case <hlfir::AsExprOp>([&](auto op) {
540565 v = op.getVar ();
@@ -554,11 +579,6 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v,
554579 defOp = v.getDefiningOp ();
555580 }
556581 })
557- .Case <fir::AllocaOp, fir::AllocMemOp>([&](auto op) {
558- // Unique memory allocation.
559- type = SourceKind::Allocate;
560- breakFromLoop = true ;
561- })
562582 .Case <fir::ConvertOp>([&](auto op) {
563583 // Skip ConvertOp's and track further through the operand.
564584 v = op->getOperand (0 );
@@ -628,16 +648,23 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v,
628648 type = SourceKind::Global;
629649 } else {
630650 auto def = llvm::cast<mlir::Value>(boxSrc.origin .u );
631- // TODO: Add support to fir.allocmem
632- if (auto allocOp = def.template getDefiningOp <fir::AllocaOp>()) {
633- v = def;
634- defOp = v.getDefiningOp ();
635- type = SourceKind::Allocate;
636- } else if (isDummyArgument (def)) {
637- defOp = nullptr ;
638- v = def;
639- } else {
640- type = SourceKind::Indirect;
651+ bool classified = false ;
652+ if (auto defDefOp = def.getDefiningOp ()) {
653+ if (classifyAllocateFromEffects (defDefOp, def) ==
654+ SourceKind::Allocate) {
655+ v = def;
656+ defOp = defDefOp;
657+ type = SourceKind::Allocate;
658+ classified = true ;
659+ }
660+ }
661+ if (!classified) {
662+ if (isDummyArgument (def)) {
663+ defOp = nullptr ;
664+ v = def;
665+ } else {
666+ type = SourceKind::Indirect;
667+ }
641668 }
642669 }
643670 breakFromLoop = true ;
0 commit comments