https://github.com/xlauko created https://github.com/llvm/llvm-project/pull/242691
`cir.load`, `cir.get_bitfield` and `cir.matrix.column_major_load` declared only a read of their address, so any dead code elimination (for example the core canonicalizer) erased them when the result was unused, even when the load was volatile or atomic, and 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's `llvm.load` does, and an inline asm reports a read and a write of each memory operand. The new test runs `-canonicalize` and checks that these ops survive while a plain unused load is still removed. Assisted-by: Claude Code (claude-opus-5-5). >From 4a575485dddd60830361d69160281c6d7ddb379c 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. Assisted-by: Claude Code (claude-opus-5-5). --- clang/include/clang/CIR/Dialect/IR/CIROps.td | 15 +++--- clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 46 +++++++++++++++++++ .../test/CIR/Transforms/side-effects-dce.cir | 42 +++++++++++++++++ 3 files changed, 97 insertions(+), 6 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 93c91a48bd0c7b..6391b3b2b64116 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -850,7 +850,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 = [{ @@ -888,8 +889,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, @@ -4004,7 +4004,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 @@ -4053,7 +4055,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 @@ -7002,6 +7004,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 = [{ @@ -7029,7 +7032,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 35f390afffe8ba..c34b0cae5f1d65 100644 --- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp @@ -4655,6 +4655,43 @@ OpFoldResult cir::VecTernaryOp::fold(FoldAdaptor adaptor) { vecTy, mlir::ArrayAttr::get(getContext(), elements)); } +//===----------------------------------------------------------------------===// +// 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( + mlir::OpOperand &addr, bool isVolatileOrAtomic, + SmallVectorImpl<SideEffects::EffectInstance<MemoryEffects::Effect>> + &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<SideEffects::EffectInstance<MemoryEffects::Effect>> + &effects) { + getLoadEffects(getAddrMutable(), getIsVolatile() || getMemOrder().has_value(), + effects); +} + +void cir::GetBitfieldOp::getEffects( + SmallVectorImpl<SideEffects::EffectInstance<MemoryEffects::Effect>> + &effects) { + getLoadEffects(getAddrMutable(), getIsVolatile(), effects); +} + +void cir::MatrixColumnMajorLoadOp::getEffects( + SmallVectorImpl<SideEffects::EffectInstance<MemoryEffects::Effect>> + &effects) { + getLoadEffects(getValueMutable(), getIsVolatile(), effects); +} + //===----------------------------------------------------------------------===// // MatrixTransposeOp //===----------------------------------------------------------------------===// @@ -5141,6 +5178,15 @@ 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 (auto [operand, attr] : + llvm::zip_equal(getOperation()->getOpOperands(), getOperandAttrs())) { + if (!mlir::isa<mlir::UnitAttr>(attr)) + continue; + effects.emplace_back(mlir::MemoryEffects::Read::get(), &operand); + effects.emplace_back(mlir::MemoryEffects::Write::get(), &operand); + } } //===----------------------------------------------------------------------===// 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..0c5685248e3b81 --- /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. + cir.func @loads(%arg0: !cir.ptr<!s32i>, %arg1: !cir.ptr<!u8i>) { + %0 = cir.load volatile %arg0 : !cir.ptr<!s32i>, !s32i + %1 = cir.load syncscope(system) atomic(seq_cst) %arg0 : !cir.ptr<!s32i>, !s32i + %2 = cir.load syncscope(system) atomic(relaxed) %arg0 : !cir.ptr<!s32i>, !s32i + %3 = cir.load %arg0 : !cir.ptr<!s32i>, !s32i + %4 = cir.get_bitfield align(4) (#bfi_f, %arg1 {is_volatile} : !cir.ptr<!u8i>) -> !s32i + %5 = cir.get_bitfield align(4) (#bfi_f, %arg1 : !cir.ptr<!u8i>) -> !s32i + cir.return + } + + // CHECK-LABEL: cir.func @loads + // CHECK-NEXT: cir.load volatile %arg0 + // CHECK-NEXT: cir.load syncscope(system) atomic(seq_cst) %arg0 + // CHECK-NEXT: cir.load syncscope(system) atomic(relaxed) %arg0 + // CHECK-NEXT: cir.get_bitfield align(4) (#bfi_f, %arg1 {is_volatile} + // CHECK-NEXT: cir.return + + // An inline asm with a memory output writes through its operand, so it is + // kept even though it has no result. + cir.func @asm_memory_output(%arg0: !cir.ptr<!s32i>) { + cir.asm(x86_att, + out = [%arg0 : !cir.ptr<!s32i> (maybe_memory)], + in = [], + in_out = [], + {"nop" "=*m,~{dirflag},~{fpsr},~{flags}"}) + cir.return + } + + // CHECK-LABEL: cir.func @asm_memory_output + // CHECK-NEXT: cir.asm(x86_att, + // CHECK-NEXT: out = [%arg0 : !cir.ptr<!s32i> (maybe_memory)], +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
