================
@@ -20,17 +21,117 @@ using namespace cir;
 // Helpers
 
//===----------------------------------------------------------------------===//
 
+static constexpr unsigned MaxLookupDepth = 6;
+
 mlir::Value CIRBasicAliasAnalysis::getUnderlyingObject(mlir::Value val) {
   LDBG() << "Getting underlying object for: " << val;
 
-  // TODO: Walk through cir.ptr_stride, cir.cast, cir.get_member, etc.
-  // to find the root allocation (cir.alloca, cir.global_addr, function args).
-  LDBG() << "Not yet implemented";
+  for (unsigned depth = 0; depth < MaxLookupDepth; ++depth) {
+    mlir::Operation *defOp = val.getDefiningOp();
+    if (!defOp) {
+      LDBG() << "No defining operation, stopping";
+      break; // Block argument (e.g. function parameter) — stop here.
+    }
+
+    // Bitcast and address-space casts don't change the underlying object.
+    // array_to_ptrdecay produces an element pointer to the same storage as
+    // the array pointer, so strip through it too.
+    if (auto castOp = mlir::dyn_cast<cir::CastOp>(defOp)) {
+      if (castOp.isAllocaPreservingCast() ||
+          castOp.getKind() == cir::CastKind::array_to_ptrdecay) {
+        LDBG() << "Walking past cast operation";
+        val = castOp.getSrc();
+        continue;
+      }
+      LDBG() << "Opaque cast operation, stopping";
+      break;
+    }
+
+    // Pointer stride: only strip through when we can prove the access stays
----------------
andykaylor wrote:

I'm going to add offset handling and bounds checking in my next PR in this 
series. This comment is an artifact of my having already explored that. It 
should have been removed. I found a strange thing with LLVM alias analysis 
here. If the pointer stride goes beyond the end of the alloca object, BasicAA 
says it doesn't alias with anything.

https://godbolt.org/z/hYqno8ea8

Apparently, this is intentional, saying the out-of-bounds offset is UB.

I think in the current state it doesn't make sense to have a shared helper to 
detect the zero offset because the helper would still need op-specific code to 
get the offset. When I introduce the more complete offset handling this will 
naturally become `offset == 0` for everything.

https://github.com/llvm/llvm-project/pull/215683
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to