[llvm-branch-commits] [clang] [CIR] Make cir-canonicalize a full canonicalizer (PR #240769)

Henrich Lauko via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sun Nov 15 04:25:10 PST 2026


https://github.com/xlauko updated https://github.com/llvm/llvm-project/pull/240769

>From 6639effda8c2817431e8fa148397e0d3b6503325 Mon Sep 17 00:00:00 2001
From: Henrich Lauko <hlauko at nvidia.com>
Date: Tue, 10 Nov 2026 09:48:02 +0000
Subject: [PATCH] [CIR] Make cir-canonicalize a full canonicalizer

cir-canonicalize applied the CIR canonicalization patterns and folders
only to a hand-written list of operations. It now builds the pattern set
once in initialize() and runs the greedy driver over every operation, so
every pattern and folder applies everywhere. Only operations that
rewriting left dead are erased, such as the inputs of a folded
operation: CIR stays close to the source, so unused locals and ignored
expressions are kept as in classic codegen. Empty scopes and switches
were removed only because they were dead, so they get explicit
canonicalization patterns.

The pass uses the multi-op driver because the region driver always
erases blocks without a CFG predecessor, and a block that starts with
cir.label is reached only through cir.goto until the goto solver runs.
Region simplification stays off for the same reason. The new
test-convergence option fails the pass when the driver does not reach a
fixpoint, as in the upstream canonicalizer.

Assisted-by: Claude Code (claude-opus-5-5).
---
 clang/include/clang/CIR/Dialect/IR/CIROps.td  |   2 +
 clang/include/clang/CIR/Dialect/Passes.td     |  12 +-
 clang/lib/CIR/Dialect/IR/CIRDialect.cpp       |  54 +++++----
 .../Dialect/Transforms/CIRCanonicalize.cpp    | 104 +++++++++---------
 clang/test/CIR/CodeGen/atomic-libcall.c       |   4 +
 clang/test/CIR/CodeGen/atomic.c               |   3 +
 clang/test/CIR/CodeGen/bitint-record-layout.c |   3 +
 clang/test/CIR/CodeGen/fold-during-cg.c       |  16 +--
 .../CIR/CodeGen/implicit-value-init-expr.cpp  |   1 +
 clang/test/CIR/CodeGen/new.cpp                |   4 +
 .../no-unique-address-empty-consteval.cpp     |   3 +
 .../test/CIR/CodeGen/paren-list-agg-init.cpp  |   2 +-
 .../CIR/CodeGen/pointer-to-data-member.cpp    |   6 +-
 clang/test/CIR/CodeGen/union.c                |   5 +
 clang/test/CIR/CodeGen/vla.c                  |  10 +-
 clang/test/CIR/CodeGen/void-value-expr.cpp    |   2 +
 clang/test/CIR/CodeGenCXX/new-array-init.cpp  |   6 +
 clang/test/CIR/CodeGenOpenACC/combined-copy.c |   1 +
 .../CIR/CodeGenSYCL/global-address-space.cpp  |   1 +
 .../CIR/Transforms/canonicalize-dead-code.cir |  51 +++++++++
 20 files changed, 198 insertions(+), 92 deletions(-)
 create mode 100644 clang/test/CIR/Transforms/canonicalize-dead-code.cir

diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index 93f4b8a90ca4fa..0b6108d0bf1dc1 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -1434,6 +1434,7 @@ def CIR_ScopeOp : CIR_EnterAnyRegionBranchOpBase<"scope", [
   let regions = (region AnyRegion:$scopeRegion);
 
   let hasFolder = 1;
+  let hasCanonicalizeMethod = 1;
   let hasVerifier = 1;
   let skipDefaultBuilders = 1;
   let assemblyFormat = [{
@@ -1814,6 +1815,7 @@ def CIR_SwitchOp : CIR_EnterAnyRegionBranchOpBase<"switch", [
 
   let regions = (region AnyRegion:$body);
 
+  let hasCanonicalizeMethod = 1;
   let skipDefaultBuilders = 1;
   let builders = [
     OpBuilder<(ins "mlir::Value":$condition,
diff --git a/clang/include/clang/CIR/Dialect/Passes.td b/clang/include/clang/CIR/Dialect/Passes.td
index 9a4bf9e576e4b1..1d957e2625d65c 100644
--- a/clang/include/clang/CIR/Dialect/Passes.td
+++ b/clang/include/clang/CIR/Dialect/Passes.td
@@ -11,10 +11,12 @@
 
 include "mlir/Pass/PassBase.td"
 
-def CIRCanonicalize : Pass<"cir-canonicalize"> {
+def CIRCanonicalize : Pass<"cir-canonicalize", "mlir::ModuleOp"> {
   let summary = "Performs CIR canonicalization";
   let description = [{
-    Perform canonicalizations on CIR and removes some redundant operations.
+    Applies the canonicalization patterns and folders of every CIR operation
+    to all operations until a fixpoint is reached. Operations that are dead
+    but not folded away are kept.
 
     This pass performs basic cleanup and canonicalization transformations that
     are not intended to affect CIR-to-source fidelity and high-level code
@@ -25,6 +27,12 @@ def CIRCanonicalize : Pass<"cir-canonicalize"> {
     `cir-simplify` pass.
   }];
 
+  let options = [
+    Option<"testConvergence", "test-convergence", "bool", /*default=*/"false",
+           "Test only: Fail pass if a second run of the patterns changes the "
+           "IR">
+  ];
+
   let constructor = "mlir::createCIRCanonicalizePass()";
   let dependentDialects = ["cir::CIRDialect"];
 }
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index 6c976e64c96bc0..ca227f7ccee4a5 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -2078,24 +2078,32 @@ LogicalResult cir::ScopeOp::verify() {
   return success();
 }
 
+/// Returns true if `region` is a single block that holds only a `cir.yield`.
+static bool isYieldOnlyRegion(Region &region) {
+  if (!region.hasOneBlock())
+    return false;
+  Block &block = region.front();
+  return llvm::hasSingleElement(block) && isa<cir::YieldOp>(block.front());
+}
+
 LogicalResult cir::ScopeOp::fold(FoldAdaptor /*adaptor*/,
                                  SmallVectorImpl<OpFoldResult> &results) {
-  // Only fold "trivial" scopes: a single block containing only a `cir.yield`.
-  if (!getRegion().hasOneBlock())
-    return failure();
-  Block &block = getRegion().front();
-  if (block.getOperations().size() != 1)
+  // Only fold a trivial scope that produces a value.
+  if (getNumResults() != 1 || !isYieldOnlyRegion(getRegion()))
     return failure();
-
-  auto yield = dyn_cast<cir::YieldOp>(block.front());
-  if (!yield)
+  auto yield = cast<cir::YieldOp>(getRegion().front().front());
+  if (yield.getNumOperands() != 1)
     return failure();
+  results.push_back(yield.getOperand(0));
+  return success();
+}
 
-  // Only fold when the scope produces a value.
-  if (getNumResults() != 1 || yield.getNumOperands() != 1)
+/// Erases a scope that yields no value and contains only its terminator.
+LogicalResult cir::ScopeOp::canonicalize(ScopeOp op,
+                                         PatternRewriter &rewriter) {
+  if (op.getNumResults() != 0 || !isYieldOnlyRegion(op.getRegion()))
     return failure();
-
-  results.push_back(yield.getOperand(0));
+  rewriter.eraseOp(op);
   return success();
 }
 
@@ -2174,28 +2182,19 @@ void cir::CleanupScopeOp::getSuccessorRegions(
 
 LogicalResult cir::CleanupScopeOp::canonicalize(CleanupScopeOp op,
                                                 PatternRewriter &rewriter) {
-  auto isRegionTrivial = [](Region &region) {
-    assert(!region.empty() && "CleanupScopeOp regions must not be empty");
-    if (!region.hasOneBlock())
-      return false;
-    Block &block = llvm::getSingleElement(region);
-    return llvm::hasSingleElement(block) &&
-           isa<cir::YieldOp>(llvm::getSingleElement(block));
-  };
-
   Region &body = op.getBodyRegion();
   Region &cleanup = op.getCleanupRegion();
 
   // An EH-only cleanup scope with an empty body can never trigger its cleanup
   // region — there are no operations in the body that could throw. Erase it.
-  if (op.getCleanupKind() == CleanupKind::EH && isRegionTrivial(body)) {
+  if (op.getCleanupKind() == CleanupKind::EH && isYieldOnlyRegion(body)) {
     rewriter.eraseOp(op);
     return success();
   }
 
   // A cleanup scope with a trivial cleanup region has no cleanup to perform.
   // Inline the body into the parent block and erase the scope.
-  if (!isRegionTrivial(cleanup) || !body.hasOneBlock())
+  if (!isYieldOnlyRegion(cleanup) || !body.hasOneBlock())
     return failure();
 
   Block &bodyBlock = body.front();
@@ -2381,6 +2380,15 @@ void cir::CaseOp::build(OpBuilder &builder, OperationState &result,
 // SwitchOp
 //===----------------------------------------------------------------------===//
 
+/// Erases a switch whose body contains only its terminator.
+LogicalResult cir::SwitchOp::canonicalize(SwitchOp op,
+                                          PatternRewriter &rewriter) {
+  if (!isYieldOnlyRegion(op.getBody()))
+    return failure();
+  rewriter.eraseOp(op);
+  return success();
+}
+
 void cir::SwitchOp::build(OpBuilder &builder, OperationState &result,
                           Value cond, BuilderOpStateCallbackRef switchBuilder) {
   assert(switchBuilder && "the builder callback for regions must be present");
diff --git a/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp b/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp
index 3d860f47eaa886..0de68064564acd 100644
--- a/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp
@@ -6,25 +6,21 @@
 //
 //===----------------------------------------------------------------------===//
 //
-// This file implements pass that canonicalizes CIR operations, eliminating
-// redundant branches, empty scopes, and other unnecessary operations.
+// This file implements the CIR canonicalization pass. It applies the
+// canonicalization patterns and folders of the CIR dialect to every operation
+// until a fixpoint is reached.
 //
 //===----------------------------------------------------------------------===//
 
 #include "PassDetail.h"
-#include "mlir/Dialect/Func/IR/FuncOps.h"
-#include "mlir/IR/Block.h"
-#include "mlir/IR/Operation.h"
+#include "mlir/IR/Iterators.h"
 #include "mlir/IR/PatternMatch.h"
-#include "mlir/IR/Region.h"
-#include "mlir/Support/LogicalResult.h"
+#include "mlir/Rewrite/FrozenRewritePatternSet.h"
 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
 #include "clang/CIR/Dialect/IR/CIRDialect.h"
 #include "clang/CIR/Dialect/Passes.h"
-#include "clang/CIR/MissingFeatures.h"
 
 using namespace mlir;
-using namespace cir;
 
 namespace mlir {
 #define GEN_PASS_DEF_CIRCANONICALIZE
@@ -33,60 +29,58 @@ namespace mlir {
 
 namespace {
 
-//===----------------------------------------------------------------------===//
-// CIRCanonicalizePass
-//===----------------------------------------------------------------------===//
-
 struct CIRCanonicalizePass
     : public impl::CIRCanonicalizeBase<CIRCanonicalizePass> {
   using CIRCanonicalizeBase::CIRCanonicalizeBase;
 
-  // The same operation rewriting done here could have been performed
-  // by CanonicalizerPass (adding hasCanonicalizer for target Ops and
-  // implementing the same from above in CIRDialects.cpp). However, it's
-  // currently too aggressive for static analysis purposes, since it might
-  // remove things where a diagnostic can be generated.
-  //
-  // FIXME: perhaps we can add one more mode to GreedyRewriteConfig to
-  // disable this behavior.
-  void runOnOperation() override;
-};
+  LogicalResult initialize(MLIRContext *context) override {
+    RewritePatternSet owningPatterns(context);
+    for (RegisteredOperationName op : context->getRegisteredOperationsByDialect(
+             cir::CIRDialect::getDialectNamespace()))
+      op.getCanonicalizationPatterns(owningPatterns, context);
+    patterns =
+        std::make_shared<FrozenRewritePatternSet>(std::move(owningPatterns));
+    return success();
+  }
 
-void CIRCanonicalizePass::runOnOperation() {
-  RewritePatternSet patterns(&getContext());
+  void runOnOperation() override {
+    Region &body = getOperation().getBodyRegion();
+    bool changed = applyPatterns(body);
+    // A single run must reach a fixpoint, so a second run changes nothing.
+    if (testConvergence && applyPatterns(body))
+      return signalPassFailure();
+    if (!changed)
+      markAllAnalysesPreserved();
+  }
 
-  // Collect canonicalization patterns from CIR ops.
-  mlir::Dialect *cir = getContext().getLoadedDialect<cir::CIRDialect>();
-  for (mlir::RegisteredOperationName op :
-       getContext().getRegisteredOperations())
-    if (&op.getDialect() == cir)
-      op.getCanonicalizationPatterns(patterns, &getContext());
+  /// Applies the patterns to every operation in `body` and returns whether
+  /// the IR changed.
+  bool applyPatterns(Region &body) {
+    // The driver pops from the back of the worklist, so seeding it in reverse
+    // pre-order visits the IR top-down.
+    SmallVector<Operation *> ops;
+    body.walk<WalkOrder::PostOrder, ReverseIterator>(
+        [&](Operation *op) { ops.push_back(op); });
 
-  // Collect operations to apply patterns.
-  llvm::SmallVector<Operation *, 16> ops;
-  getOperation()->walk([&](Operation *op) {
-    assert(!cir::MissingFeatures::tryOp());
-    assert(!cir::MissingFeatures::callOp());
+    // Use the multi-op driver rather than applyPatternsGreedily on the
+    // regions: the region driver always erases blocks that have no CFG
+    // predecessor, but before the goto solver runs, a block that starts with
+    // a `cir.label` is reached only through `cir.goto`, which is not a CFG
+    // edge. For the same reason region simplification is not run.
+    //
+    // Only operations that rewriting left dead are erased: CIR stays close to
+    // the source, so an unused local or an ignored expression is still
+    // emitted, as in classic codegen.
+    GreedyRewriteConfig config;
+    config.setScope(&body);
+    config.setDeadCodeElimination(GreedyDeadCodeElimination::NewlyDead);
+    bool changed = false;
+    (void)applyOpPatternsGreedily(ops, *patterns, config, &changed);
+    return changed;
+  }
 
-    // Many operations are here to perform a manual `fold` in
-    // applyOpPatternsGreedily.
-    if (isa<BrOp, BrCondOp, BuiltinIntCastOp, CastOp, ScopeOp, SwitchOp,
-            SelectOp, IncOp, DecOp, MinusOp, FNegOp, NotOp, AddOp, MulOp, AndOp,
-            OrOp, XorOp, MaxOp, MinOp, ComplexCreateOp, ComplexImagOp,
-            ComplexRealOp, VecCmpOp, VecFCmpOp, VecCreateOp, VecExtractOp,
-            VecShuffleOp, VecShuffleDynamicOp, VecTernaryOp, BitClrsbOp,
-            BitClzOp, BitCtzOp, BitFfsOp, BitParityOp, BitPopcountOp,
-            BitReverseOp, ByteSwapOp, RotateOp, ConstantOp, CleanupScopeOp>(op))
-      ops.push_back(op);
-  });
-
-  // Apply patterns. Scope the rewrite to the body region, which encloses
-  // the common ancestor the driver would otherwise search for.
-  GreedyRewriteConfig config;
-  config.setScope(&getOperation()->getRegion(0));
-  if (applyOpPatternsGreedily(ops, std::move(patterns), config).failed())
-    signalPassFailure();
-}
+  std::shared_ptr<const FrozenRewritePatternSet> patterns;
+};
 
 } // namespace
 
diff --git a/clang/test/CIR/CodeGen/atomic-libcall.c b/clang/test/CIR/CodeGen/atomic-libcall.c
index b20eefce22b76b..cd2fdbe2fdb2e5 100644
--- a/clang/test/CIR/CodeGen/atomic-libcall.c
+++ b/clang/test/CIR/CodeGen/atomic-libcall.c
@@ -38,6 +38,7 @@ void scoped_load(struct Big *ptr) {
   __scoped_atomic_load(ptr, &b, __ATOMIC_RELAXED, __MEMORY_SCOPE_SYSTEM);
   // CIR:      %[[DEST_SLOT:.+]] = cir.alloca "b" align(4) : !cir.ptr<!rec_Big>
   // CIR:      %[[PTR:.+]] = cir.load align(8) %{{.+}} : !cir.ptr<!cir.ptr<!rec_Big>>, !cir.ptr<!rec_Big>
+  // CIR-NEXT: %{{.+}} = cir.const #cir.int<0> : !s32i
   // CIR-NEXT: %[[PTR_INTPTR:.+]] = cir.cast bitcast %[[PTR]] : !cir.ptr<!rec_Big> -> !cir.ptr<!cir.int<u, 192>>
   // CIR-NEXT: %[[DEST_INTPTR:.+]] = cir.cast bitcast %[[DEST_SLOT]] : !cir.ptr<!rec_Big> -> !cir.ptr<!cir.int<u, 192>>
   // CIR-NEXT: %[[SIZE:.+]] = cir.const #cir.int<24> : !u64i
@@ -102,6 +103,7 @@ void scoped_store(struct Big *dest, struct Big *val) {
 
   __scoped_atomic_store(dest, val, __ATOMIC_RELAXED, __MEMORY_SCOPE_SYSTEM);
   // CIR:      %[[DEST_PTR:.+]] = cir.load align(8) %{{.+}} : !cir.ptr<!cir.ptr<!rec_Big>>, !cir.ptr<!rec_Big>
+  // CIR-NEXT: %{{.+}} = cir.const #cir.int<0> : !s32i
   // CIR-NEXT: %[[VALUE_PTR:.+]] = cir.load align(8) %{{.+}} : !cir.ptr<!cir.ptr<!rec_Big>>, !cir.ptr<!rec_Big>
   // CIR-NEXT: %[[DEST_INTPTR:.+]] = cir.cast bitcast %[[DEST_PTR]] : !cir.ptr<!rec_Big> -> !cir.ptr<!cir.int<u, 192>>
   // CIR-NEXT: %[[VALUE_INTPTR:.+]] = cir.cast bitcast %[[VALUE_PTR]] : !cir.ptr<!rec_Big> -> !cir.ptr<!cir.int<u, 192>>
@@ -177,6 +179,7 @@ void scoped_exchange(struct Big *ptr, struct Big *val, struct Big *old) {
   // CIR-NEXT: %[[VAL:.*]] = cir.alloca "val" align(8) init : !cir.ptr<!cir.ptr<!rec_Big>>
   // CIR-NEXT: %[[OLD:.*]] = cir.alloca "old" align(8) init : !cir.ptr<!cir.ptr<!rec_Big>>
   // CIR:      %[[LOAD_PTR:.*]] = cir.load align(8) %[[PTR]] : !cir.ptr<!cir.ptr<!rec_Big>>, !cir.ptr<!rec_Big>
+  // CIR-NEXT: %{{.+}} = cir.const #cir.int<0> : !s32i
   // CIR-NEXT: %[[LOAD_VAL:.*]] = cir.load align(8) %[[VAL]] : !cir.ptr<!cir.ptr<!rec_Big>>, !cir.ptr<!rec_Big>
   // CIR-NEXT: %[[LOAD_OLD:.*]] = cir.load align(8) %[[OLD]] : !cir.ptr<!cir.ptr<!rec_Big>>, !cir.ptr<!rec_Big>
   // CIR-NEXT: %[[CAST_PTR:.*]] = cir.cast bitcast %[[LOAD_PTR]] : !cir.ptr<!rec_Big> -> !cir.ptr<!cir.int<u, 192>>
@@ -284,6 +287,7 @@ void scoped_cmpxchg(struct Big *ptr, struct Big *expected, struct Big *desired)
 
   __scoped_atomic_compare_exchange(ptr, expected, desired, /*weak=*/0, __ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE, __MEMORY_SCOPE_SYSTEM);
   // CIR:      %[[PTR:.+]] = cir.load align(8) %{{.+}} : !cir.ptr<!cir.ptr<!rec_Big>>, !cir.ptr<!rec_Big>
+  // CIR-NEXT: %{{.+}} = cir.const #cir.int<0> : !s32i
   // CIR-NEXT: %[[EXPECTED:.+]] = cir.load align(8) %{{.+}} : !cir.ptr<!cir.ptr<!rec_Big>>, !cir.ptr<!rec_Big>
   // CIR-NEXT: %[[DESIRED:.+]] = cir.load align(8) %{{.+}} : !cir.ptr<!cir.ptr<!rec_Big>>, !cir.ptr<!rec_Big>
   // CIR-NEXT: %[[PTR_INTPTR:.+]] = cir.cast bitcast %[[PTR]] : !cir.ptr<!rec_Big> -> !cir.ptr<!cir.int<u, 192>>
diff --git a/clang/test/CIR/CodeGen/atomic.c b/clang/test/CIR/CodeGen/atomic.c
index 97e7435d68f7af..f151dd43e036ea 100644
--- a/clang/test/CIR/CodeGen/atomic.c
+++ b/clang/test/CIR/CodeGen/atomic.c
@@ -102,12 +102,15 @@ void init_with_padding(_Atomic struct S1 *p) {
   // CIR-NEXT: cir.libc.memset %[[MEMSET_SIZE]] bytes at %[[PTR_VOID]] align(8) to %[[MEMSET_VALUE]] : !cir.ptr<!void>, !u8i, !u64i
   // CIR-NEXT: %[[VALUE_PTR:.+]] = cir.get_member %[[PTR]][0] {name = "value"} : !cir.ptr<!rec_anon_struct> -> !cir.ptr<!rec_S1>
   // CIR-NEXT: %[[X_PTR:.+]] = cir.get_member %[[VALUE_PTR]][0] {name = "x"} : !cir.ptr<!rec_S1> -> !cir.ptr<!s16i>
+  // CIR-NEXT: %{{.+}} = cir.const #cir.int<1> : !s32i
   // CIR-NEXT: %[[X_INIT:.+]] = cir.const #cir.int<1> : !s16i
   // CIR-NEXT: cir.store align(8) %[[X_INIT]], %[[X_PTR]] : !s16i, !cir.ptr<!s16i>
   // CIR-NEXT: %[[Y_PTR:.+]] = cir.get_member %[[VALUE_PTR]][1] {name = "y"} : !cir.ptr<!rec_S1> -> !cir.ptr<!s16i>
+  // CIR-NEXT: %{{.+}} = cir.const #cir.int<2> : !s32i
   // CIR-NEXT: %[[Y_INIT:.+]] = cir.const #cir.int<2> : !s16i
   // CIR-NEXT: cir.store align(2) %[[Y_INIT]], %[[Y_PTR]] : !s16i, !cir.ptr<!s16i>
   // CIR-NEXT: %[[Z_PTR:.+]] = cir.get_member %[[VALUE_PTR]][2] {name = "z"} : !cir.ptr<!rec_S1> -> !cir.ptr<!s16i>
+  // CIR-NEXT: %{{.+}} = cir.const #cir.int<3> : !s32i
   // CIR-NEXT: %[[Z_INIT:.+]] = cir.const #cir.int<3> : !s16i
   // CIR-NEXT: cir.store align(4) %[[Z_INIT]], %[[Z_PTR]] : !s16i, !cir.ptr<!s16i>
 
diff --git a/clang/test/CIR/CodeGen/bitint-record-layout.c b/clang/test/CIR/CodeGen/bitint-record-layout.c
index d4c95bee2a49ba..aa0dc008bfe561 100644
--- a/clang/test/CIR/CodeGen/bitint-record-layout.c
+++ b/clang/test/CIR/CodeGen/bitint-record-layout.c
@@ -201,6 +201,7 @@ _BitInt(128) get_bi(void) { return l128[1].bi; }
 // CIR-LABEL: cir.func no_inline dso_local @get_bi() -> !s128i_bitint
 // CIR-NEXT: %[[RET_ALLOC:.*]] = cir.alloca "__retval" align(8) : !cir.ptr<!s128i_bitint>
 // CIR-NEXT: %[[GET_GLOB:.*]] = cir.get_global @l128 : !cir.ptr<!cir.array<!rec_Last128 x 2>>
+// CIR-NEXT: %{{.+}} = cir.const #cir.int<1> : !s32i
 // CIR-NEXT: %[[ONE:.*]] = cir.const #cir.int<1> : !s64i
 // CIR-NEXT: %[[ARR_GEP:.*]] = cir.get_element inbounds %[[GET_GLOB]][%[[ONE]] : !s64i] : !cir.ptr<!cir.array<!rec_Last128 x 2>> -> !cir.ptr<!rec_Last128>
 // CIR-NEXT: %[[GET_BI:.*]] = cir.get_member %[[ARR_GEP]][2] {name = "bi"} : !cir.ptr<!rec_Last128> -> !cir.ptr<!s128i_bitint>
@@ -215,9 +216,11 @@ _BitInt(128) get_bi2(void) { return arrMem[1].bi[1]; }
 // CIR-LABEL: cir.func no_inline dso_local @get_bi2() -> !s128i_bitint attributes {"cir.target-features" = "+cx8,+mmx,+sse,+sse2,+x87", nothrow, optnone} {
 // CIR-NEXT: %[[RET_ALLOC:.*]] = cir.alloca "__retval" align(8) : !cir.ptr<!s128i_bitint>
 // CIR-NEXT: %[[GET_GLOB:.*]] = cir.get_global @arrMem : !cir.ptr<!cir.array<!rec_ArrMem x 2>>
+// CIR-NEXT: %{{.+}} = cir.const #cir.int<1> : !s32i
 // CIR-NEXT: %[[ONE_2:.*]] = cir.const #cir.int<1> : !s64i
 // CIR-NEXT: %[[ARR_GEP:.*]] = cir.get_element inbounds %[[GET_GLOB]][%[[ONE_2]] : !s64i] : !cir.ptr<!cir.array<!rec_ArrMem x 2>> -> !cir.ptr<!rec_ArrMem>
 // CIR-NEXT: %[[GET_BI_ARR:.*]] = cir.get_member %[[ARR_GEP]][2] {name = "bi"} : !cir.ptr<!rec_ArrMem> -> !cir.ptr<!cir.array<!s128i_bitint x 2>>
+// CIR-NEXT: %{{.+}} = cir.const #cir.int<1> : !s32i
 // CIR-NEXT: %[[ONE:.*]] = cir.const #cir.int<1> : !s64i
 // CIR-NEXT: %[[GET_BI_ELT:.*]] = cir.get_element inbounds %[[GET_BI_ARR]][%[[ONE]] : !s64i] : !cir.ptr<!cir.array<!s128i_bitint x 2>> -> !cir.ptr<!s128i_bitint>
 // CIR-NEXT: %[[LOAD_BI:.*]] = cir.load align(8) %[[GET_BI_ELT]] : !cir.ptr<!s128i_bitint>, !s128i_bitint
diff --git a/clang/test/CIR/CodeGen/fold-during-cg.c b/clang/test/CIR/CodeGen/fold-during-cg.c
index 137d5bb3d4f20d..1e2eb0bbcd2419 100644
--- a/clang/test/CIR/CodeGen/fold-during-cg.c
+++ b/clang/test/CIR/CodeGen/fold-during-cg.c
@@ -1,9 +1,9 @@
 // RUN: %clang_cc1 -std=c23 -triple x86_64-unknown-linux-gnu -mmlir -mlir-print-ir-before=cir-canonicalize -Wno-unused-value -Wno-constant-conversion -Wno-literal-conversion -fclangir -emit-cir %s -o %t.cir 2> %t-before.cir
 // Note: The "before" check intentionally uses the same CIR check prefixes to
 //       verify that the IR is folded during code generation and not during
-//       canonicalization. In a few cases, it is necessary to match an unused
-//       duplicate constant that will be erased during canonicalization.
-// RUN: FileCheck --input-file=%t-before.cir %s -check-prefixes=CIR,CIR-DUP,ALL
+//       canonicalization. In a few cases, an unused duplicate constant is
+//       matched explicitly; canonicalization does not erase it.
+// RUN: FileCheck --input-file=%t-before.cir %s -check-prefixes=CIR,ALL
 // RUN: FileCheck --input-file=%t.cir %s -check-prefixes=CIR,ALL
 // RUN: %clang_cc1 -std=c23 -triple x86_64-unknown-linux-gnu -Wno-unused-value -Wno-constant-conversion -Wno-literal-conversion -fclangir -emit-llvm %s -o %t-cir.ll
 // RUN: FileCheck --input-file=%t-cir.ll %s -check-prefixes=LLVM_OGCG,ALL
@@ -133,7 +133,7 @@ void fold_int_minus() {
   // LLVM_OGCG: store i32 -2, ptr %{{.*}}
 
   n = -(-3);
-  // CIR-DUP: %[[UNUSED_THREE:.*]] = cir.const #cir.int<3> : !s32i
+  // CIR: %[[UNUSED_THREE:.*]] = cir.const #cir.int<3> : !s32i
   // CIR: %[[THREE:.*]] = cir.const #cir.int<3> : !s32i
   // CIR-NOT: cir.minus
   // CIR: cir.store{{.*}} %[[THREE]], %{{.*}}
@@ -158,7 +158,7 @@ void fold_int_minus() {
   // LLVM_OGCG: store i16 -2, ptr %{{.*}}
 
   s = -(-3);
-  // CIR-DUP: %[[UNUSED_THREE:.*]] = cir.const #cir.int<3> : !s32i
+  // CIR: %[[UNUSED_THREE:.*]] = cir.const #cir.int<3> : !s32i
   // CIR: %[[THREE:.*]] = cir.const #cir.int<3> : !s16i
   // CIR-NOT: cir.minus
   // CIR: cir.store{{.*}} %[[THREE]], %{{.*}}
@@ -183,7 +183,7 @@ void fold_int_minus() {
   // LLVM_OGCG: store i32 -2, ptr %{{.*}}
 
   u = -(-3);
-  // CIR-DUP: %[[UNUSED_THREE:.*]] = cir.const #cir.int<3> : !s32i
+  // CIR: %[[UNUSED_THREE:.*]] = cir.const #cir.int<3> : !s32i
   // CIR: %[[THREE:.*]] = cir.const #cir.int<3> : !u32i
   // CIR-NOT: cir.minus
   // CIR: cir.store{{.*}} %[[THREE]], %{{.*}}
@@ -238,7 +238,7 @@ void fold_float_minus() {
   // LLVM_OGCG: store float -2.000000e+00, ptr %{{.*}}
 
   f = -(-3.0f);
-  // CIR-DUP: %[[UNUSED_THREE:.*]] = cir.const #cir.fp<3.000000e+00> : !cir.float
+  // CIR: %[[UNUSED_THREE:.*]] = cir.const #cir.fp<3.000000e+00> : !cir.float
   // CIR: %[[THREE:.*]] = cir.const #cir.fp<3.000000e+00> : !cir.float
   // CIR-NOT: cir.minus
   // CIR: cir.store{{.*}} %[[THREE]], %{{.*}}
@@ -251,7 +251,7 @@ void fold_float_minus() {
   // LLVM_OGCG: store double -2.000000e+00, ptr %{{.*}}
 
   d = -(-3.0);
-  // CIR-DUP: %[[UNUSED_THREE:.*]] = cir.const #cir.fp<3.000000e+00> : !cir.double
+  // CIR: %[[UNUSED_THREE:.*]] = cir.const #cir.fp<3.000000e+00> : !cir.double
   // CIR: %[[THREE:.*]] = cir.const #cir.fp<3.000000e+00> : !cir.double
   // CIR-NOT: cir.minus
   // CIR: cir.store{{.*}} %[[THREE]], %{{.*}}
diff --git a/clang/test/CIR/CodeGen/implicit-value-init-expr.cpp b/clang/test/CIR/CodeGen/implicit-value-init-expr.cpp
index 399e72bcf202d4..e780b9780eb813 100644
--- a/clang/test/CIR/CodeGen/implicit-value-init-expr.cpp
+++ b/clang/test/CIR/CodeGen/implicit-value-init-expr.cpp
@@ -14,6 +14,7 @@ void test(void *p) {
 // CIR:     cir.func{{.*}} @_Z4testPv
 // CIR-NEXT:    %[[P:.*]] = cir.alloca "p" align(8) init : !cir.ptr<!cir.ptr<!void>>
 // CIR-NEXT:    cir.store %arg0, %[[P]] : !cir.ptr<!void>, !cir.ptr<!cir.ptr<!void>>
+// CIR-NEXT:    %{{.+}} = cir.const #cir.int<4> : !u64i
 // CIR-NEXT:    %[[P1:.*]] = cir.load align(8) %[[P]] : !cir.ptr<!cir.ptr<!void>>, !cir.ptr<!void>
 // CIR-NEXT:    %[[P2:.*]] = cir.cast bitcast %[[P1]] : !cir.ptr<!void> -> !cir.ptr<!s32i>
 // CIR-NEXT:    %[[P3:.*]] = cir.const #cir.int<0> : !s32i
diff --git a/clang/test/CIR/CodeGen/new.cpp b/clang/test/CIR/CodeGen/new.cpp
index 3a26f7264c0001..e89a030ab360ea 100644
--- a/clang/test/CIR/CodeGen/new.cpp
+++ b/clang/test/CIR/CodeGen/new.cpp
@@ -464,6 +464,7 @@ void t_new_huge_float_size() {
 }
 
 // CHECK:  cir.func {{.*}} @_Z21t_new_huge_float_sizev()
+// CHECK:    %{{.+}} = cir.const #cir.int<18446744073709551615> : !u64i
 // CHECK:    %[[ALLOCATION_SIZE:.*]] = cir.const #cir.int<18446744073709551615> : !u64i
 // CHECK:    %{{.*}} = cir.call @_Znam(%[[ALLOCATION_SIZE]]) {allocsize = array<i32: 0>, builtin, memory_effects = #cir.memory_effects<other = none, arg_mem = none, inaccessible_mem = readwrite, errno_mem = write, target_mem0 = none, target_mem1 = none>} : (!u64i {llvm.noundef}) -> (!cir.ptr<!void> {llvm.noalias, llvm.nonnull, llvm.noundef})
 
@@ -1140,6 +1141,7 @@ void test_array_new_with_ctor_partial_init_list() {
 
 // CIR-BEFORE-LPP: cir.func {{.*}} @_Z42test_array_new_with_ctor_partial_init_listv
 // CIR-BEFORE-LPP:    %[[P_ADDR:.*]] = cir.alloca "p" {{.*}} init : !cir.ptr<!cir.ptr<!rec_G>>
+// CIR-BEFORE-LPP:    %{{.+}} = cir.const #cir.int<8> : !u64i
 // CIR-BEFORE-LPP:    %[[EIGHT:.*]] = cir.const #cir.int<8> : !u64i
 // CIR-BEFORE-LPP:    %[[RAW_PTR:.*]] = cir.call @_Znam(%[[EIGHT]]) {allocsize = array<i32: 0>, builtin, memory_effects = #cir.memory_effects<other = none, arg_mem = none, inaccessible_mem = readwrite, errno_mem = write, target_mem0 = none, target_mem1 = none>} : (!u64i {llvm.noundef})
 // CIR-BEFORE-LPP:    %[[BEGIN:.*]] = cir.cast bitcast %[[RAW_PTR]] : !cir.ptr<!void> -> !cir.ptr<!rec_G>
@@ -1161,6 +1163,7 @@ void test_array_new_with_ctor_partial_init_list() {
 
 // CHECK:  cir.func {{.*}} @_Z42test_array_new_with_ctor_partial_init_listv
 // CHECK:    %[[P_ADDR:.*]] = cir.alloca "p" {{.*}} init : !cir.ptr<!cir.ptr<!rec_G>>
+// CHECK:    %{{.+}} = cir.const #cir.int<8> : !u64i
 // CHECK:    %[[EIGHT:.*]] = cir.const #cir.int<8> : !u64i
 // CHECK:    %[[RAW_PTR:.*]] = cir.call @_Znam(%[[EIGHT]]) {allocsize = array<i32: 0>, builtin, memory_effects = #cir.memory_effects<other = none, arg_mem = none, inaccessible_mem = readwrite, errno_mem = write, target_mem0 = none, target_mem1 = none>} : (!u64i {llvm.noundef})
 // CHECK:    %[[BEGIN:.*]] = cir.cast bitcast %[[RAW_PTR]] : !cir.ptr<!void> -> !cir.ptr<!rec_G>
@@ -1240,6 +1243,7 @@ namespace gh227980 {
   }
 
 // CHECK-LABEL: cir.func{{.*}}@_ZN8gh22798015huge_float_sizeEv()
+// CHECK: %{{.+}} = cir.const #cir.int<18446744073709551615> : !u64i
 // CHECK: %[[NEG:.*]] = cir.const #cir.int<18446744073709551615> : !u64i
 // CHECK: cir.call @_Znam(%[[NEG]]) {allocsize = array<i32: 0>, builtin, memory_effects = #cir.memory_effects<other = none, arg_mem = none, inaccessible_mem = readwrite, errno_mem = write, target_mem0 = none, target_mem1 = none>} : (!u64i {llvm.noundef})
 
diff --git a/clang/test/CIR/CodeGen/no-unique-address-empty-consteval.cpp b/clang/test/CIR/CodeGen/no-unique-address-empty-consteval.cpp
index 403a191a40f751..026f69f8d19b57 100644
--- a/clang/test/CIR/CodeGen/no-unique-address-empty-consteval.cpp
+++ b/clang/test/CIR/CodeGen/no-unique-address-empty-consteval.cpp
@@ -65,6 +65,7 @@ struct U {
 
 // CIR-LABEL: cir.func {{.*}} @_Z19test_aggregate_initv(
 // CIR:         %[[V3:.*]] = cir.get_member %{{.+}}[0] {name = "v3"}
+// CIR-NEXT:    %{{.+}} = cir.const #cir.int<39612> : !s32i
 // CIR-NEXT:    %[[C:.*]] = cir.const #cir.int<39612>
 // CIR-NEXT:    cir.store align(8) %[[C]], %[[V3]] : !u64i, !cir.ptr<!u64i>
 // CIR-NOT:      #cir.zero : !rec_Empty
@@ -94,6 +95,7 @@ unsigned long test_default_member_init() {
 // CIR:         %[[V:.*]] = cir.get_member %{{.+}}[0] {name = "v"}
 // CIR:         %[[X:.*]] = cir.load align(8) %{{.+}} : !cir.ptr<!u64i>, !u64i
 // CIR-NEXT:    cir.store align(8) %[[X]], %[[V]] : !u64i, !cir.ptr<!u64i>
+// CIR-NEXT:    %{{.+}} = cir.cast bitcast %{{.+}} : !cir.ptr<!rec_S> -> !cir.ptr<!rec_Empty>
 // CIR-NEXT:    cir.return
 
 // LLVM-LABEL: define {{.*}} void @_ZN1SC2Em(
@@ -119,6 +121,7 @@ unsigned long test_default_member_init() {
 // CIR:         %[[V4:.*]] = cir.get_member %{{.+}}[0] {name = "v4"}
 // CIR:         %[[X4:.*]] = cir.load align(8) %{{.+}} : !cir.ptr<!u64i>, !u64i
 // CIR-NEXT:    cir.store align(8) %[[X4]], %[[V4]] : !u64i, !cir.ptr<!u64i>
+// CIR-NEXT:    %{{.+}} = cir.cast bitcast %{{.+}} : !cir.ptr<!rec_V> -> !cir.ptr<!rec_Empty>
 // CIR-NEXT:    cir.return
 
 // LLVM-LABEL: define {{.*}} void @_ZN1VC2Em(
diff --git a/clang/test/CIR/CodeGen/paren-list-agg-init.cpp b/clang/test/CIR/CodeGen/paren-list-agg-init.cpp
index e240aecaabf516..beda22e79786a4 100644
--- a/clang/test/CIR/CodeGen/paren-list-agg-init.cpp
+++ b/clang/test/CIR/CodeGen/paren-list-agg-init.cpp
@@ -363,7 +363,7 @@ D foo8() {
 // CIR: %[[FP_2:.*]] = cir.const #cir.fp<2
 // CIR: cir.store align(8) %[[FP_2]], %[[GET_J]] : !cir.double, !cir.ptr<!cir.double>
 // CIR: %[[GET_C:.*]] = cir.get_member %[[D_ALLOCA]][3] {name = "c"} : !cir.ptr<![[STRUCT_D]]> -> !cir.ptr<![[STRUCT_A]]>
-// CIR: %[[GET_C_PTR_I8:.*]] = cir.cast bitcast %12 : !cir.ptr<![[STRUCT_A]]> -> !cir.ptr<!u8i>
+// CIR: %[[GET_C_PTR_I8:.*]] = cir.cast bitcast %[[GET_C]] : !cir.ptr<![[STRUCT_A]]> -> !cir.ptr<!u8i>
 // CIR: %[[CONST_0:.*]] = cir.const #cir.int<0> : !u8i
 // CIR: %[[CONST_16:.*]] = cir.const #cir.int<16> : !u64i
 // CIR: %[[GET_C_VOID_PTR:.*]] = cir.cast bitcast %[[GET_C_PTR_I8]] : !cir.ptr<!u8i> -> !cir.ptr<!void> 
diff --git a/clang/test/CIR/CodeGen/pointer-to-data-member.cpp b/clang/test/CIR/CodeGen/pointer-to-data-member.cpp
index c0dd470e4a66e5..27b60cc8ec2a03 100644
--- a/clang/test/CIR/CodeGen/pointer-to-data-member.cpp
+++ b/clang/test/CIR/CodeGen/pointer-to-data-member.cpp
@@ -458,7 +458,8 @@ void useHasField() {
 
 // CIR-BEFORE-LABEL: cir.func {{.*}}_Z14changeFieldPtrIXadL_ZN8hasField5fieldEEEEvRS0_
 // CIR-BEFORE: %[[ARG:.*]] = cir.alloca "hf" {{.*}} : !cir.ptr<!cir.ptr<!rec_hasField>>
-// CIR-BEFORE: %[[ONE:.*]] = cir.const #cir.int<1>
+// CIR-BEFORE: %{{.+}} = cir.const #cir.int<1> : !s32i
+// CIR-BEFORE: %[[ONE:.*]] = cir.const #cir.int<1> : !u32i
 // CIR-BEFORE: %[[LOAD_ARG:.*]] = cir.load %[[ARG]] : !cir.ptr<!cir.ptr<!rec_hasField>>, !cir.ptr<!rec_hasField>
 // CIR-BEFORE: %[[GET_DM:.*]] = cir.const #cir.data_member<[0]> : !cir.data_member<!u32i in !rec_hasField>
 // CIR-BEFORE: %[[GET_MEM:.*]] = cir.get_runtime_member %[[LOAD_ARG]][%[[GET_DM]] : !cir.data_member<!u32i in !rec_hasField>] : !cir.ptr<!rec_hasField> -> !cir.ptr<!u32i>
@@ -466,7 +467,8 @@ void useHasField() {
 
 // CIR-AFTER-LABEL: cir.func {{.*}}_Z14changeFieldPtrIXadL_ZN8hasField5fieldEEEEvRS0_
 // CIR-AFTER: %[[ARG:.*]] = cir.alloca "hf" {{.*}} : !cir.ptr<!cir.ptr<!rec_hasField>>
-// CIR-AFTER: %[[ONE:.*]] = cir.const #cir.int<1>
+// CIR-AFTER: %{{.+}} = cir.const #cir.int<1> : !s32i
+// CIR-AFTER: %[[ONE:.*]] = cir.const #cir.int<1> : !u32i
 // CIR-AFTER: %[[LOAD_ARG:.*]] = cir.load %[[ARG]] : !cir.ptr<!cir.ptr<!rec_hasField>>, !cir.ptr<!rec_hasField>
 // CIR-AFTER: %[[ZERO:.*]] = cir.const #cir.int<0> : !s64i
 // CIR-AFTER: %[[ARG_AS_PTR:.*]] = cir.cast bitcast %[[LOAD_ARG]] : !cir.ptr<!rec_hasField> -> !cir.ptr<!s8i>
diff --git a/clang/test/CIR/CodeGen/union.c b/clang/test/CIR/CodeGen/union.c
index 65937399a1e832..ce4130be956291 100644
--- a/clang/test/CIR/CodeGen/union.c
+++ b/clang/test/CIR/CodeGen/union.c
@@ -118,6 +118,7 @@ void shouldGenerateUnionAccess(union U2 u) {
 // CIR-NEXT:   %[[CAST:.*]] = cir.cast bitcast %[[COERCE]] : !cir.ptr<!u64i> -> !cir.ptr<!rec_U2>
 // CIR-NEXT:   %[[U:.*]] = cir.alloca "u" align(8) init : !cir.ptr<!rec_U2>
 // CIR-NEXT:   cir.copy %[[CAST]] align(8) to %[[U]] align(8) : !cir.ptr<!rec_U2>
+// CIR-NEXT:   %{{.+}} = cir.const #cir.int<0> : !s32i
 // CIR-NEXT:   %[[ZERO:.*]] = cir.const #cir.int<0> : !s8i
 // CIR-NEXT:   %[[B_PTR:.*]] = cir.get_member %[[U]][0] {name = "b"} : !cir.ptr<!rec_U2> -> !cir.ptr<!s8i>
 // CIR-NEXT:   cir.store{{.*}} %[[ZERO]], %[[B_PTR]] : !s8i, !cir.ptr<!s8i>
@@ -180,8 +181,10 @@ void f3(union U3 u) {
 // CIR-NEXT:   %[[CAST:.*]] = cir.cast bitcast %[[COERCE]] : !cir.ptr<!cir.int<u, 40>> -> !cir.ptr<!rec_U3>
 // CIR-NEXT:   %[[U:.*]] = cir.alloca "u" align(1) init : !cir.ptr<!rec_U3>
 // CIR-NEXT:   cir.copy %[[CAST]] align(8) to %[[U]] align(1) : !cir.ptr<!rec_U3>
+// CIR-NEXT:   %{{.+}} = cir.const #cir.int<0> : !s32i
 // CIR-NEXT:   %[[ZERO:.*]] = cir.const #cir.int<0> : !s8i
 // CIR-NEXT:   %[[C_PTR:.*]] = cir.get_member %[[U]][0] {name = "c"} : !cir.ptr<!rec_U3> -> !cir.ptr<!cir.array<!s8i x 5>>
+// CIR-NEXT:   %{{.+}} = cir.const #cir.int<2> : !s32i
 // CIR-NEXT:   %[[IDX:.*]] = cir.const #cir.int<2> : !s64i
 // CIR-NEXT:   %[[ELEM_PTR:.*]] = cir.get_element inbounds %[[C_PTR]][%[[IDX]] : !s64i] : !cir.ptr<!cir.array<!s8i x 5>> -> !cir.ptr<!s8i>
 // CIR-NEXT:   cir.store{{.*}} %[[ZERO]], %[[ELEM_PTR]] : !s8i, !cir.ptr<!s8i>
@@ -214,8 +217,10 @@ void f5(union U4 u) {
 // CIR-NEXT:   %[[CAST:.*]] = cir.cast bitcast %[[COERCE]] : !cir.ptr<!u64i> -> !cir.ptr<!rec_U4>
 // CIR-NEXT:   %[[U:.*]] = cir.alloca "u" align(4) init : !cir.ptr<!rec_U4>
 // CIR-NEXT:   cir.copy %[[CAST]] align(8) to %[[U]] align(4) : !cir.ptr<!rec_U4>
+// CIR-NEXT:   %{{.+}} = cir.const #cir.int<65> : !s32i
 // CIR-NEXT:   %[[CHAR_VAL:.*]] = cir.const #cir.int<65> : !s8i
 // CIR-NEXT:   %[[C_PTR:.*]] = cir.get_member %[[U]][0] {name = "c"} : !cir.ptr<!rec_U4> -> !cir.ptr<!cir.array<!s8i x 5>>
+// CIR-NEXT:   %{{.+}} = cir.const #cir.int<4> : !s32i
 // CIR-NEXT:   %[[IDX:.*]] = cir.const #cir.int<4> : !s64i
 // CIR-NEXT:   %[[ELEM_PTR:.*]] = cir.get_element inbounds %[[C_PTR]][%[[IDX]] : !s64i] : !cir.ptr<!cir.array<!s8i x 5>> -> !cir.ptr<!s8i>
 // CIR-NEXT:   cir.store{{.*}} %[[CHAR_VAL]], %[[ELEM_PTR]] : !s8i, !cir.ptr<!s8i>
diff --git a/clang/test/CIR/CodeGen/vla.c b/clang/test/CIR/CodeGen/vla.c
index 81b4cc8827138d..03fec449cc4333 100644
--- a/clang/test/CIR/CodeGen/vla.c
+++ b/clang/test/CIR/CodeGen/vla.c
@@ -445,6 +445,8 @@ double vla_param_2d(int n, double m[n][n], int i, int j) {
 // CIR:   cir.store{{.*}} %[[M_ARG]], %[[M_ADDR]]
 // CIR:   cir.store{{.*}} %[[I_ARG]], %[[I_ADDR]]
 // CIR:   cir.store{{.*}} %[[J_ARG]], %[[J_ADDR]]
+// CIR:   %[[N_OUTER:.*]] = cir.load{{.*}} %[[N_ADDR]]
+// CIR:   %{{.+}} = cir.cast integral %[[N_OUTER]] : !s32i -> !u64i
 // CIR:   %[[N:.*]] = cir.load{{.*}} %[[N_ADDR]]
 // CIR:   %[[VLA_SIZE:.*]] = cir.cast integral %[[N]] : !s32i -> !u64i
 // CIR:   %[[M:.*]] = cir.load{{.*}} %[[M_ADDR]]
@@ -467,6 +469,8 @@ double vla_param_2d(int n, double m[n][n], int i, int j) {
 // LLVM:   store ptr %[[M_ARG]], ptr %[[M_ADDR]]
 // LLVM:   store i32 %[[I_ARG]], ptr %[[I_ADDR]]
 // LLVM:   store i32 %[[J_ARG]], ptr %[[J_ADDR]]
+// LLVM:   %[[N_OUTER:.*]] = load i32, ptr %[[N_ADDR]]
+// LLVM:   %{{.+}} = sext i32 %[[N_OUTER]] to i64
 // LLVM:   %[[N:.*]] = load i32, ptr %[[N_ADDR]]
 // LLVM:   %[[VLA_SIZE:.*]] = sext i32 %[[N]] to i64
 // LLVM:   %[[M:.*]] = load ptr, ptr %[[M_ADDR]]
@@ -512,12 +516,14 @@ void complex_vla_cast(int n) {
 // CIR:   %[[LEN_ADDR:.*]] = cir.alloca "n" {{.*}} init : !cir.ptr<!s32i>
 // CIR:   %[[SAVED_STACK:.*]] = cir.alloca "saved_stack" {{.*}} : !cir.ptr<!cir.ptr<!u8i>>
 // CIR:   cir.store %{{.*}}, %[[LEN_ADDR]] : !s32i, !cir.ptr<!s32i>
+// CIR:   %[[LEN_UNUSED:.*]] = cir.load {{.*}} %[[LEN_ADDR]] : !cir.ptr<!s32i>, !s32i
+// CIR:   %{{.+}} = cir.cast integral %[[LEN_UNUSED]] : !s32i -> !u64i
 // CIR:   %[[LEN:.*]] = cir.load {{.*}} %[[LEN_ADDR]] : !cir.ptr<!s32i>, !s32i
 // CIR:   %[[LEN_SIZE_T:.*]] = cir.cast integral %[[LEN]] : !s32i -> !u64i
 // CIR:   %[[STACK_PTR:.*]] = cir.stacksave : !cir.ptr<!u8i>
 // CIR:   cir.store {{.*}} %[[STACK_PTR]], %[[SAVED_STACK]] : !cir.ptr<!u8i>, !cir.ptr<!cir.ptr<!u8i>>
 // CIR:   cir.cleanup.scope {
-// CIR:     %[[ArR:.*]] = cir.alloca "arr" {{.*}} size(%3) : !cir.ptr<!cir.complex<!cir.float>>
+// CIR:     %[[ArR:.*]] = cir.alloca "arr" {{.*}} size(%[[LEN_SIZE_T]]) : !cir.ptr<!cir.complex<!cir.float>>
 // CIR:     cir.yield
 // CIR:   } cleanup normal {
 // CIR:     %[[STACK_RESTORE_PTR:.*]] = cir.load {{.*}} %[[SAVED_STACK]] : !cir.ptr<!cir.ptr<!u8i>>, !cir.ptr<!u8i>
@@ -529,6 +535,8 @@ void complex_vla_cast(int n) {
 // LLVM:   %[[LEN_ADDR:.*]] = alloca i32, align 4
 // LLVM:   %[[SAVED_STACK:.*]] = alloca ptr, align 8
 // LLVM:   store i32 %{{.*}}, ptr %[[LEN_ADDR]], align 4
+// LLVM:   %[[LEN_UNUSED:.*]] = load i32, ptr %[[LEN_ADDR]], align 4
+// LLVM:   %{{.+}} = sext i32 %[[LEN_UNUSED]] to i64
 // LLVM:   %[[LEN:.*]] = load i32, ptr %[[LEN_ADDR]], align 4
 // LLVM:   %[[LEN_SIZE_T:.*]] = sext i32 %[[LEN]] to i64
 // LLVM:   %[[STACK_PTR:.*]] = call ptr @llvm.stacksave.p0()
diff --git a/clang/test/CIR/CodeGen/void-value-expr.cpp b/clang/test/CIR/CodeGen/void-value-expr.cpp
index fadbb05464880c..a012def049ecac 100644
--- a/clang/test/CIR/CodeGen/void-value-expr.cpp
+++ b/clang/test/CIR/CodeGen/void-value-expr.cpp
@@ -58,6 +58,8 @@ void ternaryCheap(bool b) {
 // CIR: %[[ARG:.*]] = cir.alloca {{.*}} : !cir.ptr<!cir.bool>
 // CIR: cir.load{{.*}}%[[ARG]] : !cir.ptr<!cir.bool>, !cir.bool
 // CIR-NEXT: cir.load{{.*}}%[[ARG]] : !cir.ptr<!cir.bool>, !cir.bool
+// CIR-NEXT: cir.const #cir.int<0> : !s32i
+// CIR-NEXT: cir.const #cir.int<1> : !s32i
 // CIR-NEXT: cir.load{{.*}}%[[ARG]] : !cir.ptr<!cir.bool>, !cir.bool
 // CIR-NEXT: cir.return
 // LLVM-LABEL: define{{.*}} void @_Z12ternaryCheapb
diff --git a/clang/test/CIR/CodeGenCXX/new-array-init.cpp b/clang/test/CIR/CodeGenCXX/new-array-init.cpp
index 183c9aac2a3dbf..3aff6ec9ff246a 100644
--- a/clang/test/CIR/CodeGenCXX/new-array-init.cpp
+++ b/clang/test/CIR/CodeGenCXX/new-array-init.cpp
@@ -388,6 +388,7 @@ void string_nonconst_paren_extra_paren(int n) {
 // CIR-LABEL: cir.func {{.*}}@_Z12string_exactv()
 // LLVM-LABEL: define{{.*}} void @_Z12string_exactv
 void string_exact() {
+  // CIR: %{{.+}} = cir.const #cir.int<4> : !u64i
   // CIR: %[[SIZE:.*]] = cir.const #cir.int<4> : !u64i
   // CIR: %[[ALLOC:.*]] = cir.call @_Znam(%[[SIZE]]) {allocsize = array<i32: 0>, builtin, memory_effects = #cir.memory_effects<other = none, arg_mem = none, inaccessible_mem = readwrite, errno_mem = write, target_mem0 = none, target_mem1 = none>} : (!u64i {llvm.noundef}) -> (!cir.ptr<!void> {llvm.noalias, llvm.nonnull, llvm.noundef})
   // CIR: %[[ALLOC_CAST:.*]] = cir.cast bitcast %[[ALLOC]] : !cir.ptr<!void> -> !cir.ptr<!s8i>
@@ -404,6 +405,7 @@ void string_exact() {
 // CIR-LABEL: cir.func {{.*}}@_Z18string_exact_parenv()
 // LLVM-LABEL: define{{.*}} void @_Z18string_exact_parenv
 void string_exact_paren() {
+  // CIR: %{{.+}} = cir.const #cir.int<4> : !u64i
   // CIR: %[[SIZE:.*]] = cir.const #cir.int<4> : !u64i
   // CIR: %[[ALLOC:.*]] = cir.call @_Znam(%[[SIZE]]) {allocsize = array<i32: 0>, builtin, memory_effects = #cir.memory_effects<other = none, arg_mem = none, inaccessible_mem = readwrite, errno_mem = write, target_mem0 = none, target_mem1 = none>} : (!u64i {llvm.noundef}) -> (!cir.ptr<!void> {llvm.noalias, llvm.nonnull, llvm.noundef})
   // CIR: %[[ALLOC_CAST:.*]] = cir.cast bitcast %[[ALLOC]] : !cir.ptr<!void> -> !cir.ptr<!s8i>
@@ -420,6 +422,7 @@ void string_exact_paren() {
 // CIR-LABEL: cir.func {{.*}}@_Z28string_exact_paren_extensionv()
 // LLVM-LABEL: define{{.*}} void @_Z28string_exact_paren_extensionv
 void string_exact_paren_extension() {
+  // CIR: %{{.+}} = cir.const #cir.int<4> : !u64i
   // CIR: %[[SIZE:.*]] = cir.const #cir.int<4> : !u64i
   // CIR: %[[ALLOC:.*]] = cir.call @_Znam(%[[SIZE]]) {allocsize = array<i32: 0>, builtin, memory_effects = #cir.memory_effects<other = none, arg_mem = none, inaccessible_mem = readwrite, errno_mem = write, target_mem0 = none, target_mem1 = none>} : (!u64i {llvm.noundef}) -> (!cir.ptr<!void> {llvm.noalias, llvm.nonnull, llvm.noundef})
   // CIR: %[[ALLOC_CAST:.*]] = cir.cast bitcast %[[ALLOC]] : !cir.ptr<!void> -> !cir.ptr<!s8i>
@@ -436,6 +439,7 @@ void string_exact_paren_extension() {
 // CIR-LABEL: cir.func {{.*}}@_Z17string_sufficientv()
 // LLVM-LABEL: define{{.*}} void @_Z17string_sufficientv
 void string_sufficient() {
+  // CIR: %{{.+}} = cir.const #cir.int<15> : !u64i
   // CIR: %[[SIZE:.*]] = cir.const #cir.int<15> : !u64i
   // CIR: %[[ALLOC:.*]] = cir.call @_Znam(%[[SIZE]]) {allocsize = array<i32: 0>, builtin, memory_effects = #cir.memory_effects<other = none, arg_mem = none, inaccessible_mem = readwrite, errno_mem = write, target_mem0 = none, target_mem1 = none>} : (!u64i {llvm.noundef}) -> (!cir.ptr<!void> {llvm.noalias, llvm.nonnull, llvm.noundef})
   // CIR: %[[ALLOC_CAST:.*]] = cir.cast bitcast %[[ALLOC]] : !cir.ptr<!void> -> !cir.ptr<!s8i>
@@ -452,6 +456,7 @@ void string_sufficient() {
 // CIR-LABEL: cir.func {{.*}}@_Z23string_sufficient_parenv()
 // LLVM-LABEL: define{{.*}} void @_Z23string_sufficient_parenv
 void string_sufficient_paren() {
+  // CIR: %{{.+}} = cir.const #cir.int<15> : !u64i
   // CIR: %[[SIZE:.*]] = cir.const #cir.int<15> : !u64i
   // CIR: %[[ALLOC:.*]] = cir.call @_Znam(%[[SIZE]]) {allocsize = array<i32: 0>, builtin, memory_effects = #cir.memory_effects<other = none, arg_mem = none, inaccessible_mem = readwrite, errno_mem = write, target_mem0 = none, target_mem1 = none>} : (!u64i {llvm.noundef}) -> (!cir.ptr<!void> {llvm.noalias, llvm.nonnull, llvm.noundef})
   // CIR: %[[ALLOC_CAST:.*]] = cir.cast bitcast %[[ALLOC]] : !cir.ptr<!void> -> !cir.ptr<!s8i>
@@ -637,6 +642,7 @@ void unknown_bound() {
 // CIR-LABEL: cir.func {{.*}}@_Z20unknown_bound_stringv()
 // LLVM-LABEL: define{{.*}} void @_Z20unknown_bound_stringv
 void unknown_bound_string() {
+  // CIR: %{{.+}} = cir.const #cir.int<6> : !u64i
   // CIR: %[[SIZE:.*]] = cir.const #cir.int<6> : !u64i
   // CIR: %[[ALLOC:.*]] = cir.call @_Znam(%[[SIZE]]) {allocsize = array<i32: 0>, builtin, memory_effects = #cir.memory_effects<other = none, arg_mem = none, inaccessible_mem = readwrite, errno_mem = write, target_mem0 = none, target_mem1 = none>} : (!u64i {llvm.noundef}) -> (!cir.ptr<!void> {llvm.noalias, llvm.nonnull, llvm.noundef})
   // CIR: %[[ALLOC_CHAR:.*]] = cir.cast bitcast %[[ALLOC]] : !cir.ptr<!void> -> !cir.ptr<!s8i>
diff --git a/clang/test/CIR/CodeGenOpenACC/combined-copy.c b/clang/test/CIR/CodeGenOpenACC/combined-copy.c
index b910743f16e203..40ac1bdb6add89 100644
--- a/clang/test/CIR/CodeGenOpenACC/combined-copy.c
+++ b/clang/test/CIR/CodeGenOpenACC/combined-copy.c
@@ -1087,6 +1087,7 @@ void copy_member_of_array_element_member() {
   #pragma acc parallel loop copy(outer.inner[2].b)
   for(int i = 0; i < 5; ++i);
   // CHECK-NEXT: %[[GETINNER:.*]] = cir.get_member %[[OUTER]][0] {name = "inner"} : !cir.ptr<!rec_OuterTy> -> !cir.ptr<!cir.array<!rec_InnerTy x 4>>
+  // CHECK-NEXT: %{{.+}} = cir.const #cir.int<2> : !s32i
   // CHECK-NEXT: %[[TWO:.*]] = cir.const #cir.int<2> : !s64i
   // CHECK-NEXT: %[[GET_ELT:.*]] = cir.get_element inbounds %[[GETINNER]][%[[TWO]] : !s64i] : !cir.ptr<!cir.array<!rec_InnerTy x 4>> -> !cir.ptr<!rec_InnerTy>
   // CHECK-NEXT: %[[GETB:.*]] = cir.get_member %[[GET_ELT]][1] {name = "b"} : !cir.ptr<!rec_InnerTy> -> !cir.ptr<!s32i>
diff --git a/clang/test/CIR/CodeGenSYCL/global-address-space.cpp b/clang/test/CIR/CodeGenSYCL/global-address-space.cpp
index f2220474615f98..e7f4419609ace0 100644
--- a/clang/test/CIR/CodeGenSYCL/global-address-space.cpp
+++ b/clang/test/CIR/CodeGenSYCL/global-address-space.cpp
@@ -55,6 +55,7 @@ int i;
 // CIR:         cir.return
 
 // LLVM-LABEL: define {{.*}} ptr addrspace(2) @_Z15addr_explicit_cv()
+// LLVM-CIR:     %{{.+}} = addrspacecast ptr %{{.+}} to ptr addrspace(4)
 // LLVM-NOT:     addrspacecast
 // LLVM:         ret ptr addrspace(2)
 
diff --git a/clang/test/CIR/Transforms/canonicalize-dead-code.cir b/clang/test/CIR/Transforms/canonicalize-dead-code.cir
new file mode 100644
index 00000000000000..bba906a38ec71d
--- /dev/null
+++ b/clang/test/CIR/Transforms/canonicalize-dead-code.cir
@@ -0,0 +1,51 @@
+// RUN: cir-opt %s -cir-canonicalize=test-convergence -o - | FileCheck %s
+
+!s32i = !cir.int<s, 32>
+
+module {
+  // Operations that are dead before rewriting are kept, while folding still
+  // replaces the uses of a foldable operation. The constant keeps a use in the
+  // dead addition, so it stays.
+  // CHECK-LABEL: cir.func @dead_ops
+  cir.func @dead_ops(%arg0: !cir.ptr<!s32i>) -> !s32i {
+    // CHECK-NEXT: cir.alloca "unused"
+    %0 = cir.alloca "unused" align(4) : !cir.ptr<!s32i>
+    // CHECK-NEXT: %[[LOAD:.*]] = cir.load %arg0
+    %1 = cir.load %arg0 : !cir.ptr<!s32i>, !s32i
+    // CHECK-NEXT: %[[ONE:.*]] = cir.const #cir.int<1> : !s32i
+    %2 = cir.const #cir.int<1> : !s32i
+    // CHECK-NEXT: cir.add %[[LOAD]], %[[ONE]] : !s32i
+    %3 = cir.add %1, %2 : !s32i
+    // CHECK-NEXT: %[[NEG:.*]] = cir.const #cir.int<-1> : !s32i
+    %4 = cir.minus %2 : !s32i
+    // CHECK-NEXT: cir.return %[[NEG]] : !s32i
+    cir.return %4 : !s32i
+  }
+
+  // An operation that a fold leaves unused is erased.
+  // CHECK-LABEL: cir.func @fold_leftover
+  cir.func @fold_leftover() -> !s32i {
+    %0 = cir.const #cir.int<1> : !s32i
+    // CHECK-NEXT: %[[NEG:.*]] = cir.const #cir.int<-1> : !s32i
+    %1 = cir.minus %0 : !s32i
+    // CHECK-NEXT: cir.return %[[NEG]] : !s32i
+    cir.return %1 : !s32i
+  }
+
+  // A block that starts with a label is reached through cir.goto only, which
+  // is not a CFG edge, so it must not be erased as unreachable.
+  // CHECK-LABEL: cir.func @goto_target
+  cir.func @goto_target(%arg0: !cir.bool) {
+    cir.brcond %arg0 ^bb1, ^bb2
+  ^bb1:
+    // CHECK: cir.goto "L"
+    cir.goto "L"
+  ^bb2:
+    cir.return
+  ^bb3:
+    // CHECK: cir.label "L"
+    cir.label "L"
+    // CHECK-NEXT: cir.return
+    cir.return
+  }
+}



More information about the llvm-branch-commits mailing list