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 FormatLitOp::fold(FoldAdaptor adaptor) {
return getLiteralAttr(); }
115OpFoldResult FormatDecOp::fold(FoldAdaptor adaptor) {
116 if (getValue().getType() == IntegerType::get(getContext(), 0U))
117 return StringAttr::get(getContext(),
"0");
119 if (
auto intAttr = llvm::dyn_cast_or_null<IntegerAttr>(adaptor.getValue())) {
120 SmallVector<char, 16> strBuf;
121 intAttr.getValue().toString(strBuf, 10U, getIsSigned());
123 unsigned width = intAttr.getType().getIntOrFloatBitWidth();
124 unsigned padWidth = FormatDecOp::getDecimalWidth(width, getIsSigned());
125 padWidth = padWidth > strBuf.size() ? padWidth - strBuf.size() : 0;
127 SmallVector<char, 8> padding(padWidth,
' ');
128 return StringAttr::get(getContext(), Twine(padding) + Twine(strBuf));
133OpFoldResult FormatHexOp::fold(FoldAdaptor adaptor) {
134 if (getValue().getType() == IntegerType::get(getContext(), 0U))
135 return StringAttr::get(getContext(),
"");
137 if (
auto intAttr = llvm::dyn_cast_or_null<IntegerAttr>(adaptor.getValue())) {
138 SmallVector<char, 8> strBuf;
139 intAttr.getValue().toString(strBuf, 16U,
false,
143 unsigned width = intAttr.getType().getIntOrFloatBitWidth();
144 unsigned padWidth = width / 4;
147 padWidth = padWidth > strBuf.size() ? padWidth - strBuf.size() : 0;
149 SmallVector<char, 8> padding(padWidth,
'0');
150 return StringAttr::get(getContext(), Twine(padding) + Twine(strBuf));
155OpFoldResult FormatBinOp::fold(FoldAdaptor adaptor) {
156 if (getValue().getType() == IntegerType::get(getContext(), 0U))
157 return StringAttr::get(getContext(),
"");
159 if (
auto intAttr = llvm::dyn_cast_or_null<IntegerAttr>(adaptor.getValue())) {
160 SmallVector<char, 32> strBuf;
161 intAttr.getValue().toString(strBuf, 2U,
false);
163 unsigned width = intAttr.getType().getIntOrFloatBitWidth();
164 unsigned padWidth = width > strBuf.size() ? width - strBuf.size() : 0;
166 SmallVector<char, 32> padding(padWidth,
'0');
167 return StringAttr::get(getContext(), Twine(padding) + Twine(strBuf));
172OpFoldResult FormatCharOp::fold(FoldAdaptor adaptor) {
173 auto width = getValue().getType().getIntOrFloatBitWidth();
177 return StringAttr::get(getContext(), Twine(
static_cast<char>(0)));
179 if (
auto intAttr = llvm::dyn_cast_or_null<IntegerAttr>(adaptor.getValue())) {
180 auto intValue = intAttr.getValue().getZExtValue();
181 return StringAttr::get(getContext(), Twine(
static_cast<char>(intValue)));
188 assert(!lits.empty() &&
"No literals to concatenate");
189 if (lits.size() == 1)
190 return StringAttr::get(ctxt, lits.front());
191 SmallString<64> newLit;
192 for (
auto lit : lits)
194 return StringAttr::get(ctxt, newLit);
197OpFoldResult FormatStringConcatOp::fold(FoldAdaptor adaptor) {
198 if (getNumOperands() == 0)
199 return StringAttr::get(getContext(),
"");
200 if (getNumOperands() == 1) {
202 if (getResult() == getOperand(0))
204 return getOperand(0);
208 SmallVector<StringRef> lits;
209 for (
auto attr : adaptor.getInputs()) {
210 auto lit = dyn_cast_or_null<StringAttr>(attr);
218LogicalResult FormatStringConcatOp::getFlattenedInputs(
219 llvm::SmallVectorImpl<Value> &flatOperands) {
220 llvm::SmallMapVector<FormatStringConcatOp, unsigned, 4> concatStack;
221 bool isCyclic =
false;
225 concatStack.insert({*
this, 0});
226 while (!concatStack.empty()) {
227 auto &top = concatStack.back();
228 auto currentConcat = top.first;
229 unsigned operandIndex = top.second;
232 while (operandIndex < currentConcat.getNumOperands()) {
233 auto currentOperand = currentConcat.getOperand(operandIndex);
235 if (
auto nextConcat =
236 currentOperand.getDefiningOp<FormatStringConcatOp>()) {
238 if (!concatStack.contains(nextConcat)) {
241 top.second = operandIndex + 1;
242 concatStack.insert({nextConcat, 0});
249 flatOperands.push_back(currentOperand);
254 if (operandIndex >= currentConcat.getNumOperands())
255 concatStack.pop_back();
258 return success(!isCyclic);
261LogicalResult FormatStringConcatOp::verify() {
262 if (llvm::any_of(getOperands(),
263 [&](Value operand) {
return operand == getResult(); }))
264 return emitOpError(
"is infinitely recursive.");
268LogicalResult FormatStringConcatOp::canonicalize(FormatStringConcatOp op,
269 PatternRewriter &rewriter) {
271 auto fmtStrType = FormatStringType::get(op.getContext());
274 bool hasBeenFlattened =
false;
275 SmallVector<Value, 0> flatOperands;
278 flatOperands.reserve(op.getNumOperands() + 4);
279 auto isAcyclic = op.getFlattenedInputs(flatOperands);
281 if (failed(isAcyclic)) {
284 op.emitWarning(
"Cyclic concatenation detected.");
288 hasBeenFlattened =
true;
291 if (!hasBeenFlattened && op.getNumOperands() < 2)
296 SmallVector<StringRef> litSequence;
297 SmallVector<Value> newOperands;
298 newOperands.reserve(op.getNumOperands());
299 FormatLitOp prevLitOp;
301 auto oldOperands = hasBeenFlattened ? flatOperands : op.getOperands();
302 for (
auto operand : oldOperands) {
303 if (
auto litOp = operand.getDefiningOp<FormatLitOp>()) {
304 if (!litOp.getLiteral().empty()) {
306 litSequence.push_back(litOp.getLiteral());
309 if (!litSequence.empty()) {
310 if (litSequence.size() > 1) {
312 auto newLit = rewriter.createOrFold<FormatLitOp>(
313 op.getLoc(), fmtStrType,
315 newOperands.push_back(newLit);
318 newOperands.push_back(prevLitOp.getResult());
322 newOperands.push_back(operand);
327 if (!litSequence.empty()) {
328 if (litSequence.size() > 1) {
330 auto newLit = rewriter.createOrFold<FormatLitOp>(
331 op.getLoc(), fmtStrType,
333 newOperands.push_back(newLit);
336 newOperands.push_back(prevLitOp.getResult());
340 if (!hasBeenFlattened && newOperands.size() == op.getNumOperands())
343 if (newOperands.empty())
344 rewriter.replaceOpWithNewOp<FormatLitOp>(op, fmtStrType,
345 rewriter.getStringAttr(
""));
346 else if (newOperands.size() == 1)
347 rewriter.replaceOp(op, newOperands);
349 rewriter.modifyOpInPlace(op, [&]() { op->setOperands(newOperands); });
354LogicalResult PrintFormattedOp::canonicalize(PrintFormattedOp op,
355 PatternRewriter &rewriter) {
357 if (
auto cstCond = op.getCondition().getDefiningOp<
hw::ConstantOp>()) {
358 if (cstCond.getValue().isZero()) {
359 rewriter.eraseOp(op);
366LogicalResult PrintFormattedProcOp::verify() {
368 auto *parentOp = getOperation()->getParentOp();
371 return emitOpError(
"must be within a procedural region.");
373 if (isa_and_nonnull<hw::HWDialect>(parentOp->getDialect())) {
374 if (!isa<hw::TriggeredOp>(parentOp))
375 return emitOpError(
"must be within a procedural region.");
379 if (isa_and_nonnull<sv::SVDialect>(parentOp->getDialect())) {
381 return emitOpError(
"must be within a procedural region.");
389LogicalResult PrintFormattedProcOp::canonicalize(PrintFormattedProcOp op,
390 PatternRewriter &rewriter) {
392 if (
auto litInput = op.getInput().getDefiningOp<FormatLitOp>()) {
393 if (litInput.getLiteral().empty()) {
394 rewriter.eraseOp(op);
406#define GET_OP_CLASSES
407#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.