https://github.com/erichkeane created 
https://github.com/llvm/llvm-project/pull/228261

Inline-asm was set to have recursive memory effects, but it has no blocks, so 
the result was it never was listed as having memory effects.  This patch sets 
it to have a read and write effect in the case where it has side effects.

This showed up in the libcxx test, where we defeated the DoNotOptimze macro by 
not properly respecting inline-asm side effects.

>From 65e0e0480712b070eba4944eba8c5eb7b3341364 Mon Sep 17 00:00:00 2001
From: erichkeane <[email protected]>
Date: Thu, 1 Oct 2026 15:00:41 -0700
Subject: [PATCH] [CIR] Set memory effects of inline-asm-volatile correctly

Inline-asm was set to have recursive memory effects, but it has no
blocks, so the result was it never was listed as having memory
effects.  This patch sets it to have a read and write effect in the case
where it has side effects.

This showed up in the libcxx test, where we defeated the DoNotOptimze
macro by not properly respecting inline-asm side effects.
---
 clang/include/clang/CIR/Dialect/IR/CIROps.td  |  3 +-
 clang/lib/CIR/Dialect/IR/CIRDialect.cpp       | 10 +++++++
 .../Transforms/inline-asm-side-effects.cir    | 28 +++++++++++++++++++
 3 files changed, 40 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/CIR/Transforms/inline-asm-side-effects.cir

diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td 
b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index bc9af2210a048..6be840fbb343a 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -5575,7 +5575,8 @@ def CIR_AsmFlavor : CIR_I32Enum<"AsmFlavor", "ATT or 
Intel",
 
 def CIR_AsmFlavorAttr : CIR_EnumAttr<CIR_AsmFlavor, "asm_flavor">;
 
-def CIR_InlineAsmOp : CIR_Op<"asm", [RecursiveMemoryEffects]> {
+def CIR_InlineAsmOp : CIR_Op<"asm",
+    [DeclareOpInterfaceMethods<MemoryEffectsOpInterface>]> {
   let description = [{
     The `cir.asm` operation represents C/C++ asm inline.
 
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp 
b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index 4b9e0c7f0f544..bba3e5ea94e8e 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -4672,6 +4672,16 @@ ParseResult cir::InlineAsmOp::parse(OpAsmParser &parser,
   return mlir::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
+  // write memory effect. We do this the same as the llvm dialect InlineAsmOp.
+  if (getSideEffects()) {
+    effects.emplace_back(mlir::MemoryEffects::Read::get());
+    effects.emplace_back(mlir::MemoryEffects::Write::get());
+  }
+}
+
 
//===----------------------------------------------------------------------===//
 // ThrowOp / TryThrowOp
 
//===----------------------------------------------------------------------===//
diff --git a/clang/test/CIR/Transforms/inline-asm-side-effects.cir 
b/clang/test/CIR/Transforms/inline-asm-side-effects.cir
new file mode 100644
index 0000000000000..ddc8d1955e739
--- /dev/null
+++ b/clang/test/CIR/Transforms/inline-asm-side-effects.cir
@@ -0,0 +1,28 @@
+// RUN: cir-opt %s -canonicalize -o - | FileCheck %s
+
+module {
+
+// CHECK-LABEL: @dead_pure_asm_is_removed
+// CHECK-NOT:     cir.asm
+cir.func @dead_pure_asm_is_removed() {
+  cir.asm(x86_att,
+    out = [],
+    in = [],
+    in_out = [],
+    {"nop" "~{dirflag},~{fpsr},~{flags}"})
+  cir.return
+}
+
+// CHECK-LABEL: @dead_side_effects_asm_is_not_removed
+// CHECK:         cir.asm
+// CHECK:         side_effects
+cir.func @dead_side_effects_asm_is_not_removed() {
+  cir.asm(x86_att,
+    out = [],
+    in = [],
+    in_out = [],
+    {"nop" "~{dirflag},~{fpsr},~{flags}"}) side_effects
+  cir.return
+}
+
+}

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

Reply via email to