From f4199a9b21951533625461ec5773e6bb50aa782d Mon Sep 17 00:00:00 2001
From: Philipp Zabel
Date: Wed, 27 Mar 2024 10:57:36 +0100
Subject: [PATCH] rusticl: work around reference-to-mutable-static warnings
Creating mutable or shared references to mutable static variables is
discouraged, but still possible by taking a detour to a raw pointer
via the addr_of! / addr_of_mut! macros.
For details, see: https://github.com/rust-lang/rust/issues/114447
Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/10887
Cc: mesa-stable
Reviewed-by: Karol Herbst
Signed-off-by: Philipp Zabel
Part-of:
---
src/gallium/frontends/rusticl/core/platform.rs | 14 +++++++++-----
src/gallium/frontends/rusticl/core/program.rs | 3 ++-
2 files changed, 11 insertions(+), 6 deletions(-)
diff --git a/src/gallium/frontends/rusticl/core/platform.rs b/src/gallium/frontends/rusticl/core/platform.rs
index f80ed9d3ceb..40f18f2ca51 100644
--- a/src/gallium/frontends/rusticl/core/platform.rs
+++ b/src/gallium/frontends/rusticl/core/platform.rs
@@ -7,6 +7,8 @@ use mesa_rust_gen::*;
use rusticl_opencl_gen::*;
use std::env;
+use std::ptr::addr_of;
+use std::ptr::addr_of_mut;
use std::sync::Once;
#[repr(C)]
@@ -73,7 +75,8 @@ static mut PLATFORM_FEATURES: PlatformFeatures = PlatformFeatures {
};
fn load_env() {
- let debug = unsafe { &mut PLATFORM_DBG };
+ // SAFETY: no other references exist at this point
+ let debug = unsafe { &mut *addr_of_mut!(PLATFORM_DBG) };
if let Ok(debug_flags) = env::var("RUSTICL_DEBUG") {
for flag in debug_flags.split(',') {
match flag {
@@ -88,7 +91,8 @@ fn load_env() {
}
}
- let features = unsafe { &mut PLATFORM_FEATURES };
+ // SAFETY: no other references exist at this point
+ let features = unsafe { &mut *addr_of_mut!(PLATFORM_FEATURES) };
if let Ok(feature_flags) = env::var("RUSTICL_FEATURES") {
for flag in feature_flags.split(',') {
match flag {
@@ -109,17 +113,17 @@ impl Platform {
pub fn get() -> &'static Self {
debug_assert!(PLATFORM_ONCE.is_completed());
// SAFETY: no mut references exist at this point
- unsafe { &PLATFORM }
+ unsafe { &*addr_of!(PLATFORM) }
}
pub fn dbg() -> &'static PlatformDebug {
debug_assert!(PLATFORM_ENV_ONCE.is_completed());
- unsafe { &PLATFORM_DBG }
+ unsafe { &*addr_of!(PLATFORM_DBG) }
}
pub fn features() -> &'static PlatformFeatures {
debug_assert!(PLATFORM_ENV_ONCE.is_completed());
- unsafe { &PLATFORM_FEATURES }
+ unsafe { &*addr_of!(PLATFORM_FEATURES) }
}
fn init(&mut self) {
diff --git a/src/gallium/frontends/rusticl/core/program.rs b/src/gallium/frontends/rusticl/core/program.rs
index 8b9f1cf55d2..ffdd1a5056b 100644
--- a/src/gallium/frontends/rusticl/core/program.rs
+++ b/src/gallium/frontends/rusticl/core/program.rs
@@ -20,6 +20,7 @@ use std::collections::HashSet;
use std::ffi::CString;
use std::mem::size_of;
use std::ptr;
+use std::ptr::addr_of;
use std::slice;
use std::sync::Arc;
use std::sync::Mutex;
@@ -55,7 +56,7 @@ fn get_disk_cache() -> &'static Option {
DISK_CACHE_ONCE.call_once(|| {
DISK_CACHE = DiskCache::new("rusticl", &func_ptrs, 0);
});
- &DISK_CACHE
+ &*addr_of!(DISK_CACHE)
}
}