@@ -296,22 +296,41 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
296296 }
297297 }
298298 sym:: volatile_load | sym:: unaligned_volatile_load => {
299- let mut ptr = args[ 0 ] . immediate ( ) ;
300- // Handle cast if the ABI requires it
301- if let PassMode :: Cast { cast : ty, .. } = & fn_abi. ret . mode {
302- ptr = self . pointercast ( ptr, self . type_ptr_to ( ty. llvm_type ( self ) ) ) ;
303- }
304- let load = self . volatile_load ( result. layout . llvm_type ( self ) , ptr) ;
299+ // The `*const T` or `*mut T` operand.
300+ let ptr_operand = & args[ 0 ] ;
301+ let src_ptr_llval = ptr_operand. immediate ( ) ;
302+
303+ // Determine the type T (the pointee type) and its LLVM representation.
304+ // `ptr_operand.layout.ty` is the Rust type `*const T` (or `*mut T`). We
305+ // need the layout of `T`.
306+ let pointee_rust_ty =
307+ ptr_operand
308+ . layout
309+ . ty
310+ . builtin_deref ( true )
311+ . unwrap_or_else ( || {
312+ span_bug ! ( span, "volatile_load input pointer is not a pointer type" )
313+ } ) ;
314+ let layout_of_pointee = self . layout_of ( pointee_rust_ty) ;
315+ let llvm_ty_of_pointee = layout_of_pointee. llvm_type ( self ) ;
316+
317+ // Call volatile_load with the correct LLVM type of T. The
318+ // `volatile_load` does a pointercast so we do not need to do it here.
319+ let loaded_llval = self . volatile_load ( llvm_ty_of_pointee, src_ptr_llval) ;
320+
321+ // Set alignment for the LLVM load instruction based on the alignment of
322+ // `T`.
305323 let align = if name == sym:: unaligned_volatile_load {
306324 1
307325 } else {
308- result . layout . align . abi . bytes ( ) as u32
326+ layout_of_pointee . align . abi . bytes ( ) as u32
309327 } ;
310328 unsafe {
311- llvm:: LLVMSetAlignment ( load , align) ;
329+ llvm:: LLVMSetAlignment ( loaded_llval , align) ;
312330 }
331+
313332 if !result. layout . is_zst ( ) {
314- self . store_to_place ( load , result. val ) ;
333+ self . store_to_place ( loaded_llval , result. val ) ;
315334 }
316335 return Ok ( ( ) ) ;
317336 }
0 commit comments