-
Notifications
You must be signed in to change notification settings - Fork 437
Expand file tree
/
Copy pathhw.py
More file actions
168 lines (137 loc) · 5.79 KB
/
hw.py
File metadata and controls
168 lines (137 loc) · 5.79 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# REQUIRES: bindings_python
# RUN: %PYTHON% %s | FileCheck %s
import circt
from circt.dialects import hw
from circt.support import attribute_to_var
from circt.ir import (Context, Location, InsertionPoint, IntegerType,
IntegerAttr, Module, StringAttr, TypeAttr)
with Context() as ctx, Location.unknown():
circt.register_dialects(ctx)
i1 = IntegerType.get_signless(1)
i2 = IntegerType.get_signless(2)
i32 = IntegerType.get_signless(32)
m = Module.create()
with InsertionPoint(m.body):
def build(module):
constI32 = hw.ConstantOp(IntegerAttr.get(i32, 1))
constI2 = hw.ConstantOp(IntegerAttr.get(i2, 1))
constI1 = hw.ConstantOp.create(i1, 1)
# CHECK: Argument 1 has a different element type (i32) than the element type of the array (i1)
try:
hw.ArrayCreateOp.create([constI1, constI32])
except TypeError as e:
print(e)
# CHECK: Cannot 'create' an array of length zero
try:
hw.ArrayCreateOp.create([])
except ValueError as e:
print(e)
# CHECK: %[[CONST:.+]] = hw.constant 1 : i32
# CHECK: [[ARRAY1:%.+]] = hw.array_create %[[CONST]], %[[CONST]], %[[CONST]] : i32
array1 = hw.ArrayCreateOp.create([constI32, constI32, constI32])
# CHECK: hw.array_get [[ARRAY1]][%c1_i2] : !hw.array<3xi32>
hw.ArrayGetOp.create(array1, constI2)
# CHECK: %c-2_i2 = hw.constant -2 : i2
# CHECK: hw.array_get [[ARRAY1]][%c-2_i2] : !hw.array<3xi32>
hw.ArrayGetOp.create(array1, 2)
# CHECK: [[ARRAY2:%.+]] = hw.array_create %[[CONST]] : i32
array2 = hw.ArrayCreateOp.create([constI32])
# CHECK: %c0_i0 = hw.constant 0 : i0
# CHECK: hw.array_get [[ARRAY2]][%c0_i0] : !hw.array<1xi32>
hw.ArrayGetOp.create(array2, 0)
array_type_alias = hw.TypeAliasType.get("arrayScope", "arrayAlias",
hw.ArrayType.get(i32, 1))
typed_array = hw.ArrayCreateOp.create([constI32], array_type_alias)
# CHECK: !hw.typealias<@arrayScope::@arrayAlias, !hw.array<1xi32>>
print(typed_array.result.type)
mismatch_type = hw.TypeAliasType.get("mismatchScope", "mismatchAlias",
hw.ArrayType.get(i1, 1))
try:
hw.ArrayCreateOp.create([constI32], mismatch_type)
assert False, "Expected TypeError not raised"
except TypeError as e:
assert str(e).startswith("result type:")
# CHECK: [[STRUCT1:%.+]] = hw.struct_create (%c1_i32, %true) : !hw.struct<a: i32, b: i1>
struct1 = hw.StructCreateOp.create([('a', constI32), ('b', constI1)])
# CHECK: hw.struct_extract [[STRUCT1]]["a"] : !hw.struct<a: i32, b: i1>
hw.StructExtractOp.create(struct1, 'a')
hw.HWModuleOp(name="test", body_builder=build)
print(m)
# CHECK: !hw.typealias<@myscope::@myname, i1>
# CHECK: i1
# CHECK: i1
# CHECK: myscope
# CHECK: myname
typeAlias = hw.TypeAliasType.get("myscope", "myname", i1)
print(typeAlias)
print(typeAlias.canonical_type)
print(typeAlias.inner_type)
print(typeAlias.scope)
print(typeAlias.name)
pdecl = hw.ParamDeclAttr.get("param1", i32, IntegerAttr.get(i32, 13))
# CHECK: #hw.param.decl<"param1": i32 = 13>
print(pdecl)
pdecl = hw.ParamDeclAttr.get_nodefault("param2", i32)
# CHECK: #hw.param.decl<"param2": i32>
print(pdecl)
# CHECK: #hw.param.decl.ref<"param2"> : i32
pdeclref = hw.ParamDeclRefAttr.get(ctx, "param2")
print(pdeclref)
# CHECK: !hw.int<#hw.param.decl.ref<"param2">>
pinttype = hw.ParamIntType.get_from_param(ctx, pdeclref)
print(pinttype)
pverbatim = hw.ParamVerbatimAttr.get(StringAttr.get("this is verbatim"))
# CHECK: #hw.param.verbatim<"this is verbatim">
print(pverbatim)
outfile = hw.OutputFileAttr.get_from_filename(StringAttr.get("file.txt"),
True, True)
assert outfile.filename == "file.txt"
print(outfile)
# CHECK: #hw.output_file<"file.txt", excludeFromFileList, includeReplicatedOps>
inner_sym = hw.InnerSymAttr.get(StringAttr.get("some_sym"))
# CHECK: #hw<innerSym@some_sym>
print(inner_sym)
# CHECK: "some_sym"
print(inner_sym.symName)
# CHECK: some_sym
print(attribute_to_var(inner_sym))
inner_ref = hw.InnerRefAttr.get(StringAttr.get("some_module"),
StringAttr.get("some_instance"))
# CHECK: #hw.innerNameRef<@some_module::@some_instance>
print(inner_ref)
# CHECK: "some_module"
print(inner_ref.module)
# CHECK: "some_instance"
print(inner_ref.name)
ports = [
hw.ModulePort(StringAttr.get("out"), i1, hw.ModulePortDirection.OUTPUT),
hw.ModulePort(StringAttr.get("in1"), i2, hw.ModulePortDirection.INPUT),
hw.ModulePort(StringAttr.get("in2"), i32, hw.ModulePortDirection.INPUT),
hw.ModulePort(StringAttr.get("in3"), i32, hw.ModulePortDirection.INOUT)
]
module_type = hw.ModuleType.get(ports)
# CHECK: !hw.modty<output out : i1, input in1 : i2, input in2 : i32, inout in3 : i32>
print(module_type)
# CHECK-NEXT: [IntegerType(i2), IntegerType(i32), InOutType(!hw.inout<i32>)]
print(module_type.input_types)
# CHECK-NEXT: [IntegerType(i1)]
print(module_type.output_types)
# CHECK-NEXT: ['in1', 'in2', 'in3']
print(module_type.input_names)
# CHECK-NEXT: ['out']
print(module_type.output_names)
# Test StructType
# CHECK: !hw.struct<>
print(hw.StructType.get([]))
# Test UnionType
union_type = hw.UnionType.get([('a', i32, 0), ('b', i1, 4), ('c', i2, 0)])
# CHECK: !hw.union<a: i32, b: i1 offset 4, c: i2>
print(union_type)
# CHECK-NEXT: i32
print(union_type.get_field('a'))
# CHECK-NEXT: i1
print(union_type.get_field('b'))
# CHECK-NEXT: i2
print(union_type.get_field('c'))
# CHECK-NEXT: [('a', IntegerType(i32), 0), ('b', IntegerType(i1), 4), ('c', IntegerType(i2), 0)]
print(union_type.get_fields())