Skip to content

Commit ac032e9

Browse files
committed
[PyRTG] Support integers
1 parent 53fdca4 commit ac032e9

File tree

9 files changed

+116
-5
lines changed

9 files changed

+116
-5
lines changed

frontends/PyRTG/src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ declare_mlir_python_sources(PyRTGSources
1515
SOURCES
1616
pyrtg/__init__.py
1717
pyrtg/core.py
18+
pyrtg/index.py
19+
pyrtg/integers.py
1820
pyrtg/labels.py
1921
pyrtg/rtg.py
2022
pyrtg/sets.py

frontends/PyRTG/src/pyrtg/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
from .tests import test
88
from .labels import Label
99
from .rtg import rtg
10+
from .index import index
1011
from .sets import Set
12+
from .integers import Integer

frontends/PyRTG/src/pyrtg/index.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2+
# See https://llvm.org/LICENSE.txt for license information.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
from .support import wrap_opviews_with_values
6+
from .circt.dialects import index
7+
8+
wrap_opviews_with_values(index, index.__name__)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2+
# See https://llvm.org/LICENSE.txt for license information.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
from __future__ import annotations
6+
7+
from .circt import ir
8+
from .core import Value
9+
from .index import index
10+
11+
import typing
12+
13+
14+
class Integer(Value):
15+
"""
16+
This represents an integer with the same number of bits as a 'size_t' in C.
17+
It is used to provide parameter values to meta-level constructs such as the
18+
multiple of an element in a Bag. These integers will be fully constant folded
19+
away during randomization.
20+
"""
21+
22+
def __init__(self, value: typing.Union[ir.Value, int]) -> Integer:
23+
"""
24+
Use this constructor to create an Integer from a builtin Python int.
25+
"""
26+
27+
self._value = value
28+
29+
def __add__(self, other: Integer) -> Integer:
30+
return index.AddOp(self._get_ssa_value(), other._get_ssa_value())
31+
32+
def __sub__(self, other: Integer) -> Integer:
33+
return index.SubOp(self._get_ssa_value(), other._get_ssa_value())
34+
35+
def __and__(self, other: Integer) -> Integer:
36+
return index.AndOp(self._get_ssa_value(), other._get_ssa_value())
37+
38+
def __or__(self, other: Integer) -> Integer:
39+
return index.OrOp(self._get_ssa_value(), other._get_ssa_value())
40+
41+
def __xor__(self, other: Integer) -> Integer:
42+
return index.XOrOp(self._get_ssa_value(), other._get_ssa_value())
43+
44+
def get_type(self) -> ir.Type:
45+
return ir.IndexType.get()
46+
47+
def _get_ssa_value(self) -> ir.Value:
48+
if isinstance(self._value, int):
49+
self = index.ConstantOp(self._value)
50+
51+
return self._value

frontends/PyRTG/src/pyrtg/labels.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
from .circt import ir
88
from .core import Value
99
from .rtg import rtg
10+
from .integers import Integer
11+
12+
from typing import Union
1013

1114

1215
class Label(Value):
@@ -22,23 +25,27 @@ class Label(Value):
2225
def __init__(self, value: ir.Value):
2326
self._value = value
2427

25-
def declare(string: str) -> Label:
28+
def declare(string: str, *args: Union[Integer, int]) -> Label:
2629
"""
2730
Declares a label with a fixed name. Labels returned by different calls to
2831
this function but with the same arguments refer to the same label.
2932
"""
3033

31-
return rtg.LabelDeclOp(string, [])
34+
return rtg.LabelDeclOp(
35+
string,
36+
[(arg if isinstance(arg, Integer) else Integer(arg)) for arg in args])
3237

33-
def declare_unique(string: str) -> Label:
38+
def declare_unique(string: str, *args: Union[Integer, int]) -> Label:
3439
"""
3540
Declares a unique label. This means, all usages of the value returned by this
3641
function will refer to the same label, but no other label declarations can
3742
conflict with this label, including labels returned by other calls to this
3843
function or fixed labels declared with 'declare_label'.
3944
"""
4045

41-
return rtg.LabelUniqueDeclOp(string, [])
46+
return rtg.LabelUniqueDeclOp(
47+
string,
48+
[(arg if isinstance(arg, Integer) else Integer(arg)) for arg in args])
4249

4350
def place(
4451
self,

frontends/PyRTG/src/pyrtg/support.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ def _FromCirctValue(value: ir.Value) -> Value:
1515
if isinstance(type, rtg.SetType):
1616
from .sets import Set
1717
return Set(value)
18+
if isinstance(type, ir.IndexType):
19+
from .integers import Integer
20+
return Integer(value)
1821
assert False, "Unsupported value"
1922

2023

frontends/PyRTG/test/basic.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# RUN: %rtgtool% %s --seed=0 --output-format=elaborated | FileCheck %s --check-prefix=ELABORATED
33
# RUN: %rtgtool% %s --seed=0 -o %t --output-format=asm && FileCheck %s --input-file=%t --check-prefix=ASM
44

5-
from pyrtg import test, rtg, Label, Set
5+
from pyrtg import test, rtg, Label, Set, Integer
66

77
# MLIR-LABEL: rtg.test @test0
88
# MLIR-NEXT: }
@@ -20,6 +20,8 @@ def test0():
2020

2121

2222
# MLIR-LABEL: rtg.test @test_labels
23+
# MLIR-NEXT: index.constant 5
24+
# MLIR-NEXT: index.constant 3
2325
# MLIR-NEXT: [[L0:%.+]] = rtg.label_decl "l0"
2426
# MLIR-NEXT: [[L1:%.+]] = rtg.label_unique_decl "l1"
2527
# MLIR-NEXT: [[L2:%.+]] = rtg.label_unique_decl "l1"
@@ -38,6 +40,11 @@ def test0():
3840
# MLIR-NEXT: [[RL1:%.+]] = rtg.set_select_random [[SET2_MINUS_SET0]] : !rtg.set<!rtg.label>
3941
# MLIR-NEXT: rtg.label local [[RL1]]
4042

43+
# MLIR-NEXT: rtg.label_decl "L_{{[{][{]0[}][}]}}", %idx5
44+
# MLIR-NEXT: rtg.label local
45+
# MLIR-NEXT: rtg.label_decl "L_{{[{][{]0[}][}]}}", %idx3
46+
# MLIR-NEXT: rtg.label local
47+
4148
# MLIR-NEXT: }
4249

4350
# ELABORATED-LABEL: rtg.test @test_labels
@@ -51,6 +58,11 @@ def test0():
5158
# ELABORATED-NEXT: rtg.label local [[L0]]
5259
# ELABORATED-NEXT: rtg.label local [[L2]]
5360

61+
# ELABORATED-NEXT: rtg.label_decl "L_5"
62+
# ELABORATED-NEXT: rtg.label local
63+
# ELABORATED-NEXT: rtg.label_decl "L_3"
64+
# ELABORATED-NEXT: rtg.label local
65+
5466
# ELABORATED-NEXT: }
5567

5668
# ASM-LABEL: Begin of test_labels
@@ -63,6 +75,9 @@ def test0():
6375
# ASM-NEXT: l0:
6476
# ASM-NEXT: l1_1:
6577

78+
# ASM-NEXT: L_5:
79+
# ASM-NEXT: L_3:
80+
6681
# ASM-EMPTY:
6782
# ASM: End of test_labels
6883

@@ -86,3 +101,11 @@ def test_labels():
86101
set2 -= set0
87102
rl1 = set2.get_random_and_exclude()
88103
rl1.place()
104+
105+
sub = Integer(1) - Integer(2)
106+
add = (sub & Integer(4) | Integer(3) ^ Integer(5))
107+
add += sub
108+
l3 = Label.declare(r"L_{{0}}", add)
109+
l3.place()
110+
l4 = Label.declare(r"L_{{0}}", 3)
111+
l4.place()

lib/Bindings/Python/CIRCTModule.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "circt-c/Dialect/Verif.h"
3434
#include "circt-c/ExportVerilog.h"
3535
#include "mlir-c/Bindings/Python/Interop.h"
36+
#include "mlir-c/Dialect/Index.h"
3637
#include "mlir-c/IR.h"
3738
#include "mlir-c/Transforms.h"
3839
#include "mlir/Bindings/Python/NanobindAdaptors.h"
@@ -101,6 +102,10 @@ NB_MODULE(_circt, m) {
101102
mlirDialectHandleRegisterDialect(hwarith, context);
102103
mlirDialectHandleLoadDialect(hwarith, context);
103104

105+
MlirDialectHandle index = mlirGetDialectHandle__index__();
106+
mlirDialectHandleRegisterDialect(index, context);
107+
mlirDialectHandleLoadDialect(index, context);
108+
104109
MlirDialectHandle om = mlirGetDialectHandle__om__();
105110
mlirDialectHandleRegisterDialect(om, context);
106111
mlirDialectHandleLoadDialect(om, context);

lib/Bindings/Python/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ set(PYTHON_BINDINGS_LINK_LIBS
5252
CIRCTCAPISV
5353
CIRCTCAPIVerif
5454
CIRCTCAPITransforms
55+
MLIRCAPIIndex
5556
# needed for mlirFrozenRewritePatternSetDestroy
5657
# but not the actual passes
5758
MLIRCAPITransforms
@@ -139,6 +140,15 @@ declare_mlir_dialect_python_bindings(
139140
dialects/hw.py
140141
DIALECT_NAME hw)
141142

143+
declare_mlir_dialect_python_bindings(
144+
ADD_TO_PARENT CIRCTBindingsPythonSources.Dialects
145+
ROOT_DIR "${MLIR_MAIN_SRC_DIR}/python/mlir"
146+
TD_FILE dialects/IndexOps.td
147+
SOURCES
148+
dialects/index.py
149+
DIALECT_NAME index
150+
GEN_ENUM_BINDINGS)
151+
142152
declare_mlir_dialect_python_bindings(
143153
ADD_TO_PARENT CIRCTBindingsPythonSources.Dialects
144154
ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}"

0 commit comments

Comments
 (0)