- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
Move alignment checks to codegen #117473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Closed
      
      
    
  
     Closed
                    Move alignment checks to codegen #117473
Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            162 changes: 162 additions & 0 deletions
          
          162 
        
  compiler/rustc_codegen_ssa/src/mir/pointer_alignment_check.rs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| use rustc_hir::LangItem; | ||
| use rustc_middle::mir; | ||
| use rustc_middle::mir::visit::Visitor; | ||
| use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext}; | ||
| use rustc_span::Span; | ||
|  | ||
| use super::FunctionCx; | ||
| use crate::base; | ||
| use crate::common; | ||
| use crate::mir::OperandValue; | ||
| use crate::traits::*; | ||
|  | ||
| pub fn pointers_to_check<F>( | ||
| statement: &mir::Statement<'_>, | ||
| required_align_of: F, | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is the API design here so that a large part of the logic lies with the caller? Is this expected to be used differently from different places in the future? Or was it just to avoid passing  | ||
| ) -> Vec<(mir::Local, u64)> | ||
| where | ||
| F: Fn(mir::Local) -> Option<u64>, | ||
| { | ||
| let mut finder = PointerFinder { required_align_of, pointers: Vec::new() }; | ||
| finder.visit_statement(statement, rustc_middle::mir::Location::START); | ||
| finder.pointers | ||
| } | ||
|  | ||
| struct PointerFinder<F> { | ||
| pointers: Vec<(mir::Local, u64)>, | ||
| required_align_of: F, | ||
| } | ||
|  | ||
| impl<'tcx, F> Visitor<'tcx> for PointerFinder<F> | ||
| where | ||
| F: Fn(mir::Local) -> Option<u64>, | ||
| { | ||
| fn visit_place( | ||
| &mut self, | ||
| place: &mir::Place<'tcx>, | ||
| context: PlaceContext, | ||
| location: mir::Location, | ||
| ) { | ||
| // We want to only check reads and writes to Places, so we specifically exclude | ||
| // Borrows and AddressOf. | ||
| match context { | ||
| PlaceContext::MutatingUse( | ||
| MutatingUseContext::Store | ||
| | MutatingUseContext::AsmOutput | ||
| | MutatingUseContext::Call | ||
| | MutatingUseContext::Yield | ||
| | MutatingUseContext::Drop, | ||
| ) => {} | ||
| PlaceContext::NonMutatingUse( | ||
| NonMutatingUseContext::Copy | NonMutatingUseContext::Move, | ||
| ) => {} | ||
| _ => { | ||
| return; | ||
| } | ||
| } | ||
|  | ||
| if !place.is_indirect() { | ||
| return; | ||
| } | ||
|  | ||
| let pointer = place.local; | ||
| let Some(required_alignment) = (self.required_align_of)(pointer) else { | ||
| return; | ||
| }; | ||
|  | ||
| if required_alignment == 1 { | ||
| return; | ||
| } | ||
|  | ||
| // Ensure that this place is based on an aligned pointer. | ||
| self.pointers.push((pointer, required_alignment)); | ||
|  | ||
| self.super_place(place, context, location); | ||
| } | ||
| } | ||
|  | ||
| impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { | ||
| #[instrument(level = "debug", skip(self, bx))] | ||
| pub fn codegen_alignment_check( | ||
| &mut self, | ||
| bx: &mut Bx, | ||
| pointer: mir::Operand<'tcx>, | ||
| required_alignment: u64, | ||
| source_info: mir::SourceInfo, | ||
| ) { | ||
| // Compute the alignment mask | ||
| let mask = bx.const_usize(required_alignment - 1); | ||
| let zero = bx.const_usize(0); | ||
| let required_alignment = bx.const_usize(required_alignment); | ||
|  | ||
| // And the pointer with the mask | ||
| let pointer = match self.codegen_operand(bx, &pointer).val { | ||
| OperandValue::Immediate(imm) => imm, | ||
| OperandValue::Pair(ptr, _) => ptr, | ||
| _ => { | ||
| unreachable!("{pointer:?}"); | ||
| } | ||
| }; | ||
| let addr = bx.ptrtoint(pointer, bx.cx().type_isize()); | ||
| let masked = bx.and(addr, mask); | ||
|  | ||
| // Branch on whether the masked value is zero | ||
| let is_zero = bx.icmp( | ||
| base::bin_op_to_icmp_predicate(mir::BinOp::Eq.to_hir_binop(), false), | ||
| masked, | ||
| zero, | ||
| ); | ||
|  | ||
| // Create destination blocks, branching on is_zero | ||
| let panic = bx.append_sibling_block("panic"); | ||
| let success = bx.append_sibling_block("success"); | ||
| bx.cond_br(is_zero, success, panic); | ||
|  | ||
| // Switch to the failure block and codegen a call to the panic intrinsic | ||
| bx.switch_to_block(panic); | ||
| self.set_debug_loc(bx, source_info); | ||
| let location = self.get_caller_location(bx, source_info).immediate(); | ||
| self.codegen_nounwind_panic( | ||
| bx, | ||
| LangItem::PanicMisalignedPointerDereference, | ||
| &[required_alignment, addr, location], | ||
| source_info.span, | ||
| ); | ||
|  | ||
| // Continue codegen in the success block. | ||
| bx.switch_to_block(success); | ||
| self.set_debug_loc(bx, source_info); | ||
| } | ||
|  | ||
| /// Emit a call to a diverging and `rustc_nounwind` panic helper. | ||
| #[instrument(level = "debug", skip(self, bx))] | ||
| fn codegen_nounwind_panic( | ||
| &mut self, | ||
| bx: &mut Bx, | ||
| lang_item: LangItem, | ||
| args: &[Bx::Value], | ||
| span: Span, | ||
| ) { | ||
| let (fn_abi, fn_ptr, instance) = common::build_langcall(bx, Some(span), lang_item); | ||
| let fn_ty = bx.fn_decl_backend_type(&fn_abi); | ||
| let fn_attrs = if bx.tcx().def_kind(self.instance.def_id()).has_codegen_attrs() { | ||
| Some(bx.tcx().codegen_fn_attrs(self.instance.def_id())) | ||
| } else { | ||
| None | ||
| }; | ||
|  | ||
| // bx.call requires that the call not unwind. Double-check that this LangItem can't unwind. | ||
| assert!(!fn_abi.can_unwind); | ||
|  | ||
| bx.call( | ||
| fn_ty, | ||
| fn_attrs, | ||
| Some(&fn_abi), | ||
| fn_ptr, | ||
| args, | ||
| None, /* funclet */ | ||
| Some(instance), | ||
| ); | ||
| bx.unreachable(); | ||
| } | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a nop in the success block. This is necessary for clif ir printing annotations to work correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't we need a nop in the panic block?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be necessary there too.