17#include "mlir/Dialect/Func/IR/FuncOps.h"
18#include "mlir/IR/PatternMatch.h"
19#include "mlir/Interfaces/FunctionImplementation.h"
20#include "llvm/ADT/MapVector.h"
26ParseResult DPIFuncOp::parse(OpAsmParser &parser, OperationState &result) {
27 auto builder = parser.getBuilder();
29 (void)mlir::impl::parseOptionalVisibilityKeyword(parser, result.attributes);
33 if (parser.parseSymbolName(nameAttr, SymbolTable::getSymbolAttrName(),
37 SmallVector<hw::module_like_impl::PortParse> ports;
43 result.addAttribute(DPIFuncOp::getModuleTypeAttrName(result.name), modType);
47 auto unknownLoc = builder.getUnknownLoc();
48 SmallVector<Attribute> attrs, locs;
49 auto nonEmptyLocsFn = [unknownLoc](Attribute attr) {
50 return attr && cast<Location>(attr) != unknownLoc;
53 for (
auto &port : ports) {
54 attrs.push_back(port.attrs ? port.attrs : builder.getDictionaryAttr({}));
55 locs.push_back(port.sourceLoc ? Location(*port.sourceLoc) : unknownLoc);
58 result.addAttribute(DPIFuncOp::getPerArgumentAttrsAttrName(result.name),
59 builder.getArrayAttr(attrs));
62 if (llvm::any_of(locs, nonEmptyLocsFn))
63 result.addAttribute(DPIFuncOp::getArgumentLocsAttrName(result.name),
64 builder.getArrayAttr(locs));
67 if (failed(parser.parseOptionalAttrDictWithKeyword(result.attributes)))
74sim::DPICallOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
76 symbolTable.lookupNearestSymbolFrom(*
this, getCalleeAttr());
78 return emitError(
"cannot find function declaration '")
79 << getCallee() <<
"'";
80 if (isa<func::FuncOp, sim::DPIFuncOp>(referencedOp))
82 return emitError(
"callee must be 'sim.dpi.func' or 'func.func' but got '")
83 << referencedOp->getName() <<
"'";
86void DPIFuncOp::print(OpAsmPrinter &p) {
90 op->getAttrOfType<StringAttr>(SymbolTable::getSymbolAttrName())
94 StringRef visibilityAttrName = SymbolTable::getVisibilityAttrName();
95 if (
auto visibility = op->getAttrOfType<StringAttr>(visibilityAttrName))
96 p << visibility.getValue() <<
' ';
97 p.printSymbolName(funcName);
99 p, op->getRegion(0), op.getModuleType(),
100 getPerArgumentAttrsAttr()
101 ? ArrayRef<Attribute>(getPerArgumentAttrsAttr().getValue())
102 : ArrayRef<Attribute>{},
103 getArgumentLocs() ? SmallVector<Location>(
104 getArgumentLocs().value().getAsRange<Location>())
105 : ArrayRef<Location>{});
107 mlir::function_interface_impl::printFunctionAttributes(
109 {visibilityAttrName, getModuleTypeAttrName(),
110 getPerArgumentAttrsAttrName(), getArgumentLocsAttrName()});
113OpFoldResult FormatLiteralOp::fold(FoldAdaptor adaptor) {
114 return getLiteralAttr();
117OpFoldResult FormatDecOp::fold(FoldAdaptor adaptor) {
118 if (getValue().getType() == IntegerType::get(getContext(), 0U))
119 return StringAttr::get(getContext(),
"0");
121 if (
auto intAttr = llvm::dyn_cast_or_null<IntegerAttr>(adaptor.getValue())) {
122 SmallVector<char, 16> strBuf;
123 intAttr.getValue().toString(strBuf, 10U, getIsSigned());
125 unsigned width = intAttr.getType().getIntOrFloatBitWidth();
126 unsigned padWidth = FormatDecOp::getDecimalWidth(width, getIsSigned());
127 padWidth = padWidth > strBuf.size() ? padWidth - strBuf.size() : 0;
129 SmallVector<char, 8> padding(padWidth,
' ');
130 return StringAttr::get(getContext(), Twine(padding) + Twine(strBuf));
135OpFoldResult FormatHexOp::fold(FoldAdaptor adaptor) {
136 if (getValue().getType() == IntegerType::get(getContext(), 0U))
137 return StringAttr::get(getContext(),
"");
139 if (
auto intAttr = llvm::dyn_cast_or_null<IntegerAttr>(adaptor.getValue())) {
140 SmallVector<char, 8> strBuf;
141 intAttr.getValue().toString(strBuf, 16U,
false,
145 unsigned width = intAttr.getType().getIntOrFloatBitWidth();
146 unsigned padWidth = width / 4;
149 padWidth = padWidth > strBuf.size() ? padWidth - strBuf.size() : 0;
151 SmallVector<char, 8> padding(padWidth,
'0');
152 return StringAttr::get(getContext(), Twine(padding) + Twine(strBuf));
157OpFoldResult FormatOctOp::fold(FoldAdaptor adaptor) {
158 if (getValue().getType() == IntegerType::get(getContext(), 0U))
159 return StringAttr::get(getContext(),
"");
161 if (
auto intAttr = llvm::dyn_cast_or_null<IntegerAttr>(adaptor.getValue())) {
162 SmallVector<char, 11> strBuf;
163 intAttr.getValue().toString(strBuf, 8U,
false,
167 unsigned width = intAttr.getType().getIntOrFloatBitWidth();
168 unsigned padWidth = width / 3;
171 padWidth = padWidth > strBuf.size() ? padWidth - strBuf.size() : 0;
173 SmallVector<char, 11> padding(padWidth,
'0');
174 return StringAttr::get(getContext(), Twine(padding) + Twine(strBuf));
179OpFoldResult FormatBinOp::fold(FoldAdaptor adaptor) {
180 if (getValue().getType() == IntegerType::get(getContext(), 0U))
181 return StringAttr::get(getContext(),
"");
183 if (
auto intAttr = llvm::dyn_cast_or_null<IntegerAttr>(adaptor.getValue())) {
184 SmallVector<char, 32> strBuf;
185 intAttr.getValue().toString(strBuf, 2U,
false);
187 unsigned width = intAttr.getType().getIntOrFloatBitWidth();
188 unsigned padWidth = width > strBuf.size() ? width - strBuf.size() : 0;
190 SmallVector<char, 32> padding(padWidth,
'0');
191 return StringAttr::get(getContext(), Twine(padding) + Twine(strBuf));
196OpFoldResult FormatCharOp::fold(FoldAdaptor adaptor) {
197 auto width = getValue().getType().getIntOrFloatBitWidth();
201 return StringAttr::get(getContext(), Twine(
static_cast<char>(0)));
203 if (
auto intAttr = llvm::dyn_cast_or_null<IntegerAttr>(adaptor.getValue())) {
204 auto intValue = intAttr.getValue().getZExtValue();
205 return StringAttr::get(getContext(), Twine(
static_cast<char>(intValue)));
212 assert(!lits.empty() &&
"No literals to concatenate");
213 if (lits.size() == 1)
214 return StringAttr::get(ctxt, lits.front());
215 SmallString<64> newLit;
216 for (
auto lit : lits)
218 return StringAttr::get(ctxt, newLit);
221OpFoldResult FormatStringConcatOp::fold(FoldAdaptor adaptor) {
222 if (getNumOperands() == 0)
223 return StringAttr::get(getContext(),
"");
224 if (getNumOperands() == 1) {
226 if (getResult() == getOperand(0))
228 return getOperand(0);
232 SmallVector<StringRef> lits;
233 for (
auto attr : adaptor.getInputs()) {
234 auto lit = dyn_cast_or_null<StringAttr>(attr);
242LogicalResult FormatStringConcatOp::getFlattenedInputs(
243 llvm::SmallVectorImpl<Value> &flatOperands) {
244 llvm::SmallMapVector<FormatStringConcatOp, unsigned, 4> concatStack;
245 bool isCyclic =
false;
249 concatStack.insert({*
this, 0});
250 while (!concatStack.empty()) {
251 auto &top = concatStack.back();
252 auto currentConcat = top.first;
253 unsigned operandIndex = top.second;
256 while (operandIndex < currentConcat.getNumOperands()) {
257 auto currentOperand = currentConcat.getOperand(operandIndex);
259 if (
auto nextConcat =
260 currentOperand.getDefiningOp<FormatStringConcatOp>()) {
262 if (!concatStack.contains(nextConcat)) {
265 top.second = operandIndex + 1;
266 concatStack.insert({nextConcat, 0});
273 flatOperands.push_back(currentOperand);
278 if (operandIndex >= currentConcat.getNumOperands())
279 concatStack.pop_back();
282 return success(!isCyclic);
285LogicalResult FormatStringConcatOp::verify() {
286 if (llvm::any_of(getOperands(),
287 [&](Value operand) {
return operand == getResult(); }))
288 return emitOpError(
"is infinitely recursive.");
292LogicalResult FormatStringConcatOp::canonicalize(FormatStringConcatOp op,
293 PatternRewriter &rewriter) {
295 auto fmtStrType = FormatStringType::get(op.getContext());
298 bool hasBeenFlattened =
false;
299 SmallVector<Value, 0> flatOperands;
302 flatOperands.reserve(op.getNumOperands() + 4);
303 auto isAcyclic = op.getFlattenedInputs(flatOperands);
305 if (failed(isAcyclic)) {
308 op.emitWarning(
"Cyclic concatenation detected.");
312 hasBeenFlattened =
true;
315 if (!hasBeenFlattened && op.getNumOperands() < 2)
320 SmallVector<StringRef> litSequence;
321 SmallVector<Value> newOperands;
322 newOperands.reserve(op.getNumOperands());
323 FormatLiteralOp prevLitOp;
325 auto oldOperands = hasBeenFlattened ? flatOperands : op.getOperands();
326 for (
auto operand : oldOperands) {
327 if (
auto litOp = operand.getDefiningOp<FormatLiteralOp>()) {
328 if (!litOp.getLiteral().empty()) {
330 litSequence.push_back(litOp.getLiteral());
333 if (!litSequence.empty()) {
334 if (litSequence.size() > 1) {
336 auto newLit = rewriter.createOrFold<FormatLiteralOp>(
337 op.getLoc(), fmtStrType,
339 newOperands.push_back(newLit);
342 newOperands.push_back(prevLitOp.getResult());
346 newOperands.push_back(operand);
351 if (!litSequence.empty()) {
352 if (litSequence.size() > 1) {
354 auto newLit = rewriter.createOrFold<FormatLiteralOp>(
355 op.getLoc(), fmtStrType,
357 newOperands.push_back(newLit);
360 newOperands.push_back(prevLitOp.getResult());
364 if (!hasBeenFlattened && newOperands.size() == op.getNumOperands())
367 if (newOperands.empty())
368 rewriter.replaceOpWithNewOp<FormatLiteralOp>(op, fmtStrType,
369 rewriter.getStringAttr(
""));
370 else if (newOperands.size() == 1)
371 rewriter.replaceOp(op, newOperands);
373 rewriter.modifyOpInPlace(op, [&]() { op->setOperands(newOperands); });
378LogicalResult PrintFormattedOp::canonicalize(PrintFormattedOp op,
379 PatternRewriter &rewriter) {
381 if (
auto cstCond = op.getCondition().getDefiningOp<
hw::ConstantOp>()) {
382 if (cstCond.getValue().isZero()) {
383 rewriter.eraseOp(op);
390LogicalResult PrintFormattedProcOp::verify() {
392 auto *parentOp = getOperation()->getParentOp();
395 return emitOpError(
"must be within a procedural region.");
397 if (isa_and_nonnull<hw::HWDialect>(parentOp->getDialect())) {
398 if (!isa<hw::TriggeredOp>(parentOp))
399 return emitOpError(
"must be within a procedural region.");
403 if (isa_and_nonnull<sv::SVDialect>(parentOp->getDialect())) {
405 return emitOpError(
"must be within a procedural region.");
413LogicalResult PrintFormattedProcOp::canonicalize(PrintFormattedProcOp op,
414 PatternRewriter &rewriter) {
416 if (
auto litInput = op.getInput().getDefiningOp<FormatLiteralOp>()) {
417 if (litInput.getLiteral().empty()) {
418 rewriter.eraseOp(op);
430#define GET_OP_CLASSES
431#include "circt/Dialect/Sim/Sim.cpp.inc"
assert(baseType &&"element must be base type")
static StringAttr concatLiterals(MLIRContext *ctxt, ArrayRef< StringRef > lits)
Signals that an operations regions are procedural.
ParseResult parseModuleSignature(OpAsmParser &parser, SmallVectorImpl< PortParse > &args, TypeAttr &modType)
New Style parsing.
void printModuleSignatureNew(OpAsmPrinter &p, Region &body, hw::ModuleType modType, ArrayRef< Attribute > portAttrs, ArrayRef< Location > locAttrs)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.