CIRCT  20.0.0git
RTGOps.cpp
Go to the documentation of this file.
1 //===- RTGOps.cpp - Implement the RTG operations --------------------------===//
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 //
9 // This file implements the RTG ops.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "mlir/IR/Builders.h"
15 #include "mlir/IR/DialectImplementation.h"
16 
17 using namespace mlir;
18 using namespace circt;
19 using namespace rtg;
20 
21 //===----------------------------------------------------------------------===//
22 // SequenceClosureOp
23 //===----------------------------------------------------------------------===//
24 
25 LogicalResult
26 SequenceClosureOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
27  SequenceOp seq =
28  symbolTable.lookupNearestSymbolFrom<SequenceOp>(*this, getSequenceAttr());
29  if (!seq)
30  return emitOpError()
31  << "'" << getSequence()
32  << "' does not reference a valid 'rtg.sequence' operation";
33 
34  if (seq.getBodyRegion().getArgumentTypes() != getArgs().getTypes())
35  return emitOpError("referenced 'rtg.sequence' op's argument types must "
36  "match 'args' types");
37 
38  return success();
39 }
40 
41 //===----------------------------------------------------------------------===//
42 // SetCreateOp
43 //===----------------------------------------------------------------------===//
44 
45 ParseResult SetCreateOp::parse(OpAsmParser &parser, OperationState &result) {
46  llvm::SmallVector<OpAsmParser::UnresolvedOperand, 16> operands;
47  Type elemType;
48 
49  if (parser.parseOperandList(operands) ||
50  parser.parseOptionalAttrDict(result.attributes) || parser.parseColon() ||
51  parser.parseType(elemType))
52  return failure();
53 
54  result.addTypes({SetType::get(result.getContext(), elemType)});
55 
56  for (auto operand : operands)
57  if (parser.resolveOperand(operand, elemType, result.operands))
58  return failure();
59 
60  return success();
61 }
62 
63 void SetCreateOp::print(OpAsmPrinter &p) {
64  p << " ";
65  p.printOperands(getElements());
66  p.printOptionalAttrDict((*this)->getAttrs());
67  p << " : " << getSet().getType().getElementType();
68 }
69 
70 LogicalResult SetCreateOp::verify() {
71  if (getElements().size() > 0) {
72  // We only need to check the first element because of the `SameTypeOperands`
73  // trait.
74  if (getElements()[0].getType() != getSet().getType().getElementType())
75  return emitOpError() << "operand types must match set element type";
76  }
77 
78  return success();
79 }
80 
81 //===----------------------------------------------------------------------===//
82 // BagCreateOp
83 //===----------------------------------------------------------------------===//
84 
85 ParseResult BagCreateOp::parse(OpAsmParser &parser, OperationState &result) {
86  llvm::SmallVector<OpAsmParser::UnresolvedOperand, 16> elementOperands,
87  multipleOperands;
88  Type elemType;
89 
90  if (!parser.parseOptionalLParen()) {
91  while (true) {
92  OpAsmParser::UnresolvedOperand elementOperand, multipleOperand;
93  if (parser.parseOperand(multipleOperand) || parser.parseKeyword("x") ||
94  parser.parseOperand(elementOperand))
95  return failure();
96 
97  elementOperands.push_back(elementOperand);
98  multipleOperands.push_back(multipleOperand);
99 
100  if (parser.parseOptionalComma()) {
101  if (parser.parseRParen())
102  return failure();
103  break;
104  }
105  }
106  }
107 
108  if (parser.parseColon() || parser.parseType(elemType) ||
109  parser.parseOptionalAttrDict(result.attributes))
110  return failure();
111 
112  result.addTypes({BagType::get(result.getContext(), elemType)});
113 
114  for (auto operand : elementOperands)
115  if (parser.resolveOperand(operand, elemType, result.operands))
116  return failure();
117 
118  for (auto operand : multipleOperands)
119  if (parser.resolveOperand(operand, IndexType::get(result.getContext()),
120  result.operands))
121  return failure();
122 
123  return success();
124 }
125 
126 void BagCreateOp::print(OpAsmPrinter &p) {
127  p << " ";
128  if (!getElements().empty())
129  p << "(";
130  llvm::interleaveComma(llvm::zip(getElements(), getMultiples()), p,
131  [&](auto elAndMultiple) {
132  auto [el, multiple] = elAndMultiple;
133  p << multiple << " x " << el;
134  });
135  if (!getElements().empty())
136  p << ")";
137 
138  p << " : " << getBag().getType().getElementType();
139  p.printOptionalAttrDict((*this)->getAttrs());
140 }
141 
142 LogicalResult BagCreateOp::verify() {
143  if (!llvm::all_equal(getElements().getTypes()))
144  return emitOpError() << "types of all elements must match";
145 
146  if (getElements().size() > 0)
147  if (getElements()[0].getType() != getBag().getType().getElementType())
148  return emitOpError() << "operand types must match bag element type";
149 
150  return success();
151 }
152 
153 //===----------------------------------------------------------------------===//
154 // TestOp
155 //===----------------------------------------------------------------------===//
156 
157 LogicalResult TestOp::verifyRegions() {
158  if (!getTarget().entryTypesMatch(getBody()->getArgumentTypes()))
159  return emitOpError("argument types must match dict entry types");
160 
161  return success();
162 }
163 
164 //===----------------------------------------------------------------------===//
165 // TargetOp
166 //===----------------------------------------------------------------------===//
167 
168 LogicalResult TargetOp::verifyRegions() {
169  if (!getTarget().entryTypesMatch(
170  getBody()->getTerminator()->getOperandTypes()))
171  return emitOpError("terminator operand types must match dict entry types");
172 
173  return success();
174 }
175 
176 //===----------------------------------------------------------------------===//
177 // TableGen generated logic.
178 //===----------------------------------------------------------------------===//
179 
180 #define GET_OP_CLASSES
181 #include "circt/Dialect/RTG/IR/RTG.cpp.inc"
static InstancePath empty
static LogicalResult verify(Value clock, bool eventExists, mlir::Location loc)
Definition: SVOps.cpp:2467
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
Definition: CalyxOps.cpp:55
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21
Definition: rtg.py:1
Definition: seq.py:1