Skip to content

Commit cfcd7eb

Browse files
committed
Dlopen libgccjit.so in order to support multiple targets more easily
1 parent 709f1cf commit cfcd7eb

File tree

4 files changed

+45
-16
lines changed

4 files changed

+45
-16
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,18 @@ dependencies = [
5656

5757
[[package]]
5858
name = "gccjit"
59-
version = "2.10.0"
59+
version = "3.1.0"
6060
source = "registry+https://github.com/rust-lang/crates.io-index"
61-
checksum = "60362e038e71e4bdc1a5b23fb45e1aba587b5947fe0db58f4871d95608f89eca"
61+
checksum = "9a1d137657dee9b660e884dc11d41efbff8201aa49f9f75f7bc7d400ad1e9062"
6262
dependencies = [
6363
"gccjit_sys",
6464
]
6565

6666
[[package]]
6767
name = "gccjit_sys"
68-
version = "0.9.0"
68+
version = "1.1.0"
6969
source = "registry+https://github.com/rust-lang/crates.io-index"
70-
checksum = "ddd542c8414e122217551c6af6b7d33acf51a227aee85276f218c087525e01bb"
70+
checksum = "dde7e9f1ec00279a8e901f30567f97ab7536b544b70cc1f79833f35caeed877a"
7171
dependencies = [
7272
"libc",
7373
]

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ default = ["master"]
2424
[dependencies]
2525
object = { version = "0.37.0", default-features = false, features = ["std", "read"] }
2626
tempfile = "3.20"
27-
gccjit = "2.10"
28-
#gccjit = { git = "https://github.com/rust-lang/gccjit.rs" }
27+
gccjit = { version = "3.1", features = ["dlopen"] }
28+
#gccjit = { git = "https://github.com/rust-lang/gccjit.rs", branch = "error-dlopen", features = ["dlopen"] }
2929

3030
# Local copy.
31-
#gccjit = { path = "../gccjit.rs" }
31+
#gccjit = { path = "../gccjit.rs", features = ["dlopen"] }
3232

3333
[dev-dependencies]
3434
boml = "0.3.1"

libgccjit.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
28b84db392ac0a572f1a2a2a1317aa5f2bc742cb
1+
0081ca6631abdfa02bf42bc85aaf507b8a0e6beb

src/lib.rs

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ mod type_;
6969
mod type_of;
7070

7171
use std::any::Any;
72+
use std::ffi::CString;
7273
use std::fmt::Debug;
7374
use std::ops::Deref;
7475
use std::path::PathBuf;
@@ -142,7 +143,7 @@ impl TargetInfo {
142143

143144
#[derive(Clone)]
144145
pub struct LockedTargetInfo {
145-
info: Arc<Mutex<IntoDynSyncSend<TargetInfo>>>,
146+
info: Arc<Mutex<IntoDynSyncSend<Option<TargetInfo>>>>,
146147
}
147148

148149
impl Debug for LockedTargetInfo {
@@ -153,11 +154,21 @@ impl Debug for LockedTargetInfo {
153154

154155
impl LockedTargetInfo {
155156
fn cpu_supports(&self, feature: &str) -> bool {
156-
self.info.lock().expect("lock").cpu_supports(feature)
157+
self.info
158+
.lock()
159+
.expect("lock")
160+
.as_ref()
161+
.expect("target info not initialized")
162+
.cpu_supports(feature)
157163
}
158164

159165
fn supports_target_dependent_type(&self, typ: CType) -> bool {
160-
self.info.lock().expect("lock").supports_target_dependent_type(typ)
166+
self.info
167+
.lock()
168+
.expect("lock")
169+
.as_ref()
170+
.expect("target info not initialized")
171+
.supports_target_dependent_type(typ)
161172
}
162173
}
163174

@@ -169,6 +180,19 @@ pub struct GccCodegenBackend {
169180

170181
static LTO_SUPPORTED: AtomicBool = AtomicBool::new(false);
171182

183+
fn load_libgccjit_if_needed() {
184+
if gccjit::is_loaded() {
185+
// Do not load a libgccjit second time.
186+
return;
187+
}
188+
189+
let string = CString::new("libgccjit.so").expect("string to libgccjit path");
190+
191+
if let Err(error) = gccjit::load(&string) {
192+
panic!("Cannot load libgccjit.so: {}", error);
193+
}
194+
}
195+
172196
impl CodegenBackend for GccCodegenBackend {
173197
fn locale_resource(&self) -> &'static str {
174198
crate::DEFAULT_LOCALE_RESOURCE
@@ -179,6 +203,8 @@ impl CodegenBackend for GccCodegenBackend {
179203
}
180204

181205
fn init(&self, _sess: &Session) {
206+
load_libgccjit_if_needed();
207+
182208
#[cfg(feature = "master")]
183209
{
184210
let target_cpu = target_cpu(_sess);
@@ -189,7 +215,8 @@ impl CodegenBackend for GccCodegenBackend {
189215
context.add_command_line_option(format!("-march={}", target_cpu));
190216
}
191217

192-
**self.target_info.info.lock().expect("lock") = context.get_target_info();
218+
*self.target_info.info.lock().expect("lock") =
219+
IntoDynSyncSend(Some(context.get_target_info()));
193220
}
194221

195222
#[cfg(feature = "master")]
@@ -217,6 +244,9 @@ impl CodegenBackend for GccCodegenBackend {
217244
.info
218245
.lock()
219246
.expect("lock")
247+
.0
248+
.as_ref()
249+
.expect("target info not initialized")
220250
.supports_128bit_integers
221251
.store(check_context.get_last_error() == Ok(None), Ordering::SeqCst);
222252
}
@@ -438,13 +468,12 @@ pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
438468
let info = {
439469
// Check whether the target supports 128-bit integers, and sized floating point types (like
440470
// Float16).
441-
let context = Context::default();
442-
Arc::new(Mutex::new(IntoDynSyncSend(context.get_target_info())))
471+
Arc::new(Mutex::new(IntoDynSyncSend(None)))
443472
};
444473
#[cfg(not(feature = "master"))]
445-
let info = Arc::new(Mutex::new(IntoDynSyncSend(TargetInfo {
474+
let info = Arc::new(Mutex::new(IntoDynSyncSend(Some(TargetInfo {
446475
supports_128bit_integers: AtomicBool::new(false),
447-
})));
476+
}))));
448477

449478
Box::new(GccCodegenBackend {
450479
lto_supported: Arc::new(AtomicBool::new(false)),

0 commit comments

Comments
 (0)