[clang] [CIR] -Wreturn-type (1/2): Add an analysis classifying how control reaches a function's end (PR #239774)

Henrich Lauko via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 1 12:47:51 PST 2026


================
@@ -0,0 +1,126 @@
+//===- CIRConstantFolding.cpp - Fold CIR values without rewriting ---------===//
+//
+// 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/CIRConstantFolding.h"
+
+#include "mlir/IR/Matchers.h"
+#include "mlir/Interfaces/ControlFlowInterfaces.h"
+#include "mlir/Interfaces/SideEffectInterfaces.h"
+#include "clang/CIR/Dialect/IR/CIRDialect.h"
+#include "llvm/ADT/SmallPtrSet.h"
+
+using namespace cir;
+
+static mlir::Attribute foldToConstant(mlir::Value value, unsigned depth);
+
+/// Fold result `index` of `branch` to the constant every value flowing into it
+/// agrees on, given its operands folded to `operands`.
+static mlir::Attribute
+foldRegionBranchResult(mlir::RegionBranchOpInterface branch, unsigned index,
+                       llvm::ArrayRef<mlir::Attribute> operands,
+                       unsigned depth) {
+  // The regions control may reach: those the operands let it enter, and those
+  // reached from them.
+  llvm::SmallVector<mlir::RegionSuccessor> worklist;
+  branch.getEntrySuccessorRegions(operands, worklist);
+  llvm::SmallPtrSet<mlir::Region *, 4> reachable;
+  bool mayEnterNoRegion = false;
+  while (!worklist.empty()) {
+    mlir::RegionSuccessor successor = worklist.pop_back_val();
+    if (successor.isOperation()) {
+      mayEnterNoRegion = true;
+      continue;
+    }
+    mlir::Region *region = successor.getSuccessor();
+    if (!reachable.insert(region).second)
+      continue;
+    for (mlir::Block &block : *region) {
+      if (!block.mightHaveTerminator())
+        continue;
+      if (auto terminator =
+              mlir::dyn_cast<mlir::RegionBranchTerminatorOpInterface>(
+                  block.getTerminator()))
+        branch.getSuccessorRegions(terminator, worklist);
+    }
+  }
+
+  mlir::RegionSuccessor parent(branch.getOperation());
+  llvm::SmallVector<mlir::RegionBranchPoint> predecessors;
+  branch.getPredecessors(parent, predecessors);
+  mlir::Attribute folded;
+  for (mlir::RegionBranchPoint predecessor : predecessors) {
+    std::optional<mlir::OperandRange> values;
+    if (predecessor.isParent()) {
+      if (mayEnterNoRegion)
+        values = branch.getEntrySuccessorOperands(parent);
+    } else {
+      mlir::RegionBranchTerminatorOpInterface terminator =
+          predecessor.getTerminatorPredecessorOrNull();
+      if (reachable.contains(terminator->getParentRegion()))
+        values = terminator.getSuccessorOperands(parent);
+    }
+    if (!values)
+      continue;
+    if (index >= values->size())
+      return {};
+    mlir::Attribute attr = foldToConstant((*values)[index], depth + 1);
+    if (!attr || (folded && attr != folded))
+      return {};
+    folded = attr;
+  }
+  return folded;
+}
+
+// TODO: Look into sharing this logic with MLIR's dataflow constant
+// propagation, which is similar but solves whole functions: too much compile
+// time when only a few values are needed.
+static mlir::Attribute foldToConstant(mlir::Value value, unsigned depth) {
+  mlir::Attribute attr;
+  if (mlir::matchPattern(value, mlir::m_Constant(&attr)))
+    return attr;
+
+  constexpr unsigned maxDepth = 8;
+  auto result = mlir::dyn_cast<mlir::OpResult>(value);
+  if (!result || depth >= maxDepth)
+    return {};
+
+  mlir::Operation *def = result.getOwner();
+  llvm::SmallVector<mlir::Attribute> operands;
----------------
xlauko wrote:

This folds every operand recursively with no cache, so shared subexpressions get refolded once per use, up to depth 8. It also reruns for each terminator and each goto re-walk. A per-query `DenseMap<Value, Attribute>`?

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


More information about the cfe-commits mailing list