CIRCT 23.0.0git
Loading...
Searching...
No Matches
RTG.cpp
Go to the documentation of this file.
1//===- RTG.cpp - C interface for the RTG dialect --------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
15
16#include "mlir/CAPI/Pass.h"
17#include "mlir/CAPI/Registration.h"
18
19using namespace circt;
20using namespace circt::rtg;
21
22//===----------------------------------------------------------------------===//
23// Dialect API.
24//===----------------------------------------------------------------------===//
25
27
29
30//===----------------------------------------------------------------------===//
31// Type API.
32//===----------------------------------------------------------------------===//
33
34// SequenceType
35//===----------------------------------------------------------------------===//
36
37bool rtgTypeIsASequence(MlirType type) {
38 return isa<SequenceType>(unwrap(type));
39}
40
41MlirType rtgSequenceTypeGet(MlirContext ctxt, intptr_t numElements,
42 MlirType const *elementTypes) {
43 SmallVector<Type> types;
44 for (unsigned i = 0; i < numElements; ++i)
45 types.emplace_back(unwrap(elementTypes[i]));
46 return wrap(SequenceType::get(unwrap(ctxt), types));
47}
48
49unsigned rtgSequenceTypeGetNumElements(MlirType type) {
50 return cast<SequenceType>(unwrap(type)).getElementTypes().size();
51}
52
53MlirType rtgSequenceTypeGetElement(MlirType type, unsigned i) {
54 return wrap(cast<SequenceType>(unwrap(type)).getElementTypes()[i]);
55}
56
57// RandomizedSequenceType
58//===----------------------------------------------------------------------===//
59
60bool rtgTypeIsARandomizedSequence(MlirType type) {
61 return isa<RandomizedSequenceType>(unwrap(type));
62}
63
64MlirType rtgRandomizedSequenceTypeGet(MlirContext ctxt) {
65 return wrap(RandomizedSequenceType::get(unwrap(ctxt)));
66}
67
68// LabelType
69//===----------------------------------------------------------------------===//
70
71bool rtgTypeIsALabel(MlirType type) { return isa<LabelType>(unwrap(type)); }
72
73MlirType rtgLabelTypeGet(MlirContext ctxt) {
74 return wrap(LabelType::get(unwrap(ctxt)));
75}
76
77// SetType
78//===----------------------------------------------------------------------===//
79
80bool rtgTypeIsASet(MlirType type) { return isa<SetType>(unwrap(type)); }
81
82MlirType rtgSetTypeGet(MlirType elementType) {
83 auto ty = unwrap(elementType);
84 return wrap(SetType::get(ty.getContext(), ty));
85}
86
87MlirType rtgSetTypeGetElementType(MlirType type) {
88 return wrap(cast<SetType>(unwrap(type)).getElementType());
89}
90
91// BagType
92//===----------------------------------------------------------------------===//
93
94bool rtgTypeIsABag(MlirType type) { return isa<BagType>(unwrap(type)); }
95
96MlirType rtgBagTypeGet(MlirType elementType) {
97 auto ty = unwrap(elementType);
98 return wrap(BagType::get(ty.getContext(), ty));
99}
100
101MlirType rtgBagTypeGetElementType(MlirType type) {
102 return wrap(cast<BagType>(unwrap(type)).getElementType());
103}
104
105// DictType
106//===----------------------------------------------------------------------===//
107
108bool rtgTypeIsADict(MlirType type) { return isa<DictType>(unwrap(type)); }
109
110MlirType rtgDictTypeGet(MlirContext ctxt, intptr_t numEntries,
111 MlirAttribute const *entryNames,
112 MlirType const *entryTypes) {
113 SmallVector<DictEntry> entries;
114 for (unsigned i = 0; i < numEntries; ++i) {
115 DictEntry entry;
116 entry.name = cast<StringAttr>(unwrap(entryNames[i]));
117 entry.type = unwrap(entryTypes[i]);
118 entries.emplace_back(entry);
119 }
120 return wrap(DictType::get(unwrap(ctxt), entries));
121}
122
123// ArrayType
124//===----------------------------------------------------------------------===//
125
126MlirType rtgArrayTypeGet(MlirType elementType) {
127 return wrap(
128 ArrayType::get(unwrap(elementType).getContext(), unwrap(elementType)));
129}
130
131bool rtgTypeIsAArray(MlirType type) { return isa<ArrayType>(unwrap(type)); }
132
133MlirType rtgArrayTypeGetElementType(MlirType type) {
134 return wrap(cast<ArrayType>(unwrap(type)).getElementType());
135}
136
137// MapType
138//===----------------------------------------------------------------------===//
139
140bool rtgTypeIsAMap(MlirType type) { return isa<MapType>(unwrap(type)); }
141
142MlirType rtgMapTypeGet(MlirType keyType, MlirType valueType) {
143 return wrap(MapType::get(unwrap(keyType), unwrap(valueType)));
144}
145
146MlirType rtgMapTypeGetKeyType(MlirType type) {
147 return wrap(cast<MapType>(unwrap(type)).getKeyType());
148}
149
150MlirType rtgMapTypeGetValueType(MlirType type) {
151 return wrap(cast<MapType>(unwrap(type)).getValueType());
152}
153
154// TupleType
155//===----------------------------------------------------------------------===//
156
157MlirType rtgTupleTypeGet(MlirContext ctxt, intptr_t numFields,
158 MlirType const *fieldTypes) {
159 SmallVector<Type> types;
160 for (unsigned i = 0; i < numFields; ++i)
161 types.emplace_back(unwrap(fieldTypes[i]));
162 return wrap(rtg::TupleType::get(unwrap(ctxt), types));
163}
164
165bool rtgTypeIsATuple(MlirType type) {
166 return isa<rtg::TupleType>(unwrap(type));
167}
168
169intptr_t rtgTypeGetNumFields(MlirType type) {
170 return cast<rtg::TupleType>(unwrap(type)).getFieldTypes().size();
171}
172
173MlirType rtgTupleTypeGetFieldType(MlirType type, intptr_t idx) {
174 return wrap(cast<rtg::TupleType>(unwrap(type)).getFieldTypes()[idx]);
175}
176
177// MemoryType
178//===----------------------------------------------------------------------===//
179
180bool rtgTypeIsAMemory(MlirType type) { return isa<MemoryType>(unwrap(type)); }
181
182MlirType rtgMemoryTypeGet(MlirContext ctxt, uint32_t addressWidth) {
183 return wrap(MemoryType::get(unwrap(ctxt), addressWidth));
184}
185
186uint32_t rtgMemoryTypeGetAddressWidth(MlirType type) {
187 return cast<MemoryType>(unwrap(type)).getAddressWidth();
188}
189
190// MemoryBlockType
191//===----------------------------------------------------------------------===//
192
193bool rtgTypeIsAMemoryBlock(MlirType type) {
194 return isa<MemoryBlockType>(unwrap(type));
195}
196
197MlirType rtgMemoryBlockTypeGet(MlirContext ctxt, uint32_t addressWidth) {
198 return wrap(MemoryBlockType::get(unwrap(ctxt), addressWidth));
199}
200
201uint32_t rtgMemoryBlockTypeGetAddressWidth(MlirType type) {
202 return cast<MemoryBlockType>(unwrap(type)).getAddressWidth();
203}
204
205// StringType
206//===----------------------------------------------------------------------===//
207
208bool rtgTypeIsAString(MlirType type) { return isa<StringType>(unwrap(type)); }
209
210MlirType rtgStringTypeGet(MlirContext ctxt) {
211 return wrap(StringType::get(unwrap(ctxt)));
212}
213
214//===----------------------------------------------------------------------===//
215// Attribute API.
216//===----------------------------------------------------------------------===//
217
218// DefaultContext
219//===----------------------------------------------------------------------===//
220
221bool rtgAttrIsADefaultContextAttr(MlirAttribute attr) {
222 return isa<DefaultContextAttr>(unwrap(attr));
223}
224
225MlirAttribute rtgDefaultContextAttrGet(MlirContext ctxt, MlirType type) {
226 return wrap(DefaultContextAttr::get(unwrap(ctxt), unwrap(type)));
227}
228
229// Label Visibility
230//===----------------------------------------------------------------------===//
231
232bool rtgAttrIsALabelVisibilityAttr(MlirAttribute attr) {
233 return isa<LabelVisibilityAttr>(unwrap(attr));
234}
235
237 auto convert = [](LabelVisibility visibility) {
238 switch (visibility) {
239 case LabelVisibility::local:
241 case LabelVisibility::global:
243 case LabelVisibility::external:
245 }
246 };
247 return convert(cast<LabelVisibilityAttr>(unwrap(attr)).getValue());
248}
249
250MlirAttribute rtgLabelVisibilityAttrGet(MlirContext ctxt,
251 RTGLabelVisibility visibility) {
252 auto convert = [](RTGLabelVisibility visibility) {
253 switch (visibility) {
255 return LabelVisibility::local;
257 return LabelVisibility::global;
259 return LabelVisibility::external;
260 }
261 };
262 return wrap(LabelVisibilityAttr::get(unwrap(ctxt), convert(visibility)));
263}
264
265// AnyContexts
266//===----------------------------------------------------------------------===//
267
268bool rtgAttrIsAAnyContextAttr(MlirAttribute attr) {
269 return isa<AnyContextAttr>(unwrap(attr));
270}
271
272MlirAttribute rtgAnyContextAttrGet(MlirContext ctxt, MlirType type) {
273 return wrap(AnyContextAttr::get(unwrap(ctxt), unwrap(type)));
274}
275
276// VirtualRegConfigAttr
277//===----------------------------------------------------------------------===//
278
279bool rtgAttrIsAVirtualRegisterConfig(MlirAttribute attr) {
280 return isa<VirtualRegisterConfigAttr>(unwrap(attr));
281}
282
283MlirAttribute
284rtgVirtualRegisterConfigAttrGet(MlirContext ctxt, intptr_t numRegs,
285 MlirAttribute const *allowedRegs) {
286 SmallVector<rtg::RegisterAttrInterface> regs;
287 for (intptr_t i = 0; i < numRegs; ++i)
288 regs.push_back(cast<rtg::RegisterAttrInterface>(unwrap(allowedRegs[i])));
289 return wrap(VirtualRegisterConfigAttr::get(unwrap(ctxt), regs));
290}
291
293 return cast<VirtualRegisterConfigAttr>(unwrap(attr)).getAllowedRegs().size();
294}
295
296MlirAttribute rtgVirtualRegisterConfigAttrGetRegister(MlirAttribute attr,
297 intptr_t index) {
298 auto allowedRegs =
299 cast<VirtualRegisterConfigAttr>(unwrap(attr)).getAllowedRegs();
300 return wrap(allowedRegs[index]);
301}
302
303// LabelAttr
304//===----------------------------------------------------------------------===//
305
306bool rtgAttrIsALabel(MlirAttribute attr) {
307 return isa<LabelAttr>(unwrap(attr));
308}
309
310MlirAttribute rtgLabelAttrGet(MlirContext ctx, MlirStringRef name) {
311 return wrap(LabelAttr::get(unwrap(ctx), unwrap(name)));
312}
313
314MlirStringRef rtgLabelAttrGetName(MlirAttribute attr) {
315 return wrap(cast<LabelAttr>(unwrap(attr)).getName());
316}
317
318// MapAttr
319//===----------------------------------------------------------------------===//
320
321bool rtgAttrIsAMap(MlirAttribute attr) {
322 return isa<rtg::MapAttr>(unwrap(attr));
323}
324
325MlirAttribute rtgMapAttrGet(MlirContext ctx, MlirType mapType,
326 intptr_t numEntries, MlirAttribute const *keys,
327 MlirAttribute const *values) {
328 DenseMap<TypedAttr, TypedAttr> entries;
329 for (unsigned i = 0; i < numEntries; ++i) {
330 entries.insert(
331 {cast<TypedAttr>(unwrap(keys[i])), cast<TypedAttr>(unwrap(values[i]))});
332 }
333 return wrap(rtg::MapAttr::get(cast<rtg::MapType>(unwrap(mapType)), &entries));
334}
335
336MlirAttribute rtgMapAttrLookup(MlirAttribute attr, MlirAttribute key) {
337 auto mapAttr = cast<rtg::MapAttr>(unwrap(attr));
338 auto keyAttr = cast<TypedAttr>(unwrap(key));
339
340 auto it = mapAttr.getEntries()->find(keyAttr);
341 if (it == mapAttr.getEntries()->end())
342 return wrap(Attribute()); // Return null attribute if key not found
343
344 return wrap(it->second);
345}
346
347//===----------------------------------------------------------------------===//
348// Passes
349//===----------------------------------------------------------------------===//
350
351#include "circt/Dialect/RTG/Transforms/RTGPasses.capi.cpp.inc"
return wrap(CMemoryType::get(unwrap(ctx), baseType, numElements))
MlirType uint64_t numElements
Definition CHIRRTL.cpp:30
MlirType elementType
Definition CHIRRTL.cpp:29
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(CHIRRTL, chirrtl, circt::chirrtl::CHIRRTLDialect) MlirType chirrtlTypeGetCMemory(MlirContext ctx
static LogicalResult convert(arc::ExecuteOp op, arc::ExecuteOp::Adaptor adaptor, ConversionPatternRewriter &rewriter, const TypeConverter &converter)
static EvaluatorValuePtr unwrap(OMEvaluatorValue c)
Definition OM.cpp:111
bool rtgTypeIsABag(MlirType type)
If the type is an RTG bag.
Definition RTG.cpp:94
MlirAttribute rtgAnyContextAttrGet(MlirContext ctxt, MlirType type)
Creates an RTG any context attribute in the context.
Definition RTG.cpp:272
bool rtgAttrIsAAnyContextAttr(MlirAttribute attr)
If the attribute is an RTG any context attribute.
Definition RTG.cpp:268
MlirType rtgSequenceTypeGetElement(MlirType type, unsigned i)
The type of of the substitution element at the given index.
Definition RTG.cpp:53
MlirType rtgArrayTypeGet(MlirType elementType)
Creates an RTG array type in the context.
Definition RTG.cpp:126
intptr_t rtgTypeGetNumFields(MlirType type)
Returns the number of fields in the RTG tuple.
Definition RTG.cpp:169
bool rtgTypeIsARandomizedSequence(MlirType type)
If the type is an RTG randomized sequence.
Definition RTG.cpp:60
RTGLabelVisibility rtgLabelVisibilityAttrGetValue(MlirAttribute attr)
Get the RTG label visibility from the attribute.
Definition RTG.cpp:236
MlirType rtgSetTypeGet(MlirType elementType)
Creates an RTG set type in the context.
Definition RTG.cpp:82
MlirType rtgArrayTypeGetElementType(MlirType type)
Returns the element type of the RTG array.
Definition RTG.cpp:133
MlirAttribute rtgLabelVisibilityAttrGet(MlirContext ctxt, RTGLabelVisibility visibility)
Creates an RTG label visibility attribute in the context.
Definition RTG.cpp:250
bool rtgAttrIsALabelVisibilityAttr(MlirAttribute attr)
If the attribute is an RTG label visibility.
Definition RTG.cpp:232
uint32_t rtgMemoryBlockTypeGetAddressWidth(MlirType type)
Returns the address with of an RTG memory block type.
Definition RTG.cpp:201
MlirType rtgSequenceTypeGet(MlirContext ctxt, intptr_t numElements, MlirType const *elementTypes)
Creates an RTG sequence type in the context.
Definition RTG.cpp:41
bool rtgTypeIsAArray(MlirType type)
If the type is an RTG array.
Definition RTG.cpp:131
MlirType rtgMemoryTypeGet(MlirContext ctxt, uint32_t addressWidth)
Creates an RTG memory type in the context.
Definition RTG.cpp:182
bool rtgTypeIsASet(MlirType type)
If the type is an RTG set.
Definition RTG.cpp:80
bool rtgTypeIsAMemory(MlirType type)
If the type is an RTG memory.
Definition RTG.cpp:180
MlirAttribute rtgVirtualRegisterConfigAttrGet(MlirContext ctxt, intptr_t numRegs, MlirAttribute const *allowedRegs)
Creates an RTG virtual register config attribute in the context.
Definition RTG.cpp:284
bool rtgAttrIsAVirtualRegisterConfig(MlirAttribute attr)
Checks if the attribute is an RTG virtual register config attribute.
Definition RTG.cpp:279
bool rtgTypeIsADict(MlirType type)
If the type is an RTG dict.
Definition RTG.cpp:108
unsigned rtgSequenceTypeGetNumElements(MlirType type)
The number of substitution elements of the RTG sequence.
Definition RTG.cpp:49
bool rtgAttrIsAMap(MlirAttribute attr)
Checks if the attribute is an RTG map attribute.
Definition RTG.cpp:321
MlirType rtgRandomizedSequenceTypeGet(MlirContext ctxt)
Creates an RTG randomized sequence type in the context.
Definition RTG.cpp:64
MlirType rtgStringTypeGet(MlirContext ctxt)
Creates an RTG string type in the context.
Definition RTG.cpp:210
bool rtgTypeIsALabel(MlirType type)
If the type is an RTG label.
Definition RTG.cpp:71
void registerRTGPipelines()
Definition RTG.cpp:28
MlirType rtgMapTypeGet(MlirType keyType, MlirType valueType)
Creates an RTG map type in the context.
Definition RTG.cpp:142
MlirType rtgSetTypeGetElementType(MlirType type)
Return the element type of the RTG set.
Definition RTG.cpp:87
bool rtgTypeIsAMemoryBlock(MlirType type)
If the type is an RTG memory block.
Definition RTG.cpp:193
bool rtgTypeIsAString(MlirType type)
If the type is an RTG string.
Definition RTG.cpp:208
bool rtgAttrIsALabel(MlirAttribute attr)
Checks if the attribute is an RTG label attribute.
Definition RTG.cpp:306
MlirStringRef rtgLabelAttrGetName(MlirAttribute attr)
Returns the name of the RTG label attribute.
Definition RTG.cpp:314
MlirType rtgMapTypeGetKeyType(MlirType type)
Return the key type of the RTG map.
Definition RTG.cpp:146
MlirType rtgDictTypeGet(MlirContext ctxt, intptr_t numEntries, MlirAttribute const *entryNames, MlirType const *entryTypes)
Creates an RTG dict type in the context.
Definition RTG.cpp:110
bool rtgTypeIsASequence(MlirType type)
If the type is an RTG sequence.
Definition RTG.cpp:37
bool rtgTypeIsAMap(MlirType type)
If the type is an RTG map.
Definition RTG.cpp:140
MlirType rtgTupleTypeGetFieldType(MlirType type, intptr_t idx)
Returns a field type of the RTG tuple.
Definition RTG.cpp:173
MlirType rtgBagTypeGet(MlirType elementType)
Creates an RTG bag type in the context.
Definition RTG.cpp:96
MlirAttribute rtgVirtualRegisterConfigAttrGetRegister(MlirAttribute attr, intptr_t index)
Returns the allowed register at the given index in the RTG virtual register config attribute.
Definition RTG.cpp:296
MlirType rtgLabelTypeGet(MlirContext ctxt)
Creates an RTG mode type in the context.
Definition RTG.cpp:73
MlirAttribute rtgMapAttrLookup(MlirAttribute attr, MlirAttribute key)
Looks up the value associated with the given key in the RTG map attribute.
Definition RTG.cpp:336
MlirType rtgTupleTypeGet(MlirContext ctxt, intptr_t numFields, MlirType const *fieldTypes)
Creates an RTG tuple type in the context.
Definition RTG.cpp:157
MlirType rtgMapTypeGetValueType(MlirType type)
Return the value type of the RTG map.
Definition RTG.cpp:150
uint32_t rtgMemoryTypeGetAddressWidth(MlirType type)
Returns the address with of an RTG memory type.
Definition RTG.cpp:186
MlirAttribute rtgMapAttrGet(MlirContext ctx, MlirType mapType, intptr_t numEntries, MlirAttribute const *keys, MlirAttribute const *values)
Creates an RTG map attribute in the context with the given entries.
Definition RTG.cpp:325
MlirType rtgMemoryBlockTypeGet(MlirContext ctxt, uint32_t addressWidth)
Creates an RTG memory block type in the context.
Definition RTG.cpp:197
MlirAttribute rtgDefaultContextAttrGet(MlirContext ctxt, MlirType type)
Creates an RTG default context attribute in the context.
Definition RTG.cpp:225
bool rtgTypeIsATuple(MlirType type)
If the type is an RTG tuple.
Definition RTG.cpp:165
MlirType rtgBagTypeGetElementType(MlirType type)
Return the element type of the RTG bag.
Definition RTG.cpp:101
intptr_t rtgVirtualRegisterConfigAttrGetNumRegisters(MlirAttribute attr)
Returns the number of allowed registers in the RTG virtual register config attribute.
Definition RTG.cpp:292
bool rtgAttrIsADefaultContextAttr(MlirAttribute attr)
If the attribute is an RTG default context.
Definition RTG.cpp:221
MlirAttribute rtgLabelAttrGet(MlirContext ctx, MlirStringRef name)
Creates an RTG label attribute in the context with the given name.
Definition RTG.cpp:310
RTGLabelVisibility
Definition RTG.h:149
@ RTG_LABEL_VISIBILITY_EXTERNAL
Definition RTG.h:152
@ RTG_LABEL_VISIBILITY_GLOBAL
Definition RTG.h:151
@ RTG_LABEL_VISIBILITY_LOCAL
Definition RTG.h:150
void registerPipelines()
Registers all pipelines for the rtg dialect.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition rtg.py:1
Defines an entry in an !rtg.dict.
Definition RTGTypes.h:20
mlir::StringAttr name
Definition RTGTypes.h:21