https://github.com/xlauko updated https://github.com/llvm/llvm-project/pull/242691
>From 2c256e2bbdaf03762dd290b48d10cebef4bd5a68 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. 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 | 26 +++++-- clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 69 +++++++++++++++++++ .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 5 +- clang/test/CIR/IR/invalid-inline-asm.cir | 36 ++++++++++ .../test/CIR/Transforms/side-effects-dce.cir | 42 +++++++++++ 5 files changed, 169 insertions(+), 9 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..1ef495e4edd840 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,7 +6123,18 @@ 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; + let hasVerifier = 1; } //===----------------------------------------------------------------------===// @@ -7093,6 +7106,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 +7134,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..11019ba3f37ccc 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 //===----------------------------------------------------------------------===// @@ -5295,6 +5328,34 @@ ParseResult cir::InlineAsmOp::parse(OpAsmParser &parser, return mlir::success(); } +LogicalResult cir::InlineAsmOp::verify() { + // `operand_attrs` has one entry per asm operand: a unit attribute marks a + // memory operand, and an empty dictionary marks any other operand. + ArrayAttr operandAttrs = getOperandAttrs(); + if (operandAttrs.size() != getNumOperands()) + return emitOpError() << "expects one operand attribute per asm operand, " + "but got " + << operandAttrs.size() << " for " << getNumOperands() + << " operands"; + + for (auto [index, attr] : llvm::enumerate(operandAttrs)) { + if (isMemoryOperand(index)) { + Type type = getOperand(index).getType(); + if (!isa<cir::PointerType>(type)) + return emitOpError() << "memory operand #" << index + << " must be a pointer, but got " << type; + continue; + } + auto dict = dyn_cast<DictionaryAttr>(attr); + if (!dict || !dict.empty()) + return emitOpError() << "operand attribute #" << index + << " must be a unit attribute or an empty " + "dictionary, but got " + << attr; + } + 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 +5364,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/IR/invalid-inline-asm.cir b/clang/test/CIR/IR/invalid-inline-asm.cir new file mode 100644 index 00000000000000..79da6001b970cc --- /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@+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@+1 {{'cir.asm' op expects one operand attribute per asm operand, but got 0 for 1 operands}} + "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@+1 {{'cir.asm' op operand attribute #0 must be a unit attribute or an empty dictionary, but got 1 : i32}} + "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..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
