[PyRTG] Add tuples (#8371)

This commit is contained in:
Martin Erhart 2025-04-28 15:59:08 +02:00 committed by GitHub
parent 420a0231be
commit a6e1c1afa9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 75 additions and 1 deletions

View File

@ -30,6 +30,7 @@ declare_mlir_python_sources(PyRTGSources
pyrtg/resources.py
pyrtg/rtgtest.py
pyrtg/scf.py
pyrtg/tuples.py
rtgtool/rtgtool.py
)

View File

@ -18,3 +18,4 @@ from .target import target, entry
from .resources import IntegerRegister, Immediate
from .arrays import Array
from .control_flow import If, Else, EndIf, For, Foreach
from .tuples import Tuple

View File

@ -40,6 +40,9 @@ def _FromCirctValue(value: ir.Value) -> Value:
if isinstance(type, rtg.ImmediateType):
from .resources import Immediate
return Immediate(type.width, value)
if isinstance(type, ir.TupleType):
from .tuples import Tuple
return Tuple(value)
assert False, "Unsupported value"

View File

@ -0,0 +1,53 @@
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
from __future__ import annotations
from .circt import ir
from .rtg import rtg
from .core import Value
class Tuple(Value):
"""
Represents a statically-typed immutable tuple. Each tuple has a fixed number
of elements of potentially different types.
"""
def __init__(self, value: ir.Value) -> Tuple:
"""
Intended for library internal usage only.
"""
self._value = value
def create(*elements: Value) -> Tuple:
"""
Create a tuple containing the provided values. At least one
element must be provided. Each element can be of a different type.
"""
assert len(elements) > 0, "at least one element must be present"
return rtg.TupleCreateOp(elements)
def __getitem__(self, i) -> Value:
"""
Access an element in the tuple at the specified index (read-only).
"""
assert isinstance(i, int), "index must be a python int"
return rtg.TupleExtractOp(self, i)
def _get_ssa_value(self) -> ir.Value:
return self._value
def get_type(self) -> ir.Type:
return self._value.type
def type(*args) -> ir.Type:
"""
Returns the tuple type for the given element types.
"""
return ir.TupleType.get_tuple(args)

View File

@ -2,7 +2,7 @@
# RUN: %rtgtool% %s --seed=0 --output-format=elaborated | FileCheck %s --check-prefix=ELABORATED
# RUN: %rtgtool% %s --seed=0 -o %t --output-format=asm && FileCheck %s --input-file=%t --check-prefix=ASM
from pyrtg import test, sequence, target, entry, rtg, Label, Set, Integer, Bag, rtgtest, Immediate, IntegerRegister, Array, Bool
from pyrtg import test, sequence, target, entry, rtg, Label, Set, Integer, Bag, rtgtest, Immediate, IntegerRegister, Array, Bool, Tuple
# MLIR-LABEL: rtg.target @Tgt0 : !rtg.dict<entry0: !rtg.set<index>>
# MLIR-NEXT: [[C0:%.+]] = index.constant 0
@ -388,3 +388,15 @@ def int_consumer(b):
@test(("a", Integer.type()), ("b", Integer.type()))
def test8_random_integer(a, b):
int_consumer(Integer.random(a, b))
# MLIR-LABEL: rtg.test @test9_tuples
# MLIR-NEXT: [[V0:%.+]] = rtg.tuple_create %a, %b : index, i1
# MLIR-NEXT: rtg.tuple_extract [[V0]] at 1 : tuple<index, i1>
@test(("a", Integer.type()), ("b", Bool.type()),
("tup", Tuple.type(Integer.type(), Bool.type())))
def test9_tuples(a, b, tup):
tup = Tuple.create(a, b)
consumer(tup[1])

View File

@ -97,6 +97,10 @@ def type_to_pytype(t) -> ir.Type:
return ir.NoneType(t)
except ValueError:
pass
try:
return ir.TupleType(t)
except ValueError:
pass
try:
return hw.ArrayType(t)
except ValueError: