Welcome to mirror list, hosted at ThFree Co, Russian Federation.

VectorFieldLayoutAlgorithm.cs « Compiler « Common « tools « coreclr « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a4e49bde5fec5ff2a11343a651a52b9f14ad0e49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Internal.TypeSystem;

using Debug = System.Diagnostics.Debug;

namespace ILCompiler
{
    /// <summary>
    /// Represents an algorithm that computes field layout for intrinsic vector types (Vector64/Vector128/Vector256).
    /// </summary>
    public class VectorFieldLayoutAlgorithm : FieldLayoutAlgorithm
    {
        private readonly FieldLayoutAlgorithm _fallbackAlgorithm;
        private readonly bool _vectorAbiIsStable;

        public VectorFieldLayoutAlgorithm(FieldLayoutAlgorithm fallbackAlgorithm, bool vectorAbiIsStable = true)
        {
            _vectorAbiIsStable = vectorAbiIsStable;
            _fallbackAlgorithm = fallbackAlgorithm;
        }

        public override ComputedInstanceFieldLayout ComputeInstanceLayout(DefType defType, InstanceLayoutKind layoutKind)
        {
            Debug.Assert(IsVectorType(defType));

            LayoutInt alignment;

            string name = defType.Name;
            if (name == "Vector64`1")
            {
                alignment = new LayoutInt(8);
            }
            else if (name == "Vector128`1")
            {
                if (defType.Context.Target.Architecture == TargetArchitecture.ARM)
                {
                    // The Procedure Call Standard for ARM defaults to 8-byte alignment for __m128 
                    alignment = new LayoutInt(8);
                }
                else
                {
                    alignment = new LayoutInt(16);
                }
            }
            else
            {
                Debug.Assert(name == "Vector256`1");

                if (defType.Context.Target.Architecture == TargetArchitecture.ARM)
                {
                    // No such type exists for the Procedure Call Standard for ARM. We will default 
                    // to the same alignment as __m128, which is supported by the ABI.
                    alignment = new LayoutInt(8);
                }
                else if (defType.Context.Target.Architecture == TargetArchitecture.ARM64)
                {
                    // The Procedure Call Standard for ARM 64-bit (with SVE support) defaults to 
                    // 16-byte alignment for __m256.
                    alignment = new LayoutInt(16);
                }
                else
                {
                    alignment = new LayoutInt(32);
                }
            }

            ComputedInstanceFieldLayout layoutFromMetadata = _fallbackAlgorithm.ComputeInstanceLayout(defType, layoutKind);

            return new ComputedInstanceFieldLayout
            {
                ByteCountUnaligned = layoutFromMetadata.ByteCountUnaligned,
                ByteCountAlignment = layoutFromMetadata.ByteCountAlignment,
                FieldAlignment = alignment,
                FieldSize = layoutFromMetadata.FieldSize,
                Offsets = layoutFromMetadata.Offsets,
                LayoutAbiStable = _vectorAbiIsStable
            };
        }

        public override ComputedStaticFieldLayout ComputeStaticFieldLayout(DefType defType, StaticLayoutKind layoutKind)
        {
            return _fallbackAlgorithm.ComputeStaticFieldLayout(defType, layoutKind);
        }

        public override bool ComputeContainsGCPointers(DefType type)
        {
            Debug.Assert(!_fallbackAlgorithm.ComputeContainsGCPointers(type));
            return false;
        }

        public override bool ComputeIsUnsafeValueType(DefType type)
        {
            Debug.Assert(!_fallbackAlgorithm.ComputeIsUnsafeValueType(type));
            return false;
        }

        public override ValueTypeShapeCharacteristics ComputeValueTypeShapeCharacteristics(DefType type)
        {
            if (type.Context.Target.Architecture == TargetArchitecture.ARM64 &&
                type.Instantiation[0].IsPrimitiveNumeric)
            {
                return type.InstanceFieldSize.AsInt switch
                {
                    8 => ValueTypeShapeCharacteristics.Vector64Aggregate,
                    16 => ValueTypeShapeCharacteristics.Vector128Aggregate,
                    _ => ValueTypeShapeCharacteristics.None
                };
            }
            return ValueTypeShapeCharacteristics.None;
        }

        public static bool IsVectorType(DefType type)
        {
            return type.IsIntrinsic &&
                type.Namespace == "System.Runtime.Intrinsics" &&
                (type.Name == "Vector64`1" ||
                type.Name == "Vector128`1" ||
                type.Name == "Vector256`1");
        }
    }
}