Commit 2add31bfde

Alex Rønne Petersen <alex@alexrp.com>
2025-06-04 06:04:34
valgrind: Add riscv64-linux support.
This appeared in Valgrind 3.25.0.
1 parent 100b76e
Changed files (4)
lib
src
lib/std/valgrind.zig
@@ -66,6 +66,20 @@ pub fn doClientRequest(default: usize, request: usize, a1: usize, a2: usize, a3:
               [_] "{r3}" (default),
             : "cc", "memory"
         ),
+        .riscv64 => asm volatile (
+            \\ .option push
+            \\ .option norvc
+            \\ srli zero, zero, 3
+            \\ srli zero, zero, 13
+            \\ srli zero, zero, 51
+            \\ srli zero, zero, 61
+            \\ or   a0,   a0,   a0
+            \\ .option pop
+            : [_] "={a3}" (-> usize),
+            : [_] "{a4}" (args),
+              [_] "{a3}" (default),
+            : "cc", "memory"
+        ),
         .s390x => asm volatile (
             \\ lr %%r15, %%r15
             \\ lr %%r1,  %%r1
src/codegen/llvm.zig
@@ -11604,7 +11604,7 @@ pub const FuncGen = struct {
         const pt = o.pt;
         const zcu = pt.zcu;
         const target = zcu.getTarget();
-        if (!target_util.hasValgrindSupport(target)) return default_value;
+        if (!target_util.hasValgrindSupport(target, .stage2_llvm)) return default_value;
 
         const llvm_usize = try o.lowerType(Type.usize);
         const usize_alignment = Type.usize.abiAlignment(zcu).toLlvm();
@@ -11678,6 +11678,19 @@ pub const FuncGen = struct {
                 ,
                 .constraints = "={r3},{r4},{r3},~{cc},~{memory}",
             },
+            .riscv64 => .{
+                .template =
+                \\ .option push
+                \\ .option norvc
+                \\ srli zero, zero, 3
+                \\ srli zero, zero, 13
+                \\ srli zero, zero, 51
+                \\ srli zero, zero, 61
+                \\ or   a0,   a0,   a0
+                \\ .option pop
+                ,
+                .constraints = "={a3},{a4},{a3},~{cc},~{memory}",
+            },
             .s390x => .{
                 .template =
                 \\ lr %r15, %r15
src/Package/Module.zig
@@ -113,8 +113,10 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
         break :b options.global.root_strip;
     };
 
+    const zig_backend = target_util.zigBackend(target, options.global.use_llvm);
+
     const valgrind = b: {
-        if (!target_util.hasValgrindSupport(target)) {
+        if (!target_util.hasValgrindSupport(target, zig_backend)) {
             if (options.inherited.valgrind == true)
                 return error.ValgrindUnsupportedOnTarget;
             break :b false;
@@ -125,8 +127,6 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
         break :b optimize_mode == .Debug;
     };
 
-    const zig_backend = target_util.zigBackend(target, options.global.use_llvm);
-
     const single_threaded = b: {
         if (target_util.alwaysSingleThreaded(target)) {
             if (options.inherited.single_threaded == false)
src/target.zig
@@ -84,7 +84,7 @@ pub fn defaultSingleThreaded(target: std.Target) bool {
     return false;
 }
 
-pub fn hasValgrindSupport(target: std.Target) bool {
+pub fn hasValgrindSupport(target: std.Target, backend: std.builtin.CompilerBackend) bool {
     // We can't currently output the necessary Valgrind client request assembly when using the C
     // backend and compiling with an MSVC-like compiler.
     const ofmt_c_msvc = (target.abi == .msvc or target.abi == .itanium) and target.ofmt == .c;
@@ -103,7 +103,11 @@ pub fn hasValgrindSupport(target: std.Target) bool {
             else => false,
         },
         .powerpc, .powerpcle, .powerpc64, .powerpc64le => switch (target.os.tag) {
-            .linux => true,
+            .linux => backend != .stage2_powerpc, // Insufficient inline assembly support in self-hosted.
+            else => false,
+        },
+        .riscv64 => switch (target.os.tag) {
+            .linux => backend != .stage2_riscv64, // Insufficient inline assembly support in self-hosted.
             else => false,
         },
         .s390x => switch (target.os.tag) {