Return type RwLock<i32> becomes i128

Hi guys, I am learning the Rust language and the compiler recently. I knew a bit about LLVM, so I usually generate the IR for the test code I wrote. One thing I don't understand is that, why some types are lowered to i128 or i64 in LLVM IR. For example:

use std::sync::RwLock;

fn main() {
    let v = RwLock::new(0);
    *v.write().unwrap() = 7777;
    println!("{}", *v.read().unwrap());
}

The return type of function RwLock::new becomes i128 in LLVM IR. I know this is due to some optimizations. But I don't quite understand where is such optimization performed? I checked the MIR, the type is still RwLock there. And I don't remember LLVM opt will do such optimizations. Probably this happens in lowering stage from MIR to LLVM IR?

Any hints are appreciated!

1 Like

I mean, the size of an RwLock is indeed 16 bytes, so it doesn't seem that crazy.

use std::sync::RwLock;

fn main() {
    println!("{}", std::mem::size_of::<RwLock<i32>>());
}
16

@alice Yeah, that's why it can be optimized to i128. I just want to know in which module or function that this optimization is performed. I spent a few hours but only found stuff related to monomorphization and substitution in rust_codegen_ssa and rustc_codegen_llvm.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.