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