[clang] [CIR] Add a points-to analysis skeleton (PR #244356)

Henrich Lauko via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 4 03:49:05 PST 2026


================
@@ -0,0 +1,269 @@
+//===- CIRPointsToAnalysis.cpp - Points-to analysis for CIR ---------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/CIR/Dialect/Analysis/CIRPointsToAnalysis.h"
+#include "mlir/IR/Remarks.h"
+#include "clang/CIR/Dialect/IR/CIRDialect.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/TypeSwitch.h"
+#include "llvm/Support/DebugLog.h"
+
+#include <algorithm>
+
+#define DEBUG_TYPE "cir-points-to-analysis"
+
+using namespace cir;
+
+/// The name used to print `object`: the name of the cir.alloca allocating it.
+static llvm::StringRef getObjectName(mlir::Value object) {
+  llvm::StringRef name = object.getDefiningOp<cir::AllocaOp>().getName();
+  return name.empty() ? "<unnamed>" : name;
+}
+
+/// The options of every remark emitted for the analysis of `root`.
+static mlir::remark::RemarkOpts getRemarkOpts(llvm::StringRef name,
+                                              mlir::Operation *root,
+                                              llvm::StringRef subCategory) {
+  mlir::remark::RemarkOpts opts = mlir::remark::RemarkOpts::name(name)
+                                      .category("points-to")
+                                      .subCategory(subCategory);
+  if (auto func = mlir::dyn_cast<cir::FuncOp>(root))
+    opts = opts.function(func.getSymName());
+  return opts;
+}
+
+//===----------------------------------------------------------------------===//
+// PointsToSet
+//===----------------------------------------------------------------------===//
+
+bool PointsToSet::merge(const PointsToSet &other) {
+  assert(!sizeLimitHit && "merging into a set that hit the size limit");
+  bool changed = objects.set_union(other.objects);
+  if (other.unknown)
+    changed |= insertUnknown();
+  return changed;
+}
+
+void PointsToSet::replaceWithUnknown() {
+  objects.clear();
+  unknown = true;
+  sizeLimitHit = true;
+}
+
+void PointsToSet::print(llvm::raw_ostream &os) const {
+  llvm::SmallVector<llvm::StringRef> names =
+      llvm::map_to_vector(objects, getObjectName);
+  llvm::sort(names);
+  if (unknown)
+    names.push_back("unknown");
+  os << '[';
+  llvm::interleaveComma(names, os);
+  os << ']';
+}
+
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+void PointsToSet::dump() const {
+  print(llvm::errs());
+  llvm::errs() << '\n';
+}
+#endif
+
+//===----------------------------------------------------------------------===//
+// PointsToAnalysis
+//===----------------------------------------------------------------------===//
+
+PointsToAnalysis::PointsToAnalysis(mlir::Operation *op,
+                                   const PointsToLimits &limits)
+    : root(op), limits(limits) {
+  unknownSet.insertUnknown();
+  compute(op);
+}
+
+void PointsToAnalysis::compute(mlir::Operation *op) {
+  // Create the sets of the values the analysis computes before solving, so a
+  // value never changes from "not analyzed", which reads as "unknown", to a set
+  // with fewer members. Each cir.alloca result points to the object it
+  // allocates, each cir.load result starts out pointing to nothing, and a null
+  // pointer constant points to nothing.
+  uint64_t numOperations = 0;
+  mlir::WalkResult tooLarge = op->walk([&](mlir::Operation *nested) {
+    if (nested == op)
+      return mlir::WalkResult::advance();
+    if (++numOperations > limits.maxOperations)
+      return mlir::WalkResult::interrupt();
+    llvm::TypeSwitch<mlir::Operation *>(nested)
+        .Case([&](cir::AllocaOp alloca) {
+          valuePointsTo[alloca.getResult()].insert(alloca.getResult());
+        })
+        .Case([&](cir::LoadOp load) {
+          valuePointsTo.try_emplace(load.getResult());
+        })
+        .Case([&](cir::ConstantOp constant) {
+          if (constant.isNullPtr())
+            valuePointsTo.try_emplace(constant.getResult());
+        });
+    return mlir::WalkResult::advance();
+  });
+  if (tooLarge.wasInterrupted()) {
+    dropAllSets();
+    return;
+  }
+
+  // Sets only grow, so revisiting every operation until nothing changes
+  // terminates. The work limit bounds how long that may take.
+  uint64_t budget = std::max<uint64_t>(
+      limits.minWork, uint64_t(limits.workPerOperation) * numOperations);
+  uint64_t work = 0;
+  unsigned iterations = 0;
+  bool changed;
+  do {
+    changed = false;
+    mlir::WalkResult result = op->walk([&](mlir::Operation *nested) {
+      if (nested == op)
+        return mlir::WalkResult::advance();
+      if (++work > budget)
+        return mlir::WalkResult::interrupt();
+      changed |= visit(nested);
+      return mlir::WalkResult::advance();
+    });
+    if (result.wasInterrupted()) {
+      dropAllSets();
+      return;
+    }
+    ++iterations;
+  } while (changed);
+  LDBG() << "Solved in " << iterations << " iterations";
+}
+
+void PointsToAnalysis::emitRemarks(llvm::StringRef subCategory) const {
+  if (!root->getContext()->getRemarkEngine())
+    return;
+
+  for (auto [object, op] : escaped)
+    mlir::remark::missed(op->getLoc(),
+                         getRemarkOpts("Escaped", root, subCategory))
+        << mlir::remark::add("the address of {0} escapes",
+                             getObjectName(object));
+  for (auto [op, size] : sizeLimitHits)
+    mlir::remark::missed(op->getLoc(),
+                         getRemarkOpts("SizeLimit", root, subCategory))
+        << mlir::remark::add(
+               "a set of {0} objects is replaced by unknown, the limit is {1}",
+               size, limits.maxSetSize);
+
+  root->walk([&](cir::LoadOp load) {
+    if (!mlir::isa<cir::PointerType>(load.getType()))
+      return;
+    mlir::remark::analysis(load.getLoc(),
+                           getRemarkOpts("PointsTo", root, subCategory))
+        << mlir::remark::add("points to {0}", getPointsTo(load.getResult()));
+  });
+}
+
+const PointsToSet &PointsToAnalysis::getPointsTo(mlir::Value value) const {
+  auto it = valuePointsTo.find(value);
+  if (it == valuePointsTo.end())
+    return unknownSet;
+  return it->second;
+}
+
+void PointsToAnalysis::dropAllSets() {
+  // Values without a set point to "unknown", so dropping every set is enough
+  // to make every answer conservative.
+  valuePointsTo.clear();
+  memoryPointsTo.clear();
+  escaped.clear();
+  sizeLimitHits.clear();
+}
+
+bool PointsToAnalysis::addTo(PointsToSet &to, const PointsToSet &from,
+                             mlir::Operation *op) {
+  // A set that hit the size limit no longer names its objects, so the objects
+  // added to it are lost. Make them escape, so that dropping them is
+  // conservative.
+  if (to.hitSizeLimit())
+    return markEscaped(from.getObjects(), op);
+
+  bool changed = to.merge(from);
+  if (to.getObjects().size() <= limits.maxSetSize)
+    return changed;
+
+  // The objects the set held are lost too. They would escape when the
+  // operations that added them are visited again, but making them escape here
+  // doesn't depend on the order operations are visited in.
+  llvm::SmallVector<mlir::Value> objects(to.getObjects());
+  sizeLimitHits.emplace_back(op, objects.size());
+  to.replaceWithUnknown();
+  // `to` may be an element of memoryPointsTo, which markEscaped() can grow, so
+  // it must not be used after this point.
+  markEscaped(objects, op);
+  return true;
+}
+
+bool PointsToAnalysis::markEscaped(llvm::ArrayRef<mlir::Value> objects,
+                                   mlir::Operation *op) {
+  bool changed = false;
+  llvm::SmallVector<mlir::Value> worklist;
+  for (mlir::Value object : objects)
+    if (escaped.insert({object, op}).second)
+      worklist.push_back(object);
+
+  while (!worklist.empty()) {
+    mlir::Value object = worklist.pop_back_val();
+    changed = true;
+
+    // Code the analysis cannot see may store any address it can reach into an
+    // escaped object, and may follow the addresses already stored there.
+    PointsToSet &memory = memoryPointsTo[object];
+    memory.insertUnknown();
+    for (mlir::Value inner : memory.getObjects())
+      if (escaped.insert({inner, op}).second)
+        worklist.push_back(inner);
+  }
+  return changed;
+}
+
+bool PointsToAnalysis::visit(mlir::Operation *op) {
+  return llvm::TypeSwitch<mlir::Operation *, bool>(op)
+      .Case([&](cir::StoreOp store) {
+        const PointsToSet &addr = getPointsTo(store.getAddr());
+        const PointsToSet &value = getPointsTo(store.getValue());
+        bool changed = false;
+        for (mlir::Value object : addr.getObjects()) {
+          changed |= addTo(memoryPointsTo[object], value, op);
+          if (escaped.contains(object))
+            changed |= markEscaped(value.getObjects(), op);
+        }
+        // A store through an address the analysis does not know may write to
+        // an escaped object or to memory it does not track at all.
+        if (addr.containsUnknown())
+          changed |= markEscaped(value.getObjects(), op);
+        return changed;
+      })
+      .Case([&](cir::LoadOp load) {
+        const PointsToSet &addr = getPointsTo(load.getAddr());
+        PointsToSet loaded;
+        for (mlir::Value object : addr.getObjects()) {
+          auto it = memoryPointsTo.find(object);
+          if (it != memoryPointsTo.end())
+            loaded.merge(it->second);
+        }
+        if (addr.containsUnknown())
+          loaded.insertUnknown();
+        return addTo(valuePointsTo[load.getResult()], loaded, op);
+      })
+      .Default([&](mlir::Operation *) {
----------------
xlauko wrote:

The default case makes the objects behind every operand escape. That includes `cir.cast ptr_to_bool` and `cir.cmp`, which CIRGen emits for `if (p)` and `p == q`. Neither can write an address to memory or return one (LLVM's CaptureTracking doesn't treat a null comparison as a capture). Could these two keep their operands from escaping, or should they go on the roadmap in `PointsToAnalysis.md`? As it is, `int **pp = &p; if (pp) ...` makes `p` escape, and every later load through `*pp` then gives `unknown`.

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


More information about the cfe-commits mailing list