[llvm-branch-commits] [clang] [CIR] Report unknown effects of volatile and atomic stores (PR #242709)
Henrich Lauko via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sun Nov 15 00:49:36 PST 2026
https://github.com/xlauko updated https://github.com/llvm/llvm-project/pull/242709
>From f425850f0714ea12060bb309215df9ed86c7a69b Mon Sep 17 00:00:00 2001
From: Henrich Lauko <hlauko at nvidia.com>
Date: Sat, 14 Nov 2026 21:51:07 +0000
Subject: [PATCH] [CIR][NFC] Add isVolatileOrAtomic() to cir.load and cir.store
Several places spelled out getIsVolatile() || getMemOrder() to decide
whether a load or store must stay as written. Name that check once on
both ops and use it in the load effects, mem2reg, CIRSimplify and the
ABI rewriter.
Assisted-by: Claude Code (claude-opus-5-5).
---
clang/include/clang/CIR/Dialect/IR/CIROps.td | 13 +++++++++++++
clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 3 +--
clang/lib/CIR/Dialect/IR/CIRMemorySlot.cpp | 6 ++----
clang/lib/CIR/Dialect/Transforms/CIRSimplify.cpp | 5 ++---
.../TargetLowering/CIRABIRewriteContext.cpp | 2 +-
5 files changed, 19 insertions(+), 10 deletions(-)
diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index 93f4b8a90ca4fa..289b7ed0d38b02 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -229,6 +229,15 @@ defvar CIR_FenvOpTraits = [
// dynamically via FenvOpTrait, so they must not inherit NoMemoryEffect.
defvar CIR_DynamicMemoryEffects = 0;
+// Declares isVolatileOrAtomic() on cir.load and cir.store.
+defvar CIR_VolatileOrAtomicAccessDecl = [{
+ /// Returns true if the access is volatile or atomic. Such an access must
+ /// stay as written: it cannot be removed, merged or reordered.
+ bool isVolatileOrAtomic() {
+ return getIsVolatile() || getMemOrder().has_value();
+ }
+}];
+
//===----------------------------------------------------------------------===//
// CastOp
//===----------------------------------------------------------------------===//
@@ -942,6 +951,8 @@ def CIR_LoadOp : CIR_Op<"load", [
}]>
];
+ let extraClassDeclaration = CIR_VolatileOrAtomicAccessDecl;
+
// FIXME: add verifier.
}
@@ -1052,6 +1063,8 @@ def CIR_StoreOp : CIR_Op<"store", [
}]>
];
+ let extraClassDeclaration = CIR_VolatileOrAtomicAccessDecl;
+
// FIXME: add verifier.
}
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index 6c976e64c96bc0..e0107573819b1f 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -4827,8 +4827,7 @@ getLoadEffects(OpOperand &addr, bool isVolatileOrAtomic,
void cir::LoadOp::getEffects(
SmallVectorImpl<MemoryEffects::EffectInstance> &effects) {
- getLoadEffects(getAddrMutable(), getIsVolatile() || getMemOrder().has_value(),
- effects);
+ getLoadEffects(getAddrMutable(), isVolatileOrAtomic(), effects);
}
void cir::GetBitfieldOp::getEffects(
diff --git a/clang/lib/CIR/Dialect/IR/CIRMemorySlot.cpp b/clang/lib/CIR/Dialect/IR/CIRMemorySlot.cpp
index 089303c0cbf2e4..8bcfb4db6f513b 100644
--- a/clang/lib/CIR/Dialect/IR/CIRMemorySlot.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRMemorySlot.cpp
@@ -73,8 +73,7 @@ bool cir::LoadOp::canUsesBeRemoved(
if (blockingUses.size() != 1)
return false;
- // Volatile load or atomic load should not be removed.
- if (getIsVolatile() || getMemOrder().has_value())
+ if (isVolatileOrAtomic())
return false;
Value blockingUse = (*blockingUses.begin())->get();
@@ -112,8 +111,7 @@ bool cir::StoreOp::canUsesBeRemoved(
if (blockingUses.size() != 1)
return false;
- // Volatile store or atomic store should not be removed.
- if (getIsVolatile() || getMemOrder().has_value())
+ if (isVolatileOrAtomic())
return false;
Value blockingUse = (*blockingUses.begin())->get();
diff --git a/clang/lib/CIR/Dialect/Transforms/CIRSimplify.cpp b/clang/lib/CIR/Dialect/Transforms/CIRSimplify.cpp
index 6c8902692993f9..12d7f2ac795dd7 100644
--- a/clang/lib/CIR/Dialect/Transforms/CIRSimplify.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/CIRSimplify.cpp
@@ -108,8 +108,7 @@ struct SimplifyConstantLoad final : public OpRewritePattern<LoadOp> {
LogicalResult matchAndRewrite(LoadOp op,
PatternRewriter &rewriter) const override {
- // Volatile or atomic loads should not be simplified.
- if (op.getIsVolatile() || op.getMemOrder())
+ if (op.isVolatileOrAtomic())
return mlir::failure();
auto allocaOp = op.getAddr().getDefiningOp<cir::AllocaOp>();
@@ -120,7 +119,7 @@ struct SimplifyConstantLoad final : public OpRewritePattern<LoadOp> {
findDominatingInitOp(allocaOp, op, domInfo.getDominanceInfo());
if (!initStoreOp)
return mlir::failure();
- if (initStoreOp.getIsVolatile() || initStoreOp.getMemOrder()) {
+ if (initStoreOp.isVolatileOrAtomic()) {
// We intentionally act conservatively here and we don't want to simplify
// the load if the corresponding store is either volatile or atomic.
return mlir::failure();
diff --git a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp
index ed36a6180dd673..3d5237519e6e65 100644
--- a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp
@@ -657,7 +657,7 @@ mlir::Block *coercionSlotBlock(mlir::Operation *op) {
/// load at all.
static cir::LoadOp maybeGetSimpleLoad(mlir::Value val) {
cir::LoadOp load = val.getDefiningOp<cir::LoadOp>();
- if (!load || load.getIsVolatile() || load.getMemOrder())
+ if (!load || load.isVolatileOrAtomic())
return {};
return load;
}
More information about the llvm-branch-commits
mailing list