From 1758f07cb832e9a854f34af437878a10f22dae82 Mon Sep 17 00:00:00 2001
From: quininer <quininer@live.com>
Date: Thu, 8 May 2025 17:56:09 +0800
Subject: [PATCH 1/2] Enable xray support for Mac

* https://maskray.me/blog/2023-06-18-port-llvm-xray-to-apple-systems
* https://github.com/llvm/llvm-project/blob/llvmorg-20.1.4/clang/lib/Driver/XRayArgs.cpp#L31
---
 compiler/rustc_target/src/spec/targets/aarch64_apple_darwin.rs | 1 +
 compiler/rustc_target/src/spec/targets/x86_64_apple_darwin.rs  | 1 +
 tests/ui/instrument-xray/target-not-supported.rs               | 2 +-
 3 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/compiler/rustc_target/src/spec/targets/aarch64_apple_darwin.rs b/compiler/rustc_target/src/spec/targets/aarch64_apple_darwin.rs
index d3e0a32c8b8fa..6587abb2ba7e4 100644
--- a/compiler/rustc_target/src/spec/targets/aarch64_apple_darwin.rs
+++ b/compiler/rustc_target/src/spec/targets/aarch64_apple_darwin.rs
@@ -22,6 +22,7 @@ pub(crate) fn target() -> Target {
             max_atomic_width: Some(128),
             // FIXME: The leak sanitizer currently fails the tests, see #88132.
             supported_sanitizers: SanitizerSet::ADDRESS | SanitizerSet::CFI | SanitizerSet::THREAD,
+            supports_xray: true,
             ..opts
         },
     }
diff --git a/compiler/rustc_target/src/spec/targets/x86_64_apple_darwin.rs b/compiler/rustc_target/src/spec/targets/x86_64_apple_darwin.rs
index 2f868e38f1a51..64c1705478054 100644
--- a/compiler/rustc_target/src/spec/targets/x86_64_apple_darwin.rs
+++ b/compiler/rustc_target/src/spec/targets/x86_64_apple_darwin.rs
@@ -23,6 +23,7 @@ pub(crate) fn target() -> Target {
                 | SanitizerSet::CFI
                 | SanitizerSet::LEAK
                 | SanitizerSet::THREAD,
+            supports_xray: true,
             ..opts
         },
     }
diff --git a/tests/ui/instrument-xray/target-not-supported.rs b/tests/ui/instrument-xray/target-not-supported.rs
index 2045913b186cd..697db6bd4b747 100644
--- a/tests/ui/instrument-xray/target-not-supported.rs
+++ b/tests/ui/instrument-xray/target-not-supported.rs
@@ -1,7 +1,7 @@
 // Verifies that `-Z instrument-xray` cannot be used with unsupported targets,
 //
 //@ needs-llvm-components: x86
-//@ compile-flags: -Z instrument-xray --target x86_64-apple-darwin
+//@ compile-flags: -Z instrument-xray --target x86_64-pc-windows-msvc
 
 #![feature(no_core)]
 #![no_core]

From 45ed022d636ec72dfbb22269196007b0e71a9c7d Mon Sep 17 00:00:00 2001
From: quininer <quininer@live.com>
Date: Mon, 19 May 2025 10:44:00 +0800
Subject: [PATCH 2/2] Add compiler tests for xray

---
 tests/assembly/aarch64-xray.rs                | 25 +++++++++++++++++
 tests/assembly/x86_64-xray.rs                 | 25 +++++++++++++++++
 tests/ui/instrument-xray/platform-support.rs  | 27 +++++++++++++++++++
 ...rr => platform-support.unsupported.stderr} |  0
 .../instrument-xray/target-not-supported.rs   | 10 -------
 5 files changed, 77 insertions(+), 10 deletions(-)
 create mode 100644 tests/assembly/aarch64-xray.rs
 create mode 100644 tests/assembly/x86_64-xray.rs
 create mode 100644 tests/ui/instrument-xray/platform-support.rs
 rename tests/ui/instrument-xray/{target-not-supported.stderr => platform-support.unsupported.stderr} (100%)
 delete mode 100644 tests/ui/instrument-xray/target-not-supported.rs

diff --git a/tests/assembly/aarch64-xray.rs b/tests/assembly/aarch64-xray.rs
new file mode 100644
index 0000000000000..d5ee011184394
--- /dev/null
+++ b/tests/assembly/aarch64-xray.rs
@@ -0,0 +1,25 @@
+//@ assembly-output: emit-asm
+//@ compile-flags: -Zinstrument-xray=always
+
+//@ revisions: aarch64-linux
+//@[aarch64-linux] compile-flags: --target=aarch64-unknown-linux-gnu
+//@[aarch64-linux] needs-llvm-components: aarch64
+//@[aarch64-linux] only-aarch64-unknown-linux-gnu
+
+//@ revisions: aarch64-darwin
+//@[aarch64-darwin] compile-flags: --target=aarch64-apple-darwin
+//@[aarch64-darwin] needs-llvm-components: aarch64
+//@[aarch64-darwin] only-aarch64-apple-darwin
+
+#![crate_type = "lib"]
+
+// CHECK-LABEL: xray_func:
+#[no_mangle]
+pub fn xray_func() {
+    // CHECK: nop
+
+    std::hint::black_box(());
+
+    // CHECK: b #32
+    // CHECK-NEXT: nop
+}
diff --git a/tests/assembly/x86_64-xray.rs b/tests/assembly/x86_64-xray.rs
new file mode 100644
index 0000000000000..4cf3e8cda13c0
--- /dev/null
+++ b/tests/assembly/x86_64-xray.rs
@@ -0,0 +1,25 @@
+//@ assembly-output: emit-asm
+//@ compile-flags: -Zinstrument-xray=always -Cllvm-args=-x86-asm-syntax=intel
+
+//@ revisions: x86_64-linux
+//@[x86_64-linux] compile-flags: --target=x86_64-unknown-linux-gnu
+//@[x86_64-linux] needs-llvm-components: x86
+//@[x86_64-linux] only-x86_64-unknown-linux-gnu
+
+//@ revisions: x86_64-darwin
+//@[x86_64-darwin] compile-flags: --target=x86_64-apple-darwin
+//@[x86_64-darwin] needs-llvm-components: x86
+//@[x86_64-darwin] only-x86_64-apple-darwin
+
+#![crate_type = "lib"]
+
+// CHECK-LABEL: xray_func:
+#[no_mangle]
+pub fn xray_func() {
+    // CHECK: nop word ptr [rax + rax + 512]
+
+    std::hint::black_box(());
+
+    // CHECK: ret
+    // CHECK-NEXT: nop word ptr cs:[rax + rax + 512]
+}
diff --git a/tests/ui/instrument-xray/platform-support.rs b/tests/ui/instrument-xray/platform-support.rs
new file mode 100644
index 0000000000000..238018b348dee
--- /dev/null
+++ b/tests/ui/instrument-xray/platform-support.rs
@@ -0,0 +1,27 @@
+//@ only-nightly (flag is still unstable)
+//@ needs-xray
+
+//@ revisions: unsupported
+//@[unsupported] needs-llvm-components: x86
+//@[unsupported] compile-flags: -Z instrument-xray --target=x86_64-pc-windows-msvc
+
+//@ revisions: x86_64-linux
+//@[x86_64-linux] needs-llvm-components: x86
+//@[x86_64-linux] compile-flags: -Z instrument-xray --target=x86_64-unknown-linux-gnu
+//@[x86_64-linux] check-pass
+
+//@ revisions: x86_64-darwin
+//@[x86_64-darwin] needs-llvm-components: x86
+//@[x86_64-darwin] compile-flags: -Z instrument-xray --target=x86_64-apple-darwin
+//@[x86_64-darwin] check-pass
+
+//@ revisions: aarch64-darwin
+//@[aarch64-darwin] needs-llvm-components: aarch64
+//@[aarch64-darwin] compile-flags: -Z instrument-xray --target=aarch64-apple-darwin
+//@[aarch64-darwin] check-pass
+
+#![feature(no_core)]
+#![no_core]
+#![no_main]
+
+//[unsupported]~? ERROR XRay instrumentation is not supported for this target
diff --git a/tests/ui/instrument-xray/target-not-supported.stderr b/tests/ui/instrument-xray/platform-support.unsupported.stderr
similarity index 100%
rename from tests/ui/instrument-xray/target-not-supported.stderr
rename to tests/ui/instrument-xray/platform-support.unsupported.stderr
diff --git a/tests/ui/instrument-xray/target-not-supported.rs b/tests/ui/instrument-xray/target-not-supported.rs
deleted file mode 100644
index 697db6bd4b747..0000000000000
--- a/tests/ui/instrument-xray/target-not-supported.rs
+++ /dev/null
@@ -1,10 +0,0 @@
-// Verifies that `-Z instrument-xray` cannot be used with unsupported targets,
-//
-//@ needs-llvm-components: x86
-//@ compile-flags: -Z instrument-xray --target x86_64-pc-windows-msvc
-
-#![feature(no_core)]
-#![no_core]
-#![no_main]
-
-//~? ERROR XRay instrumentation is not supported for this target