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// ImmediateType
178//===----------------------------------------------------------------------===//
179
180bool rtgTypeIsAImmediate(MlirType type) {
181 return isa<ImmediateType>(unwrap(type));
182}
183
184MlirType rtgImmediateTypeGet(MlirContext ctx, uint32_t width) {
185 return wrap(ImmediateType::get(unwrap(ctx), width));
186}
187
188uint32_t rtgImmediateTypeGetWidth(MlirType type) {
189 return cast<ImmediateType>(unwrap(type)).getWidth();
190}
191
192// MemoryType
193//===----------------------------------------------------------------------===//
194
195bool rtgTypeIsAMemory(MlirType type) { return isa<MemoryType>(unwrap(type)); }
196
197MlirType rtgMemoryTypeGet(MlirContext ctxt, uint32_t addressWidth) {
198 return wrap(MemoryType::get(unwrap(ctxt), addressWidth));
199}
200
201uint32_t rtgMemoryTypeGetAddressWidth(MlirType type) {
202 return cast<MemoryType>(unwrap(type)).getAddressWidth();
203}
204
205// MemoryBlockType
206//===----------------------------------------------------------------------===//
207
208bool rtgTypeIsAMemoryBlock(MlirType type) {
209 return isa<MemoryBlockType>(unwrap(type));
210}
211
212MlirType rtgMemoryBlockTypeGet(MlirContext ctxt, uint32_t addressWidth) {
213 return wrap(MemoryBlockType::get(unwrap(ctxt), addressWidth));
214}
215
216uint32_t rtgMemoryBlockTypeGetAddressWidth(MlirType type) {
217 return cast<MemoryBlockType>(unwrap(type)).getAddressWidth();
218}
219
220// StringType
221//===----------------------------------------------------------------------===//
222
223bool rtgTypeIsAString(MlirType type) { return isa<StringType>(unwrap(type)); }
224
225MlirType rtgStringTypeGet(MlirContext ctxt) {
226 return wrap(StringType::get(unwrap(ctxt)));
227}
228
229//===----------------------------------------------------------------------===//
230// Attribute API.
231//===----------------------------------------------------------------------===//
232
233// DefaultContext
234//===----------------------------------------------------------------------===//
235
236bool rtgAttrIsADefaultContextAttr(MlirAttribute attr) {
237 return isa<DefaultContextAttr>(unwrap(attr));
238}
239
240MlirAttribute rtgDefaultContextAttrGet(MlirContext ctxt, MlirType type) {
241 return wrap(DefaultContextAttr::get(unwrap(ctxt), unwrap(type)));
242}
243
244// Label Visibility
245//===----------------------------------------------------------------------===//
246
247bool rtgAttrIsALabelVisibilityAttr(MlirAttribute attr) {
248 return isa<LabelVisibilityAttr>(unwrap(attr));
249}
250
252 auto convert = [](LabelVisibility visibility) {
253 switch (visibility) {
254 case LabelVisibility::local:
256 case LabelVisibility::global:
258 case LabelVisibility::external:
260 }
261 };
262 return convert(cast<LabelVisibilityAttr>(unwrap(attr)).getValue());
263}
264
265MlirAttribute rtgLabelVisibilityAttrGet(MlirContext ctxt,
266 RTGLabelVisibility visibility) {
267 auto convert = [](RTGLabelVisibility visibility) {
268 switch (visibility) {
270 return LabelVisibility::local;
272 return LabelVisibility::global;
274 return LabelVisibility::external;
275 }
276 };
277 return wrap(LabelVisibilityAttr::get(unwrap(ctxt), convert(visibility)));
278}
279
280// ImmediateAttr
281//===----------------------------------------------------------------------===//
282
283bool rtgAttrIsAImmediate(MlirAttribute attr) {
284 return isa<ImmediateAttr>(unwrap(attr));
285}
286
287MlirAttribute rtgImmediateAttrGet(MlirContext ctx, uint32_t width,
288 uint64_t value) {
289 return wrap(rtg::ImmediateAttr::get(unwrap(ctx), APInt(width, value)));
290}
291
292uint32_t rtgImmediateAttrGetWidth(MlirAttribute attr) {
293 return cast<ImmediateAttr>(unwrap(attr)).getValue().getBitWidth();
294}
295
296uint64_t rtgImmediateAttrGetValue(MlirAttribute attr) {
297 return cast<ImmediateAttr>(unwrap(attr)).getValue().getZExtValue();
298}
299
300// AnyContexts
301//===----------------------------------------------------------------------===//
302
303bool rtgAttrIsAAnyContextAttr(MlirAttribute attr) {
304 return isa<AnyContextAttr>(unwrap(attr));
305}
306
307MlirAttribute rtgAnyContextAttrGet(MlirContext ctxt, MlirType type) {
308 return wrap(AnyContextAttr::get(unwrap(ctxt), unwrap(type)));
309}
310
311// VirtualRegConfigAttr
312//===----------------------------------------------------------------------===//
313
314bool rtgAttrIsAVirtualRegisterConfig(MlirAttribute attr) {
315 return isa<VirtualRegisterConfigAttr>(unwrap(attr));
316}
317
318MlirAttribute
319rtgVirtualRegisterConfigAttrGet(MlirContext ctxt, intptr_t numRegs,
320 MlirAttribute const *allowedRegs) {
321 SmallVector<rtg::RegisterAttrInterface> regs;
322 for (intptr_t i = 0; i < numRegs; ++i)
323 regs.push_back(cast<rtg::RegisterAttrInterface>(unwrap(allowedRegs[i])));
324 return wrap(VirtualRegisterConfigAttr::get(unwrap(ctxt), regs));
325}
326
328 return cast<VirtualRegisterConfigAttr>(unwrap(attr)).getAllowedRegs().size();
329}
330
331MlirAttribute rtgVirtualRegisterConfigAttrGetRegister(MlirAttribute attr,
332 intptr_t index) {
333 auto allowedRegs =
334 cast<VirtualRegisterConfigAttr>(unwrap(attr)).getAllowedRegs();
335 return wrap(allowedRegs[index]);
336}
337
338// LabelAttr
339//===----------------------------------------------------------------------===//
340
341bool rtgAttrIsALabel(MlirAttribute attr) {
342 return isa<LabelAttr>(unwrap(attr));
343}
344
345MlirAttribute rtgLabelAttrGet(MlirContext ctx, MlirStringRef name) {
346 return wrap(LabelAttr::get(unwrap(ctx), unwrap(name)));
347}
348
349MlirStringRef rtgLabelAttrGetName(MlirAttribute attr) {
350 return wrap(cast<LabelAttr>(unwrap(attr)).getName());
351}
352
353// MapAttr
354//===----------------------------------------------------------------------===//
355
356bool rtgAttrIsAMap(MlirAttribute attr) {
357 return isa<rtg::MapAttr>(unwrap(attr));
358}
359
360MlirAttribute rtgMapAttrGet(MlirContext ctx, MlirType mapType,
361 intptr_t numEntries, MlirAttribute const *keys,
362 MlirAttribute const *values) {
363 DenseMap<TypedAttr, TypedAttr> entries;
364 for (unsigned i = 0; i < numEntries; ++i) {
365 entries.insert(
366 {cast<TypedAttr>(unwrap(keys[i])), cast<TypedAttr>(unwrap(values[i]))});
367 }
368 return wrap(rtg::MapAttr::get(cast<rtg::MapType>(unwrap(mapType)), &entries));
369}
370
371MlirAttribute rtgMapAttrLookup(MlirAttribute attr, MlirAttribute key) {
372 auto mapAttr = cast<rtg::MapAttr>(unwrap(attr));
373 auto keyAttr = cast<TypedAttr>(unwrap(key));
374
375 auto it = mapAttr.getEntries()->find(keyAttr);
376 if (it == mapAttr.getEntries()->end())
377 return wrap(Attribute()); // Return null attribute if key not found
378
379 return wrap(it->second);
380}
381
382//===----------------------------------------------------------------------===//
383// Passes
384//===----------------------------------------------------------------------===//
385
386#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:307
bool rtgAttrIsAAnyContextAttr(MlirAttribute attr)
If the attribute is an RTG any context attribute.
Definition RTG.cpp:303
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:251
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:265
bool rtgAttrIsALabelVisibilityAttr(MlirAttribute attr)
If the attribute is an RTG label visibility.
Definition RTG.cpp:247
uint32_t rtgMemoryBlockTypeGetAddressWidth(MlirType type)
Returns the address with of an RTG memory block type.
Definition RTG.cpp:216
MlirType rtgImmediateTypeGet(MlirContext ctx, uint32_t width)
Creates an RTG immediate type in the context.
Definition RTG.cpp:184
bool rtgAttrIsAImmediate(MlirAttribute attr)
Checks if the attribute is an RTG immediate attribute.
Definition RTG.cpp:283
MlirType rtgSequenceTypeGet(MlirContext ctxt, intptr_t numElements, MlirType const *elementTypes)
Creates an RTG sequence type in the context.
Definition RTG.cpp:41
uint32_t rtgImmediateAttrGetWidth(MlirAttribute attr)
Returns the width of the RTG immediate attribute.
Definition RTG.cpp:292
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:197
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:195
MlirAttribute rtgVirtualRegisterConfigAttrGet(MlirContext ctxt, intptr_t numRegs, MlirAttribute const *allowedRegs)
Creates an RTG virtual register config attribute in the context.
Definition RTG.cpp:319
bool rtgAttrIsAVirtualRegisterConfig(MlirAttribute attr)
Checks if the attribute is an RTG virtual register config attribute.
Definition RTG.cpp:314
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:356
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:225
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:208
bool rtgTypeIsAString(MlirType type)
If the type is an RTG string.
Definition RTG.cpp:223
bool rtgAttrIsALabel(MlirAttribute attr)
Checks if the attribute is an RTG label attribute.
Definition RTG.cpp:341
MlirStringRef rtgLabelAttrGetName(MlirAttribute attr)
Returns the name of the RTG label attribute.
Definition RTG.cpp:349
MlirType rtgMapTypeGetKeyType(MlirType type)
Return the key type of the RTG map.
Definition RTG.cpp:146
MlirAttribute rtgImmediateAttrGet(MlirContext ctx, uint32_t width, uint64_t value)
Creates an RTG immediate attribute in the context with the given width and value.
Definition RTG.cpp:287
uint64_t rtgImmediateAttrGetValue(MlirAttribute attr)
Returns the value of the RTG immediate attribute.
Definition RTG.cpp:296
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
uint32_t rtgImmediateTypeGetWidth(MlirType type)
Returns the width of the RTG immediate type.
Definition RTG.cpp:188
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:331
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:371
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
bool rtgTypeIsAImmediate(MlirType type)
If the type is an RTG immediate.
Definition RTG.cpp:180
uint32_t rtgMemoryTypeGetAddressWidth(MlirType type)
Returns the address with of an RTG memory type.
Definition RTG.cpp:201
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:360
MlirType rtgMemoryBlockTypeGet(MlirContext ctxt, uint32_t addressWidth)
Creates an RTG memory block type in the context.
Definition RTG.cpp:212
MlirAttribute rtgDefaultContextAttrGet(MlirContext ctxt, MlirType type)
Creates an RTG default context attribute in the context.
Definition RTG.cpp:240
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:327
bool rtgAttrIsADefaultContextAttr(MlirAttribute attr)
If the attribute is an RTG default context.
Definition RTG.cpp:236
MlirAttribute rtgLabelAttrGet(MlirContext ctx, MlirStringRef name)
Creates an RTG label attribute in the context with the given name.
Definition RTG.cpp:345
RTGLabelVisibility
Definition RTG.h:159
@ RTG_LABEL_VISIBILITY_EXTERNAL
Definition RTG.h:162
@ RTG_LABEL_VISIBILITY_GLOBAL
Definition RTG.h:161
@ RTG_LABEL_VISIBILITY_LOCAL
Definition RTG.h:160
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