]> git.lizzy.rs Git - rust.git/commitdiff
Don't cache projection if no padding is used.
authorHans Kratz <hans@appfour.com>
Tue, 20 Jul 2021 05:38:00 +0000 (07:38 +0200)
committerHans Kratz <hans@appfour.com>
Wed, 4 Aug 2021 21:36:13 +0000 (23:36 +0200)
In this case we can just return memory_index(index) which is readily available.

compiler/rustc_codegen_llvm/src/type_of.rs

index ad9696588952e2809b6de2740a8153059811c43c..da2927566730f11ea0b27b1a3259c2c8781be20d 100644 (file)
@@ -99,6 +99,7 @@ fn struct_llfields<'a, 'tcx>(
     let mut prev_effective_align = layout.align.abi;
     let mut result: Vec<_> = Vec::with_capacity(1 + field_count * 2);
     let mut projection = vec![0; field_count];
+    let mut padding_used = false;
     for i in layout.fields.index_by_increasing_offset() {
         let target_offset = layout.fields.offset(i as usize);
         let field = layout.field(cx, i);
@@ -118,6 +119,7 @@ fn struct_llfields<'a, 'tcx>(
         assert!(target_offset >= offset);
         let padding = target_offset - offset;
         if padding != Size::ZERO {
+            padding_used = true;
             let padding_align = prev_effective_align.min(effective_field_align);
             assert_eq!(offset.align_to(padding_align) + padding, target_offset);
             result.push(cx.type_padding_filler(padding, padding_align));
@@ -145,7 +147,9 @@ fn struct_llfields<'a, 'tcx>(
     } else {
         debug!("struct_llfields: offset: {:?} stride: {:?}", offset, layout.size);
     }
-    cx.field_projection_cache.borrow_mut().insert(layout, projection);
+    if padding_used {
+        cx.field_projection_cache.borrow_mut().insert(layout, projection);
+    }
 
     (result, packed)
 }
@@ -361,9 +365,7 @@ fn llvm_field_index<'a>(&self, cx: &CodegenCx<'a, 'tcx>, index: usize) -> u64 {
 
             FieldsShape::Arbitrary { .. } => match cx.field_projection_cache.borrow().get(self) {
                 Some(projection) => projection[index] as u64,
-                None => {
-                    bug!("TyAndLayout::llvm_field_index({:?}): field projection not cached", self)
-                }
+                None => self.fields.memory_index(index) as u64,
             },
         }
     }