|
22 | 22 | #include "llvm/ADT/TypeSwitch.h" |
23 | 23 | #include "llvm/Support/Casting.h" |
24 | 24 | #include "llvm/Support/Debug.h" |
| 25 | +#include "mlir/Interfaces/ViewLikeInterface.h" |
25 | 26 |
|
26 | 27 | using namespace mlir; |
27 | 28 |
|
@@ -535,6 +536,42 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v, |
535 | 536 | mlir::Operation *instantiationPoint{nullptr}; |
536 | 537 | while (defOp && !breakFromLoop) { |
537 | 538 | ty = defOp->getResultTypes()[0]; |
| 539 | + |
| 540 | + // Effect-based detection using op-scoped Allocate with conservative |
| 541 | + // heuristics (ignore value-scoped signals per request). |
| 542 | + if (auto memIface = llvm::dyn_cast<mlir::MemoryEffectOpInterface>(defOp)) { |
| 543 | + llvm::SmallVector<mlir::MemoryEffects::EffectInstance, 4> effects; |
| 544 | + memIface.getEffects(effects); |
| 545 | + bool sawOpScopedAlloc = false; |
| 546 | + for (auto &ei : effects) { |
| 547 | + bool isAlloc = mlir::isa<mlir::MemoryEffects::Allocate>(ei.getEffect()); |
| 548 | + if (!ei.getValue() && isAlloc) { |
| 549 | + sawOpScopedAlloc = true; |
| 550 | + } |
| 551 | + } |
| 552 | + if (sawOpScopedAlloc) { |
| 553 | + auto isMemoryRefLikeType = [](mlir::Type t) { |
| 554 | + return fir::isa_ref_type(t) || mlir::isa<mlir::BaseMemRefType>(t) || |
| 555 | + mlir::isa<mlir::LLVM::LLVMPointerType>(t); |
| 556 | + }; |
| 557 | + bool opIsViewLike = (bool)mlir::dyn_cast_or_null<mlir::ViewLikeOpInterface>(defOp); |
| 558 | + bool hasMemOperands = llvm::any_of(defOp->getOperands(), [&](mlir::Value opnd) { |
| 559 | + return isMemoryRefLikeType(opnd.getType()); |
| 560 | + }); |
| 561 | + if (!opIsViewLike && !hasMemOperands) { |
| 562 | + for (mlir::Value res : defOp->getResults()) { |
| 563 | + if (res == v && isMemoryRefLikeType(res.getType())) { |
| 564 | + type = SourceKind::Allocate; |
| 565 | + breakFromLoop = true; |
| 566 | + break; |
| 567 | + } |
| 568 | + } |
| 569 | + if (breakFromLoop) |
| 570 | + break; |
| 571 | + } |
| 572 | + } |
| 573 | + } |
| 574 | + |
538 | 575 | llvm::TypeSwitch<Operation *>(defOp) |
539 | 576 | .Case<hlfir::AsExprOp>([&](auto op) { |
540 | 577 | v = op.getVar(); |
|
0 commit comments