[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()) {
----------------
xlauko wrote:

When the operation limit or the work limit is hit, all sets are dropped and no missed remark is emitted. The only trace is every load printing `[unknown]`. Since the size limit already gets a `SizeLimit` remark, could these two get one as well? That way `--remarks-filter-missed` would show why precision was lost here too.

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


More information about the cfe-commits mailing list