[clang] [CIR] Report side effects of volatile loads and memory asm operands (PR #242691)

Henrich Lauko via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 14 14:11:33 PST 2026


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

>From 7183f544a834fa604f84e3b4a3d7ad3b337818ea Mon Sep 17 00:00:00 2001
From: Henrich Lauko <hlauko at nvidia.com>
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. A cir.asm verifier now checks that operand_attrs has one
entry per operand, each a unit attribute on a pointer operand or an
empty dictionary.

Assisted-by: Claude Code (claude-opus-5-5).
---
 clang/include/clang/CIR/Dialect/IR/CIROps.td  | 45 ++++++++---
 clang/lib/CIR/Dialect/IR/CIRDialect.cpp       | 75 +++++++++++++++----
 .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 17 +----
 clang/test/CIR/IR/invalid-inline-asm.cir      | 36 +++++++++
 .../test/CIR/Transforms/side-effects-dce.cir  | 48 ++++++++++++
 5 files changed, 185 insertions(+), 36 deletions(-)
 create mode 100644 clang/test/CIR/IR/invalid-inline-asm.cir
 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..93f4b8a90ca4fa 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
@@ -6047,6 +6049,15 @@ def CIR_AsmFlavor : CIR_I32Enum<"AsmFlavor", "ATT or Intel",
 
 def CIR_AsmFlavorAttr : CIR_EnumAttr<CIR_AsmFlavor, "asm_flavor">;
 
+def CIR_EmptyDictAttr : Attr<CPred<
+  "::llvm::isa<::mlir::DictionaryAttr>($_self) && "
+  "::llvm::cast<::mlir::DictionaryAttr>($_self).empty()">,
+  "empty dictionary">;
+
+def CIR_AsmOperandAttrArray : CIR_TypedArrayAttrBase<
+  AnyAttrOf<[UnitAttr, CIR_EmptyDictAttr]>,
+  "array of unit attributes or empty dictionaries">;
+
 def CIR_InlineAsmOp : CIR_Op<"asm",
     [DeclareOpInterfaceMethods<MemoryEffectsOpInterface>]> {
   let description = [{
@@ -6060,9 +6071,9 @@ def CIR_InlineAsmOp : CIR_Op<"asm",
     - the output variable index referenced by the input operands.
     - the index of early-clobber operand
 
-    Operand attributes are a storage, where each element corresponds to the
-    operand with the same index. The first index relates to the operation
-    result (if any).
+    `operand_attrs` has one entry per operand, in operand order: a unit
+    attribute marks a memory operand (printed as `maybe_memory`) and an empty
+    dictionary marks any other operand.
     The operands themselves are stored as VariadicOfVariadic in the following
     order: output, input and then in/out operands. When several output operands
     are present, the result type may be represented as an anonymous record type.
@@ -6112,7 +6123,8 @@ def CIR_InlineAsmOp : CIR_Op<"asm",
   let arguments =
       (ins VariadicOfVariadic<AnyType, "operands_segments">:$asm_operands,
           StrAttr:$asm_string, StrAttr:$constraints, UnitAttr:$side_effects,
-          UnitAttr:$convergent, CIR_AsmFlavorAttr:$asm_flavor, ArrayAttr:$operand_attrs,
+          UnitAttr:$convergent, CIR_AsmFlavorAttr:$asm_flavor,
+          CIR_AsmOperandAttrArray:$operand_attrs,
           DenseI32ArrayAttr:$operands_segments);
 
   let builders = [OpBuilder<(ins
@@ -6121,7 +6133,19 @@ 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) {
+      assert(index < getOperandAttrs().size() && "operand index out of range");
+      return mlir::isa<mlir::UnitAttr>(getOperandAttrs()[index]);
+    }
+  }];
+
   let hasCustomAssemblyFormat = 1;
+  let hasVerifier = 1;
 }
 
 //===----------------------------------------------------------------------===//
@@ -7093,6 +7117,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 +7145,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..6c976e64c96bc0 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
 //===----------------------------------------------------------------------===//
@@ -5096,24 +5129,20 @@ void cir::InlineAsmOp::print(OpAsmPrinter &p) {
   p.printNewline();
 
   llvm::SmallVector<std::string, 3> names{"out", "in", "in_out"};
-  auto *nameIt = names.begin();
-  auto *attrIt = getOperandAttrs().begin();
+  mlir::MutableOperandRangeRange groups = getAsmOperandsMutable();
 
-  for (mlir::OperandRange ops : getAsmOperands()) {
-    p << *nameIt << " = ";
+  for (auto [index, name] : llvm::enumerate(names)) {
+    p << name << " = ";
 
     p << '[';
-    llvm::interleaveComma(llvm::make_range(ops.begin(), ops.end()), p,
-                          [&](Value value) {
-                            p.printOperand(value);
-                            p << " : " << value.getType();
-                            if (mlir::isa<mlir::UnitAttr>(*attrIt))
-                              p << " (maybe_memory)";
-                            attrIt++;
-                          });
+    llvm::interleaveComma(groups[index], p, [&](OpOperand &operand) {
+      p.printOperand(operand.get());
+      p << " : " << operand.get().getType();
+      if (isMemoryOperand(operand.getOperandNumber()))
+        p << " (maybe_memory)";
+    });
     p << "],";
     p.printNewline();
-    ++nameIt;
   }
 
   p << "{";
@@ -5295,6 +5324,19 @@ ParseResult cir::InlineAsmOp::parse(OpAsmParser &parser,
   return mlir::success();
 }
 
+LogicalResult cir::InlineAsmOp::verify() {
+  if (getOperandAttrs().size() != getNumOperands())
+    return emitOpError() << "expects " << getNumOperands()
+                         << " operand attributes, but got "
+                         << getOperandAttrs().size();
+  for (auto [index, operand] : llvm::enumerate(getOperands()))
+    if (isMemoryOperand(index) && !isa<cir::PointerType>(operand.getType()))
+      return emitOpError() << "memory operand #" << index
+                           << " must be a pointer, but got "
+                           << operand.getType();
+  return success();
+}
+
 void InlineAsmOp::getEffects(
     llvm::SmallVectorImpl<mlir::MemoryEffects::EffectInstance> &effects) {
   // If we have any side effects (that is, we're volatile asm), add a read and
@@ -5303,6 +5345,13 @@ void InlineAsmOp::getEffects(
     effects.emplace_back(mlir::MemoryEffects::Read::get());
     effects.emplace_back(mlir::MemoryEffects::Write::get());
   }
+  // Memory operands are read and written through.
+  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..5a2453d583e37c 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -6584,19 +6584,10 @@ mlir::LogicalResult CIRToLLVMInlineAsmOpLowering::matchAndRewrite(
   if (!op.getNumResults())
     opAttrs.push_back(mlir::Attribute());
 
-  SmallVector<mlir::Value> llvmOperands;
-  SmallVector<mlir::Value> cirOperands;
-  for (auto const &[llvmOp, cirOp] :
-       zip(adaptor.getAsmOperands(), op.getAsmOperands())) {
-    append_range(llvmOperands, llvmOp);
-    append_range(cirOperands, cirOp);
-  }
-
   // 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(op->getOperands())) {
+    if (!op.isMemoryOperand(index)) {
       opAttrs.push_back(mlir::Attribute());
       continue;
     }
@@ -6615,8 +6606,8 @@ mlir::LogicalResult CIRToLLVMInlineAsmOpLowering::matchAndRewrite(
   }
 
   rewriter.replaceOpWithNewOp<mlir::LLVM::InlineAsmOp>(
-      op, llResTy, llvmOperands, op.getAsmStringAttr(), op.getConstraintsAttr(),
-      op.getSideEffectsAttr(),
+      op, llResTy, adaptor.getOperands(), op.getAsmStringAttr(),
+      op.getConstraintsAttr(), op.getSideEffectsAttr(),
       /*is_align_stack*/ mlir::UnitAttr(),
       /*tail_call_kind*/
       mlir::LLVM::TailCallKindAttr::get(
diff --git a/clang/test/CIR/IR/invalid-inline-asm.cir b/clang/test/CIR/IR/invalid-inline-asm.cir
new file mode 100644
index 00000000000000..b9a6a96b3fa9de
--- /dev/null
+++ b/clang/test/CIR/IR/invalid-inline-asm.cir
@@ -0,0 +1,36 @@
+// RUN: cir-opt %s -verify-diagnostics -split-input-file
+
+!s32i = !cir.int<s, 32>
+module {
+  cir.func @memory_operand_not_pointer(%arg0: !s32i) {
+    // expected-error at +1 {{'cir.asm' op memory operand #0 must be a pointer, but got '!cir.int<s, 32>'}}
+    cir.asm(x86_att,
+      out = [%arg0 : !s32i (maybe_memory)],
+      in = [],
+      in_out = [],
+      {"nop" "=*m,~{dirflag},~{fpsr},~{flags}"})
+    cir.return
+  }
+}
+
+// -----
+
+!s32i = !cir.int<s, 32>
+module {
+  cir.func @missing_operand_attr(%arg0: !cir.ptr<!s32i>) {
+    // expected-error at +1 {{'cir.asm' op expects 1 operand attributes, but got 0}}
+    "cir.asm"(%arg0) <{asm_flavor = #cir.asm_flavor<x86_att>, asm_string = "nop", constraints = "=*m", operand_attrs = [], operands_segments = array<i32: 1, 0, 0>}> : (!cir.ptr<!s32i>) -> ()
+    cir.return
+  }
+}
+
+// -----
+
+!s32i = !cir.int<s, 32>
+module {
+  cir.func @bad_operand_attr(%arg0: !cir.ptr<!s32i>) {
+    // expected-error at +1 {{'cir.asm' op attribute 'operand_attrs' failed to satisfy constraint: array of unit attributes or empty dictionaries}}
+    "cir.asm"(%arg0) <{asm_flavor = #cir.asm_flavor<x86_att>, asm_string = "nop", constraints = "=*m", operand_attrs = [1 : i32], operands_segments = array<i32: 1, 0, 0>}> : (!cir.ptr<!s32i>) -> ()
+    cir.return
+  }
+}
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..c27199c50a5b3a
--- /dev/null
+++ b/clang/test/CIR/Transforms/side-effects-dce.cir
@@ -0,0 +1,48 @@
+// RUN: cir-opt %s -canonicalize -o - | FileCheck %s
+
+!s32i = !cir.int<s, 32>
+!u8i = !cir.int<u, 8>
+!u64i = !cir.int<u, 64>
+
+#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>,
+                  %arg2: !cir.ptr<!cir.float>, %arg3: !u64i) {
+    // 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.matrix.column_major_load %arg2 {{.*}} volatile
+    %6 = cir.matrix.column_major_load %arg2 : <!cir.float>, %arg3 : !u64i volatile, !cir.matrix<2 x 2 x !cir.float>
+    // Erased.
+    %7 = cir.matrix.column_major_load %arg2 : <!cir.float>, %arg3 : !u64i, !cir.matrix<2 x 2 x !cir.float>
+    // 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
+  }
+}



More information about the cfe-commits mailing list