Skip to content

Arch

arch

UNKNOWN = X86Arch() module-attribute

AVX2 = AVX2Arch() module-attribute

AVX512 = AVX512Arch() module-attribute

X86Arch

Bases: Arch

Source code in xdsl/backend/x86/arch.py
 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
class X86Arch(Arch):
    VECTOR_TYPES_BY_BITWIDTH: ClassVar[dict[int, type[X86VectorRegisterType]]] = {
        128: SSERegisterType
    }
    """
    Supported vector type for a given vector size.
    """

    @staticmethod
    def name() -> str:
        return "unknown"

    @staticmethod
    def arch_for_name(name: str | None) -> X86Arch:
        if name is None:
            return UNKNOWN
        try:
            return _ARCH_BY_NAME[name]
        except KeyError:
            raise DiagnosticException(f"Unsupported arch {name}")

    def _register_type_for_vector_type(
        self, value_type: VectorType
    ) -> type[X86VectorRegisterType]:
        """
        Given any vector type, returns the appropriate register type.
        The vector type must fit exactly into a full bitwidth vector supported by the
        ISA, otherwise a `DiagnosticException` is raised.
        """
        vector_num_elements = value_type.element_count()
        element_type = cast(FixedBitwidthType, value_type.get_element_type())
        element_size = element_type.bitwidth
        vector_size = vector_num_elements * element_size
        try:
            return self.VECTOR_TYPES_BY_BITWIDTH[vector_size]
        except KeyError:
            raise DiagnosticException(
                f"The vector size ({vector_size} bits) and target architecture `{self.name()}` are inconsistent."
            )

    def _scalar_type_for_type(self, value_type: Attribute) -> type[GeneralRegisterType]:
        if isinstance(value_type, FixedBitwidthType):
            match value_type.bitwidth:
                case 64:
                    return Reg64Type
                case 32:
                    return Reg32Type
                case 16:
                    return Reg16Type
                case 8:
                    return Reg8Type
                case _:
                    ...
        if isinstance(value_type, IndexType) or isinstance(value_type, ptr.PtrType):
            return Reg64Type
        raise DiagnosticException(f"Register type for type {value_type} not supported.")

    @overload
    def register_type_for_type(
        self, value_type: VectorType
    ) -> type[X86VectorRegisterType]: ...

    @overload
    def register_type_for_type(
        self, value_type: Attribute
    ) -> type[X86RegisterType]: ...

    def register_type_for_type(self, value_type: Attribute) -> type[X86RegisterType]:
        if isinstance(value_type, X86RegisterType):
            return type(value_type)
        if isa(value_type, VectorType):
            return self._register_type_for_vector_type(value_type)
        return self._scalar_type_for_type(value_type)

    def cast_to_regs(
        self, values: Sequence[SSAValue], builder: Builder
    ) -> list[SSAValue]:
        return [
            builder.insert(
                asm.ToRegOp.get(v, self.register_type_for_type(v.type).unallocated())
            ).register
            for v in values
        ]

    def move_value_to_unallocated(
        self, value: SSAValue, value_type: Attribute, builder: Builder
    ) -> SSAValue:
        if isa(value_type, VectorType[FixedBitwidthType]):
            if not isinstance(reg_type := value.type, X86VectorRegisterType):
                raise ValueError(f"Invalid type for move {value_type}")
            # Choose the x86 vector instruction according to the
            # abstract vector element size
            match value_type.get_element_type().bitwidth:
                case 16:
                    raise DiagnosticException(
                        "Half-precision floating point vector move is not implemented yet."
                    )
                case 32:
                    raise DiagnosticException(
                        "Half-precision floating point vector move is not implemented yet."
                    )
                case 64:
                    mov_op = x86.ops.DS_VmovapdOp(
                        value, destination=type(reg_type).unallocated()
                    )
                case _:
                    raise DiagnosticException(
                        "Float precision must be half, single or double."
                    )
        else:
            if not isinstance(reg_type := value.type, GeneralRegisterType):
                raise ValueError(f"Invalid type for move {value_type}")
            mov_op = x86.DS_MovOp(value, destination=type(reg_type).unallocated())

        return builder.insert_op(mov_op).results[0]

VECTOR_TYPES_BY_BITWIDTH: dict[int, type[X86VectorRegisterType]] = {128: SSERegisterType} class-attribute

Supported vector type for a given vector size.

name() -> str staticmethod

Source code in xdsl/backend/x86/arch.py
39
40
41
@staticmethod
def name() -> str:
    return "unknown"

arch_for_name(name: str | None) -> X86Arch staticmethod

Source code in xdsl/backend/x86/arch.py
43
44
45
46
47
48
49
50
@staticmethod
def arch_for_name(name: str | None) -> X86Arch:
    if name is None:
        return UNKNOWN
    try:
        return _ARCH_BY_NAME[name]
    except KeyError:
        raise DiagnosticException(f"Unsupported arch {name}")

register_type_for_type(value_type: Attribute) -> type[X86RegisterType]

register_type_for_type(
    value_type: VectorType,
) -> type[X86VectorRegisterType]
register_type_for_type(
    value_type: Attribute,
) -> type[X86RegisterType]
Source code in xdsl/backend/x86/arch.py
 98
 99
100
101
102
103
def register_type_for_type(self, value_type: Attribute) -> type[X86RegisterType]:
    if isinstance(value_type, X86RegisterType):
        return type(value_type)
    if isa(value_type, VectorType):
        return self._register_type_for_vector_type(value_type)
    return self._scalar_type_for_type(value_type)

cast_to_regs(values: Sequence[SSAValue], builder: Builder) -> list[SSAValue]

Source code in xdsl/backend/x86/arch.py
105
106
107
108
109
110
111
112
113
def cast_to_regs(
    self, values: Sequence[SSAValue], builder: Builder
) -> list[SSAValue]:
    return [
        builder.insert(
            asm.ToRegOp.get(v, self.register_type_for_type(v.type).unallocated())
        ).register
        for v in values
    ]

move_value_to_unallocated(value: SSAValue, value_type: Attribute, builder: Builder) -> SSAValue

Source code in xdsl/backend/x86/arch.py
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
def move_value_to_unallocated(
    self, value: SSAValue, value_type: Attribute, builder: Builder
) -> SSAValue:
    if isa(value_type, VectorType[FixedBitwidthType]):
        if not isinstance(reg_type := value.type, X86VectorRegisterType):
            raise ValueError(f"Invalid type for move {value_type}")
        # Choose the x86 vector instruction according to the
        # abstract vector element size
        match value_type.get_element_type().bitwidth:
            case 16:
                raise DiagnosticException(
                    "Half-precision floating point vector move is not implemented yet."
                )
            case 32:
                raise DiagnosticException(
                    "Half-precision floating point vector move is not implemented yet."
                )
            case 64:
                mov_op = x86.ops.DS_VmovapdOp(
                    value, destination=type(reg_type).unallocated()
                )
            case _:
                raise DiagnosticException(
                    "Float precision must be half, single or double."
                )
    else:
        if not isinstance(reg_type := value.type, GeneralRegisterType):
            raise ValueError(f"Invalid type for move {value_type}")
        mov_op = x86.DS_MovOp(value, destination=type(reg_type).unallocated())

    return builder.insert_op(mov_op).results[0]

AVX2Arch

Bases: X86Arch

Source code in xdsl/backend/x86/arch.py
151
152
153
154
155
156
class AVX2Arch(X86Arch):
    @staticmethod
    def name() -> str:
        return "avx2"

    VECTOR_TYPES_BY_BITWIDTH = {128: SSERegisterType, 256: AVX2RegisterType}

VECTOR_TYPES_BY_BITWIDTH = {128: SSERegisterType, 256: AVX2RegisterType} class-attribute instance-attribute

name() -> str staticmethod

Source code in xdsl/backend/x86/arch.py
152
153
154
@staticmethod
def name() -> str:
    return "avx2"

AVX512Arch

Bases: X86Arch

Source code in xdsl/backend/x86/arch.py
162
163
164
165
166
167
168
169
170
171
class AVX512Arch(X86Arch):
    @staticmethod
    def name() -> str:
        return "avx512"

    VECTOR_TYPES_BY_BITWIDTH = {
        128: SSERegisterType,
        256: AVX2RegisterType,
        512: AVX512RegisterType,
    }

VECTOR_TYPES_BY_BITWIDTH = {128: SSERegisterType, 256: AVX2RegisterType, 512: AVX512RegisterType} class-attribute instance-attribute

name() -> str staticmethod

Source code in xdsl/backend/x86/arch.py
163
164
165
@staticmethod
def name() -> str:
    return "avx512"