19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/TypeSwitch.h"
22#include "llvm/Support/Casting.h"
27#include "mlir/Dialect/EmitC/IR/EmitCDialect.cpp.inc"
33void EmitCDialect::initialize() {
36#include "mlir/Dialect/EmitC/IR/EmitC.cpp.inc"
39#define GET_TYPEDEF_LIST
40#include "mlir/Dialect/EmitC/IR/EmitCTypes.cpp.inc"
43#define GET_ATTRDEF_LIST
44#include "mlir/Dialect/EmitC/IR/EmitCAttributes.cpp.inc"
53 return emitc::ConstantOp::create(builder, loc, type, value);
59 emitc::YieldOp::create(builder, loc);
63 if (llvm::isa<emitc::OpaqueType>(type))
65 if (
auto ptrType = llvm::dyn_cast<emitc::PointerType>(type))
67 if (
auto arrayType = llvm::dyn_cast<emitc::ArrayType>(type)) {
68 auto elemType = arrayType.getElementType();
69 return !llvm::isa<emitc::ArrayType>(elemType) &&
74 if (llvm::isa<IntegerType>(type))
76 if (llvm::isa<FloatType>(type))
78 if (
auto tensorType = llvm::dyn_cast<TensorType>(type)) {
79 if (!tensorType.hasStaticShape()) {
82 auto elemType = tensorType.getElementType();
83 if (llvm::isa<emitc::ArrayType>(elemType)) {
88 if (
auto tupleType = llvm::dyn_cast<TupleType>(type)) {
89 return llvm::all_of(tupleType.getTypes(), [](
Type type) {
90 return !llvm::isa<emitc::ArrayType>(type) && isSupportedEmitCType(type);
97 if (
auto intType = llvm::dyn_cast<IntegerType>(type)) {
98 switch (intType.getWidth()) {
113 return llvm::isa<IndexType, emitc::OpaqueType>(type) ||
118 if (
auto floatType = llvm::dyn_cast<FloatType>(type)) {
119 switch (floatType.getWidth()) {
121 return llvm::isa<Float16Type, BFloat16Type>(type);
133 return isa<emitc::SignedSizeTType, emitc::SizeTType, emitc::PtrDiffTType>(
140 isa<emitc::PointerType>(type);
147 assert(op->
getNumResults() == 1 &&
"operation must have 1 result");
149 if (llvm::isa<emitc::OpaqueAttr>(value))
152 if (llvm::isa<StringAttr>(value))
154 <<
"string attributes are not supported, use #emitc.opaque instead";
157 if (
auto lType = dyn_cast<LValueType>(resultType))
158 resultType = lType.getValueType();
159 Type attrType = cast<TypedAttr>(value).getType();
164 if (resultType != attrType)
166 <<
"requires attribute to either be an #emitc.opaque attribute or "
168 << attrType <<
") to match the op's result type (" << resultType
179template <
class ArgType>
181 StringRef toParse, ArgType fmtArgs,
186 if (fmtArgs.empty()) {
187 items.push_back(toParse);
191 while (!toParse.empty()) {
192 size_t idx = toParse.find(
'{');
193 if (idx == StringRef::npos) {
195 items.push_back(toParse);
200 items.push_back(toParse.take_front(idx));
201 toParse = toParse.drop_front(idx);
204 if (toParse.size() < 2) {
205 return emitError() <<
"expected '}' after unescaped '{' at end of string";
208 char nextChar = toParse[1];
209 if (nextChar ==
'{') {
211 items.push_back(toParse.take_front(1));
212 toParse = toParse.drop_front(2);
215 if (nextChar ==
'}') {
217 toParse = toParse.drop_front(2);
222 return emitError() <<
"expected '}' after unescaped '{'";
233LogicalResult AddressOfOp::verify() {
234 emitc::LValueType referenceType = getReference().getType();
235 emitc::PointerType resultType = getResult().getType();
237 if (referenceType.getValueType() != resultType.getPointee())
238 return emitOpError(
"requires result to be a pointer to the type "
239 "referenced by operand");
248LogicalResult AddOp::verify() {
249 Type lhsType = getLhs().getType();
250 Type rhsType = getRhs().getType();
252 if (isa<emitc::PointerType>(lhsType) && isa<emitc::PointerType>(rhsType))
253 return emitOpError(
"requires that at most one operand is a pointer");
255 if ((isa<emitc::PointerType>(lhsType) &&
256 !isa<IntegerType, emitc::OpaqueType>(rhsType)) ||
257 (isa<emitc::PointerType>(rhsType) &&
258 !isa<IntegerType, emitc::OpaqueType>(lhsType)))
259 return emitOpError(
"requires that one operand is an integer or of opaque "
260 "type if the other is a pointer");
269LogicalResult ApplyOp::verify() {
270 StringRef applicableOperatorStr = getApplicableOperator();
273 if (applicableOperatorStr.empty())
274 return emitOpError(
"applicable operator must not be empty");
277 if (applicableOperatorStr !=
"&" && applicableOperatorStr !=
"*")
278 return emitOpError(
"applicable operator is illegal");
280 Type operandType = getOperand().getType();
281 Type resultType = getResult().getType();
282 if (applicableOperatorStr ==
"&") {
283 if (!llvm::isa<emitc::LValueType>(operandType))
284 return emitOpError(
"operand type must be an lvalue when applying `&`");
285 if (!llvm::isa<emitc::PointerType>(resultType))
286 return emitOpError(
"result type must be a pointer when applying `&`");
288 if (!llvm::isa<emitc::PointerType>(operandType))
289 return emitOpError(
"operand type must be a pointer when applying `*`");
301LogicalResult emitc::AssignOp::verify() {
304 if (!variable.getDefiningOp())
305 return emitOpError() <<
"cannot assign to block argument";
307 Type valueType = getValue().getType();
308 Type variableType = variable.getType().getValueType();
309 if (variableType != valueType)
310 return emitOpError() <<
"requires value's type (" << valueType
311 <<
") to match variable's type (" << variableType
312 <<
")\n variable: " << variable
313 <<
"\n value: " << getValue() <<
"\n";
322 Type input = inputs.front(), output = outputs.front();
324 if (
auto arrayType = dyn_cast<emitc::ArrayType>(input)) {
325 if (
auto pointerType = dyn_cast<emitc::PointerType>(output)) {
326 return (arrayType.getElementType() == pointerType.getPointee()) &&
327 arrayType.getShape().size() == 1 && arrayType.getShape()[0] >= 1;
343LogicalResult emitc::CallOpaqueOp::verify() {
345 if (getCallee().empty())
348 if (std::optional<ArrayAttr> argsAttr = getArgs()) {
350 auto intAttr = llvm::dyn_cast<IntegerAttr>(arg);
351 if (intAttr && llvm::isa<IndexType>(intAttr.getType())) {
356 return emitOpError(
"index argument is out of range");
359 }
else if (llvm::isa<ArrayAttr>(
367 if (std::optional<ArrayAttr> templateArgsAttr = getTemplateArgs()) {
368 for (
Attribute tArg : *templateArgsAttr) {
369 if (!llvm::isa<TypeAttr, IntegerAttr, FloatAttr, emitc::OpaqueAttr>(tArg))
370 return emitOpError(
"template argument has invalid type");
374 if (llvm::any_of(getResultTypes(), llvm::IsaPred<ArrayType>)) {
375 return emitOpError() <<
"cannot return array type";
385LogicalResult emitc::ConstantOp::verify() {
389 if (
auto opaqueValue = llvm::dyn_cast<emitc::OpaqueAttr>(value)) {
390 if (opaqueValue.getValue().empty())
396OpFoldResult emitc::ConstantOp::fold(FoldAdaptor adaptor) {
return getValue(); }
402LogicalResult DereferenceOp::verify() {
403 emitc::PointerType pointerType = getPointer().getType();
406 return emitOpError(
"requires result to be an lvalue of the type "
407 "pointed to by operand");
418struct RemoveRecurringExpressionOperands
420 using OpRewritePattern<ExpressionOp>::OpRewritePattern;
421 LogicalResult matchAndRewrite(ExpressionOp expressionOp,
422 PatternRewriter &rewriter)
const override {
427 for (
auto [i, operand] : llvm::enumerate(expressionOp.getDefs())) {
428 if (uniqueOperands.contains(operand))
430 uniqueOperands.insert(operand);
431 firstIndexOf[operand] = i;
435 if (uniqueOperands.size() == expressionOp.getDefs().size())
440 auto uniqueExpression = emitc::ExpressionOp::create(
441 rewriter, expressionOp.getLoc(), expressionOp.getResult().getType(),
442 uniqueOperands.getArrayRef(), expressionOp.getDoNotInline());
443 Block &uniqueExpressionBody = uniqueExpression.createBody();
448 Block *expressionBody = expressionOp.getBody();
449 for (
auto [operand, arg] :
450 llvm::zip(expressionOp.getOperands(), expressionBody->
getArguments()))
451 mapper.
map(arg, uniqueExpressionBody.
getArgument(firstIndexOf[operand]));
454 for (Operation &opToClone : *expressionOp.getBody())
455 rewriter.
clone(opToClone, mapper);
458 rewriter.
replaceOp(expressionOp, uniqueExpression);
468 results.
add<RemoveRecurringExpressionOperands>(context);
476 result.addAttribute(ExpressionOp::getDoNotInlineAttrName(
result.name),
481 "expected function type");
482 auto fnType = llvm::dyn_cast<FunctionType>(type);
485 "expected function type");
489 if (fnType.getNumResults() != 1)
491 "expected single return type");
492 result.addTypes(fnType.getResults());
496 bool enableNameShadowing = uniqueOperands.size() ==
result.operands.size();
498 if (enableNameShadowing) {
499 for (
auto [unresolvedOperand, operandType] :
500 llvm::zip(operands, fnType.getInputs())) {
502 argInfo.
ssaName = unresolvedOperand;
503 argInfo.
type = operandType;
504 argsInfo.push_back(argInfo);
508 if (parser.
parseRegion(*body, argsInfo, enableNameShadowing))
510 if (!enableNameShadowing) {
513 beforeRegionLoc,
"with recurring operands expected block arguments");
521 auto operands = getDefs();
526 bool printEntryBlockArgs =
true;
527 if (uniqueOperands.size() == operands.size()) {
529 printEntryBlockArgs =
false;
536 auto yieldOp = cast<YieldOp>(getBody()->getTerminator());
537 Value yieldedValue = yieldOp.getResult();
541LogicalResult ExpressionOp::verify() {
542 Type resultType = getResult().getType();
543 Region ®ion = getRegion();
548 return emitOpError(
"must yield a value at termination");
551 Value yieldResult = yield.getResult();
554 return emitOpError(
"must yield a value at termination");
559 return emitOpError(
"yielded value has no defining op");
562 return emitOpError(
"yielded value not defined within expression");
566 if (resultType != yieldType)
567 return emitOpError(
"requires yielded type to match return type");
570 auto expressionInterface = dyn_cast<emitc::CExpressionInterface>(op);
571 if (!expressionInterface)
572 return emitOpError(
"contains an unsupported operation");
573 if (op.getNumResults() != 1)
574 return emitOpError(
"requires exactly one result for each operation");
577 return emitOpError(
"contains an unused operation");
584 worklist.push_back(rootOp);
585 while (!worklist.empty()) {
588 if (visited.contains(op)) {
591 "requires exactly one use for operations with side effects");
595 if (
Operation *def = operand.getDefiningOp()) {
596 worklist.push_back(def);
618 ForOp::ensureTerminator(*bodyRegion, builder,
result.location);
645 regionArgs.push_back(inductionVariable);
654 regionArgs.front().type = type;
665 ForOp::ensureTerminator(*body, builder,
result.location);
675 p <<
" " << getInductionVar() <<
" = " <<
getLowerBound() <<
" to "
680 p <<
" : " << t <<
' ';
687LogicalResult ForOp::verifyRegions() {
690 if (getBody()->getNumArguments() != 1)
691 return emitOpError(
"expected body to have a single block argument for the "
692 "induction variable");
696 "expected induction variable to be same type as bounds and step");
709 return emitOpError(
"requires a 'callee' symbol reference attribute");
713 <<
"' does not reference a valid function";
716 auto fnType = fn.getFunctionType();
717 if (fnType.getNumInputs() != getNumOperands())
718 return emitOpError(
"incorrect number of operands for callee");
720 for (
unsigned i = 0, e = fnType.getNumInputs(); i != e; ++i)
721 if (getOperand(i).
getType() != fnType.getInput(i))
722 return emitOpError(
"operand type mismatch: expected operand type ")
723 << fnType.getInput(i) <<
", but provided "
724 << getOperand(i).getType() <<
" for operand number " << i;
726 if (fnType.getNumResults() != getNumResults())
727 return emitOpError(
"incorrect number of results for callee");
729 for (
unsigned i = 0, e = fnType.getNumResults(); i != e; ++i)
730 if (getResult(i).
getType() != fnType.getResult(i)) {
732 diag.attachNote() <<
" op result types: " << getResultTypes();
733 diag.attachNote() <<
"function result types: " << fnType.getResults();
740FunctionType CallOp::getCalleeType() {
741 return FunctionType::get(
getContext(), getOperandTypes(), getResultTypes());
751 auto fnAttr = getSymNameAttr();
753 return emitOpError(
"requires a 'sym_name' symbol reference attribute");
757 <<
"' does not reference a valid function";
771 state.
addAttribute(getFunctionTypeAttrName(state.
name), TypeAttr::get(type));
775 if (argAttrs.empty())
777 assert(type.getNumInputs() == argAttrs.size());
779 builder, state, argAttrs, {},
780 getArgAttrsAttrName(state.
name), getResAttrsAttrName(state.
name));
791 getFunctionTypeAttrName(
result.name), buildFuncType,
792 getArgAttrsAttrName(
result.name), getResAttrsAttrName(
result.name));
797 p, *
this,
false, getFunctionTypeAttrName(),
798 getArgAttrsAttrName(), getResAttrsAttrName());
801LogicalResult FuncOp::verify() {
802 if (llvm::any_of(getArgumentTypes(), llvm::IsaPred<LValueType>)) {
803 return emitOpError(
"cannot have lvalue type as argument");
806 if (getNumResults() > 1)
807 return emitOpError(
"requires zero or exactly one result, but has ")
810 if (getNumResults() == 1 && isa<ArrayType>(getResultTypes()[0]))
820LogicalResult ReturnOp::verify() {
821 auto function = cast<FuncOp>((*this)->getParentOp());
824 if (getNumOperands() != function.getNumResults())
826 << getNumOperands() <<
" operands, but enclosing function (@"
827 << function.getName() <<
") returns " << function.getNumResults();
829 if (function.getNumResults() == 1)
830 if (getOperand().
getType() != function.getResultTypes()[0])
831 return emitError() <<
"type of the return operand ("
832 << getOperand().getType()
833 <<
") doesn't match function result type ("
834 << function.getResultTypes()[0] <<
")"
835 <<
" in function @" << function.getName();
844 bool addThenBlock,
bool addElseBlock) {
845 assert((!addElseBlock || addThenBlock) &&
846 "must not create else block w/o then block");
860 bool withElseRegion) {
870 if (withElseRegion) {
878 assert(thenBuilder &&
"the builder callback for 'then' must be present");
885 thenBuilder(builder,
result.location);
891 elseBuilder(builder,
result.location);
897 result.regions.reserve(2);
926 bool printBlockTerminators =
false;
928 p <<
" " << getCondition();
932 printBlockTerminators);
935 Region &elseRegion = getElseRegion();
936 if (!elseRegion.
empty()) {
940 printBlockTerminators);
962 Region *elseRegion = &this->getElseRegion();
963 if (elseRegion->
empty())
976 FoldAdaptor adaptor(operands, *
this);
977 auto boolAttr = dyn_cast_or_null<BoolAttr>(adaptor.getCondition());
978 if (!boolAttr || boolAttr.getValue())
979 regions.emplace_back(&getThenRegion());
982 if (!boolAttr || !boolAttr.getValue()) {
983 if (!getElseRegion().empty())
984 regions.emplace_back(&getElseRegion());
990void IfOp::getRegionInvocationBounds(
993 if (
auto cond = llvm::dyn_cast_or_null<BoolAttr>(operands[0])) {
996 invocationBounds.emplace_back(0, cond.getValue() ? 1 : 0);
997 invocationBounds.emplace_back(0, cond.getValue() ? 0 : 1);
1000 invocationBounds.assign(2, {0, 1});
1009 bool standardInclude = getIsStandardInclude();
1012 if (standardInclude)
1014 p <<
"\"" << getInclude() <<
"\"";
1015 if (standardInclude)
1030 <<
"expected trailing '>' for standard include";
1032 if (standardInclude)
1033 result.addAttribute(
"is_standard_include",
1044LogicalResult emitc::LiteralOp::verify() {
1045 if (getValue().empty())
1046 return emitOpError() <<
"value must not be empty";
1053LogicalResult SubOp::verify() {
1054 Type lhsType = getLhs().getType();
1055 Type rhsType = getRhs().getType();
1056 Type resultType = getResult().getType();
1058 if (isa<emitc::PointerType>(rhsType) && !isa<emitc::PointerType>(lhsType))
1059 return emitOpError(
"rhs can only be a pointer if lhs is a pointer");
1061 if (isa<emitc::PointerType>(lhsType) &&
1062 !isa<IntegerType, emitc::OpaqueType, emitc::PointerType>(rhsType))
1063 return emitOpError(
"requires that rhs is an integer, pointer or of opaque "
1064 "type if lhs is a pointer");
1066 if (isa<emitc::PointerType>(lhsType) && isa<emitc::PointerType>(rhsType) &&
1067 !isa<IntegerType, emitc::PtrDiffTType, emitc::OpaqueType>(resultType))
1068 return emitOpError(
"requires that the result is an integer, ptrdiff_t or "
1069 "of opaque type if lhs and rhs are pointers");
1077LogicalResult emitc::VariableOp::verify() {
1085LogicalResult emitc::YieldOp::verify() {
1090 return emitOpError() <<
"yields a value not returned by parent";
1093 return emitOpError() <<
"does not yield a value to be returned by parent";
1102LogicalResult emitc::SubscriptOp::verify() {
1104 if (
auto arrayType = llvm::dyn_cast<emitc::ArrayType>(getValue().
getType())) {
1106 if (
getIndices().size() != (
size_t)arrayType.getRank()) {
1107 return emitOpError() <<
"on array operand requires number of indices ("
1109 <<
") to match the rank of the array type ("
1110 << arrayType.getRank() <<
")";
1113 for (
unsigned i = 0, e =
getIndices().size(); i != e; ++i) {
1116 return emitOpError() <<
"on array operand requires index operand " << i
1117 <<
" to be integer-like, but got " << type;
1121 Type elementType = arrayType.getElementType();
1123 if (elementType != resultType) {
1124 return emitOpError() <<
"on array operand requires element type ("
1125 << elementType <<
") and result type (" << resultType
1132 if (
auto pointerType =
1133 llvm::dyn_cast<emitc::PointerType>(getValue().
getType())) {
1137 <<
"on pointer operand requires one index operand, but got "
1143 return emitOpError() <<
"on pointer operand requires index operand to be "
1144 "integer-like, but got "
1148 Type pointeeType = pointerType.getPointee();
1150 if (pointeeType != resultType) {
1151 return emitOpError() <<
"on pointer operand requires pointee type ("
1152 << pointeeType <<
") and result type (" << resultType
1167LogicalResult emitc::VerbatimOp::verify() {
1171 FailureOr<SmallVector<ReplacementItem>> fmt =
1176 size_t numPlaceholders = llvm::count_if(*fmt, [](
ReplacementItem &item) {
1177 return std::holds_alternative<Placeholder>(item);
1180 if (numPlaceholders != getFmtArgs().size()) {
1182 <<
"requires operands for each placeholder in the format string";
1187FailureOr<SmallVector<ReplacementItem>> emitc::VerbatimOp::parseFormatString() {
1189 return ::parseFormatString(getValue(), getFmtArgs());
1196#include "mlir/Dialect/EmitC/IR/EmitCEnums.cpp.inc"
1202#define GET_ATTRDEF_CLASSES
1203#include "mlir/Dialect/EmitC/IR/EmitCAttributes.cpp.inc"
1209#define GET_TYPEDEF_CLASSES
1210#include "mlir/Dialect/EmitC/IR/EmitCTypes.cpp.inc"
1231 if (!isValidElementType(elementType))
1232 return parser.
emitError(typeLoc,
"invalid array element type '")
1233 << elementType <<
"'",
1237 return parser.
getChecked<ArrayType>(dimensions, elementType);
1240void emitc::ArrayType::print(
AsmPrinter &printer)
const {
1243 printer << dim <<
'x';
1249LogicalResult emitc::ArrayType::verify(
1253 return emitError() <<
"shape must not be empty";
1257 return emitError() <<
"dimensions must have non-negative size";
1261 return emitError() <<
"element type must not be none";
1263 if (!isValidElementType(elementType))
1264 return emitError() <<
"invalid array element type";
1271 Type elementType)
const {
1273 return emitc::ArrayType::get(
getShape(), elementType);
1274 return emitc::ArrayType::get(*
shape, elementType);
1281LogicalResult mlir::emitc::LValueType::verify(
1288 <<
"!emitc.lvalue must wrap supported emitc type, but got " << value;
1290 if (llvm::isa<emitc::ArrayType>(value))
1291 return emitError() <<
"!emitc.lvalue cannot wrap !emitc.array type";
1300LogicalResult mlir::emitc::OpaqueType::verify(
1302 llvm::StringRef value) {
1303 if (value.empty()) {
1304 return emitError() <<
"expected non empty string in !emitc.opaque type";
1306 if (value.back() ==
'*') {
1307 return emitError() <<
"pointer not allowed as outer type with "
1308 "!emitc.opaque, use !emitc.ptr instead";
1317LogicalResult mlir::emitc::PointerType::verify(
1319 if (llvm::isa<emitc::LValueType>(value))
1320 return emitError() <<
"pointers to lvalues are not allowed";
1339 if (
auto array = llvm::dyn_cast<ArrayType>(type))
1340 return RankedTensorType::get(array.getShape(), array.getElementType());
1351 typeAttr = TypeAttr::get(type);
1359 if (!llvm::isa<ElementsAttr, IntegerAttr, FloatAttr, emitc::OpaqueAttr>(
1362 <<
"initial value should be a integer, float, elements or opaque "
1367LogicalResult GlobalOp::verify() {
1371 if (getInitialValue().has_value()) {
1372 Attribute initValue = getInitialValue().value();
1375 if (
auto elementsAttr = llvm::dyn_cast<ElementsAttr>(initValue)) {
1376 auto arrayType = llvm::dyn_cast<ArrayType>(
getType());
1380 Type initType = elementsAttr.getType();
1382 if (initType != tensorType) {
1383 return emitOpError(
"initial value expected to be of type ")
1384 <<
getType() <<
", but was of type " << initType;
1386 }
else if (
auto intAttr = dyn_cast<IntegerAttr>(initValue)) {
1387 if (intAttr.getType() !=
getType()) {
1388 return emitOpError(
"initial value expected to be of type ")
1389 <<
getType() <<
", but was of type " << intAttr.getType();
1391 }
else if (
auto floatAttr = dyn_cast<FloatAttr>(initValue)) {
1392 if (floatAttr.getType() !=
getType()) {
1393 return emitOpError(
"initial value expected to be of type ")
1394 <<
getType() <<
", but was of type " << floatAttr.getType();
1396 }
else if (!isa<emitc::OpaqueAttr>(initValue)) {
1397 return emitOpError(
"initial value should be a integer, float, elements "
1398 "or opaque attribute, but got ")
1402 if (getStaticSpecifier() && getExternSpecifier()) {
1403 return emitOpError(
"cannot have both static and extern specifiers");
1419 << getName() <<
"' does not reference a valid emitc.global";
1421 Type resultType = getResult().getType();
1422 Type globalType = global.getType();
1425 if (llvm::isa<ArrayType>(globalType)) {
1426 if (globalType != resultType)
1427 return emitOpError(
"on array type expects result type ")
1428 << resultType <<
" to match type " << globalType
1429 <<
" of the global @" << getName();
1434 auto lvalueType = dyn_cast<LValueType>(resultType);
1436 return emitOpError(
"on non-array type expects result type to be an "
1437 "lvalue type for the global @")
1439 if (lvalueType.getValueType() != globalType)
1440 return emitOpError(
"on non-array type expects result inner type ")
1441 << lvalueType.getValueType() <<
" to match type " << globalType
1442 <<
" of the global @" << getName();
1457 Region ®ion = *caseRegions.emplace_back(std::make_unique<Region>());
1461 caseValues.push_back(value);
1470 for (
auto [value, region] : llvm::zip(cases.
asArrayRef(), caseRegions)) {
1472 p <<
"case " << value <<
' ';
1478 const Twine &name) {
1479 auto yield = dyn_cast<emitc::YieldOp>(region.
front().
back());
1481 return op.emitOpError(
"expected region to end with emitc.yield, but got ")
1484 if (yield.getNumOperands() != 0) {
1485 return (op.emitOpError(
"expected each region to return ")
1486 <<
"0 values, but " << name <<
" returns "
1487 << yield.getNumOperands())
1488 .attachNote(yield.getLoc())
1489 <<
"see yield operation here";
1495LogicalResult emitc::SwitchOp::verify() {
1497 return emitOpError(
"unsupported type ") << getArg().getType();
1499 if (getCases().size() != getCaseRegions().size()) {
1501 << getCaseRegions().size() <<
" case regions but "
1502 << getCases().size() <<
" case values";
1506 for (
int64_t value : getCases())
1507 if (!valueSet.insert(value).second)
1508 return emitOpError(
"has duplicate case value: ") << value;
1513 for (
auto [idx, caseRegion] : llvm::enumerate(getCaseRegions()))
1520unsigned emitc::SwitchOp::getNumCases() {
return getCases().size(); }
1522Block &emitc::SwitchOp::getDefaultBlock() {
return getDefaultRegion().
front(); }
1524Block &emitc::SwitchOp::getCaseBlock(
unsigned idx) {
1525 assert(idx < getNumCases() &&
"case index out-of-bounds");
1526 return getCaseRegions()[idx].front();
1529void SwitchOp::getSuccessorRegions(
1531 llvm::append_range(successors, getRegions());
1534void SwitchOp::getEntrySuccessorRegions(
1537 FoldAdaptor adaptor(operands, *
this);
1540 auto arg = dyn_cast_or_null<IntegerAttr>(adaptor.getArg());
1542 llvm::append_range(successors, getRegions());
1548 for (
auto [caseValue, caseRegion] : llvm::zip(getCases(), getCaseRegions())) {
1549 if (caseValue == arg.getInt()) {
1550 successors.emplace_back(&caseRegion);
1554 successors.emplace_back(&getDefaultRegion());
1557void SwitchOp::getRegionInvocationBounds(
1559 auto operandValue = llvm::dyn_cast_or_null<IntegerAttr>(operands.front());
1560 if (!operandValue) {
1566 unsigned liveIndex = getNumRegions() - 1;
1567 const auto *iteratorToInt = llvm::find(getCases(), operandValue.getInt());
1569 liveIndex = iteratorToInt != getCases().end()
1570 ? std::distance(getCases().begin(), iteratorToInt)
1573 for (
unsigned regIndex = 0, regNum = getNumRegions(); regIndex < regNum;
1575 bounds.emplace_back(0, regIndex == liveIndex);
1602 if (
auto array = llvm::dyn_cast<ArrayType>(type))
1603 return RankedTensorType::get(array.getShape(), array.getElementType());
1614 typeAttr = TypeAttr::get(type);
1622 if (!llvm::isa<ElementsAttr, IntegerAttr, FloatAttr, emitc::OpaqueAttr>(
1625 <<
"initial value should be a integer, float, elements or opaque "
1630LogicalResult FieldOp::verify() {
1635 if (!parentOp || !isa<emitc::ClassOp>(parentOp))
1636 return emitOpError(
"field must be nested within an emitc.class operation");
1638 StringAttr symName = getSymNameAttr();
1639 if (!symName || symName.getValue().empty())
1640 return emitOpError(
"field must have a non-empty symbol name");
1649LogicalResult GetFieldOp::verify() {
1650 auto parentClassOp = getOperation()->getParentOfType<emitc::ClassOp>();
1651 if (!parentClassOp.getOperation())
1652 return emitOpError(
" must be nested within an emitc.class operation");
1663 << fieldNameAttr <<
"' not found in the class";
1665 Type getFieldResultType = getResult().getType();
1666 Type fieldType = fieldOp.getType();
1668 if (fieldType != getFieldResultType)
1670 << getFieldResultType <<
" does not match field '" << fieldNameAttr
1671 <<
"' type " << fieldType;
1688LogicalResult emitc::DoOp::verify() {
1689 Block &condBlock = getConditionRegion().
front();
1693 "condition region must contain exactly two operations: "
1694 "'emitc.expression' followed by 'emitc.yield', but found ")
1698 auto exprOp = dyn_cast<emitc::ExpressionOp>(first);
1700 return emitOpError(
"expected first op in condition region to be "
1701 "'emitc.expression', but got ")
1704 if (!exprOp.getResult().getType().isInteger(1))
1705 return emitOpError(
"emitc.expression in condition region must return "
1706 "'i1', but returns ")
1707 << exprOp.getResult().getType();
1710 auto condYield = dyn_cast<emitc::YieldOp>(last);
1712 return emitOpError(
"expected last op in condition region to be "
1713 "'emitc.yield', but got ")
1716 if (condYield.getNumOperands() != 1)
1717 return emitOpError(
"expected condition region to return 1 value, but "
1719 << condYield.getNumOperands() <<
" values";
1721 if (condYield.getOperand(0) != exprOp.getResult())
1722 return emitError(
"'emitc.yield' must return result of "
1723 "'emitc.expression' from this condition region");
1727 return emitOpError(
"body region must not contain terminator");
1740 if (bodyRegion->
empty())
1750#include "mlir/Dialect/EmitC/IR/EmitCInterfaces.cpp.inc"
1752#define GET_OP_CLASSES
1753#include "mlir/Dialect/EmitC/IR/EmitC.cpp.inc"
p<< " : "<< getMemRefType()<< ", "<< getType();}static LogicalResult verifyVectorMemoryOp(Operation *op, MemRefType memrefType, VectorType vectorType) { if(memrefType.getElementType() !=vectorType.getElementType()) return op-> emitOpError("requires memref and vector types of the same elemental type")
Given a list of lists of parsed operands, populates uniqueOperands with unique operands.
static std::optional< int64_t > getUpperBound(Value iv)
Gets the constant upper bound on an affine.for iv.
static std::optional< int64_t > getLowerBound(Value iv)
Gets the constant lower bound on an iv.
static bool hasSideEffects(Operation *op)
static LogicalResult verifyInitializationAttribute(Operation *op, Attribute value)
Check that the type of the initial value is compatible with the operations result type.
static LogicalResult verifyRegion(emitc::SwitchOp op, Region ®ion, const Twine &name)
static ParseResult parseEmitCGlobalOpTypeAndInitialValue(OpAsmParser &parser, TypeAttr &typeAttr, Attribute &initialValue)
static Type getInitializerTypeForField(Type type)
static ParseResult parseEmitCFieldOpTypeAndInitialValue(OpAsmParser &parser, TypeAttr &typeAttr, Attribute &initialValue)
FailureOr< SmallVector< ReplacementItem > > parseFormatString(StringRef toParse, ArgType fmtArgs, llvm::function_ref< mlir::InFlightDiagnostic()> emitError={})
Parse a format string and return a list of its parts.
static void printEmitCGlobalOpTypeAndInitialValue(OpAsmPrinter &p, GlobalOp op, TypeAttr type, Attribute initialValue)
static ParseResult parseSwitchCases(OpAsmParser &parser, DenseI64ArrayAttr &cases, SmallVectorImpl< std::unique_ptr< Region > > &caseRegions)
Parse the case regions and values.
static void printEmitCFieldOpTypeAndInitialValue(OpAsmPrinter &p, FieldOp op, TypeAttr type, Attribute initialValue)
static void printSwitchCases(OpAsmPrinter &p, Operation *op, DenseI64ArrayAttr cases, RegionRange caseRegions)
Print the case regions and values.
static Type getInitializerTypeForGlobal(Type type)
static Type getElementType(Type type)
Determine the element type of type.
static std::string diag(const llvm::Value &value)
static Type getValueType(Attribute attr)
static ArrayRef< int64_t > getShape(Type type)
Returns the shape of the given type.
This base class exposes generic asm parser hooks, usable across the various derived parsers.
virtual Builder & getBuilder() const =0
Return a builder which provides useful access to MLIRContext, global objects like types and attribute...
virtual ParseResult parseOptionalAttrDict(NamedAttrList &result)=0
Parse a named dictionary into 'result' if it is present.
virtual ParseResult parseOptionalEqual()=0
Parse a = token if present.
virtual ParseResult parseOptionalKeyword(StringRef keyword)=0
Parse the given keyword if present.
MLIRContext * getContext() const
virtual InFlightDiagnostic emitError(SMLoc loc, const Twine &message={})=0
Emit a diagnostic at the specified location and return failure.
virtual ParseResult parseOptionalColon()=0
Parse a : token if present.
ParseResult parseInteger(IntT &result)
Parse an integer value from the stream.
virtual ParseResult parseLess()=0
Parse a '<' token.
virtual ParseResult parseDimensionList(SmallVectorImpl< int64_t > &dimensions, bool allowDynamic=true, bool withTrailingX=true)=0
Parse a dimension list of a tensor or memref type.
virtual ParseResult parseOptionalGreater()=0
Parse a '>' token if present.
virtual ParseResult parseEqual()=0
Parse a = token.
virtual ParseResult parseOptionalAttrDictWithKeyword(NamedAttrList &result)=0
Parse a named dictionary into 'result' if the attributes keyword is present.
virtual ParseResult parseColonType(Type &result)=0
Parse a colon followed by a type.
virtual OptionalParseResult parseOptionalAttribute(Attribute &result, Type type={})=0
Parse an arbitrary optional attribute of a given type and return it in result.
virtual SMLoc getCurrentLocation()=0
Get the location of the next token and store it into the argument.
auto getChecked(SMLoc loc, ParamsT &&...params)
Invoke the getChecked method of the given Attribute or Type class, using the provided location to emi...
virtual SMLoc getNameLoc() const =0
Return the location of the original name token.
virtual ParseResult parseOptionalLess()=0
Parse a '<' token if present.
virtual ParseResult parseGreater()=0
Parse a '>' token.
virtual ParseResult parseType(Type &result)=0
Parse a type.
ParseResult parseKeyword(StringRef keyword)
Parse a given keyword.
virtual ParseResult parseAttribute(Attribute &result, Type type={})=0
Parse an arbitrary attribute of a given type and return it in result.
This base class exposes generic asm printer hooks, usable across the various derived printers.
virtual void printAttributeWithoutType(Attribute attr)
Print the given attribute without its type.
virtual void printType(Type type)
virtual void printNewline()
Print a newline and indent the printer to the start of the current operation/attribute/type.
Attributes are known-constant values of operations.
Block represents an ordered list of Operations.
BlockArgument getArgument(unsigned i)
OpListType & getOperations()
Operation * getTerminator()
Get the terminator operation of this block.
BlockArgument addArgument(Type type, Location loc)
Add one value to the argument list.
bool mightHaveTerminator()
Return "true" if this block might have a terminator.
BlockArgListType getArguments()
iterator_range< iterator > without_terminator()
Return an iterator range over the operation within this block excluding the terminator operation at t...
This class is a general helper class for creating context-global objects like types,...
DenseI64ArrayAttr getDenseI64ArrayAttr(ArrayRef< int64_t > values)
FunctionType getFunctionType(TypeRange inputs, TypeRange results)
IntegerType getIntegerType(unsigned width)
StringAttr getStringAttr(const Twine &bytes)
NamedAttribute getNamedAttr(StringRef name, Attribute val)
A symbol reference with a reference path containing a single element.
void map(Value from, Value to)
Inserts a new mapping for 'from' to 'to'.
This class represents a diagnostic that is inflight and set to be reported.
This class represents upper and lower bounds on the number of times a region of a RegionBranchOpInter...
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
MLIRContext is the top-level object for a collection of MLIR operations.
void push_back(NamedAttribute newAttribute)
Add an attribute with the specified name.
void append(StringRef name, Attribute attr)
Add an attribute with the specified name.
The OpAsmParser has methods for interacting with the asm parser: parsing things from it,...
virtual ParseResult parseRegion(Region ®ion, ArrayRef< Argument > arguments={}, bool enableNameShadowing=false)=0
Parses a region.
virtual ParseResult resolveOperand(const UnresolvedOperand &operand, Type type, SmallVectorImpl< Value > &result)=0
Resolve an operand to an SSA value, emitting an error on failure.
ParseResult resolveOperands(Operands &&operands, Type type, SmallVectorImpl< Value > &result)
Resolve a list of operands to SSA values, emitting an error on failure, or appending the results to t...
virtual ParseResult parseOperand(UnresolvedOperand &result, bool allowResultNumber=true)=0
Parse a single SSA value operand name along with a result number if allowResultNumber is true.
virtual ParseResult parseOperandList(SmallVectorImpl< UnresolvedOperand > &result, Delimiter delimiter=Delimiter::None, bool allowResultNumber=true, int requiredOperandCount=-1)=0
Parse zero or more SSA comma-separated operand references with a specified surrounding delimiter,...
This is a pure-virtual base class that exposes the asmprinter hooks necessary to implement a custom p...
virtual void shadowRegionArgs(Region ®ion, ValueRange namesToUse)=0
Renumber the arguments for the specified region to the same names as the SSA values in namesToUse.
void printOperands(const ContainerType &container)
Print a comma separated list of operands.
virtual void printOptionalAttrDictWithKeyword(ArrayRef< NamedAttribute > attrs, ArrayRef< StringRef > elidedAttrs={})=0
If the specified operation has attributes, print out an attribute dictionary prefixed with 'attribute...
virtual void printOptionalAttrDict(ArrayRef< NamedAttribute > attrs, ArrayRef< StringRef > elidedAttrs={})=0
If the specified operation has attributes, print out an attribute dictionary with their values.
void printFunctionalType(Operation *op)
Print the complete type of an operation in functional form.
virtual void printRegion(Region &blocks, bool printEntryBlockArgs=true, bool printBlockTerminators=true, bool printEmptyBlock=false)=0
Prints a region.
RAII guard to reset the insertion point of the builder when destroyed.
This class helps build Operations.
Block * createBlock(Region *parent, Region::iterator insertPt={}, TypeRange argTypes={}, ArrayRef< Location > locs={})
Add new block with 'argTypes' arguments and set the insertion point to the end of it.
Operation * clone(Operation &op, IRMapping &mapper)
Creates a deep copy of the specified operation, remapping any operands that use values outside of the...
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
void setInsertionPointAfter(Operation *op)
Sets the insertion point to the node after the specified operation, which will cause subsequent inser...
This class represents a single result from folding an operation.
type_range getType() const
Operation is the basic unit of execution within MLIR.
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
Operation * getParentOp()
Returns the closest surrounding operation that contains this operation or nullptr if this is a top-le...
OperationName getName()
The name of an operation is the key identifier for it.
operand_range getOperands()
Returns an iterator on the underlying Value's.
InFlightDiagnostic emitOpError(const Twine &message={})
Emit an error with the op name prefixed, like "'dim' op " which is convenient for verifiers.
unsigned getNumResults()
Return the number of results held by this operation.
This class implements Optional functionality for ParseResult.
bool has_value() const
Returns true if we contain a valid ParseResult value.
This class represents a point being branched from in the methods of the RegionBranchOpInterface.
bool isParent() const
Returns true if branching from the parent op.
This class provides an abstraction over the different types of ranges over Regions.
This class represents a successor of a region.
static RegionSuccessor parent()
Initialize a successor that branches after/out of the parent operation.
bool isParent() const
Return true if the successor is the parent operation.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
This class represents a collection of SymbolTables.
virtual Operation * lookupNearestSymbolFrom(Operation *from, StringAttr symbol)
Returns the operation registered with the given symbol name within the closest parent operation of,...
static StringRef getSymbolAttrName()
Return the name of the attribute used for symbol names.
This class provides an abstraction over the various different ranges of value types.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class provides an abstraction over the different types of ranges over Values.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Type getType() const
Return the type of this value.
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
ArrayRef< T > asArrayRef() const
A named class for passing around the variadic flag.
mlir::Value getVar(mlir::Operation *accDataClauseOp)
Used to obtain the var from a data clause operation.
void addArgAndResultAttrs(Builder &builder, OperationState &result, ArrayRef< DictionaryAttr > argAttrs, ArrayRef< DictionaryAttr > resultAttrs, StringAttr argAttrsName, StringAttr resAttrsName)
Adds argument and result attributes, provided as argAttrs and resultAttrs arguments,...
void buildTerminatedBody(OpBuilder &builder, Location loc)
Default callback for builders of ops carrying a region.
std::variant< StringRef, Placeholder > ReplacementItem
bool isFundamentalType(mlir::Type type)
Determines whether type is a valid fundamental C++ type in EmitC.
bool isSupportedFloatType(mlir::Type type)
Determines whether type is a valid floating-point type in EmitC.
bool isSupportedEmitCType(mlir::Type type)
Determines whether type is valid in EmitC.
bool isPointerWideType(mlir::Type type)
Determines whether type is a emitc.size_t/ssize_t type.
bool isIntegerIndexOrOpaqueType(Type type)
Determines whether type is integer like, i.e.
bool isSupportedIntegerType(mlir::Type type)
Determines whether type is a valid integer type in EmitC.
void printFunctionOp(OpAsmPrinter &p, FunctionOpInterface op, bool isVariadic, StringRef typeAttrName, StringAttr argAttrsName, StringAttr resAttrsName)
Printer implementation for function-like operations.
ParseResult parseFunctionOp(OpAsmParser &parser, OperationState &result, bool allowVariadic, StringAttr typeAttrName, FuncTypeBuilder funcTypeBuilder, StringAttr argAttrsName, StringAttr resAttrsName)
Parser implementation for function-like operations.
Operation::operand_range getIndices(Operation *op)
Get the indices that the given load/store operation is operating on.
Include the generated interface declarations.
detail::DenseArrayAttrImpl< int64_t > DenseI64ArrayAttr
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
llvm::DenseSet< ValueT, ValueInfoT > DenseSet
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
llvm::SetVector< T, Vector, Set, N > SetVector
std::conditional_t< std::is_same_v< Ty, mlir::Type >, mlir::Value, detail::TypedValue< Ty > > TypedValue
If Ty is mlir::Type this will select Value instead of having a wrapper around it.
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
llvm::function_ref< Fn > function_ref
UnresolvedOperand ssaName
This is the representation of an operand reference.
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...
This represents an operation in an abstracted form, suitable for use with the builder APIs.
void addAttribute(StringRef name, Attribute attr)
Add an attribute with the specified name.
Region * addRegion()
Create a region that should be attached to the operation.