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

>From 50e077b6a37faaae1e5a16d2cf33440029148c30 Mon Sep 17 00:00:00 2001
From: Henrich Lauko <[email protected]>
Date: Tue, 10 Nov 2026 09:50:54 +0000
Subject: [PATCH] [CIR] Report side effects of volatile loads and memory asm
 operands

cir.load, cir.get_bitfield and cir.matrix.column_major_load declared only
a read of their address, so dead code elimination erased them when the
result was unused, even when the load was volatile or atomic. A cir.asm
with a memory operand such as "=*m" and no result was erased the same
way. A volatile or atomic load now also reports unknown read and write
effects, as the LLVM dialect does, and an inline asm reports a read and
a write of each memory operand. The new InlineAsmOp::isMemoryOperand
names the operand_attrs encoding of memory operands; the LLVM lowering
uses it too.

Assisted-by: Claude Code (claude-opus-5-5).
---
 clang/include/clang/CIR/Dialect/IR/CIROps.td  | 25 ++++++++---
 clang/lib/CIR/Dialect/IR/CIRDialect.cpp       | 41 ++++++++++++++++++
 .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp |  5 +--
 .../test/CIR/Transforms/side-effects-dce.cir  | 42 +++++++++++++++++++
 4 files changed, 104 insertions(+), 9 deletions(-)
 create mode 100644 clang/test/CIR/Transforms/side-effects-dce.cir

diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td 
b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index 82e3095e527b13..38279882fb9f91 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -867,7 +867,8 @@ def CIR_AllocaOp : CIR_Op<"alloca", [
 def CIR_LoadOp : CIR_Op<"load", [
   TypesMatchWith<"type of 'result' matches pointee type of 'addr'",
     "addr", "result", "mlir::cast<cir::PointerType>($_self).getPointee()">,
-  DeclareOpInterfaceMethods<PromotableMemOpInterface>
+  DeclareOpInterfaceMethods<PromotableMemOpInterface>,
+  DeclareOpInterfaceMethods<MemoryEffectsOpInterface>
 ]> {
   let summary = "Load value from memory adddress";
   let description = [{
@@ -905,8 +906,7 @@ def CIR_LoadOp : CIR_Op<"load", [
     ```
   }];
 
-  let arguments = (ins Arg<CIR_PointerType, "the address to load from",
-                           [MemRead]>:$addr,
+  let arguments = (ins Arg<CIR_PointerType, "the address to load from">:$addr,
                        UnitAttr:$isDeref,
                        UnitAttr:$is_volatile,
                        UnitAttr:$is_nontemporal,
@@ -4052,7 +4052,9 @@ def CIR_SetBitfieldOp : CIR_Op<"set_bitfield"> {
 // GetBitfieldOp
 
//===----------------------------------------------------------------------===//
 
-def CIR_GetBitfieldOp : CIR_Op<"get_bitfield"> {
+def CIR_GetBitfieldOp : CIR_Op<"get_bitfield", [
+  DeclareOpInterfaceMethods<MemoryEffectsOpInterface>
+]> {
   let summary = "Get the information for a bitfield member";
   let description = [{
     The `cir.get_bitfield` operation provides a load-like access to
@@ -4101,7 +4103,7 @@ def CIR_GetBitfieldOp : CIR_Op<"get_bitfield"> {
     }];
 
   let arguments = (ins
-    Arg<CIR_PointerType, "the address to load from", [MemRead]>:$addr,
+    Arg<CIR_PointerType, "the address to load from">:$addr,
     CIR_BitfieldInfoAttr:$bitfield_info,
     DefaultValuedOptionalAttr<I64Attr, "0">:$alignment,
     UnitAttr:$is_volatile
@@ -6121,6 +6123,16 @@ def CIR_InlineAsmOp : CIR_Op<"asm",
       "bool":$sideEffects, "bool":$convergent, "AsmFlavor":$asmFlavor,
       "llvm::ArrayRef<mlir::Attribute>":$operandAttrs)>];
 
+  let extraClassDeclaration = [{
+    /// Returns true if the asm operand at `index`, counted over all asm
+    /// operands, is a memory operand: the address of an lvalue that the asm
+    /// reads or writes, from a constraint such as "=*m". It is printed as
+    /// `maybe_memory`.
+    bool isMemoryOperand(unsigned index) {
+      return mlir::isa<mlir::UnitAttr>(getOperandAttrs()[index]);
+    }
+  }];
+
   let hasCustomAssemblyFormat = 1;
 }
 
@@ -7093,6 +7105,7 @@ def CIR_MatrixInsertOp : CIR_Op<"matrix.insert", [
 
 def CIR_MatrixColumnMajorLoadOp : CIR_Op<"matrix.column_major_load", [
   DeclareOpInterfaceMethods<PromotableMemOpInterface>,
+  DeclareOpInterfaceMethods<MemoryEffectsOpInterface>
 ]> {
   let summary = "Matrix column major load";
   let description = [{
@@ -7120,7 +7133,7 @@ def CIR_MatrixColumnMajorLoadOp : 
CIR_Op<"matrix.column_major_load", [
   }];
 
   let arguments = (ins
-    Arg<CIR_PointerType, "the address to load from", [MemRead]>:$value,
+    Arg<CIR_PointerType, "the address to load from">:$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 10a61434a4a30d..e4759b5e49e049 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -4808,6 +4808,39 @@ LogicalResult cir::LdexpOp::verify() {
   return success();
 }
 
+//===----------------------------------------------------------------------===//
+// LoadOp, GetBitfieldOp and MatrixColumnMajorLoadOp
+//===----------------------------------------------------------------------===//
+
+/// 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.
+static void
+getLoadEffects(OpOperand &addr, bool isVolatileOrAtomic,
+               SmallVectorImpl<MemoryEffects::EffectInstance> &effects) {
+  effects.emplace_back(MemoryEffects::Read::get(), &addr);
+  if (isVolatileOrAtomic) {
+    effects.emplace_back(MemoryEffects::Write::get());
+    effects.emplace_back(MemoryEffects::Read::get());
+  }
+}
+
+void cir::LoadOp::getEffects(
+    SmallVectorImpl<MemoryEffects::EffectInstance> &effects) {
+  getLoadEffects(getAddrMutable(), getIsVolatile() || 
getMemOrder().has_value(),
+                 effects);
+}
+
+void cir::GetBitfieldOp::getEffects(
+    SmallVectorImpl<MemoryEffects::EffectInstance> &effects) {
+  getLoadEffects(getAddrMutable(), getIsVolatile(), effects);
+}
+
+void cir::MatrixColumnMajorLoadOp::getEffects(
+    SmallVectorImpl<MemoryEffects::EffectInstance> &effects) {
+  getLoadEffects(getValueMutable(), getIsVolatile(), effects);
+}
+
 
//===----------------------------------------------------------------------===//
 // MatrixTransposeOp
 
//===----------------------------------------------------------------------===//
@@ -5303,6 +5336,14 @@ void InlineAsmOp::getEffects(
     effects.emplace_back(mlir::MemoryEffects::Read::get());
     effects.emplace_back(mlir::MemoryEffects::Write::get());
   }
+  // A memory operand (such as "=*m") passes the address of an lvalue that
+  // the asm may read and write.
+  for (OpOperand &operand : getOperation()->getOpOperands()) {
+    if (!isMemoryOperand(operand.getOperandNumber()))
+      continue;
+    effects.emplace_back(mlir::MemoryEffects::Read::get(), &operand);
+    effects.emplace_back(mlir::MemoryEffects::Write::get(), &operand);
+  }
 }
 
 
//===----------------------------------------------------------------------===//
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp 
b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 461b72839ec702..3e3dce363b4265 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -6594,9 +6594,8 @@ mlir::LogicalResult 
CIRToLLVMInlineAsmOpLowering::matchAndRewrite(
 
   // so far we infer the llvm dialect element type attr from
   // CIR operand type.
-  for (auto const &[cirOpAttr, cirOp] :
-       zip(op.getOperandAttrs(), cirOperands)) {
-    if (!mlir::isa<mlir::UnitAttr>(cirOpAttr)) {
+  for (auto [index, cirOp] : llvm::enumerate(cirOperands)) {
+    if (!op.isMemoryOperand(index)) {
       opAttrs.push_back(mlir::Attribute());
       continue;
     }
diff --git a/clang/test/CIR/Transforms/side-effects-dce.cir 
b/clang/test/CIR/Transforms/side-effects-dce.cir
new file mode 100644
index 00000000000000..d2aff2ba88b76a
--- /dev/null
+++ b/clang/test/CIR/Transforms/side-effects-dce.cir
@@ -0,0 +1,42 @@
+// RUN: cir-opt %s -canonicalize -o - | FileCheck %s
+
+!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>
+
+module {
+  // Volatile and atomic loads are kept by dead code elimination even when
+  // their result is unused; plain loads are erased.
+  // CHECK-LABEL: cir.func @loads
+  cir.func @loads(%arg0: !cir.ptr<!s32i>, %arg1: !cir.ptr<!u8i>) {
+    // CHECK-NEXT: cir.load volatile %arg0
+    %0 = cir.load volatile %arg0 : !cir.ptr<!s32i>, !s32i
+    // CHECK-NEXT: cir.load syncscope(system) atomic(seq_cst) %arg0
+    %1 = cir.load syncscope(system) atomic(seq_cst) %arg0 : !cir.ptr<!s32i>, 
!s32i
+    // CHECK-NEXT: cir.load syncscope(system) atomic(relaxed) %arg0
+    %2 = cir.load syncscope(system) atomic(relaxed) %arg0 : !cir.ptr<!s32i>, 
!s32i
+    // Erased.
+    %3 = cir.load %arg0 : !cir.ptr<!s32i>, !s32i
+    // CHECK-NEXT: cir.get_bitfield align(4) (#bfi_f, %arg1 {is_volatile}
+    %4 = cir.get_bitfield align(4) (#bfi_f, %arg1 {is_volatile} : 
!cir.ptr<!u8i>) -> !s32i
+    // Erased.
+    %5 = cir.get_bitfield align(4) (#bfi_f, %arg1 : !cir.ptr<!u8i>) -> !s32i
+    // CHECK-NEXT: cir.return
+    cir.return
+  }
+
+  // An inline asm with a memory output writes through its operand, so it is
+  // kept even though it has no result.
+  // CHECK-LABEL: cir.func @asm_memory_output
+  cir.func @asm_memory_output(%arg0: !cir.ptr<!s32i>) {
+    // CHECK-NEXT: cir.asm(x86_att,
+    // CHECK-NEXT:   out = [%arg0 : !cir.ptr<!s32i> (maybe_memory)],
+    cir.asm(x86_att,
+      out = [%arg0 : !cir.ptr<!s32i> (maybe_memory)],
+      in = [],
+      in_out = [],
+      {"nop" "=*m,~{dirflag},~{fpsr},~{flags}"})
+    cir.return
+  }
+}

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to