https://github.com/xlauko updated https://github.com/llvm/llvm-project/pull/242709
>From d3146ceaea8d70ca8c8d995ecd125363e2e27910 Mon Sep 17 00:00:00 2001 From: Henrich Lauko <[email protected]> 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. A new isVolatileOrAtomic() on cir.load and cir.store replaces the hand-written check in the effects, mem2reg, CIRSimplify and the ABI rewriter. Assisted-by: Claude Code (claude-opus-5-5). --- clang/include/clang/CIR/Dialect/IR/CIROps.td | 29 +++++++++--- clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 44 ++++++++++++++----- clang/lib/CIR/Dialect/IR/CIRMemorySlot.cpp | 6 +-- .../CIR/Dialect/Transforms/CIRSimplify.cpp | 5 +-- .../TargetLowering/CIRABIRewriteContext.cpp | 2 +- .../CIR/Analysis/alias-analysis-modref.cir | 36 +++++++++++++++ 6 files changed, 97 insertions(+), 25 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index 93f4b8a90ca4fa..9cd1aa486361b6 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. } @@ -997,7 +1008,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 +1036,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, @@ -1052,6 +1064,8 @@ def CIR_StoreOp : CIR_Op<"store", [ }]> ]; + let extraClassDeclaration = CIR_VolatileOrAtomicAccessDecl; + // FIXME: add verifier. } @@ -3958,7 +3972,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 +4027,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, @@ -7216,6 +7232,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 = [{ @@ -7238,7 +7255,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 6c976e64c96bc0..6b68e4b8f6d4f8 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 dead code elimination keeps it and alias +/// analysis treats it as touching any memory. 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(), + isVolatileOrAtomic(), effects); +} + +void cir::StoreOp::getEffects( + SmallVectorImpl<MemoryEffects::EffectInstance> &effects) { + getAccessEffects(MemoryEffects::Write::get(), getAddrMutable(), + isVolatileOrAtomic(), 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/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; } diff --git a/clang/test/CIR/Analysis/alias-analysis-modref.cir b/clang/test/CIR/Analysis/alias-analysis-modref.cir index 781d3bd3baa60f..819aea25543183 100644 --- a/clang/test/CIR/Analysis/alias-analysis-modref.cir +++ b/clang/test/CIR/Analysis/alias-analysis-modref.cir @@ -54,3 +54,39 @@ 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 load or store also reads and writes unknown memory, +// so it Mod-s and Ref-s every location. A plain access to `x` leaves `y` +// alone. + +// CHECK-LABEL: Testing : "modref_volatile_atomic_access" +!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_access(%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 + // CHECK-DAG: plain_store -> y#0: NoModRef + cir.store %val, %x {test.ptr = "plain_store"} : !s32i, !cir.ptr<!s32i> + // CHECK-DAG: volatile_store -> y#0: ModRef + cir.store volatile %val, %x {test.ptr = "volatile_store"} : !s32i, !cir.ptr<!s32i> + // CHECK-DAG: atomic_store -> y#0: ModRef + cir.store syncscope(system) atomic(seq_cst) %val, %x {test.ptr = "atomic_store"} : !s32i, !cir.ptr<!s32i> + // CHECK-DAG: plain_load -> y#0: NoModRef + %0 = cir.load %x : !cir.ptr<!s32i>, !s32i {test.ptr = "plain_load"} + // CHECK-DAG: volatile_load -> y#0: ModRef + %1 = cir.load volatile %x : !cir.ptr<!s32i>, !s32i {test.ptr = "volatile_load"} + // CHECK-DAG: atomic_load -> y#0: ModRef + %3 = cir.load syncscope(system) atomic(seq_cst) %x : !cir.ptr<!s32i>, !s32i {test.ptr = "atomic_load"} + // CHECK-DAG: volatile_set_bitfield -> y#0: ModRef + %2 = cir.set_bitfield align(1) (#bfi_f, %b : !cir.ptr<!u8i>, %val : !s32i) {is_volatile} {test.ptr = "volatile_set_bitfield"} -> !s32i + // CHECK-DAG: volatile_matrix_store -> y#0: ModRef + 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 +} _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
