From 93465e6c3106043b8db7089ff7a1a4d610d8f79f Mon Sep 17 00:00:00 2001
From: Taiki Endo <te316e89@gmail.com>
Date: Wed, 29 Jan 2025 06:46:05 +0900
Subject: [PATCH] Mark condition/carry bit as clobbered in C-SKY inline
 assembly

---
 compiler/rustc_codegen_llvm/src/asm.rs        |  4 +++-
 .../asm-experimental-arch.md                  |  2 ++
 tests/codegen/asm/csky-clobbers.rs            | 24 +++++++++++++++++++
 3 files changed, 29 insertions(+), 1 deletion(-)
 create mode 100644 tests/codegen/asm/csky-clobbers.rs

diff --git a/compiler/rustc_codegen_llvm/src/asm.rs b/compiler/rustc_codegen_llvm/src/asm.rs
index 3722d4350a291..be5673eddf93e 100644
--- a/compiler/rustc_codegen_llvm/src/asm.rs
+++ b/compiler/rustc_codegen_llvm/src/asm.rs
@@ -286,7 +286,9 @@ impl<'ll, 'tcx> AsmBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
                 InlineAsmArch::M68k => {
                     constraints.push("~{ccr}".to_string());
                 }
-                InlineAsmArch::CSKY => {}
+                InlineAsmArch::CSKY => {
+                    constraints.push("~{psr}".to_string());
+                }
             }
         }
         if !options.contains(InlineAsmOptions::NOMEM) {
diff --git a/src/doc/unstable-book/src/language-features/asm-experimental-arch.md b/src/doc/unstable-book/src/language-features/asm-experimental-arch.md
index c2f4170d7d295..d9566c9f55c5c 100644
--- a/src/doc/unstable-book/src/language-features/asm-experimental-arch.md
+++ b/src/doc/unstable-book/src/language-features/asm-experimental-arch.md
@@ -199,3 +199,5 @@ These flags registers must be restored upon exiting the asm block if the `preser
 - SPARC
   - Integer condition codes (`icc` and `xcc`)
   - Floating-point condition codes (`fcc[0-3]`)
+- CSKY
+  - Condition/carry bit (C) in `PSR`.
diff --git a/tests/codegen/asm/csky-clobbers.rs b/tests/codegen/asm/csky-clobbers.rs
new file mode 100644
index 0000000000000..4986d0fe56dfe
--- /dev/null
+++ b/tests/codegen/asm/csky-clobbers.rs
@@ -0,0 +1,24 @@
+//@ add-core-stubs
+//@ compile-flags: --target csky-unknown-linux-gnuabiv2
+//@ needs-llvm-components: csky
+
+#![crate_type = "rlib"]
+#![feature(no_core, asm_experimental_arch)]
+#![no_core]
+
+extern crate minicore;
+use minicore::*;
+
+// CHECK-LABEL: @flags_clobber
+// CHECK: call void asm sideeffect "", "~{psr}"()
+#[no_mangle]
+pub unsafe fn flags_clobber() {
+    asm!("", options(nostack, nomem));
+}
+
+// CHECK-LABEL: @no_clobber
+// CHECK: call void asm sideeffect "", ""()
+#[no_mangle]
+pub unsafe fn no_clobber() {
+    asm!("", options(nostack, nomem, preserves_flags));
+}