18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/ADT/TypeSwitch.h"
21 #include "llvm/Support/Casting.h"
22 #include "llvm/Support/FormatVariadic.h"
27 #include "mlir/Dialect/EmitC/IR/EmitCDialect.cpp.inc"
33 void 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 builder.
create<emitc::ConstantOp>(loc, type, value);
59 builder.
create<emitc::YieldOp>(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 if (llvm::isa<Float16Type, BFloat16Type>(type))
136 return isa<emitc::SignedSizeTType, emitc::SizeTType, emitc::PtrDiffTType>(
144 assert(op->
getNumResults() == 1 &&
"operation must have 1 result");
146 if (llvm::isa<emitc::OpaqueAttr>(value))
149 if (llvm::isa<StringAttr>(value))
151 <<
"string attributes are not supported, use #emitc.opaque instead";
154 if (
auto lType = dyn_cast<LValueType>(resultType))
155 resultType = lType.getValueType();
156 Type attrType = cast<TypedAttr>(value).getType();
161 if (resultType != attrType)
163 <<
"requires attribute to either be an #emitc.opaque attribute or "
165 << attrType <<
") to match the op's result type (" << resultType
176 template <
class ArgType>
177 FailureOr<SmallVector<ReplacementItem>>
184 if (fmtArgs.empty()) {
185 items.push_back(toParse);
189 while (!toParse.empty()) {
190 size_t idx = toParse.find(
'{');
191 if (idx == StringRef::npos) {
193 items.push_back(toParse);
198 items.push_back(toParse.take_front(idx));
199 toParse = toParse.drop_front(idx);
202 if (toParse.size() < 2) {
204 <<
"expected '}' after unescaped '{' at end of string";
207 char nextChar = toParse[1];
208 if (nextChar ==
'{') {
210 items.push_back(toParse.take_front(1));
211 toParse = toParse.drop_front(2);
214 if (nextChar ==
'}') {
216 toParse = toParse.drop_front(2);
221 return (*
emitError)() <<
"expected '}' after unescaped '{'";
233 Type lhsType = getLhs().getType();
234 Type rhsType = getRhs().getType();
236 if (isa<emitc::PointerType>(lhsType) && isa<emitc::PointerType>(rhsType))
237 return emitOpError(
"requires that at most one operand is a pointer");
239 if ((isa<emitc::PointerType>(lhsType) &&
240 !isa<IntegerType, emitc::OpaqueType>(rhsType)) ||
241 (isa<emitc::PointerType>(rhsType) &&
242 !isa<IntegerType, emitc::OpaqueType>(lhsType)))
243 return emitOpError(
"requires that one operand is an integer or of opaque "
244 "type if the other is a pointer");
254 StringRef applicableOperatorStr = getApplicableOperator();
257 if (applicableOperatorStr.empty())
258 return emitOpError(
"applicable operator must not be empty");
261 if (applicableOperatorStr !=
"&" && applicableOperatorStr !=
"*")
262 return emitOpError(
"applicable operator is illegal");
264 Type operandType = getOperand().getType();
265 Type resultType = getResult().getType();
266 if (applicableOperatorStr ==
"&") {
267 if (!llvm::isa<emitc::LValueType>(operandType))
268 return emitOpError(
"operand type must be an lvalue when applying `&`");
269 if (!llvm::isa<emitc::PointerType>(resultType))
270 return emitOpError(
"result type must be a pointer when applying `&`");
272 if (!llvm::isa<emitc::PointerType>(operandType))
273 return emitOpError(
"operand type must be a pointer when applying `*`");
288 if (!variable.getDefiningOp())
289 return emitOpError() <<
"cannot assign to block argument";
291 Type valueType = getValue().getType();
292 Type variableType = variable.getType().getValueType();
293 if (variableType != valueType)
294 return emitOpError() <<
"requires value's type (" << valueType
295 <<
") to match variable's type (" << variableType
296 <<
")\n variable: " << variable
297 <<
"\n value: " << getValue() <<
"\n";
306 Type input = inputs.front(), output = outputs.front();
308 if (
auto arrayType = dyn_cast<emitc::ArrayType>(input)) {
309 if (
auto pointerType = dyn_cast<emitc::PointerType>(output)) {
310 return (arrayType.getElementType() == pointerType.getPointee()) &&
311 arrayType.getShape().size() == 1 && arrayType.getShape()[0] >= 1;
329 if (getCallee().empty())
330 return emitOpError(
"callee must not be empty");
332 if (std::optional<ArrayAttr> argsAttr = getArgs()) {
334 auto intAttr = llvm::dyn_cast<IntegerAttr>(arg);
335 if (intAttr && llvm::isa<IndexType>(intAttr.getType())) {
336 int64_t index = intAttr.getInt();
339 if ((index < 0) || (index >=
static_cast<int64_t
>(getNumOperands())))
340 return emitOpError(
"index argument is out of range");
343 }
else if (llvm::isa<ArrayAttr>(
346 return emitOpError(
"array argument has no type");
351 if (std::optional<ArrayAttr> templateArgsAttr = getTemplateArgs()) {
352 for (
Attribute tArg : *templateArgsAttr) {
353 if (!llvm::isa<TypeAttr, IntegerAttr, FloatAttr, emitc::OpaqueAttr>(tArg))
354 return emitOpError(
"template argument has invalid type");
358 if (llvm::any_of(getResultTypes(), llvm::IsaPred<ArrayType>)) {
359 return emitOpError() <<
"cannot return array type";
373 if (
auto opaqueValue = llvm::dyn_cast<emitc::OpaqueAttr>(value)) {
374 if (opaqueValue.getValue().empty())
375 return emitOpError() <<
"value must not be empty";
380 OpFoldResult emitc::ConstantOp::fold(FoldAdaptor adaptor) {
return getValue(); }
387 auto yieldOp = cast<YieldOp>(getBody()->getTerminator());
388 Value yieldedValue = yieldOp.getResult();
390 assert(rootOp &&
"Yielded value not defined within expression");
395 Type resultType = getResult().getType();
396 Region ®ion = getRegion();
401 return emitOpError(
"must yield a value at termination");
404 Value yieldResult = yield.getResult();
407 return emitOpError(
"must yield a value at termination");
411 if (resultType != yieldType)
412 return emitOpError(
"requires yielded type to match return type");
416 return emitOpError(
"contains an unsupported operation");
417 if (op.getNumResults() != 1)
418 return emitOpError(
"requires exactly one result for each operation");
419 if (!op.getResult(0).hasOneUse())
420 return emitOpError(
"requires exactly one use for each operation");
431 Value ub,
Value step, BodyBuilderFn bodyBuilder) {
441 ForOp::ensureTerminator(*bodyRegion, builder, result.
location);
468 regionArgs.push_back(inductionVariable);
477 regionArgs.front().type = type;
488 ForOp::ensureTerminator(*body, builder, result.
location);
498 p <<
" " << getInductionVar() <<
" = " <<
getLowerBound() <<
" to "
503 p <<
" : " << t <<
' ';
510 LogicalResult ForOp::verifyRegions() {
515 "expected induction variable to be same type as bounds and step");
528 return emitOpError(
"requires a 'callee' symbol reference attribute");
531 return emitOpError() <<
"'" << fnAttr.getValue()
532 <<
"' does not reference a valid function";
535 auto fnType = fn.getFunctionType();
536 if (fnType.getNumInputs() != getNumOperands())
537 return emitOpError(
"incorrect number of operands for callee");
539 for (
unsigned i = 0, e = fnType.getNumInputs(); i != e; ++i)
540 if (getOperand(i).
getType() != fnType.getInput(i))
541 return emitOpError(
"operand type mismatch: expected operand type ")
542 << fnType.getInput(i) <<
", but provided "
543 << getOperand(i).getType() <<
" for operand number " << i;
545 if (fnType.getNumResults() != getNumResults())
546 return emitOpError(
"incorrect number of results for callee");
548 for (
unsigned i = 0, e = fnType.getNumResults(); i != e; ++i)
549 if (getResult(i).
getType() != fnType.getResult(i)) {
550 auto diag = emitOpError(
"result type mismatch at index ") << i;
551 diag.attachNote() <<
" op result types: " << getResultTypes();
552 diag.attachNote() <<
"function result types: " << fnType.getResults();
559 FunctionType CallOp::getCalleeType() {
570 auto fnAttr = getSymNameAttr();
572 return emitOpError(
"requires a 'sym_name' symbol reference attribute");
575 return emitOpError() <<
"'" << fnAttr.getValue()
576 <<
"' does not reference a valid function";
590 state.addAttribute(getFunctionTypeAttrName(state.name),
TypeAttr::get(type));
591 state.attributes.append(attrs.begin(), attrs.end());
594 if (argAttrs.empty())
596 assert(type.getNumInputs() == argAttrs.size());
598 builder, state, argAttrs, std::nullopt,
599 getArgAttrsAttrName(state.name), getResAttrsAttrName(state.name));
609 parser, result,
false,
610 getFunctionTypeAttrName(result.
name), buildFuncType,
611 getArgAttrsAttrName(result.
name), getResAttrsAttrName(result.
name));
616 p, *
this,
false, getFunctionTypeAttrName(),
617 getArgAttrsAttrName(), getResAttrsAttrName());
621 if (llvm::any_of(getArgumentTypes(), llvm::IsaPred<LValueType>)) {
622 return emitOpError(
"cannot have lvalue type as argument");
625 if (getNumResults() > 1)
626 return emitOpError(
"requires zero or exactly one result, but has ")
629 if (getNumResults() == 1 && isa<ArrayType>(getResultTypes()[0]))
630 return emitOpError(
"cannot return array type");
640 auto function = cast<FuncOp>((*this)->getParentOp());
643 if (getNumOperands() !=
function.getNumResults())
644 return emitOpError(
"has ")
645 << getNumOperands() <<
" operands, but enclosing function (@"
646 <<
function.getName() <<
") returns " <<
function.getNumResults();
648 if (
function.getNumResults() == 1)
649 if (getOperand().
getType() !=
function.getResultTypes()[0])
650 return emitError() <<
"type of the return operand ("
651 << getOperand().getType()
652 <<
") doesn't match function result type ("
653 <<
function.getResultTypes()[0] <<
")"
654 <<
" in function @" <<
function.getName();
663 bool addThenBlock,
bool addElseBlock) {
664 assert((!addElseBlock || addThenBlock) &&
665 "must not create else block w/o then block");
679 bool withElseRegion) {
689 if (withElseRegion) {
697 assert(thenBuilder &&
"the builder callback for 'then' must be present");
704 thenBuilder(builder, result.
location);
710 elseBuilder(builder, result.
location);
745 bool printBlockTerminators =
false;
747 p <<
" " << getCondition();
751 printBlockTerminators);
754 Region &elseRegion = getElseRegion();
755 if (!elseRegion.
empty()) {
759 printBlockTerminators);
781 Region *elseRegion = &this->getElseRegion();
782 if (elseRegion->
empty())
790 FoldAdaptor adaptor(operands, *
this);
791 auto boolAttr = dyn_cast_or_null<BoolAttr>(adaptor.getCondition());
792 if (!boolAttr || boolAttr.getValue())
793 regions.emplace_back(&getThenRegion());
796 if (!boolAttr || !boolAttr.getValue()) {
797 if (!getElseRegion().empty())
798 regions.emplace_back(&getElseRegion());
800 regions.emplace_back();
804 void IfOp::getRegionInvocationBounds(
807 if (
auto cond = llvm::dyn_cast_or_null<BoolAttr>(operands[0])) {
810 invocationBounds.emplace_back(0, cond.getValue() ? 1 : 0);
811 invocationBounds.emplace_back(0, cond.getValue() ? 0 : 1);
814 invocationBounds.assign(2, {0, 1});
823 bool standardInclude = getIsStandardInclude();
828 p <<
"\"" << getInclude() <<
"\"";
844 <<
"expected trailing '>' for standard include";
859 if (getValue().empty())
860 return emitOpError() <<
"value must not be empty";
868 Type lhsType = getLhs().getType();
869 Type rhsType = getRhs().getType();
870 Type resultType = getResult().getType();
872 if (isa<emitc::PointerType>(rhsType) && !isa<emitc::PointerType>(lhsType))
873 return emitOpError(
"rhs can only be a pointer if lhs is a pointer");
875 if (isa<emitc::PointerType>(lhsType) &&
876 !isa<IntegerType, emitc::OpaqueType, emitc::PointerType>(rhsType))
877 return emitOpError(
"requires that rhs is an integer, pointer or of opaque "
878 "type if lhs is a pointer");
880 if (isa<emitc::PointerType>(lhsType) && isa<emitc::PointerType>(rhsType) &&
881 !isa<IntegerType, emitc::PtrDiffTType, emitc::OpaqueType>(resultType))
882 return emitOpError(
"requires that the result is an integer, ptrdiff_t or "
883 "of opaque type if lhs and rhs are pointers");
900 Value result = getResult();
904 return emitOpError() <<
"yields a value not returned by parent";
907 return emitOpError() <<
"does not yield a value to be returned by parent";
918 if (
auto arrayType = llvm::dyn_cast<emitc::ArrayType>(getValue().
getType())) {
920 if (
getIndices().size() != (
size_t)arrayType.getRank()) {
921 return emitOpError() <<
"on array operand requires number of indices ("
923 <<
") to match the rank of the array type ("
924 << arrayType.getRank() <<
")";
927 for (
unsigned i = 0, e =
getIndices().size(); i != e; ++i) {
930 return emitOpError() <<
"on array operand requires index operand " << i
931 <<
" to be integer-like, but got " << type;
935 Type elementType = arrayType.getElementType();
937 if (elementType != resultType) {
938 return emitOpError() <<
"on array operand requires element type ("
939 << elementType <<
") and result type (" << resultType
946 if (
auto pointerType =
947 llvm::dyn_cast<emitc::PointerType>(getValue().
getType())) {
951 <<
"on pointer operand requires one index operand, but got "
957 return emitOpError() <<
"on pointer operand requires index operand to be "
958 "integer-like, but got "
962 Type pointeeType = pointerType.getPointee();
964 if (pointeeType != resultType) {
965 return emitOpError() <<
"on pointer operand requires pointee type ("
966 << pointeeType <<
") and result type (" << resultType
983 return this->emitOpError();
985 FailureOr<SmallVector<ReplacementItem>> fmt =
990 size_t numPlaceholders = llvm::count_if(*fmt, [](
ReplacementItem &item) {
991 return std::holds_alternative<Placeholder>(item);
994 if (numPlaceholders != getFmtArgs().size()) {
996 <<
"requires operands for each placeholder in the format string";
1010 #include "mlir/Dialect/EmitC/IR/EmitCEnums.cpp.inc"
1016 #define GET_ATTRDEF_CLASSES
1017 #include "mlir/Dialect/EmitC/IR/EmitCAttributes.cpp.inc"
1023 #define GET_TYPEDEF_CLASSES
1024 #include "mlir/Dialect/EmitC/IR/EmitCTypes.cpp.inc"
1045 if (!isValidElementType(elementType))
1046 return parser.
emitError(typeLoc,
"invalid array element type '")
1047 << elementType <<
"'",
1051 return parser.
getChecked<ArrayType>(dimensions, elementType);
1057 printer << dim <<
'x';
1067 return emitError() <<
"shape must not be empty";
1069 for (int64_t dim : shape) {
1071 return emitError() <<
"dimensions must have non-negative size";
1075 return emitError() <<
"element type must not be none";
1077 if (!isValidElementType(elementType))
1078 return emitError() <<
"invalid array element type";
1085 Type elementType)
const {
1102 <<
"!emitc.lvalue must wrap supported emitc type, but got " << value;
1104 if (llvm::isa<emitc::ArrayType>(value))
1105 return emitError() <<
"!emitc.lvalue cannot wrap !emitc.array type";
1116 llvm::StringRef value) {
1117 if (value.empty()) {
1118 return emitError() <<
"expected non empty string in !emitc.opaque type";
1120 if (value.back() ==
'*') {
1121 return emitError() <<
"pointer not allowed as outer type with "
1122 "!emitc.opaque, use !emitc.ptr instead";
1133 if (llvm::isa<emitc::LValueType>(value))
1134 return emitError() <<
"pointers to lvalues are not allowed";
1153 if (
auto array = llvm::dyn_cast<ArrayType>(type))
1173 if (!llvm::isa<ElementsAttr, IntegerAttr, FloatAttr, emitc::OpaqueAttr>(
1176 <<
"initial value should be a integer, float, elements or opaque "
1183 return emitOpError(
"expected valid emitc type");
1185 if (getInitialValue().has_value()) {
1186 Attribute initValue = getInitialValue().value();
1189 if (
auto elementsAttr = llvm::dyn_cast<ElementsAttr>(initValue)) {
1190 auto arrayType = llvm::dyn_cast<ArrayType>(
getType());
1192 return emitOpError(
"expected array type, but got ") <<
getType();
1194 Type initType = elementsAttr.getType();
1196 if (initType != tensorType) {
1197 return emitOpError(
"initial value expected to be of type ")
1198 <<
getType() <<
", but was of type " << initType;
1200 }
else if (
auto intAttr = dyn_cast<IntegerAttr>(initValue)) {
1201 if (intAttr.getType() !=
getType()) {
1202 return emitOpError(
"initial value expected to be of type ")
1203 <<
getType() <<
", but was of type " << intAttr.getType();
1205 }
else if (
auto floatAttr = dyn_cast<FloatAttr>(initValue)) {
1206 if (floatAttr.getType() !=
getType()) {
1207 return emitOpError(
"initial value expected to be of type ")
1208 <<
getType() <<
", but was of type " << floatAttr.getType();
1210 }
else if (!isa<emitc::OpaqueAttr>(initValue)) {
1211 return emitOpError(
"initial value should be a integer, float, elements "
1212 "or opaque attribute, but got ")
1216 if (getStaticSpecifier() && getExternSpecifier()) {
1217 return emitOpError(
"cannot have both static and extern specifiers");
1232 return emitOpError(
"'")
1233 << getName() <<
"' does not reference a valid emitc.global";
1235 Type resultType = getResult().getType();
1236 Type globalType = global.getType();
1239 if (llvm::isa<ArrayType>(globalType)) {
1240 if (globalType != resultType)
1241 return emitOpError(
"on array type expects result type ")
1242 << resultType <<
" to match type " << globalType
1243 <<
" of the global @" << getName();
1248 auto lvalueType = dyn_cast<LValueType>(resultType);
1249 if (!lvalueType || lvalueType.getValueType() != globalType)
1250 return emitOpError(
"on non-array type expects result inner type ")
1251 << lvalueType.getValueType() <<
" to match type " << globalType
1252 <<
" of the global @" << getName();
1267 Region ®ion = *caseRegions.emplace_back(std::make_unique<Region>());
1271 caseValues.push_back(value);
1280 for (
auto [value, region] : llvm::zip(cases.
asArrayRef(), caseRegions)) {
1282 p <<
"case " << value <<
' ';
1288 const Twine &name) {
1289 auto yield = dyn_cast<emitc::YieldOp>(region.
front().
back());
1291 return op.emitOpError(
"expected region to end with emitc.yield, but got ")
1294 if (yield.getNumOperands() != 0) {
1295 return (op.emitOpError(
"expected each region to return ")
1296 <<
"0 values, but " << name <<
" returns "
1297 << yield.getNumOperands())
1298 .attachNote(yield.getLoc())
1299 <<
"see yield operation here";
1307 return emitOpError(
"unsupported type ") << getArg().getType();
1309 if (getCases().size() != getCaseRegions().size()) {
1310 return emitOpError(
"has ")
1311 << getCaseRegions().size() <<
" case regions but "
1312 << getCases().size() <<
" case values";
1316 for (int64_t value : getCases())
1317 if (!valueSet.insert(value).second)
1318 return emitOpError(
"has duplicate case value: ") << value;
1320 if (failed(
verifyRegion(*
this, getDefaultRegion(),
"default region")))
1324 if (failed(
verifyRegion(*
this, caseRegion,
"case region #" + Twine(idx))))
1330 unsigned emitc::SwitchOp::getNumCases() {
return getCases().size(); }
1332 Block &emitc::SwitchOp::getDefaultBlock() {
return getDefaultRegion().
front(); }
1334 Block &emitc::SwitchOp::getCaseBlock(
unsigned idx) {
1335 assert(idx < getNumCases() &&
"case index out-of-bounds");
1336 return getCaseRegions()[idx].front();
1339 void SwitchOp::getSuccessorRegions(
1341 llvm::append_range(successors, getRegions());
1344 void SwitchOp::getEntrySuccessorRegions(
1347 FoldAdaptor adaptor(operands, *
this);
1350 auto arg = dyn_cast_or_null<IntegerAttr>(adaptor.getArg());
1352 llvm::append_range(successors, getRegions());
1358 for (
auto [caseValue, caseRegion] : llvm::zip(getCases(), getCaseRegions())) {
1359 if (caseValue == arg.getInt()) {
1360 successors.emplace_back(&caseRegion);
1364 successors.emplace_back(&getDefaultRegion());
1367 void SwitchOp::getRegionInvocationBounds(
1369 auto operandValue = llvm::dyn_cast_or_null<IntegerAttr>(operands.front());
1370 if (!operandValue) {
1376 unsigned liveIndex = getNumRegions() - 1;
1377 const auto *iteratorToInt = llvm::find(getCases(), operandValue.getInt());
1379 liveIndex = iteratorToInt != getCases().end()
1380 ? std::distance(getCases().begin(), iteratorToInt)
1383 for (
unsigned regIndex = 0, regNum = getNumRegions(); regIndex < regNum;
1385 bounds.emplace_back(0, regIndex == liveIndex);
1392 state.addRegion()->emplaceBlock();
1393 state.attributes.push_back(
1401 #define GET_OP_CLASSES
1402 #include "mlir/Dialect/EmitC/IR/EmitC.cpp.inc"
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 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 ParseResult parseSwitchCases(OpAsmParser &parser, DenseI64ArrayAttr &cases, SmallVectorImpl< std::unique_ptr< Region >> &caseRegions)
Parse the case regions and values.
static void printEmitCGlobalOpTypeAndInitialValue(OpAsmPrinter &p, GlobalOp 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)
FailureOr< SmallVector< ReplacementItem > > parseFormatString(StringRef toParse, ArgType fmtArgs, std::optional< llvm::function_ref< mlir::InFlightDiagnostic()>> emitError={})
Parse a format string and return a list of its parts.
static Operation * materializeConstant(Dialect *dialect, OpBuilder &builder, Attribute value, Type type, Location loc)
A utility function used to materialize a constant for a given attribute and type.
static MLIRContext * getContext(OpFoldResult val)
static std::string diag(const llvm::Value &value)
static void print(spirv::VerCapExtAttr triple, DialectAsmPrinter &printer)
static Type getElementType(Type type, ArrayRef< int32_t > indices, function_ref< InFlightDiagnostic(StringRef)> emitErrorFn)
Walks the given type hierarchy with the given indices, potentially down to component granularity,...
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 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)
Attributes are known-constant values of operations.
Block represents an ordered list of Operations.
BlockArgument getArgument(unsigned i)
Operation * getTerminator()
Get the terminator operation of this block.
BlockArgument addArgument(Type type, Location loc)
Add one value to the argument list.
bool mightHaveTerminator()
Check whether this block might have a terminator.
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.
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.
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.
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.
This is a pure-virtual base class that exposes the asmprinter hooks necessary to implement a custom p...
virtual void printNewline()=0
Print a newline and indent the printer to the start of the current operation.
virtual void printOptionalAttrDict(ArrayRef< NamedAttribute > attrs, ArrayRef< StringRef > elidedAttrs={})=0
If the specified operation has attributes, print out an attribute dictionary with their values.
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.
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
Block * createBlock(Region *parent, Region::iterator insertPt={}, TypeRange argTypes=std::nullopt, ArrayRef< Location > locs=std::nullopt)
Add new block with 'argTypes' arguments and set the insertion point to the end of it.
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
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.
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.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
This class represents a collection of SymbolTables.
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 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.
Base class for DenseArrayAttr that is instantiated and specialized for each supported element type be...
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,...
constexpr void enumerate(std::tuple< Tys... > &tuple, CallbackT &&callback)
std::variant< StringRef, Placeholder > ReplacementItem
void buildTerminatedBody(OpBuilder &builder, Location loc)
Default callback for builders of ops carrying a region.
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.
QueryRef parse(llvm::StringRef line, const QuerySession &qs)
Include the generated interface declarations.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
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.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
LogicalResult verify(Operation *op, bool verifyRecursively=true)
Perform (potentially expensive) checks of invariants, used to detect compiler bugs,...
UnresolvedOperand ssaName
This is the representation of an operand reference.
This represents an operation in an abstracted form, suitable for use with the builder APIs.
SmallVector< Value, 4 > operands
void addOperands(ValueRange newOperands)
void addAttribute(StringRef name, Attribute attr)
Add an attribute with the specified name.
SmallVector< std::unique_ptr< Region >, 1 > regions
Regions that the op will hold.
Region * addRegion()
Create a region that should be attached to the operation.