[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
Sat Nov 14 13:51:29 PST 2026


https://github.com/xlauko created https://github.com/llvm/llvm-project/pull/242709

`cir.store`, `cir.set_bitfield` and `cir.matrix.column_major_store` declared only a write of their address, so alias analysis treated a volatile or atomic store as touching nothing else. Like `llvm.store`, a volatile or atomic store now also reads and writes unknown memory. The load effect helper now takes the access effect, so loads and stores share it. The new mod/ref test checks that these stores are ModRef for an unrelated location while a plain store is not.

Assisted-by: Claude Code (claude-opus-5-5).


>From a840bb0568cb2acd4d376b53fd7919293b5a87cb 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] Report unknown effects of volatile and atomic stores

cir.store, cir.set_bitfield and cir.matrix.column_major_store declared
only a write of their address, so alias analysis treated a volatile or
atomic store as touching nothing else. Like llvm.store, a volatile or
atomic store now also reads and writes unknown memory. The load effect
helper is generalized to take the access effect and serves both loads
and stores.

Assisted-by: Claude Code (claude-opus-5-5).
---
 clang/include/clang/CIR/Dialect/IR/CIROps.td  | 16 ++++---
 clang/lib/CIR/Dialect/IR/CIRDialect.cpp       | 44 ++++++++++++++-----
 .../CIR/Analysis/alias-analysis-modref.cir    | 30 +++++++++++++
 3 files changed, 73 insertions(+), 17 deletions(-)

diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index 4edba881035cdb..0c331dd5bfeb60 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -997,7 +997,8 @@ def CIR_VecMaskedLoadOp : CIR_Op<"vec.masked_load", [
 def CIR_StoreOp : CIR_Op<"store", [
   TypesMatchWith<"type of 'value' matches pointee type of 'addr'",
     "addr", "value", "mlir::cast<cir::PointerType>($_self).getPointee()">,
-  DeclareOpInterfaceMethods<PromotableMemOpInterface>
+  DeclareOpInterfaceMethods<PromotableMemOpInterface>,
+  DeclareOpInterfaceMethods<MemoryEffectsOpInterface>
 ]> {
   let summary = "Store value to memory address";
   let description = [{
@@ -1024,8 +1025,8 @@ def CIR_StoreOp : CIR_Op<"store", [
   }];
 
   let arguments = (ins CIR_AnyType:$value,
-                       Arg<CIR_PointerType, "the address to store the value",
-                           [MemWrite]>:$addr,
+                       Arg<CIR_PointerType,
+                           "the address to store the value">:$addr,
                        UnitAttr:$is_volatile,
                        UnitAttr:$is_nontemporal,
                        OptionalAttr<I64Attr>:$alignment,
@@ -3958,7 +3959,9 @@ def CIR_VTTAddrPointOp : CIR_Op<"vtt.address_point", [
 // SetBitfieldOp
 //===----------------------------------------------------------------------===//
 
-def CIR_SetBitfieldOp : CIR_Op<"set_bitfield"> {
+def CIR_SetBitfieldOp : CIR_Op<"set_bitfield", [
+  DeclareOpInterfaceMethods<MemoryEffectsOpInterface>
+]> {
   let summary = "Set the value of a bitfield member";
   let description = [{
     The `cir.set_bitfield` operation provides a store-like access to
@@ -4011,7 +4014,7 @@ def CIR_SetBitfieldOp : CIR_Op<"set_bitfield"> {
    }];
 
   let arguments = (ins
-    Arg<CIR_PointerType, "the address to store the value", [MemWrite]>:$addr,
+    Arg<CIR_PointerType, "the address to store the value">:$addr,
     CIR_AnyType:$src,
     CIR_BitfieldInfoAttr:$bitfield_info,
     DefaultValuedOptionalAttr<I64Attr, "0">:$alignment,
@@ -7206,6 +7209,7 @@ def CIR_MatrixMultiplyOp : CIR_Op<"matrix.multiply", [
 
 def CIR_MatrixColumnMajorStoreOp : CIR_Op<"matrix.column_major_store", [
   DeclareOpInterfaceMethods<PromotableMemOpInterface>,
+  DeclareOpInterfaceMethods<MemoryEffectsOpInterface>
 ]> {
   let summary = "Matrix column major store";
   let description = [{
@@ -7228,7 +7232,7 @@ def CIR_MatrixColumnMajorStoreOp : CIR_Op<"matrix.column_major_store", [
 
   let arguments = (ins
     CIR_MatrixType:$matrix,
-    Arg<CIR_PointerType, "the address to store the value", [MemWrite]>:$value,
+    Arg<CIR_PointerType, "the address to store the value">:$value,
     CIR_IntType:$stride,
     UnitAttr:$is_volatile
   );
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index cb15beefaaae6d..abafe9d560548c 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -4809,16 +4809,18 @@ LogicalResult cir::LdexpOp::verify() {
 }
 
 //===----------------------------------------------------------------------===//
-// LoadOp, GetBitfieldOp and MatrixColumnMajorLoadOp
+// Memory access effects of loads and stores
 //===----------------------------------------------------------------------===//
 
-/// Adds the effects of a load from `addr`. A volatile or atomic load also has
-/// unknown read and write effects, as in the LLVM dialect, so that it is never
-/// erased when its result is unused.
+/// Adds the effects of a load or store through `addr`, where `access` is a
+/// read or a write. A volatile or atomic access also reads and writes unknown
+/// memory, as in the LLVM dialect, so that it is never erased or reordered
+/// past other memory accesses.
 static void
-getLoadEffects(OpOperand &addr, bool isVolatileOrAtomic,
-               SmallVectorImpl<MemoryEffects::EffectInstance> &effects) {
-  effects.emplace_back(MemoryEffects::Read::get(), &addr);
+getAccessEffects(MemoryEffects::Effect *access, OpOperand &addr,
+                 bool isVolatileOrAtomic,
+                 SmallVectorImpl<MemoryEffects::EffectInstance> &effects) {
+  effects.emplace_back(access, &addr);
   if (isVolatileOrAtomic) {
     effects.emplace_back(MemoryEffects::Write::get());
     effects.emplace_back(MemoryEffects::Read::get());
@@ -4827,18 +4829,38 @@ getLoadEffects(OpOperand &addr, bool isVolatileOrAtomic,
 
 void cir::LoadOp::getEffects(
     SmallVectorImpl<MemoryEffects::EffectInstance> &effects) {
-  getLoadEffects(getAddrMutable(), getIsVolatile() || getMemOrder().has_value(),
-                 effects);
+  getAccessEffects(MemoryEffects::Read::get(), getAddrMutable(),
+                   getIsVolatile() || getMemOrder().has_value(), effects);
+}
+
+void cir::StoreOp::getEffects(
+    SmallVectorImpl<MemoryEffects::EffectInstance> &effects) {
+  getAccessEffects(MemoryEffects::Write::get(), getAddrMutable(),
+                   getIsVolatile() || getMemOrder().has_value(), effects);
 }
 
 void cir::GetBitfieldOp::getEffects(
     SmallVectorImpl<MemoryEffects::EffectInstance> &effects) {
-  getLoadEffects(getAddrMutable(), getIsVolatile(), effects);
+  getAccessEffects(MemoryEffects::Read::get(), getAddrMutable(),
+                   getIsVolatile(), effects);
+}
+
+void cir::SetBitfieldOp::getEffects(
+    SmallVectorImpl<MemoryEffects::EffectInstance> &effects) {
+  getAccessEffects(MemoryEffects::Write::get(), getAddrMutable(),
+                   getIsVolatile(), effects);
 }
 
 void cir::MatrixColumnMajorLoadOp::getEffects(
     SmallVectorImpl<MemoryEffects::EffectInstance> &effects) {
-  getLoadEffects(getValueMutable(), getIsVolatile(), effects);
+  getAccessEffects(MemoryEffects::Read::get(), getValueMutable(),
+                   getIsVolatile(), effects);
+}
+
+void cir::MatrixColumnMajorStoreOp::getEffects(
+    SmallVectorImpl<MemoryEffects::EffectInstance> &effects) {
+  getAccessEffects(MemoryEffects::Write::get(), getValueMutable(),
+                   getIsVolatile(), effects);
 }
 
 //===----------------------------------------------------------------------===//
diff --git a/clang/test/CIR/Analysis/alias-analysis-modref.cir b/clang/test/CIR/Analysis/alias-analysis-modref.cir
index 781d3bd3baa60f..3ae4fab1f86553 100644
--- a/clang/test/CIR/Analysis/alias-analysis-modref.cir
+++ b/clang/test/CIR/Analysis/alias-analysis-modref.cir
@@ -54,3 +54,33 @@ cir.func @modref_fenv(%a: !cir.float, %b: !cir.float) {
   %0 = cir.fadd %a, %b : !cir.float fenv<> {test.ptr = "fadd_op"}
   cir.return
 }
+
+// -----
+
+// A volatile or atomic store also reads and writes unknown memory, so it
+// Mod-s and Ref-s every location. A plain store to `x` leaves `y` alone.
+
+// CHECK-LABEL: Testing : "modref_volatile_atomic_store"
+// CHECK-DAG: plain_store -> y#0: NoModRef
+// CHECK-DAG: volatile_store -> y#0: ModRef
+// CHECK-DAG: atomic_store -> y#0: ModRef
+// CHECK-DAG: volatile_set_bitfield -> y#0: ModRef
+// CHECK-DAG: volatile_matrix_store -> y#0: ModRef
+
+!s32i = !cir.int<s, 32>
+!u8i = !cir.int<u, 8>
+#bfi_f = #cir.bitfield_info<name = "f", storage_type = !u8i, size = 3, offset = 0, is_signed = true>
+cir.func @modref_volatile_atomic_store(%m: !cir.matrix<2 x 2 x !cir.float>,
+                                       %p: !cir.ptr<!cir.float>,
+                                       %stride: !cir.int<u, 64>) {
+  %x = cir.alloca "x" align(4) : !cir.ptr<!s32i>
+  %y = cir.alloca "y" align(4) : !cir.ptr<!s32i> {test.ptr = "y"}
+  %b = cir.alloca "b" align(1) : !cir.ptr<!u8i>
+  %val = cir.const #cir.int<1> : !s32i
+  cir.store %val, %x {test.ptr = "plain_store"} : !s32i, !cir.ptr<!s32i>
+  cir.store volatile %val, %x {test.ptr = "volatile_store"} : !s32i, !cir.ptr<!s32i>
+  cir.store syncscope(system) atomic(seq_cst) %val, %x {test.ptr = "atomic_store"} : !s32i, !cir.ptr<!s32i>
+  %0 = cir.set_bitfield align(1) (#bfi_f, %b : !cir.ptr<!u8i>, %val : !s32i) {is_volatile} {test.ptr = "volatile_set_bitfield"} -> !s32i
+  cir.matrix.column_major_store %m : <2 x 2 x !cir.float>, %p : <!cir.float>, %stride : !cir.int<u, 64> volatile {test.ptr = "volatile_matrix_store"}
+  cir.return
+}



More information about the llvm-branch-commits mailing list