|
1 | 1 | use crate::abi::call::{ArgAttribute, FnAbi, PassMode, Reg, RegKind}; |
2 | | -use crate::abi::{Align, HasDataLayout, TyAbiInterface}; |
| 2 | +use crate::abi::{Abi, Align, HasDataLayout, TyAbiInterface, TyAndLayout}; |
3 | 3 | use crate::spec::HasTargetSpec; |
4 | 4 |
|
5 | 5 | #[derive(PartialEq)] |
@@ -53,38 +53,58 @@ where |
53 | 53 | if arg.is_ignore() { |
54 | 54 | continue; |
55 | 55 | } |
56 | | - if !arg.layout.is_aggregate() { |
57 | | - arg.extend_integer_width_to(32); |
58 | | - continue; |
59 | | - } |
60 | 56 |
|
61 | | - // We need to compute the alignment of the `byval` argument. The rules can be found in |
62 | | - // `X86_32ABIInfo::getTypeStackAlignInBytes` in Clang's `TargetInfo.cpp`. Summarized here, |
63 | | - // they are: |
64 | | - // |
65 | | - // 1. If the natural alignment of the type is less than or equal to 4, the alignment is 4. |
66 | | - // |
67 | | - // 2. Otherwise, on Linux, the alignment of any vector type is the natural alignment. |
68 | | - // (This doesn't matter here because we ensure we have an aggregate with the check above.) |
69 | | - // |
70 | | - // 3. Otherwise, on Apple platforms, the alignment of anything that contains a vector type |
71 | | - // is 16. |
72 | | - // |
73 | | - // 4. If none of these conditions are true, the alignment is 4. |
74 | | - let t = cx.target_spec(); |
75 | | - let align_4 = Align::from_bytes(4).unwrap(); |
76 | | - let align_16 = Align::from_bytes(16).unwrap(); |
77 | | - let byval_align = if arg.layout.align.abi < align_4 { |
78 | | - align_4 |
79 | | - } else if t.is_like_osx && arg.layout.align.abi >= align_16 { |
80 | | - // FIXME(pcwalton): This is dubious--we should actually be looking inside the type to |
81 | | - // determine if it contains SIMD vector values--but I think it's fine? |
82 | | - align_16 |
83 | | - } else { |
84 | | - align_4 |
85 | | - }; |
| 57 | + if arg.layout.is_aggregate() { |
| 58 | + // We need to compute the alignment of the `byval` argument. The rules can be found in |
| 59 | + // `X86_32ABIInfo::getTypeStackAlignInBytes` in Clang's `TargetInfo.cpp`. Summarized |
| 60 | + // here, they are: |
| 61 | + // |
| 62 | + // 1. If the natural alignment of the type is <= 4, the alignment is 4. |
| 63 | + // |
| 64 | + // 2. Otherwise, on Linux, the alignment of any vector type is the natural alignment. |
| 65 | + // This doesn't matter here because we only pass aggregates via `byval`, not vectors. |
| 66 | + // |
| 67 | + // 3. Otherwise, on Apple platforms, the alignment of anything that contains a vector |
| 68 | + // type is 16. |
| 69 | + // |
| 70 | + // 4. If none of these conditions are true, the alignment is 4. |
| 71 | + |
| 72 | + fn contains_vector<'a, Ty, C>(cx: &C, layout: TyAndLayout<'a, Ty>) -> bool |
| 73 | + where |
| 74 | + Ty: TyAbiInterface<'a, C> + Copy, |
| 75 | + { |
| 76 | + match layout.abi { |
| 77 | + Abi::Uninhabited | Abi::Scalar(_) | Abi::ScalarPair(..) => false, |
| 78 | + Abi::Vector { .. } => true, |
| 79 | + Abi::Aggregate { .. } => { |
| 80 | + for i in 0..layout.fields.count() { |
| 81 | + if contains_vector(cx, layout.field(cx, i)) { |
| 82 | + return true; |
| 83 | + } |
| 84 | + } |
| 85 | + false |
| 86 | + } |
| 87 | + } |
| 88 | + } |
86 | 89 |
|
87 | | - arg.make_indirect_byval(Some(byval_align)); |
| 90 | + let t = cx.target_spec(); |
| 91 | + let align_4 = Align::from_bytes(4).unwrap(); |
| 92 | + let align_16 = Align::from_bytes(16).unwrap(); |
| 93 | + let byval_align = if arg.layout.align.abi < align_4 { |
| 94 | + // (1.) |
| 95 | + align_4 |
| 96 | + } else if t.is_like_osx && contains_vector(cx, arg.layout) { |
| 97 | + // (3.) |
| 98 | + align_16 |
| 99 | + } else { |
| 100 | + // (4.) |
| 101 | + align_4 |
| 102 | + }; |
| 103 | + |
| 104 | + arg.make_indirect_byval(Some(byval_align)); |
| 105 | + } else { |
| 106 | + arg.extend_integer_width_to(32); |
| 107 | + } |
88 | 108 | } |
89 | 109 |
|
90 | 110 | if flavor == Flavor::FastcallOrVectorcall { |
|
0 commit comments