[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
Wed Nov 18 01:18:36 PST 2026


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

>From 23cf9bbe79ea0ed3db8dba88f5218aa380535f46 Mon Sep 17 00:00:00 2001
From: Henrich Lauko <hlauko at nvidia.com>
Date: Tue, 17 Nov 2026 19:13:26 +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 MLIR's greedy driver top-down, so the
patterns and folders of every CIR operation apply. Like MLIR's
canonicalizer, it rewrites each region of the operation it runs on
separately, so it can also run nested on an operation with no region or
several, such as an OpenACC recipe.

CIR stays close to the source, so operations that are unused in the
input stay, as in classic codegen. The pass leaves trivially dead
operations off the worklist, and ExistingAndNewOps strictness keeps the
driver away from them unless a rewrite creates or moves them. This also
keeps unused values that CIRGen emits, such as constants and casts that
the old list erased, so several CodeGen tests now check them. Empty
scopes, cleanup scopes and switches hold no code, so they are seeded and
erased where control reaches them, and so is an operation that a rewrite
leaves unused by erasing its last user, such as a constant that only a
folded operation used.

The label tests used unused constants to show which code the driver
skips. Those constants now stay either way, so the tests use empty
scopes, which the driver erases once it visits them.

The new test-convergence option fails the pass when a second run of the
patterns changes the IR.

Assisted-by: Claude Code (claude-opus-5-5).
---
 clang/include/clang/CIR/Dialect/Passes.td     |  12 +-
 .../Dialect/Transforms/CIRCanonicalize.cpp    | 168 ++++++++++--------
 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 | 103 +++++++++++
 .../CIR/Transforms/canonicalize-label.cir     |  59 +++---
 .../CIR/Transforms/canonicalize-nested.cir    |  42 +++++
 clang/test/CIR/Transforms/canonicalize.cir    |  31 +++-
 21 files changed, 368 insertions(+), 114 deletions(-)
 create mode 100644 clang/test/CIR/Transforms/canonicalize-dead-code.cir
 create mode 100644 clang/test/CIR/Transforms/canonicalize-nested.cir

diff --git a/clang/include/clang/CIR/Dialect/Passes.td b/clang/include/clang/CIR/Dialect/Passes.td
index 9a4bf9e576e4b1..44ee808924207b 100644
--- a/clang/include/clang/CIR/Dialect/Passes.td
+++ b/clang/include/clang/CIR/Dialect/Passes.td
@@ -14,7 +14,11 @@ include "mlir/Pass/PassBase.td"
 def CIRCanonicalize : Pass<"cir-canonicalize"> {
   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
+    that control reaches. An operation that is unused in the input, such as an
+    unused local, stays unless a rewrite moves it. Empty scopes like
+    `cir.scope {}` are erased, and so is an operation that a rewrite leaves
+    unused by erasing its last user.
 
     This pass performs basic cleanup and canonicalization transformations that
     are not intended to affect CIR-to-source fidelity and high-level code
@@ -25,6 +29,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/Transforms/CIRCanonicalize.cpp b/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp
index d480f5d99b04a8..4ca77269b172aa 100644
--- a/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp
@@ -6,106 +6,132 @@
 //
 //===----------------------------------------------------------------------===//
 //
-// This file implements pass that canonicalizes CIR operations, eliminating
-// redundant branches, empty scopes, and other unnecessary operations.
+// This file implements the CIR canonicalization pass, which applies the
+// canonicalization patterns and folders of the CIR dialect.
 //
 //===----------------------------------------------------------------------===//
 
 #include "PassDetail.h"
 #include "mlir/Config/mlir-config.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/Interfaces/SideEffectInterfaces.h"
+#include "mlir/Rewrite/FrozenRewritePatternSet.h"
 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
 #include "clang/CIR/Dialect/Analysis/GotoAnalysis.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
 #include "clang/CIR/Dialect/Passes.h.inc"
 } // namespace mlir
 
-namespace {
+/// Returns true if `op` is a scope, cleanup scope or switch that holds no code:
+/// each of its regions is a single block that ends in `cir.yield` and holds
+/// only such operations, like `cir.scope {}`. An empty `cir.case` does not
+/// count, since it still decides where its switch goes.
+static bool holdsNoCode(Operation *op) {
+  if (!isa<cir::ScopeOp, cir::CleanupScopeOp, cir::SwitchOp>(op))
+    return false;
+  return llvm::all_of(op->getRegions(), [](Region &region) {
+    if (!region.hasOneBlock())
+      return false;
+    Block &block = region.front();
+    return isa<cir::YieldOp>(block.getTerminator()) &&
+           llvm::all_of(block.without_terminator(),
+                        [](Operation &nested) { return holdsNoCode(&nested); });
+  });
+}
 
-//===----------------------------------------------------------------------===//
-// CIRCanonicalizePass
-//===----------------------------------------------------------------------===//
+namespace {
 
 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 {
+    bool changed = applyPatterns();
+    // A test can check that one run reaches a fixpoint on its input. One run
+    // need not in general: the driver does not revisit the parent of an
+    // erased operation, which may then fold or hold no code.
+    if (testConvergence && applyPatterns())
+      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 each region of the pass's operation and returns
+  /// whether the IR changed.
+  bool applyPatterns() {
+    Operation *root = getOperation();
+    // Until cir-flatten-cfg and cir-goto-solver turn cir.goto and cir.switch
+    // into branches, the code they enter has no CFG predecessor, so
+    // GotoAnalysis tells the driver what control reaches. As the driver's
+    // listener, it stays up to date while the patterns rewrite the IR.
+    cir::GotoAnalysis reachability(root);
+    GreedyRewriteConfig config;
+    config.setStrictness(GreedyRewriteStrictness::ExistingAndNewOps)
+        .setListener(&reachability)
+        .setReachability([&](Operation *op) {
+          bool reachable = reachability.isReachable(op);
+#if MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS
+          // The analysis may over-approximate after a jump target moves, but
+          // it must reach whatever a fresh analysis reaches.
+          assert((reachable || !cir::GotoAnalysis(root).isReachable(op)) &&
+                 "cached reachability misses an operation that control "
+                 "reaches");
+#endif // MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS
+          return reachable;
+        });
 
-  // Collect operations to apply patterns.
-  llvm::SmallVector<Operation *, 16> ops;
-  getOperation()->walk([&](Operation *op) {
-    assert(!cir::MissingFeatures::tryOp());
-    assert(!cir::MissingFeatures::callOp());
+    // As MLIR's canonicalizer does, rewrite each region on its own, with the
+    // region as the driver's scope.
+    bool changed = false;
+    for (Region &region : root->getRegions()) {
+      // The driver pops from the back of the worklist, so seeding it in
+      // reverse pre-order visits the region top-down.
+      //
+      // An operation that is trivially dead in the input stays, as in classic
+      // codegen: it is not seeded, and ExistingAndNewOps keeps the driver away
+      // from it unless a rewrite creates or moves it. An operation that holds
+      // no code has nothing to keep, so it is seeded and erased. The walk
+      // checks nested operations before their parent, which lets the cache
+      // reuse what it learned about them.
+      SmallVector<Operation *> ops;
+      DeadnessCache deadness;
+      auto seed = [&](Operation *op) {
+        if (!deadness.isOpTriviallyDead(op) || holdsNoCode(op))
+          ops.push_back(op);
+      };
+      for (Block &block : llvm::reverse(region))
+        for (Operation &op : llvm::reverse(block))
+          op.walk<WalkOrder::PostOrder, ReverseIterator>(seed);
 
-    // 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);
-  });
+      config.setScope(&region);
+      // Without a rewrite limit, the driver empties its worklist and so
+      // cannot fail.
+      bool regionChanged = false;
+      (void)applyOpPatternsGreedily(ops, *patterns, config, &regionChanged);
+      changed |= regionChanged;
+    }
+    return changed;
+  }
 
-  // Apply patterns. Scope the rewrite to the body region, which encloses
-  // the common ancestor the driver would otherwise search for. Until
-  // cir-flatten-cfg and cir-goto-solver turn cir.goto and cir.switch into
-  // branches, the code they enter has no CFG predecessor, so GotoAnalysis
-  // tells the driver what control reaches. As the driver's listener, it stays
-  // up to date while the patterns rewrite the IR.
-  cir::GotoAnalysis reachability(getOperation());
-  GreedyRewriteConfig config;
-  config.setScope(&getOperation()->getRegion(0))
-      .setListener(&reachability)
-      .setReachability([&](Operation *op) {
-        bool reachable = reachability.isReachable(op);
-#if MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS
-        // The analysis may over-approximate after a jump target moves, but it
-        // must reach whatever a fresh analysis reaches.
-        assert(
-            (reachable || !cir::GotoAnalysis(getOperation()).isReachable(op)) &&
-            "cached reachability misses an operation that control reaches");
-#endif // MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS
-        return reachable;
-      });
-  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..9e2117197eac9e
--- /dev/null
+++ b/clang/test/CIR/Transforms/canonicalize-dead-code.cir
@@ -0,0 +1,103 @@
+// RUN: cir-opt %s -cir-canonicalize=test-convergence -o - | FileCheck %s
+
+!s32i = !cir.int<s, 32>
+
+module {
+  // Operations that are unused 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 load that only a folded operation used is erased too.
+  // CHECK-LABEL: cir.func @load_left_dead
+  cir.func @load_left_dead(%arg0: !cir.ptr<!s32i>) -> !s32i {
+    // CHECK-NEXT: %[[SEVEN:.*]] = cir.const #cir.int<7> : !s32i
+    %0 = cir.const #cir.int<7> : !s32i
+    %1 = cir.const #cir.bool<true> : !cir.bool
+    %2 = cir.load %arg0 : !cir.ptr<!s32i>, !s32i
+    %3 = cir.select if %1 then %0 else %2 : (!cir.bool, !s32i, !s32i) -> !s32i
+    // CHECK-NEXT: cir.return %[[SEVEN]] : !s32i
+    cir.return %3 : !s32i
+  }
+
+  // A fold replaces the operands of the unused addition, which still stays.
+  // The driver does not visit it although the fold modifies it.
+  // CHECK-LABEL: cir.func @unused_user_of_fold
+  // CHECK-SAME:    (%[[ARG0:.*]]: !s32i)
+  cir.func @unused_user_of_fold(%arg0: !s32i) -> !s32i {
+    %0 = cir.cast integral %arg0 : !s32i -> !s32i
+    // CHECK-NEXT: cir.add %[[ARG0]], %[[ARG0]] : !s32i
+    %1 = cir.add %0, %0 : !s32i
+    // CHECK-NEXT: cir.return %[[ARG0]] : !s32i
+    cir.return %0 : !s32i
+  }
+
+  // A fold in an unused scope does not make the driver visit the scope.
+  // CHECK-LABEL: cir.func @fold_in_unused_scope
+  // CHECK-SAME:    (%[[ARG0:.*]]: !s32i)
+  cir.func @fold_in_unused_scope(%arg0: !s32i) {
+    // CHECK-NEXT: cir.scope {
+    // CHECK-NEXT:   cir.add %[[ARG0]], %[[ARG0]] : !s32i
+    // CHECK-NEXT: }
+    cir.scope {
+      %0 = cir.cast integral %arg0 : !s32i -> !s32i
+      %1 = cir.add %0, %0 : !s32i
+    }
+    // CHECK-NEXT: cir.return
+    cir.return
+  }
+
+  // The driver does not visit an operation that is unused before rewriting
+  // unless a rewrite moves it, so the unused cir.minus does not fold.
+  // CHECK-LABEL: cir.func @unused_fold
+  cir.func @unused_fold() {
+    // CHECK-NEXT: %[[ONE:.*]] = cir.const #cir.int<1> : !s32i
+    %0 = cir.const #cir.int<1> : !s32i
+    // CHECK-NEXT: cir.minus %[[ONE]] : !s32i
+    %1 = cir.minus %0 : !s32i
+    // CHECK-NEXT: cir.return
+    cir.return
+  }
+
+  // A scope that holds no code is erased, together with the empty scopes in
+  // it. A scope that holds an unused local stays.
+  // CHECK-LABEL: cir.func @empty_scopes
+  cir.func @empty_scopes() {
+    cir.scope {
+      cir.scope {
+      }
+    }
+    // CHECK-NEXT: cir.scope {
+    // CHECK-NEXT:   cir.alloca "unused"
+    // CHECK-NEXT: }
+    cir.scope {
+      %0 = cir.alloca "unused" align(4) : !cir.ptr<!s32i>
+    }
+    // CHECK-NEXT: cir.return
+    cir.return
+  }
+}
diff --git a/clang/test/CIR/Transforms/canonicalize-label.cir b/clang/test/CIR/Transforms/canonicalize-label.cir
index c183f5df97f74a..6d4f11da551cdc 100644
--- a/clang/test/CIR/Transforms/canonicalize-label.cir
+++ b/clang/test/CIR/Transforms/canonicalize-label.cir
@@ -50,12 +50,13 @@ module {
 
   // A goto to a label inside a scope enters the outer block at that scope.
   // The canonicalizer simplifies the scope and the operations after it, but
-  // not the constant before it, which no jump reaches. It keeps the label at
-  // the start of its block.
+  // not the empty scope before it, which no jump reaches. It keeps the label
+  // at the start of its block.
   cir.func @label_in_scope(%arg0: !s32i) -> !s32i {
     cir.goto "in"
   ^bb1:
-    %0 = cir.const #cir.int<7> : !s32i
+    cir.scope {
+    }
     cir.scope {
       cir.br ^bb2
     ^bb2:
@@ -71,7 +72,8 @@ module {
   // CHECK:      cir.func{{.*}} @label_in_scope(%[[ARG0:.*]]: !s32i) -> !s32i {
   // CHECK-NEXT:   cir.goto "in"
   // CHECK-NEXT: ^bb1:
-  // CHECK-NEXT:   cir.const #cir.int<7> : !s32i
+  // CHECK-NEXT:   cir.scope {
+  // CHECK-NEXT:   }
   // CHECK-NEXT:   cir.scope {
   // CHECK-NEXT:     cir.br ^[[LABEL:bb[0-9]+]]
   // CHECK-NEXT:   ^[[LABEL]]:
@@ -82,15 +84,17 @@ module {
   // CHECK-NEXT: }
 
   // Every block on the way to a nested label is entered at the operation that
-  // holds the label. The constants before those operations stay.
+  // holds the label. The empty scopes before those operations stay.
   cir.func @nested_holders() {
     cir.goto "in"
   ^bb1:
-    %0 = cir.const #cir.int<1> : !s32i
+    cir.scope {
+    }
     cir.scope {
       cir.goto "in"
     ^bb2:
-      %1 = cir.const #cir.int<2> : !s32i
+      cir.scope {
+      }
       cir.scope {
         cir.br ^bb3
       ^bb3:
@@ -110,11 +114,13 @@ module {
   // CHECK:      cir.func{{.*}} @nested_holders() {
   // CHECK-NEXT:   cir.goto "in"
   // CHECK-NEXT: ^bb1:
-  // CHECK-NEXT:   cir.const #cir.int<1> : !s32i
+  // CHECK-NEXT:   cir.scope {
+  // CHECK-NEXT:   }
   // CHECK-NEXT:   cir.scope {
   // CHECK-NEXT:     cir.goto "in"
   // CHECK-NEXT:   ^bb{{[0-9]+}}:
-  // CHECK-NEXT:     cir.const #cir.int<2> : !s32i
+  // CHECK-NEXT:     cir.scope {
+  // CHECK-NEXT:     }
   // CHECK-NEXT:     cir.scope {
   // CHECK-NEXT:       cir.br ^[[LABEL:bb[0-9]+]]
   // CHECK-NEXT:     ^[[LABEL]]:
@@ -131,7 +137,8 @@ module {
   cir.func @two_holders() {
     cir.goto "a"
   ^bb1:
-    %0 = cir.const #cir.int<1> : !s32i
+    cir.scope {
+    }
     cir.scope {
       cir.br ^bb2
     ^bb2:
@@ -151,7 +158,8 @@ module {
   // CHECK:      cir.func{{.*}} @two_holders() {
   // CHECK-NEXT:   cir.goto "a"
   // CHECK-NEXT: ^bb1:
-  // CHECK-NEXT:   cir.const #cir.int<1> : !s32i
+  // CHECK-NEXT:   cir.scope {
+  // CHECK-NEXT:   }
   // CHECK-NEXT:   cir.scope {
   // CHECK-NEXT:     cir.br ^bb1
   // CHECK-NEXT:   ^bb1:
@@ -171,7 +179,8 @@ module {
   cir.func @holder_successors(%arg0: !cir.bool) {
     cir.goto "in"
   ^bb1:
-    %0 = cir.const #cir.int<1> : !s32i
+    cir.scope {
+    }
     cir.scope {
       cir.br ^bb2
     ^bb2:
@@ -189,7 +198,8 @@ module {
   // CHECK:      cir.func{{.*}} @holder_successors(%[[ARG0:.*]]: !cir.bool) {
   // CHECK-NEXT:   cir.goto "in"
   // CHECK-NEXT: ^bb1:
-  // CHECK-NEXT:   cir.const #cir.int<1> : !s32i
+  // CHECK-NEXT:   cir.scope {
+  // CHECK-NEXT:   }
   // CHECK-NEXT:   cir.scope {
   // CHECK-NEXT:     cir.br ^bb1
   // CHECK-NEXT:   ^bb1:
@@ -323,15 +333,16 @@ module {
   // CHECK-NEXT: }
 
   // The block that holds a cir.case after a cir.break has no CFG predecessor
-  // either, but the cir.switch jumps to the case. The constant before the case
-  // stays.
+  // either, but the cir.switch jumps to the case. The empty scope before the
+  // case stays.
   cir.func @case_after_break(%arg0: !s32i) {
     cir.switch(%arg0 : !s32i) {
       cir.case(equal, [#cir.int<1> : !s32i]) {
         cir.scope {
           cir.break
         ^bb1:
-          %0 = cir.const #cir.int<1> : !s32i
+          cir.scope {
+          }
           cir.case(equal, [#cir.int<2> : !s32i]) {
             cir.scope {
             }
@@ -353,7 +364,8 @@ module {
   // CHECK-NEXT:       cir.scope {
   // CHECK-NEXT:         cir.break
   // CHECK-NEXT:       ^bb1:
-  // CHECK-NEXT:         cir.const #cir.int<1> : !s32i
+  // CHECK-NEXT:         cir.scope {
+  // CHECK-NEXT:         }
   // CHECK-NEXT:         cir.case(equal, [#cir.int<2> : !s32i]) {
   // CHECK-NEXT:           cir.yield
   // CHECK-NEXT:         }
@@ -373,7 +385,8 @@ module {
       cir.case(equal, [#cir.int<1> : !s32i]) {
         cir.break
       ^bb1:
-        %0 = cir.const #cir.int<1> : !s32i
+        cir.scope {
+        }
         cir.scope {
           cir.case(equal, [#cir.int<2> : !s32i]) {
             cir.scope {
@@ -393,7 +406,8 @@ module {
   // CHECK-NEXT:     cir.case(equal, [#cir.int<1> : !s32i]) {
   // CHECK-NEXT:       cir.break
   // CHECK-NEXT:     ^bb1:
-  // CHECK-NEXT:       cir.const #cir.int<1> : !s32i
+  // CHECK-NEXT:       cir.scope {
+  // CHECK-NEXT:       }
   // CHECK-NEXT:       cir.scope {
   // CHECK-NEXT:         cir.case(equal, [#cir.int<2> : !s32i]) {
   // CHECK-NEXT:           cir.yield
@@ -496,13 +510,14 @@ module {
   // CHECK-NEXT: }
 
   // The verifier checks no dominance in a block that only a jump enters, so
-  // it can hold a cycle of casts. Folding the cast chain must stop. The unused
-  // constant goes away, which shows that the block is visited.
+  // it can hold a cycle of casts. Folding the cast chain must stop. The empty
+  // scope goes away, which shows that the block is visited.
   cir.func @cast_cycle_after_label() {
     cir.goto "out"
   ^bb1:
     cir.label "out"
-    %c = cir.const #cir.int<0> : !s32i
+    cir.scope {
+    }
     %0 = cir.cast int_to_bool %1 : !s32i -> !cir.bool
     %1 = cir.cast bool_to_int %0 : !cir.bool -> !s32i
     cir.return
diff --git a/clang/test/CIR/Transforms/canonicalize-nested.cir b/clang/test/CIR/Transforms/canonicalize-nested.cir
new file mode 100644
index 00000000000000..55dc328a2a397d
--- /dev/null
+++ b/clang/test/CIR/Transforms/canonicalize-nested.cir
@@ -0,0 +1,42 @@
+// RUN: cir-opt %s -cir-canonicalize -o - | FileCheck %s
+// RUN: cir-opt %s --pass-pipeline='builtin.module(any(cir-canonicalize))' -o - | FileCheck %s
+
+!s32i = !cir.int<s, 32>
+
+// Nested under the module, the pass rewrites each region of the operation it
+// runs on, so it gives the same result as on the module.
+module {
+  // CHECK-LABEL: cir.func @routine
+  // CHECK-NEXT:    cir.return
+  cir.func @routine() attributes {acc.routine_info = #acc.routine_info<[@r]>} {
+    cir.scope {
+    }
+    cir.return
+  }
+
+  // An operation without regions has nothing to rewrite.
+  // CHECK: acc.routine @r func(@routine) seq
+  acc.routine @r func(@routine) seq
+
+  // CHECK-LABEL: acc.private.recipe @priv
+  // CHECK-SAME:    init {
+  // CHECK-NEXT:  ^bb0(%{{.*}}: !cir.ptr<!s32i>):
+  // CHECK-NEXT:    %[[ALLOCA:.*]] = cir.alloca
+  // CHECK-NEXT:    acc.yield %[[ALLOCA]]
+  // CHECK-NEXT:  } destroy {
+  // CHECK-NEXT:  ^bb0(%{{.*}}: !cir.ptr<!s32i>, %{{.*}}: !cir.ptr<!s32i>):
+  // CHECK-NEXT:    acc.yield
+  // CHECK-NEXT:  }
+  acc.private.recipe @priv : !cir.ptr<!s32i> init {
+  ^bb0(%arg0: !cir.ptr<!s32i>):
+    cir.scope {
+    }
+    %0 = cir.alloca "p" align(4) : !cir.ptr<!s32i>
+    acc.yield %0 : !cir.ptr<!s32i>
+  } destroy {
+  ^bb0(%arg0: !cir.ptr<!s32i>, %arg1: !cir.ptr<!s32i>):
+    cir.scope {
+    }
+    acc.yield
+  }
+}
diff --git a/clang/test/CIR/Transforms/canonicalize.cir b/clang/test/CIR/Transforms/canonicalize.cir
index 5743e9aaac6eb8..8e75aac038c78e 100644
--- a/clang/test/CIR/Transforms/canonicalize.cir
+++ b/clang/test/CIR/Transforms/canonicalize.cir
@@ -371,10 +371,9 @@ module {
   // CHECK-NEXT:   cir.return %[[P]] : !s32i
   // CHECK-NEXT: }
 
-  // The driver visits a user before the operations it uses. The outer cir.not
-  // folds to the constant, and the inner one then dies unused. Visited the
-  // other way round, each would fold to a new constant, and the store would
-  // keep the original one too.
+  // The driver visits the operations top-down. The inner cir.not folds to a
+  // new constant first and the outer one then folds to another, while the
+  // store keeps the original constant.
   cir.func @not_not_order(%arg0: !cir.ptr<!cir.bool>) -> !cir.bool {
     %0 = cir.const #true
     %1 = cir.not %0 : !cir.bool
@@ -385,7 +384,8 @@ module {
   // CHECK:      cir.func{{.*}} @not_not_order(%[[ARG0:.*]]: !cir.ptr<!cir.bool>) -> !cir.bool
   // CHECK-NEXT:   %[[TRUE:.*]] = cir.const #true
   // CHECK-NEXT:   cir.store %[[TRUE]], %[[ARG0]] : !cir.bool, !cir.ptr<!cir.bool>
-  // CHECK-NEXT:   cir.return %[[TRUE]] : !cir.bool
+  // CHECK-NEXT:   %[[FOLDED:.*]] = cir.const #true
+  // CHECK-NEXT:   cir.return %[[FOLDED]] : !cir.bool
   // CHECK-NEXT: }
 
   // Folding the cast in the scope revisits the scope, which then folds away.
@@ -412,15 +412,30 @@ module {
   // CHECK-NEXT:   cir.return
   // CHECK-NEXT: }
 
-  // Erasing the unused cast revisits the load that defines its operand, which
-  // is then dead too.
+  // The cast is unused before rewriting, so it stays, and so does the load
+  // that defines its operand.
   cir.func @dead_operand_definer(%arg0: !cir.ptr<!s32i>) {
     %0 = cir.load %arg0 : !cir.ptr<!s32i>, !s32i
     %1 = cir.cast int_to_bool %0 : !s32i -> !cir.bool
     cir.return
   }
-  // CHECK:      cir.func{{.*}} @dead_operand_definer(%{{.*}}: !cir.ptr<!s32i>)
+  // CHECK:      cir.func{{.*}} @dead_operand_definer(%[[ARG0:.*]]: !cir.ptr<!s32i>)
+  // CHECK-NEXT:   %[[LOAD:.*]] = cir.load %[[ARG0]] : !cir.ptr<!s32i>, !s32i
+  // CHECK-NEXT:   cir.cast int_to_bool %[[LOAD]] : !s32i -> !cir.bool
   // CHECK-NEXT:   cir.return
   // CHECK-NEXT: }
 
+  // The pass applies the folds of every operation, such as the commutative
+  // trait of cir.add.overflow, which moves the constant to the right.
+  cir.func @add_overflow_const_lhs(%arg0: !s32i) -> !cir.bool {
+    %0 = cir.const #cir.int<1> : !s32i
+    %1, %2 = cir.add.overflow %0, %arg0 : !s32i -> !s32i
+    cir.return %2 : !cir.bool
+  }
+  // CHECK:      cir.func{{.*}} @add_overflow_const_lhs(%[[ARG0:.*]]: !s32i) -> !cir.bool
+  // CHECK-NEXT:   %[[ONE:.*]] = cir.const #cir.int<1> : !s32i
+  // CHECK-NEXT:   %{{.*}}, %[[OVF:.*]] = cir.add.overflow %[[ARG0]], %[[ONE]] : !s32i -> !s32i
+  // CHECK-NEXT:   cir.return %[[OVF]] : !cir.bool
+  // CHECK-NEXT: }
+
 }



More information about the llvm-branch-commits mailing list