27#include "llvm/ADT/APFloat.h"
28#include "llvm/ADT/TypeSwitch.h"
29#include "llvm/IR/DataLayout.h"
30#include "llvm/Support/Error.h"
39using mlir::LLVM::cconv::getMaxEnumValForCConv;
40using mlir::LLVM::linkage::getMaxEnumValForLinkage;
41using mlir::LLVM::tailcallkind::getMaxEnumValForTailCallKind;
43#include "mlir/Dialect/LLVMIR/LLVMOpsDialect.cpp.inc"
54 if (attr.
getName() ==
"fastmathFlags") {
74 << name <<
"' does not reference a valid LLVM function";
75 if (
func.isExternal())
76 return op->
emitOpError(
"'") << name <<
"' does not have a definition";
94 for (
const auto &en : llvm::enumerate(keywords)) {
102template <
typename Ty>
105#define REGISTER_ENUM_TYPE(Ty) \
107 struct EnumTraits<Ty> { \
108 static StringRef stringify(Ty value) { return stringify##Ty(value); } \
109 static unsigned getMaxEnumVal() { return getMaxEnumValFor##Ty(); } \
122template <
typename EnumTy,
typename RetTy = EnumTy>
124 EnumTy defaultValue) {
126 for (
unsigned i = 0, e = EnumTraits<EnumTy>::getMaxEnumVal(); i <= e; ++i)
127 names.push_back(EnumTraits<EnumTy>::stringify(
static_cast<EnumTy
>(i)));
131 return static_cast<RetTy
>(defaultValue);
132 return static_cast<RetTy
>(
index);
136 p << stringifyLinkage(val.getLinkage());
140 val = LinkageAttr::get(
148 uint64_t alignment = 1) {
154 if (alignment == 1) {
161 builder.
getNamedAttr(LLVMDialect::getAlignAttrName(), alignmentAttr);
171 int pos = isExpandLoad ? 0 : 1;
173 {alignDictAttr, emptyDictAttr, emptyDictAttr})
175 {emptyDictAttr, alignDictAttr, emptyDictAttr});
187 if (!operands.empty()) {
190 llvm::interleaveComma(operandTypes, p);
199 std::optional<ArrayAttr> opBundleTags) {
200 if (opBundleOperands.empty())
202 assert(opBundleTags &&
"expect operand bundle tags");
205 llvm::interleaveComma(
206 llvm::zip(opBundleOperands, opBundleOperandTypes, *opBundleTags), p,
208 auto bundleTag = cast<StringAttr>(std::get<2>(bundle)).getValue();
226 return p.
emitError(currentParserLoc,
"expect operand bundle tag");
237 opBundleOperands.push_back(std::move(operands));
238 opBundleOperandTypes.push_back(std::move(types));
239 opBundleTags.push_back(StringAttr::get(p.
getContext(), tag));
256 auto bundleParser = [&] {
266 opBundleTags = ArrayAttr::get(p.
getContext(), opBundleTagAttrs);
276 p <<
" \"" << stringifyICmpPredicate(getPredicate()) <<
"\" " << getOperand(0)
277 <<
", " << getOperand(1);
279 p <<
" : " << getLhs().getType();
283 p <<
" \"" << stringifyFCmpPredicate(getPredicate()) <<
"\" " << getOperand(0)
284 <<
", " << getOperand(1);
286 p <<
" : " << getLhs().getType();
293template <
typename CmpPredicateType>
295 StringAttr predicateAttr;
298 SMLoc predicateLoc, trailingTypeLoc;
311 if (std::is_same<CmpPredicateType, ICmpPredicate>()) {
312 std::optional<ICmpPredicate> predicate =
313 symbolizeICmpPredicate(predicateAttr.getValue());
316 <<
"'" << predicateAttr.getValue()
317 <<
"' is an incorrect value of the 'predicate' attribute";
318 predicateValue =
static_cast<int64_t>(*predicate);
320 std::optional<FCmpPredicate> predicate =
321 symbolizeFCmpPredicate(predicateAttr.getValue());
324 <<
"'" << predicateAttr.getValue()
325 <<
"' is an incorrect value of the 'predicate' attribute";
326 predicateValue =
static_cast<int64_t>(*predicate);
329 result.attributes.set(
"predicate",
336 "expected LLVM dialect-compatible type");
352 ShapedType shapedType = dyn_cast<ShapedType>(type);
359 if (getPredicate() != ICmpPredicate::eq &&
360 getPredicate() != ICmpPredicate::ne)
364 if (getLhs() == getRhs())
366 getPredicate() == ICmpPredicate::eq);
369 if (getLhs().getDefiningOp<AllocaOp>() && getRhs().getDefiningOp<ZeroOp>())
371 getPredicate() == ICmpPredicate::ne);
374 if (getLhs().getDefiningOp<ZeroOp>() && getRhs().getDefiningOp<AllocaOp>()) {
377 getLhsMutable().assign(
rhs);
378 getRhsMutable().assign(
lhs);
396 p <<
' ' << getArraySize() <<
" x " << getElemType();
397 if (getAlignment() && *getAlignment() != 0)
399 {kElemTypeAttrName, getInallocaAttrName()});
403 {getAlignmentAttrName(), kElemTypeAttrName, getInallocaAttrName()});
404 p <<
" : " << funcTy;
412 SMLoc trailingTypeLoc;
424 std::optional<NamedAttribute> alignmentAttr =
425 result.attributes.getNamed(
"alignment");
426 if (alignmentAttr.has_value()) {
427 auto alignmentInt = llvm::dyn_cast<IntegerAttr>(alignmentAttr->getValue());
430 "expected integer alignment");
431 if (alignmentInt.getValue().isZero())
432 result.attributes.erase(
"alignment");
436 auto funcType = llvm::dyn_cast<FunctionType>(type);
437 if (!funcType || funcType.getNumInputs() != 1 ||
438 funcType.getNumResults() != 1)
441 "expected trailing function type with one argument and one result");
446 Type resultType = funcType.getResult(0);
447 if (
auto ptrResultType = llvm::dyn_cast<LLVMPointerType>(resultType))
450 result.addTypes({funcType.getResult(0)});
454LogicalResult AllocaOp::verify() {
456 if (
auto targetExtType = dyn_cast<LLVMTargetExtType>(getElemType());
457 targetExtType && !targetExtType.supportsMemOps())
459 <<
"this target extension type cannot be used in alloca";
469 assert(
index == 0 &&
"invalid successor index");
478 assert(
index < getNumSuccessors() &&
"invalid successor index");
480 : getFalseDestOperandsMutable());
486 std::optional<std::pair<uint32_t, uint32_t>> weights) {
491 static_cast<int32_t
>(weights->second)});
493 build(builder,
result, condition, trueOperands, falseOperands, weightsAttr,
494 {}, trueDest, falseDest);
508 if (!branchWeights.empty())
511 build(builder,
result, value, defaultOperands, caseOperands, caseValues,
512 weightsAttr, defaultDestination, caseDestinations);
521 if (!caseValues.empty()) {
522 ShapedType caseValueType = VectorType::get(
527 build(builder,
result, value, defaultDestination, defaultOperands,
528 caseValuesAttr, caseDestinations, caseOperands, branchWeights);
537 if (!caseValues.empty()) {
538 ShapedType caseValueType = VectorType::get(
543 build(builder,
result, value, defaultDestination, defaultOperands,
544 caseValuesAttr, caseDestinations, caseOperands, branchWeights);
560 auto parseCase = [&]() {
564 values.push_back(APInt(bitWidth, value,
true));
577 caseDestinations.push_back(destination);
578 caseOperands.emplace_back(operands);
579 caseOperandTypes.emplace_back(operandTypes);
585 ShapedType caseValueType =
586 VectorType::get(
static_cast<int64_t>(values.size()), flagType);
605 llvm::zip(caseValues, caseDestinations),
620LogicalResult SwitchOp::verify() {
621 if ((!getCaseValues() && !getCaseDestinations().empty()) ||
623 getCaseValues()->size() !=
624 static_cast<int64_t>(getCaseDestinations().size())))
625 return emitOpError(
"expects number of case values to match number of "
626 "case destinations");
627 if (getCaseValues() &&
629 return emitError(
"expects case value type to match condition value type");
634 assert(
index < getNumSuccessors() &&
"invalid successor index");
636 : getCaseOperandsMutable(
index - 1));
645 getDynamicIndices());
650 if (
auto vectorType = llvm::dyn_cast<VectorType>(type))
651 return vectorType.getElementType();
668 bool requiresConst = !rawConstantIndices.empty() &&
669 isa_and_nonnull<LLVMStructType>(currType);
670 if (
Value val = llvm::dyn_cast_if_present<Value>(iter)) {
674 rawConstantIndices.push_back(intC.getSExtValue());
676 rawConstantIndices.push_back(GEPOp::kDynamicIndex);
677 dynamicIndices.push_back(val);
680 rawConstantIndices.push_back(cast<GEPConstantIndex>(iter));
685 if (rawConstantIndices.size() == 1 || !currType)
689 .Case<VectorType, LLVMArrayType>([](
auto containerType) {
690 return containerType.getElementType();
692 .Case([&](LLVMStructType structType) ->
Type {
693 int64_t memberIndex = rawConstantIndices.back();
694 if (memberIndex >= 0 &&
static_cast<size_t>(memberIndex) <
695 structType.getBody().size())
696 return structType.getBody()[memberIndex];
705 GEPNoWrapFlags noWrapFlags,
711 result.addTypes(resultType);
712 result.addAttributes(attributes);
713 result.getOrAddProperties<Properties>().rawConstantIndices =
715 result.getOrAddProperties<Properties>().noWrapFlags = noWrapFlags;
716 result.getOrAddProperties<Properties>().elem_type =
717 TypeAttr::get(elementType);
718 result.addOperands(basePtr);
719 result.addOperands(dynamicIndices);
724 GEPNoWrapFlags noWrapFlags,
726 build(builder,
result, resultType, elementType, basePtr,
736 auto idxParser = [&]() -> ParseResult {
737 int32_t constantIndex;
741 if (failed(parsedInteger.
value()))
743 constantIndices.push_back(constantIndex);
747 constantIndices.push_back(LLVM::GEPOp::kDynamicIndex);
761 llvm::interleaveComma(
764 if (
Value val = llvm::dyn_cast_if_present<Value>(cst))
767 printer << cast<IntegerAttr>(cst).getInt();
777 if (indexPos >=
indices.size())
782 .Case([&](LLVMStructType structType) -> LogicalResult {
783 auto attr = dyn_cast<IntegerAttr>(
indices[indexPos]);
785 return emitOpError() <<
"expected index " << indexPos
786 <<
" indexing a struct to be constant";
788 int32_t gepIndex = attr.getInt();
791 static_cast<size_t>(gepIndex) >= elementTypes.size())
793 <<
" indexing a struct is out of bounds";
800 .Case<VectorType, LLVMArrayType>(
801 [&](
auto containerType) -> LogicalResult {
805 .Default([&](
auto otherType) -> LogicalResult {
807 <<
"type " << otherType <<
" cannot be indexed (index #"
819LogicalResult LLVM::GEPOp::verify() {
820 if (
static_cast<size_t>(
821 llvm::count(getRawConstantIndices(), kDynamicIndex)) !=
822 getDynamicIndices().size())
823 return emitOpError(
"expected as many dynamic indices as specified in '")
824 << getRawConstantIndicesAttrName().getValue() <<
"'";
826 if (getNoWrapFlags() == GEPNoWrapFlags::inboundsFlag)
827 return emitOpError(
"'inbounds_flag' cannot be used directly.");
837void LoadOp::getEffects(
846 if (getVolatile_() || (getOrdering() != AtomicOrdering::not_atomic &&
847 getOrdering() != AtomicOrdering::unordered)) {
858 if (!isa<IntegerType, LLVMPointerType>(type))
863 if (bitWidth.isScalable())
866 return bitWidth >= 8 && (bitWidth & (bitWidth - 1)) == 0;
870template <
typename OpTy>
874 if (memOp.getOrdering() != AtomicOrdering::not_atomic) {
877 return memOp.emitOpError(
"unsupported type ")
878 << valueType <<
" for atomic access";
879 if (llvm::is_contained(unsupportedOrderings, memOp.getOrdering()))
880 return memOp.emitOpError(
"unsupported ordering '")
881 << stringifyAtomicOrdering(memOp.getOrdering()) <<
"'";
882 if (!memOp.getAlignment())
883 return memOp.emitOpError(
"expected alignment for atomic access");
886 if (memOp.getSyncscope())
887 return memOp.emitOpError(
888 "expected syncscope to be null for non-atomic access");
892LogicalResult LoadOp::verify() {
893 Type valueType = getResult().getType();
895 {AtomicOrdering::release, AtomicOrdering::acq_rel});
899 Value addr,
unsigned alignment,
bool isVolatile,
900 bool isNonTemporal,
bool isInvariant,
bool isInvariantGroup,
901 AtomicOrdering ordering, StringRef syncscope) {
902 build(builder, state, type, addr,
904 isNonTemporal, isInvariant, isInvariantGroup, ordering,
905 syncscope.empty() ?
nullptr : builder.
getStringAttr(syncscope),
916void StoreOp::getEffects(
925 if (getVolatile_() || (getOrdering() != AtomicOrdering::not_atomic &&
926 getOrdering() != AtomicOrdering::unordered)) {
932LogicalResult StoreOp::verify() {
933 Type valueType = getValue().getType();
935 {AtomicOrdering::acquire, AtomicOrdering::acq_rel});
939 Value addr,
unsigned alignment,
bool isVolatile,
940 bool isNonTemporal,
bool isInvariantGroup,
941 AtomicOrdering ordering, StringRef syncscope) {
942 build(builder, state, value, addr,
944 isNonTemporal, isInvariantGroup, ordering,
945 syncscope.empty() ?
nullptr : builder.
getStringAttr(syncscope),
947 nullptr,
nullptr,
nullptr);
957 Type resultType = calleeType.getReturnType();
958 if (!isa<LLVM::LLVMVoidType>(resultType))
959 results.push_back(resultType);
965 return calleeType.isVarArg() ? TypeAttr::get(calleeType) :
nullptr;
973 resultType = LLVMVoidType::get(context);
975 resultType = results.front();
976 return LLVMFunctionType::get(resultType, llvm::to_vector(args.
getTypes()),
982 build(builder, state, results, builder.
getStringAttr(callee), args);
987 build(builder, state, results, SymbolRefAttr::get(callee), args);
992 assert(callee &&
"expected non-null callee in direct call builder");
993 build(builder, state, results,
994 nullptr, callee, args,
nullptr,
997 nullptr,
nullptr,
nullptr,
998 nullptr,
nullptr,
nullptr,
1002 nullptr,
nullptr,
nullptr,
1016 LLVMFunctionType calleeType, StringRef callee,
1018 build(builder, state, calleeType, builder.
getStringAttr(callee), args);
1022 LLVMFunctionType calleeType, StringAttr callee,
1024 build(builder, state, calleeType, SymbolRefAttr::get(callee), args);
1042 nullptr,
nullptr,
nullptr,
1050 nullptr,
nullptr,
nullptr,
1056 LLVMFunctionType calleeType,
ValueRange args) {
1061 nullptr,
nullptr,
nullptr,
1062 nullptr,
nullptr,
nullptr,
1068 nullptr,
nullptr,
nullptr,
1083 auto calleeType =
func.getFunctionType();
1087 nullptr,
nullptr,
nullptr,
1088 nullptr,
nullptr,
nullptr,
1094 nullptr,
nullptr,
nullptr,
1112 return getOperand(0);
1118 auto symRef = cast<SymbolRefAttr>(callee);
1119 return setCalleeAttr(cast<FlatSymbolRefAttr>(symRef));
1122 return setOperand(0, cast<Value>(callee));
1126 return getCalleeOperands().drop_front(getCallee().has_value() ? 0 : 1);
1131 getCalleeOperands().size());
1138 if (callee.isExternal())
1140 auto parentFunc = callOp->getParentOfType<FunctionOpInterface>();
1144 auto hasSubprogram = [](
Operation *op) {
1149 if (!hasSubprogram(parentFunc) || !hasSubprogram(callee))
1151 bool containsLoc = !isa<UnknownLoc>(callOp->getLoc());
1153 return callOp.emitError()
1154 <<
"inlinable function call in a function with a DISubprogram "
1155 "location must have a debug location";
1161template <
typename OpTy>
1163 std::optional<LLVMFunctionType> varCalleeType = callOp.getVarCalleeType();
1168 if (!varCalleeType->isVarArg())
1169 return callOp.emitOpError(
1170 "expected var_callee_type to be a variadic function type");
1174 if (varCalleeType->getNumParams() > callOp.getArgOperands().size())
1175 return callOp.emitOpError(
"expected var_callee_type to have at most ")
1176 << callOp.getArgOperands().size() <<
" parameters";
1179 for (
auto [paramType, operand] :
1180 llvm::zip(varCalleeType->getParams(), callOp.getArgOperands()))
1181 if (paramType != operand.getType())
1182 return callOp.emitOpError()
1183 <<
"var_callee_type parameter type mismatch: " << paramType
1184 <<
" != " << operand.getType();
1187 if (!callOp.getNumResults()) {
1188 if (!isa<LLVMVoidType>(varCalleeType->getReturnType()))
1189 return callOp.emitOpError(
"expected var_callee_type to return void");
1191 if (callOp.getResult().getType() != varCalleeType->getReturnType())
1192 return callOp.emitOpError(
"var_callee_type return type mismatch: ")
1193 << varCalleeType->getReturnType()
1194 <<
" != " << callOp.getResult().getType();
1199template <
typename OpType>
1202 std::optional<ArrayAttr> opBundleTags = op.getOpBundleTags();
1204 auto isStringAttr = [](
Attribute tagAttr) {
1205 return isa<StringAttr>(tagAttr);
1207 if (opBundleTags && !llvm::all_of(*opBundleTags, isStringAttr))
1208 return op.emitError(
"operand bundle tag must be a StringAttr");
1210 size_t numOpBundles = opBundleOperands.size();
1211 size_t numOpBundleTags = opBundleTags ? opBundleTags->size() : 0;
1212 if (numOpBundles != numOpBundleTags)
1213 return op.emitError(
"expected ")
1214 << numOpBundles <<
" operand bundle tags, but actually got "
1230 bool isIndirect =
false;
1236 if (!getNumOperands())
1238 "must have either a `callee` attribute or at least an operand");
1239 auto ptrType = llvm::dyn_cast<LLVMPointerType>(getOperand(0).
getType());
1241 return emitOpError(
"indirect call expects a pointer as callee: ")
1242 << getOperand(0).getType();
1251 <<
"' does not reference a symbol in the current scope";
1252 if (
auto fn = dyn_cast<LLVMFuncOp>(callee)) {
1255 fnType = fn.getFunctionType();
1256 }
else if (
auto ifunc = dyn_cast<IFuncOp>(callee)) {
1257 fnType = ifunc.getIFuncType();
1258 }
else if (isa<AliasOp>(callee)) {
1262 fnType = getCalleeFunctionType();
1266 <<
"' does not reference a valid LLVM function, IFunc, or alias";
1270 LLVMFunctionType funcType = llvm::dyn_cast<LLVMFunctionType>(fnType);
1272 return emitOpError(
"callee does not have a functional type: ") << fnType;
1274 if (funcType.isVarArg() && !getVarCalleeType())
1275 return emitOpError() <<
"missing var_callee_type attribute for vararg call";
1279 if (!funcType.isVarArg() &&
1280 funcType.getNumParams() != (getCalleeOperands().size() - isIndirect))
1281 return emitOpError() <<
"incorrect number of operands ("
1282 << (getCalleeOperands().size() - isIndirect)
1283 <<
") for callee (expecting: "
1284 << funcType.getNumParams() <<
")";
1286 if (funcType.getNumParams() > (getCalleeOperands().size() - isIndirect))
1287 return emitOpError() <<
"incorrect number of operands ("
1288 << (getCalleeOperands().size() - isIndirect)
1289 <<
") for varargs callee (expecting at least: "
1290 << funcType.getNumParams() <<
")";
1292 for (
unsigned i = 0, e = funcType.getNumParams(); i != e; ++i)
1293 if (getOperand(i + isIndirect).
getType() != funcType.getParamType(i))
1294 return emitOpError() <<
"operand type mismatch for operand " << i <<
": "
1295 << getOperand(i + isIndirect).getType()
1296 <<
" != " << funcType.getParamType(i);
1298 if (getNumResults() == 0 &&
1299 !llvm::isa<LLVM::LLVMVoidType>(funcType.getReturnType()))
1300 return emitOpError() <<
"expected function call to produce a value";
1302 if (getNumResults() != 0 &&
1303 llvm::isa<LLVM::LLVMVoidType>(funcType.getReturnType()))
1305 <<
"calling function with void result must not produce values";
1307 if (getNumResults() > 1)
1309 <<
"expected LLVM function call to produce 0 or 1 result";
1311 if (getNumResults() && getResult().
getType() != funcType.getReturnType())
1312 return emitOpError() <<
"result type mismatch: " << getResult().getType()
1313 <<
" != " << funcType.getReturnType();
1319 auto callee = getCallee();
1320 bool isDirect = callee.has_value();
1325 if (getCConv() != LLVM::CConv::C)
1326 p << stringifyCConv(getCConv()) <<
' ';
1328 if (getTailCallKind() != LLVM::TailCallKind::None)
1329 p << tailcallkind::stringifyTailCallKind(getTailCallKind()) <<
' ';
1338 auto args = getCalleeOperands().drop_front(isDirect ? 0 : 1);
1339 p <<
'(' << args <<
')';
1342 if (std::optional<LLVMFunctionType> varCalleeType = getVarCalleeType())
1343 p <<
" vararg(" << *varCalleeType <<
")";
1345 if (!getOpBundleOperands().empty()) {
1348 getOpBundleOperands().getTypes(), getOpBundleTags());
1352 {getCalleeAttrName(), getTailCallKindAttrName(),
1353 getVarCalleeTypeAttrName(), getCConvAttrName(),
1354 getOperandSegmentSizesAttrName(),
1355 getOpBundleSizesAttrName(),
1356 getOpBundleTagsAttrName(), getArgAttrsAttrName(),
1357 getResAttrsAttrName()});
1361 p << getOperand(0).getType() <<
", ";
1365 p, args.getTypes(), getArgAttrsAttr(),
1366 false, getResultTypes(), getResAttrsAttr());
1381 types.emplace_back();
1386 trailingTypesLoc,
"expected indirect call to have 2 trailing types");
1391 resTypes, resultAttrs)) {
1393 return parser.
emitError(trailingTypesLoc,
1394 "expected direct call to have 1 trailing types");
1395 return parser.
emitError(trailingTypesLoc,
1396 "expected trailing function type");
1399 if (resTypes.size() > 1)
1400 return parser.
emitError(trailingTypesLoc,
1401 "expected function with 0 or 1 result");
1402 if (resTypes.size() == 1 && llvm::isa<LLVM::LLVMVoidType>(resTypes[0]))
1403 return parser.
emitError(trailingTypesLoc,
1404 "expected a non-void result type");
1410 llvm::append_range(types, argTypes);
1414 if (!resTypes.empty())
1415 result.addTypes(resTypes);
1428 if (failed(*parseResult))
1429 return *parseResult;
1430 operands.push_back(funcPtrOperand);
1439 StringAttr opBundleSizesAttrName) {
1440 unsigned opBundleIndex = 0;
1441 for (
const auto &[operands, types] :
1442 llvm::zip_equal(opBundleOperands, opBundleOperandTypes)) {
1443 if (operands.size() != types.size())
1444 return parser.
emitError(loc,
"expected ")
1446 <<
" types for operand bundle operands for operand bundle #"
1447 << opBundleIndex <<
", but actually got " << types.size();
1453 opBundleSizes.reserve(opBundleOperands.size());
1454 for (
const auto &operands : opBundleOperands)
1455 opBundleSizes.push_back(operands.size());
1458 opBundleSizesAttrName,
1470 SymbolRefAttr funcAttr;
1471 TypeAttr varCalleeType;
1479 getCConvAttrName(
result.name),
1484 getTailCallKindAttrName(
result.name),
1487 parser, LLVM::TailCallKind::None)));
1492 bool isDirect = operands.empty();
1505 StringAttr varCalleeTypeAttrName =
1506 CallOp::getVarCalleeTypeAttrName(
result.name);
1518 parser, opBundleOperands, opBundleOperandTypes, opBundleTags);
1521 if (opBundleTags && !opBundleTags.empty())
1522 result.addAttribute(CallOp::getOpBundleTagsAttrName(
result.name).getValue(),
1532 argAttrs, resultAttrs))
1536 getArgAttrsAttrName(
result.name), getResAttrsAttrName(
result.name));
1538 opBundleOperandTypes,
1539 getOpBundleSizesAttrName(
result.name)))
1542 int32_t numOpBundleOperands = 0;
1543 for (
const auto &operands : opBundleOperands)
1544 numOpBundleOperands += operands.size();
1547 CallOp::getOperandSegmentSizeAttr(),
1549 {static_cast<int32_t>(operands.size()), numOpBundleOperands}));
1553LLVMFunctionType CallOp::getCalleeFunctionType() {
1554 if (std::optional<LLVMFunctionType> varCalleeType = getVarCalleeType())
1555 return *varCalleeType;
1566 auto calleeType =
func.getFunctionType();
1569 nullptr,
nullptr, normalOps, unwindOps,
1570 nullptr,
nullptr, {}, {}, normal, unwind);
1577 build(builder, state, tys,
1578 nullptr, callee, ops,
nullptr,
1579 nullptr, normalOps, unwindOps,
nullptr,
nullptr, {}, {},
1589 nullptr,
nullptr, normalOps, unwindOps,
1590 nullptr,
nullptr, {}, {}, normal, unwind);
1594 assert(
index < getNumSuccessors() &&
"invalid successor index");
1596 : getUnwindDestOperandsMutable());
1604 return getOperand(0);
1610 auto symRef = cast<SymbolRefAttr>(callee);
1611 return setCalleeAttr(cast<FlatSymbolRefAttr>(symRef));
1614 return setOperand(0, cast<Value>(callee));
1618 return getCalleeOperands().drop_front(getCallee().has_value() ? 0 : 1);
1623 getCalleeOperands().size());
1626LogicalResult InvokeOp::verify() {
1630 Block *unwindDest = getUnwindDest();
1631 if (unwindDest->
empty())
1632 return emitError(
"must have at least one operation in unwind destination");
1635 if (!isa<LandingpadOp>(unwindDest->
front()))
1636 return emitError(
"first operation in unwind destination should be a "
1637 "llvm.landingpad operation");
1646 auto callee = getCallee();
1647 bool isDirect = callee.has_value();
1652 if (getCConv() != LLVM::CConv::C)
1653 p << stringifyCConv(getCConv()) <<
' ';
1661 p <<
'(' << getCalleeOperands().drop_front(isDirect ? 0 : 1) <<
')';
1668 if (std::optional<LLVMFunctionType> varCalleeType = getVarCalleeType())
1669 p <<
" vararg(" << *varCalleeType <<
")";
1671 if (!getOpBundleOperands().empty()) {
1674 getOpBundleOperands().getTypes(), getOpBundleTags());
1678 {getCalleeAttrName(), getOperandSegmentSizeAttr(),
1679 getCConvAttrName(), getVarCalleeTypeAttrName(),
1680 getOpBundleSizesAttrName(),
1681 getOpBundleTagsAttrName(), getArgAttrsAttrName(),
1682 getResAttrsAttrName()});
1686 p << getOperand(0).getType() <<
", ";
1688 p, getCalleeOperands().drop_front(isDirect ? 0 : 1).getTypes(),
1690 false, getResultTypes(), getResAttrsAttr());
1703 SymbolRefAttr funcAttr;
1704 TypeAttr varCalleeType;
1708 Block *normalDest, *unwindDest;
1714 getCConvAttrName(
result.name),
1721 bool isDirect = operands.empty();
1737 StringAttr varCalleeTypeAttrName =
1738 InvokeOp::getVarCalleeTypeAttrName(
result.name);
1750 parser, opBundleOperands, opBundleOperandTypes, opBundleTags);
1753 if (opBundleTags && !opBundleTags.empty())
1755 InvokeOp::getOpBundleTagsAttrName(
result.name).getValue(),
1765 argAttrs, resultAttrs))
1769 getArgAttrsAttrName(
result.name), getResAttrsAttrName(
result.name));
1772 opBundleOperandTypes,
1773 getOpBundleSizesAttrName(
result.name)))
1776 result.addSuccessors({normalDest, unwindDest});
1777 result.addOperands(normalOperands);
1778 result.addOperands(unwindOperands);
1780 int32_t numOpBundleOperands = 0;
1781 for (
const auto &operands : opBundleOperands)
1782 numOpBundleOperands += operands.size();
1785 InvokeOp::getOperandSegmentSizeAttr(),
1787 static_cast<int32_t>(normalOperands.size()),
1788 static_cast<int32_t>(unwindOperands.size()),
1789 numOpBundleOperands}));
1793LLVMFunctionType InvokeOp::getCalleeFunctionType() {
1794 if (std::optional<LLVMFunctionType> varCalleeType = getVarCalleeType())
1795 return *varCalleeType;
1803LogicalResult LandingpadOp::verify() {
1805 if (LLVMFuncOp
func = (*this)->getParentOfType<LLVMFuncOp>()) {
1806 if (!
func.getPersonality())
1808 "llvm.landingpad needs to be in a function with a personality");
1814 if (!getCleanup() && getOperands().empty())
1815 return emitError(
"landingpad instruction expects at least one clause or "
1816 "cleanup attribute");
1818 for (
unsigned idx = 0, ie = getNumOperands(); idx < ie; idx++) {
1819 value = getOperand(idx);
1820 bool isFilter = llvm::isa<LLVMArrayType>(value.
getType());
1827 if (
auto addrOp = bcOp.getArg().getDefiningOp<AddressOfOp>())
1830 <<
"global addresses expected as operand to "
1831 "bitcast used in clauses for landingpad";
1839 << idx <<
" is not a known constant - null, addressof, bitcast";
1846 p << (getCleanup() ?
" cleanup " :
" ");
1849 for (
auto value : getOperands()) {
1852 bool isArrayTy = llvm::isa<LLVMArrayType>(value.
getType());
1853 p <<
'(' << (isArrayTy ?
"filter " :
"catch ") << value <<
" : "
1900 Type llvmType = containerType;
1902 emitError(
"expected LLVM IR Dialect type, got ") << containerType;
1910 for (
int64_t idx : position) {
1911 if (
auto arrayType = llvm::dyn_cast<LLVMArrayType>(llvmType)) {
1912 if (idx < 0 ||
static_cast<unsigned>(idx) >= arrayType.getNumElements()) {
1913 emitError(
"position out of bounds: ") << idx;
1916 llvmType = arrayType.getElementType();
1917 }
else if (
auto structType = llvm::dyn_cast<LLVMStructType>(llvmType)) {
1919 static_cast<unsigned>(idx) >= structType.getBody().size()) {
1920 emitError(
"position out of bounds: ") << idx;
1923 llvmType = structType.getBody()[idx];
1925 emitError(
"expected LLVM IR structure/array type, got: ") << llvmType;
1936 for (
int64_t idx : position) {
1937 if (
auto structType = llvm::dyn_cast<LLVMStructType>(llvmType))
1938 llvmType = structType.getBody()[idx];
1940 llvmType = llvm::cast<LLVMArrayType>(llvmType).getElementType();
1951 if (
auto elementsAttr = dyn_cast<ElementsAttr>(attr)) {
1952 if (
index <
static_cast<size_t>(elementsAttr.getNumElements()))
1956 if (
auto arrayAttr = dyn_cast<ArrayAttr>(attr)) {
1957 if (
index < arrayAttr.getValue().size())
1958 return arrayAttr[
index];
1961 if (isa<ZeroAttr, UndefAttr, PoisonAttr>(attr))
1966OpFoldResult LLVM::ExtractValueOp::fold(FoldAdaptor adaptor) {
1967 if (
auto extractValueOp = getContainer().getDefiningOp<ExtractValueOp>()) {
1969 newPos.append(getPosition().begin(), getPosition().end());
1970 setPosition(newPos);
1971 getContainerMutable().set(extractValueOp.getContainer());
1977 for (
int64_t pos : getPosition()) {
1982 return containerAttr;
1985 Value container = getContainer();
1987 while (
auto insertValueOp = container.
getDefiningOp<InsertValueOp>()) {
1989 auto extractPosSize = extractPos.size();
1990 auto insertPosSize = insertPos.size();
1993 if (extractPos == insertPos)
1994 return insertValueOp.getValue();
2008 if (extractPosSize > insertPosSize &&
2009 extractPos.take_front(insertPosSize) == insertPos) {
2010 container = insertValueOp.getValue();
2011 extractPos = extractPos.drop_front(insertPosSize);
2027 if (insertPosSize > extractPosSize &&
2028 extractPos == insertPos.take_front(extractPosSize))
2033 container = insertValueOp.getContainer();
2039 if (container == getContainer())
2041 setPosition(extractPos);
2042 getContainerMutable().assign(container);
2046LogicalResult ExtractValueOp::verify() {
2053 if (getRes().
getType() != valueType)
2054 return emitOpError() <<
"Type mismatch: extracting from "
2055 << getContainer().getType() <<
" should produce "
2056 << valueType <<
" but this op returns "
2057 << getRes().getType();
2063 build(builder, state,
2102 LogicalResult matchAndRewrite(InsertValueOp insertOp,
2103 PatternRewriter &rewriter)
const override {
2104 bool changed =
false;
2110 auto insertBaseIdx = insertOp.getPosition()[0];
2111 for (
auto &use : insertOp->getUses()) {
2112 if (
auto extractOp = dyn_cast<ExtractValueOp>(use.getOwner())) {
2113 auto baseIdx = extractOp.getPosition()[0];
2116 if (baseIdx == insertBaseIdx)
2118 posToExtractOps[baseIdx].push_back(extractOp);
2123 Value nextContainer = insertOp.getContainer();
2124 while (!posToExtractOps.empty()) {
2126 dyn_cast_or_null<InsertValueOp>(nextContainer.
getDefiningOp());
2129 nextContainer = curInsert.getContainer();
2132 auto curInsertBaseIdx = curInsert.getPosition()[0];
2133 auto it = posToExtractOps.find(curInsertBaseIdx);
2134 if (it == posToExtractOps.end())
2138 for (
auto &extractOp : it->second) {
2140 extractOp.getContainerMutable().assign(curInsert);
2145 assert(!it->second.empty());
2147 posToExtractOps.erase(it);
2151 for (
auto &[baseIdx, extracts] : posToExtractOps) {
2152 for (
auto &extractOp : extracts) {
2154 extractOp.getContainerMutable().assign(nextContainer);
2157 assert(!extracts.empty() &&
"Empty list in map");
2167 patterns.
add<ResolveExtractValueSource>(context);
2176 [&](StringRef msg) {
2189LogicalResult InsertValueOp::verify() {
2196 if (getValue().
getType() != valueType)
2197 return emitOpError() <<
"Type mismatch: cannot insert "
2198 << getValue().getType() <<
" into "
2199 << getContainer().getType();
2208LogicalResult ReturnOp::verify() {
2209 auto parent = (*this)->getParentOfType<LLVMFuncOp>();
2213 Type expectedType = parent.getFunctionType().getReturnType();
2214 if (llvm::isa<LLVMVoidType>(expectedType)) {
2218 diag.attachNote(parent->getLoc()) <<
"when returning from function";
2222 if (llvm::isa<LLVMVoidType>(expectedType))
2225 diag.attachNote(parent->getLoc()) <<
"when returning from function";
2228 if (expectedType != getArg().
getType()) {
2230 diag.attachNote(parent->getLoc()) <<
"when returning from function";
2241 return dyn_cast_or_null<GlobalOp>(
2246 return dyn_cast_or_null<LLVMFuncOp>(
2251 return dyn_cast_or_null<AliasOp>(
2256 return dyn_cast_or_null<IFuncOp>(
2265 auto global = dyn_cast_or_null<GlobalOp>(symbol);
2266 auto function = dyn_cast_or_null<LLVMFuncOp>(symbol);
2267 auto alias = dyn_cast_or_null<AliasOp>(symbol);
2268 auto ifunc = dyn_cast_or_null<IFuncOp>(symbol);
2270 if (!global && !function && !alias && !ifunc)
2271 return emitOpError(
"must reference a global defined by 'llvm.mlir.global', "
2272 "'llvm.mlir.alias' or 'llvm.func' or 'llvm.mlir.ifunc'");
2274 LLVMPointerType type =
getType();
2275 if ((global && global.getAddrSpace() != type.getAddressSpace()) ||
2276 (alias && alias.getAddrSpace() != type.getAddressSpace()))
2277 return emitOpError(
"pointer address space must match address space of the "
2278 "referenced global or alias");
2285 return getGlobalNameAttr();
2306 getFunctionNameAttr());
2307 auto function = dyn_cast_or_null<LLVMFuncOp>(symbol);
2308 auto alias = dyn_cast_or_null<AliasOp>(symbol);
2310 if (!function && !alias)
2312 "must reference a global defined by 'llvm.func' or 'llvm.mlir.alias'");
2315 if (alias.getInitializer()
2316 .walk([&](AddressOfOp addrOp) {
2317 if (addrOp.getGlobal(symbolTable))
2318 return WalkResult::interrupt();
2319 return WalkResult::advance();
2322 return emitOpError(
"must reference an alias to a function");
2325 if ((function && function.getLinkage() == LLVM::Linkage::ExternWeak) ||
2326 (alias && alias.getLinkage() == LLVM::Linkage::ExternWeak))
2328 "target function with 'extern_weak' linkage not allowed");
2336 return DSOLocalEquivalentAttr::get(
getContext(), getFunctionNameAttr());
2344 StringRef symName) {
2347 Region *body =
result.addRegion();
2351LogicalResult ComdatOp::verifyRegions() {
2352 Region &body = getBody();
2353 for (Operation &op : body.
getOps())
2354 if (!isa<ComdatSelectorOp>(op))
2355 return op.emitError(
2356 "only comdat selector symbols can appear in a comdat region");
2366 bool isConstant, Linkage linkage, StringRef name,
2367 Attribute value, uint64_t alignment,
unsigned addrSpace,
2368 bool dsoLocal,
bool threadLocal, SymbolRefAttr comdat,
2373 result.addAttribute(getGlobalTypeAttrName(
result.name), TypeAttr::get(type));
2378 result.addAttribute(getValueAttrName(
result.name), value);
2383 result.addAttribute(getThreadLocal_AttrName(
result.name),
2386 result.addAttribute(getComdatAttrName(
result.name), comdat);
2396 LinkageAttr::get(builder.
getContext(), linkage));
2400 result.attributes.append(attrs.begin(), attrs.end());
2402 if (!dbgExprs.empty())
2404 ArrayAttr::get(builder.
getContext(), dbgExprs));
2409template <
typename OpType>
2411 p <<
' ' << stringifyLinkage(op.getLinkage()) <<
' ';
2412 StringRef visibility = stringifyVisibility(op.getVisibility_());
2413 if (!visibility.empty())
2414 p << visibility <<
' ';
2415 if (op.getThreadLocal_())
2416 p <<
"thread_local ";
2417 if (
auto unnamedAddr = op.getUnnamedAddr()) {
2418 StringRef str = stringifyUnnamedAddr(*unnamedAddr);
2430 if (
auto value = getValueOrNull())
2433 if (
auto comdat = getComdat())
2434 p <<
" comdat(" << *comdat <<
')';
2440 {SymbolTable::getSymbolAttrName(),
2441 getGlobalTypeAttrName(), getConstantAttrName(),
2442 getValueAttrName(), getLinkageAttrName(),
2443 getUnnamedAddrAttrName(), getThreadLocal_AttrName(),
2444 getVisibility_AttrName(), getComdatAttrName()});
2447 if (llvm::dyn_cast_or_null<StringAttr>(getValueOrNull()))
2451 Region &initializer = getInitializerRegion();
2452 if (!initializer.
empty()) {
2459 std::optional<SymbolRefAttr> attr) {
2464 if (!isa_and_nonnull<ComdatSelectorOp>(comdatSelector))
2465 return op->
emitError() <<
"expected comdat symbol";
2475 WalkResult res = funcOp.walk([&](BlockTagOp blockTagOp) {
2476 if (blockTags.contains(blockTagOp.getTag())) {
2477 blockTagOp.emitError()
2478 <<
"duplicate block tag '" << blockTagOp.getTag().getId()
2479 <<
"' in the same function: ";
2482 blockTags.insert(blockTagOp.getTag());
2491template <
typename OpType>
2497 OpType::getLinkageAttrName(
result.name),
2499 parser, LLVM::Linkage::External)));
2502 result.addAttribute(OpType::getVisibility_AttrName(
result.name),
2505 parser, LLVM::Visibility::Default)));
2508 result.addAttribute(OpType::getThreadLocal_AttrName(
result.name),
2512 result.addAttribute(OpType::getUnnamedAddrAttrName(
result.name),
2515 parser, LLVM::UnnamedAddr::None)));
2552 SymbolRefAttr comdat;
2557 result.addAttribute(getComdatAttrName(
result.name), comdat);
2565 if (types.size() > 1)
2569 if (types.empty()) {
2570 if (
auto strAttr = llvm::dyn_cast_or_null<StringAttr>(value)) {
2572 auto arrayType = LLVM::LLVMArrayType::get(IntegerType::get(context, 8),
2573 strAttr.getValue().size());
2574 types.push_back(arrayType);
2577 "type can only be omitted for string globals");
2587 result.addAttribute(getGlobalTypeAttrName(
result.name),
2588 TypeAttr::get(types[0]));
2593 if (
auto intValue = llvm::dyn_cast<IntegerAttr>(value))
2594 return intValue.getValue().isZero();
2595 if (
auto fpValue = llvm::dyn_cast<FloatAttr>(value))
2596 return fpValue.getValue().isZero();
2597 if (
auto splatValue = llvm::dyn_cast<SplatElementsAttr>(value))
2599 if (
auto elementsValue = llvm::dyn_cast<ElementsAttr>(value))
2601 if (
auto arrayValue = llvm::dyn_cast<ArrayAttr>(value))
2606LogicalResult GlobalOp::verify() {
2608 ? !llvm::isa<LLVMVoidType, LLVMTokenType,
2609 LLVMMetadataType, LLVMLabelType>(
getType())
2610 :
llvm::isa<PointerElementTypeInterface>(
getType());
2613 "expects type to be a valid element type for an LLVM global");
2615 return emitOpError(
"must appear at the module level");
2617 if (
auto strAttr = llvm::dyn_cast_or_null<StringAttr>(getValueOrNull())) {
2618 auto type = llvm::dyn_cast<LLVMArrayType>(
getType());
2619 IntegerType elementType =
2620 type ? llvm::dyn_cast<IntegerType>(type.getElementType()) :
nullptr;
2621 if (!elementType || elementType.getWidth() != 8 ||
2622 type.getNumElements() != strAttr.getValue().size())
2624 "requires an i8 array type of the length equal to that of the string "
2628 if (
auto targetExtType = dyn_cast<LLVMTargetExtType>(
getType())) {
2629 if (!targetExtType.hasProperty(LLVMTargetExtType::CanBeGlobal))
2631 <<
"this target extension type cannot be used in a global";
2634 return emitOpError() <<
"global with target extension type can only be "
2635 "initialized with zero-initializer";
2638 if (getLinkage() == Linkage::Common) {
2639 if (
Attribute value = getValueOrNull()) {
2642 <<
"expected zero value for '"
2643 << stringifyLinkage(Linkage::Common) <<
"' linkage";
2648 if (getLinkage() == Linkage::Appending) {
2649 if (!llvm::isa<LLVMArrayType>(
getType())) {
2650 return emitOpError() <<
"expected array type for '"
2651 << stringifyLinkage(Linkage::Appending)
2659 std::optional<uint64_t> alignAttr = getAlignment();
2660 if (alignAttr.has_value()) {
2661 uint64_t value = alignAttr.value();
2662 if (!llvm::isPowerOf2_64(value))
2663 return emitError() <<
"alignment attribute is not a power of 2";
2669LogicalResult GlobalOp::verifyRegions() {
2670 if (
Block *
b = getInitializerBlock()) {
2671 ReturnOp ret = cast<ReturnOp>(
b->getTerminator());
2672 if (ret.operand_type_begin() == ret.operand_type_end())
2673 return emitOpError(
"initializer region cannot return void");
2674 if (*ret.operand_type_begin() !=
getType())
2676 << *ret.operand_type_begin() <<
" does not match global type "
2680 auto iface = dyn_cast<MemoryEffectOpInterface>(op);
2681 if (!iface || !iface.hasNoEffect())
2682 return op.emitError()
2683 <<
"ops with side effects not allowed in global initializers";
2686 if (getValueOrNull())
2687 return emitOpError(
"cannot have both initializer value and region");
2702 return isa<FlatSymbolRefAttr, ZeroAttr>(v);
2705 return op->
emitError(
"data element must be symbol or #llvm.zero");
2718LogicalResult GlobalCtorsOp::verify() {
2722 if (getCtors().size() == getPriorities().size() &&
2723 getCtors().size() == getData().size())
2726 "ctors, priorities, and data must have the same number of elements");
2743LogicalResult GlobalDtorsOp::verify() {
2747 if (getDtors().size() == getPriorities().size() &&
2748 getDtors().size() == getData().size())
2751 "dtors, priorities, and data must have the same number of elements");
2759 Linkage linkage, StringRef name,
bool dsoLocal,
2763 result.addAttribute(getAliasTypeAttrName(
result.name), TypeAttr::get(type));
2768 result.addAttribute(getThreadLocal_AttrName(
result.name),
2772 LinkageAttr::get(builder.
getContext(), linkage));
2773 result.attributes.append(attrs.begin(), attrs.end());
2783 {SymbolTable::getSymbolAttrName(),
2784 getAliasTypeAttrName(), getLinkageAttrName(),
2785 getUnnamedAddrAttrName(), getThreadLocal_AttrName(),
2786 getVisibility_AttrName()});
2789 p <<
" : " <<
getType() <<
' ';
2815 if (types.size() > 1)
2823 TypeAttr::get(types[0]));
2827LogicalResult AliasOp::verify() {
2829 ? !llvm::isa<LLVMVoidType, LLVMTokenType,
2830 LLVMMetadataType, LLVMLabelType>(
getType())
2831 :
llvm::isa<PointerElementTypeInterface>(
getType());
2834 "expects type to be a valid element type for an LLVM global alias");
2837 switch (getLinkage()) {
2838 case Linkage::External:
2839 case Linkage::Internal:
2840 case Linkage::Private:
2842 case Linkage::WeakODR:
2843 case Linkage::Linkonce:
2844 case Linkage::LinkonceODR:
2845 case Linkage::AvailableExternally:
2849 <<
"'" << stringifyLinkage(getLinkage())
2850 <<
"' linkage not supported in aliases, available options: private, "
2851 "internal, linkonce, weak, linkonce_odr, weak_odr, external or "
2852 "available_externally";
2858LogicalResult AliasOp::verifyRegions() {
2859 Block &
b = getInitializerBlock();
2860 auto ret = cast<ReturnOp>(
b.getTerminator());
2861 if (ret.getNumOperands() == 0 ||
2862 !isa<LLVM::LLVMPointerType>(ret.getOperand(0).getType()))
2863 return emitOpError(
"initializer region must always return a pointer");
2866 auto iface = dyn_cast<MemoryEffectOpInterface>(op);
2867 if (!iface || !iface.hasNoEffect())
2868 return op.emitError()
2869 <<
"ops with side effects are not allowed in alias initializers";
2875unsigned AliasOp::getAddrSpace() {
2876 Block &initializer = getInitializerBlock();
2878 auto ptrTy = cast<LLVMPointerType>(ret.getOperand(0).getType());
2879 return ptrTy.getAddressSpace();
2887 Type iFuncType, StringRef resolverName,
Type resolverType,
2888 Linkage linkage, LLVM::Visibility visibility) {
2889 return build(builder,
result, name, iFuncType, resolverName, resolverType,
2891 UnnamedAddr::None, visibility);
2898 auto resolver = dyn_cast<LLVMFuncOp>(symbol);
2899 auto alias = dyn_cast<AliasOp>(symbol);
2901 Block &initBlock = alias.getInitializerBlock();
2903 auto addrOp = returnOp.getArg().getDefiningOp<AddressOfOp>();
2910 resolver = addrOp.getFunction(symbolTable);
2911 alias = addrOp.getAlias(symbolTable);
2914 return emitOpError(
"must have a function resolver");
2915 Linkage linkage = resolver.getLinkage();
2916 if (resolver.isExternal() || linkage == Linkage::AvailableExternally)
2917 return emitOpError(
"resolver must be a definition");
2918 if (!isa<LLVMPointerType>(resolver.getFunctionType().getReturnType()))
2919 return emitOpError(
"resolver must return a pointer");
2920 auto resolverPtr = dyn_cast<LLVMPointerType>(getResolverType());
2921 if (!resolverPtr || resolverPtr.getAddressSpace() != getAddressSpace())
2922 return emitOpError(
"resolver has incorrect type");
2926LogicalResult IFuncOp::verify() {
2927 switch (getLinkage()) {
2928 case Linkage::External:
2929 case Linkage::Internal:
2930 case Linkage::Private:
2932 case Linkage::WeakODR:
2933 case Linkage::Linkonce:
2934 case Linkage::LinkonceODR:
2937 return emitOpError() <<
"'" << stringifyLinkage(getLinkage())
2938 <<
"' linkage not supported in ifuncs, available "
2939 "options: private, internal, linkonce, weak, "
2940 "linkonce_odr, weak_odr, or external linkage";
2952 auto containerType = v1.
getType();
2956 build(builder, state, vType, v1, v2, mask);
2970 "expected an LLVM compatible vector type");
2981LogicalResult ShuffleVectorOp::verify() {
2983 llvm::any_of(getMask(), [](int32_t v) {
return v != 0; }))
2984 return emitOpError(
"expected a splat operation for scalable vectors");
2990OpFoldResult ShuffleVectorOp::fold(FoldAdaptor adaptor) {
2992 auto vecType = llvm::dyn_cast<VectorType>(getV1().
getType());
2993 if (!vecType || vecType.getRank() != 1 || vecType.getNumElements() != 1)
2997 if (getMask().size() != 1 || getMask()[0] != 0)
3008 assert(empty() &&
"function already has an entry block");
3013 LLVMFunctionType type = getFunctionType();
3014 for (
unsigned i = 0, e = type.getNumParams(); i < e; ++i)
3015 entry->
addArgument(type.getParamType(i), getLoc());
3020 StringRef name,
Type type, LLVM::Linkage linkage,
3021 bool dsoLocal, CConv cconv, SymbolRefAttr comdat,
3024 std::optional<uint64_t> functionEntryCount) {
3028 result.addAttribute(getFunctionTypeAttrName(
result.name),
3029 TypeAttr::get(type));
3031 LinkageAttr::get(builder.
getContext(), linkage));
3033 CConvAttr::get(builder.
getContext(), cconv));
3034 result.attributes.append(attrs.begin(), attrs.end());
3039 result.addAttribute(getComdatAttrName(
result.name), comdat);
3040 if (functionEntryCount)
3041 result.addAttribute(getFunctionEntryCountAttrName(
result.name),
3044 std::optional<NamedAttribute> duplicate =
result.attributes.findDuplicate();
3045 if (duplicate.has_value()) {
3046 llvm::report_fatal_error(
3047 Twine(
"LLVMFuncOp propagated an attribute that is meant "
3048 "to be constructed by the builder: ") +
3049 duplicate->getName().str());
3052 if (argAttrs.empty())
3055 assert(llvm::cast<LLVMFunctionType>(type).getNumParams() == argAttrs.size() &&
3056 "expected as many argument attribute lists as arguments");
3058 builder,
result, argAttrs, {},
3059 getArgAttrsAttrName(
result.name), getResAttrsAttrName(
result.name));
3070 if (outputs.size() > 1) {
3071 parser.
emitError(loc,
"failed to construct function type: expected zero or "
3072 "one function result");
3078 for (
auto t : inputs) {
3080 parser.
emitError(loc,
"failed to construct function type: expected LLVM "
3081 "type for function arguments");
3084 llvmInputs.push_back(t);
3089 outputs.empty() ? LLVMVoidType::get(
b.getContext()) : outputs.front();
3091 parser.
emitError(loc,
"failed to construct function type: expected LLVM "
3092 "type for function results")
3096 return LLVMFunctionType::get(llvmOutput, llvmInputs,
3112 parser, LLVM::Linkage::External)));
3115 result.addAttribute(getVisibility_AttrName(
result.name),
3118 parser, LLVM::Visibility::Default)));
3121 result.addAttribute(getUnnamedAddrAttrName(
result.name),
3124 parser, LLVM::UnnamedAddr::None)));
3128 getCConvAttrName(
result.name),
3132 StringAttr nameAttr;
3142 parser,
true, entryArgs, isVariadic, resultTypes,
3147 for (
auto &arg : entryArgs)
3148 argTypes.push_back(arg.type);
3154 result.addAttribute(getFunctionTypeAttrName(
result.name),
3155 TypeAttr::get(type));
3163 auto intTy = IntegerType::get(parser.
getContext(), 32);
3165 getVscaleRangeAttrName(
result.name),
3166 LLVM::VScaleRangeAttr::get(parser.
getContext(),
3167 IntegerAttr::get(intTy, minRange),
3168 IntegerAttr::get(intTy, maxRange)));
3172 SymbolRefAttr comdat;
3177 result.addAttribute(getComdatAttrName(
result.name), comdat);
3184 getArgAttrsAttrName(
result.name), getResAttrsAttrName(
result.name));
3186 auto *body =
result.addRegion();
3197 if (getLinkage() != LLVM::Linkage::External)
3198 p << stringifyLinkage(getLinkage()) <<
' ';
3199 StringRef visibility = stringifyVisibility(getVisibility_());
3200 if (!visibility.empty())
3201 p << visibility <<
' ';
3202 if (
auto unnamedAddr = getUnnamedAddr()) {
3203 StringRef str = stringifyUnnamedAddr(*unnamedAddr);
3207 if (getCConv() != LLVM::CConv::C)
3208 p << stringifyCConv(getCConv()) <<
' ';
3212 LLVMFunctionType fnType = getFunctionType();
3215 argTypes.reserve(fnType.getNumParams());
3216 for (
unsigned i = 0, e = fnType.getNumParams(); i < e; ++i)
3217 argTypes.push_back(fnType.getParamType(i));
3219 Type returnType = fnType.getReturnType();
3220 if (!llvm::isa<LLVMVoidType>(returnType))
3221 resTypes.push_back(returnType);
3224 isVarArg(), resTypes);
3227 if (std::optional<VScaleRangeAttr> vscale = getVscaleRange())
3228 p <<
" vscale_range(" << vscale->getMinRange().getInt() <<
", "
3229 << vscale->getMaxRange().getInt() <<
')';
3232 if (
auto comdat = getComdat())
3233 p <<
" comdat(" << *comdat <<
')';
3237 {getFunctionTypeAttrName(), getArgAttrsAttrName(), getResAttrsAttrName(),
3238 getLinkageAttrName(), getCConvAttrName(), getVisibility_AttrName(),
3239 getComdatAttrName(), getUnnamedAddrAttrName(),
3240 getVscaleRangeAttrName()});
3243 Region &body = getBody();
3244 if (!body.empty()) {
3255LogicalResult LLVMFuncOp::verify() {
3256 if (getLinkage() == LLVM::Linkage::Common)
3258 << stringifyLinkage(LLVM::Linkage::Common)
3265 if (getLinkage() != LLVM::Linkage::External &&
3266 getLinkage() != LLVM::Linkage::ExternWeak)
3267 return emitOpError() <<
"external functions must have '"
3268 << stringifyLinkage(LLVM::Linkage::External)
3270 << stringifyLinkage(LLVM::Linkage::ExternWeak)
3276 if (isNoInline() && isAlwaysInline())
3277 return emitError(
"no_inline and always_inline attributes are incompatible");
3279 if (isOptimizeNone() && !isNoInline())
3280 return emitOpError(
"with optimize_none must also be no_inline");
3282 Type landingpadResultTy;
3283 StringRef diagnosticMessage;
3284 bool isLandingpadTypeConsistent =
3286 const auto checkType = [&](
Type type, StringRef errorMessage) {
3287 if (!landingpadResultTy) {
3288 landingpadResultTy = type;
3291 if (landingpadResultTy != type) {
3292 diagnosticMessage = errorMessage;
3298 .Case([&](LandingpadOp landingpad) {
3299 constexpr StringLiteral errorMessage =
3300 "'llvm.landingpad' should have a consistent result type "
3301 "inside a function";
3302 return checkType(landingpad.getType(), errorMessage);
3304 .Case([&](ResumeOp resume) {
3305 constexpr StringLiteral errorMessage =
3306 "'llvm.resume' should have a consistent input type inside a "
3308 return checkType(resume.getValue().getType(), errorMessage);
3311 }).wasInterrupted();
3312 if (!isLandingpadTypeConsistent) {
3313 assert(!diagnosticMessage.empty() &&
3314 "Expecting a non-empty diagnostic message");
3326LogicalResult LLVMFuncOp::verifyRegions() {
3330 unsigned numArguments = getFunctionType().getNumParams();
3331 Block &entryBlock = front();
3332 for (
unsigned i = 0; i < numArguments; ++i) {
3336 << i <<
" is not of LLVM type";
3342Region *LLVMFuncOp::getCallableRegion() {
3370LogicalResult LLVM::ZeroOp::verify() {
3371 if (
auto targetExtType = dyn_cast<LLVMTargetExtType>(
getType()))
3372 if (!targetExtType.hasProperty(LLVM::LLVMTargetExtType::HasZeroInit))
3374 <<
"target extension type does not support zero-initializer";
3396 if (
auto vecType = dyn_cast<VectorType>(t)) {
3397 assert(!vecType.isScalable() &&
3398 "number of elements of a scalable vector type is unknown");
3399 return vecType.getNumElements() *
getNumElements(vecType.getElementType());
3401 if (
auto arrayType = dyn_cast<LLVM::LLVMArrayType>(t))
3402 return arrayType.getNumElements() *
3410 while (
auto arrayType = dyn_cast<LLVM::LLVMArrayType>(type))
3411 type = arrayType.getElementType();
3412 if (
auto vecType = dyn_cast<VectorType>(type))
3413 return vecType.getElementType();
3414 if (
auto tenType = dyn_cast<TensorType>(type))
3415 return tenType.getElementType();
3422 if (
auto vecType = dyn_cast<VectorType>(t)) {
3423 if (vecType.isScalable())
3427 if (
auto arrayType = dyn_cast<LLVM::LLVMArrayType>(t))
3435 LLVM::LLVMArrayType arrayType,
3437 if (arrayType.getNumElements() != arrayAttr.size())
3438 return op.emitOpError()
3439 <<
"array attribute size does not match array type size in "
3441 << dim <<
": " << arrayAttr.size() <<
" vs. "
3442 << arrayType.getNumElements();
3447 if (
auto subArrayType =
3448 dyn_cast<LLVM::LLVMArrayType>(arrayType.getElementType())) {
3449 for (
auto [idx, elementAttr] : llvm::enumerate(arrayAttr))
3450 if (elementsVerified.insert(elementAttr).second) {
3451 if (isa<LLVM::ZeroAttr, LLVM::UndefAttr>(elementAttr))
3453 auto subArrayAttr = dyn_cast<ArrayAttr>(elementAttr);
3455 return op.emitOpError()
3456 <<
"nested attribute for sub-array in dimension " << dim
3457 <<
" at index " << idx
3458 <<
" must be a zero, or undef, or array attribute";
3472 Type elementType = arrayType.getElementType();
3473 if (isa<LLVM::LLVMPointerType>(elementType)) {
3474 for (
auto [idx, elementAttr] : llvm::enumerate(arrayAttr)) {
3476 LLVM::PoisonAttr>(elementAttr))
3478 return op.emitOpError()
3479 <<
"pointer array element at index " << idx
3480 <<
" must be a flat symbol reference, zero, undef, or poison";
3484 auto structType = dyn_cast<LLVM::LLVMStructType>(elementType);
3486 return op.emitOpError() <<
"for array with an array attribute must have a "
3487 "struct element type";
3491 size_t numStructElements = structType.getBody().size();
3492 for (
auto [idx, elementAttr] : llvm::enumerate(arrayAttr)) {
3493 if (elementsVerified.insert(elementAttr).second) {
3494 if (isa<LLVM::ZeroAttr, LLVM::UndefAttr>(elementAttr))
3496 auto subArrayAttr = dyn_cast<ArrayAttr>(elementAttr);
3498 return op.emitOpError()
3499 <<
"nested attribute for struct element at index " << idx
3500 <<
" must be a zero, or undef, or array attribute";
3501 if (subArrayAttr.size() != numStructElements)
3502 return op.emitOpError()
3503 <<
"nested array attribute size for struct element at index "
3504 << idx <<
" must match struct size: " << subArrayAttr.size()
3505 <<
" vs. " << numStructElements;
3512LogicalResult LLVM::ConstantOp::verify() {
3513 if (StringAttr sAttr = llvm::dyn_cast<StringAttr>(getValue())) {
3514 auto arrayType = llvm::dyn_cast<LLVMArrayType>(
getType());
3515 if (!arrayType || arrayType.getNumElements() != sAttr.getValue().size() ||
3516 !arrayType.getElementType().isInteger(8)) {
3518 << sAttr.getValue().size()
3519 <<
" i8 elements for the string constant";
3523 if (
auto structType = dyn_cast<LLVMStructType>(
getType())) {
3524 auto arrayAttr = dyn_cast<ArrayAttr>(getValue());
3526 return emitOpError() <<
"expected array attribute for struct type";
3529 if (arrayAttr.size() != elementTypes.size()) {
3530 return emitOpError() <<
"expected array attribute of size "
3531 << elementTypes.size();
3533 for (
auto [i, attr, type] : llvm::enumerate(arrayAttr, elementTypes)) {
3535 return emitOpError() <<
"expected struct element types to be floating "
3536 "point type or integer type";
3538 if (!isa<FloatAttr, IntegerAttr>(attr)) {
3539 return emitOpError() <<
"expected element of array attribute to be "
3540 "floating point or integer";
3542 if (cast<TypedAttr>(attr).
getType() != type)
3544 <<
"struct element at index " << i <<
" is of wrong type";
3549 if (
auto targetExtType = dyn_cast<LLVMTargetExtType>(
getType()))
3550 return emitOpError() <<
"does not support target extension type.";
3561 auto verifyFloatSemantics =
3562 [
this](
const llvm::fltSemantics &attributeFloatSemantics,
3563 Type constantElementType) -> LogicalResult {
3564 if (
auto floatType = dyn_cast<FloatType>(constantElementType)) {
3565 if (&floatType.getFloatSemantics() != &attributeFloatSemantics) {
3567 <<
"attribute and type have different float semantics";
3571 unsigned floatWidth = APFloat::getSizeInBits(attributeFloatSemantics);
3572 if (isa<IntegerType>(constantElementType)) {
3573 if (!constantElementType.isInteger(floatWidth))
3574 return emitOpError() <<
"expected integer type of width " << floatWidth;
3582 if (isa<IntegerAttr>(getValue())) {
3583 if (!llvm::isa<IntegerType>(
getType()))
3585 }
else if (
auto floatAttr = dyn_cast<FloatAttr>(getValue())) {
3586 return verifyFloatSemantics(floatAttr.getValue().getSemantics(),
getType());
3587 }
else if (
auto elementsAttr = dyn_cast<ElementsAttr>(getValue())) {
3591 auto splatElementsAttr = dyn_cast<SplatElementsAttr>(getValue());
3592 if (!splatElementsAttr)
3594 <<
"scalable vector type requires a splat attribute";
3597 if (!isa<VectorType, LLVM::LLVMArrayType>(
getType()))
3598 return emitOpError() <<
"expected vector or array type";
3601 int64_t attrNumElements = elementsAttr.getNumElements();
3604 <<
"type and attribute have a different number of elements: "
3610 if (
auto floatType = dyn_cast<FloatType>(attrElmType))
3611 return verifyFloatSemantics(floatType.getFloatSemantics(), resultElmType);
3613 if (isa<IntegerType>(attrElmType) && !isa<IntegerType>(resultElmType)) {
3615 "expected integer element type for integer elements attribute");
3617 }
else if (
auto arrayAttr = dyn_cast<ArrayAttr>(getValue())) {
3620 auto arrayType = dyn_cast<LLVM::LLVMArrayType>(
getType());
3623 <<
"expected array or struct type for array attribute";
3630 <<
"only supports integer, float, string or elements attributes";
3636bool LLVM::ConstantOp::isBuildableWith(
Attribute value,
Type type) {
3638 auto typedAttr = dyn_cast<TypedAttr>(value);
3645 return isa<IntegerAttr, FloatAttr, ElementsAttr>(value);
3650 if (isBuildableWith(value, type))
3651 return LLVM::ConstantOp::create(builder, loc, cast<TypedAttr>(value));
3656OpFoldResult LLVM::ConstantOp::fold(FoldAdaptor) {
return getValue(); }
3664 AtomicOrdering ordering, StringRef syncscope,
3665 unsigned alignment,
bool isVolatile) {
3666 build(builder, state, val.
getType(), binOp,
ptr, val, ordering,
3667 !syncscope.empty() ? builder.
getStringAttr(syncscope) :
nullptr,
3670 nullptr,
nullptr,
nullptr);
3673LogicalResult AtomicRMWOp::verify() {
3674 auto valType = getVal().getType();
3675 if (getBinOp() == AtomicBinOp::fadd || getBinOp() == AtomicBinOp::fsub ||
3676 getBinOp() == AtomicBinOp::fmin || getBinOp() == AtomicBinOp::fmax ||
3677 getBinOp() == AtomicBinOp::fminimum ||
3678 getBinOp() == AtomicBinOp::fmaximum ||
3679 getBinOp() == AtomicBinOp::fminimumnum ||
3680 getBinOp() == AtomicBinOp::fmaximumnum) {
3683 return emitOpError(
"expected LLVM IR fixed vector type");
3684 Type elemType = llvm::cast<VectorType>(valType).getElementType();
3687 "expected LLVM IR floating point type for vector element");
3689 return emitOpError(
"expected LLVM IR floating point type");
3691 }
else if (getBinOp() == AtomicBinOp::xchg) {
3694 return emitOpError(
"unexpected LLVM IR type for 'xchg' bin_op");
3696 auto intType = llvm::dyn_cast<IntegerType>(valType);
3697 unsigned intBitWidth = intType ? intType.getWidth() : 0;
3698 if (intBitWidth != 8 && intBitWidth != 16 && intBitWidth != 32 &&
3700 return emitOpError(
"expected LLVM IR integer type");
3703 if (
static_cast<unsigned>(getOrdering()) <
3704 static_cast<unsigned>(AtomicOrdering::monotonic))
3706 << stringifyAtomicOrdering(AtomicOrdering::monotonic)
3718 auto boolType = IntegerType::get(valType.
getContext(), 1);
3719 return LLVMStructType::getLiteral(valType.
getContext(), {valType, boolType});
3724 AtomicOrdering successOrdering,
3725 AtomicOrdering failureOrdering, StringRef syncscope,
3726 unsigned alignment,
bool isWeak,
bool isVolatile) {
3728 successOrdering, failureOrdering,
3729 !syncscope.empty() ? builder.
getStringAttr(syncscope) :
nullptr,
3731 isVolatile,
nullptr,
3732 nullptr,
nullptr,
nullptr);
3735LogicalResult AtomicCmpXchgOp::verify() {
3736 auto ptrType = llvm::cast<LLVM::LLVMPointerType>(getPtr().
getType());
3738 return emitOpError(
"expected LLVM IR pointer type for operand #0");
3739 auto valType = getVal().getType();
3743 if (getSuccessOrdering() < AtomicOrdering::monotonic ||
3744 getFailureOrdering() < AtomicOrdering::monotonic)
3745 return emitOpError(
"ordering must be at least 'monotonic'");
3746 if (getFailureOrdering() == AtomicOrdering::release ||
3747 getFailureOrdering() == AtomicOrdering::acq_rel)
3748 return emitOpError(
"failure ordering cannot be 'release' or 'acq_rel'");
3757 AtomicOrdering ordering, StringRef syncscope) {
3758 build(builder, state, ordering,
3759 syncscope.empty() ?
nullptr : builder.
getStringAttr(syncscope));
3762LogicalResult FenceOp::verify() {
3763 if (getOrdering() == AtomicOrdering::not_atomic ||
3764 getOrdering() == AtomicOrdering::unordered ||
3765 getOrdering() == AtomicOrdering::monotonic)
3766 return emitOpError(
"can be given only acquire, release, acq_rel, "
3767 "and seq_cst orderings");
3777template <
class ExtOp>
3779 IntegerType inputType, outputType;
3782 return op.emitError(
3783 "input type is a vector but output type is an integer");
3786 return op.emitError(
"input and output vectors are of incompatible shape");
3789 inputType = cast<IntegerType>(
3790 cast<VectorType>(op.getArg().getType()).getElementType());
3791 outputType = cast<IntegerType>(
3792 cast<VectorType>(op.getResult().getType()).getElementType());
3796 inputType = cast<IntegerType>(op.getArg().getType());
3797 outputType = dyn_cast<IntegerType>(op.getResult().getType());
3799 return op.emitError(
3800 "input type is an integer but output type is a vector");
3803 if (outputType.getWidth() <= inputType.getWidth())
3804 return op.emitError(
"integer width of the output type is smaller or "
3805 "equal to the integer width of the input type");
3816 auto arg = dyn_cast_or_null<IntegerAttr>(adaptor.getArg());
3820 size_t targetSize = cast<IntegerType>(
getType()).getWidth();
3821 return IntegerAttr::get(
getType(), arg.getValue().zext(targetSize));
3835template <
typename T>
3837 typename T::FoldAdaptor adaptor) {
3839 if (castOp.getArg().getType() == castOp.getType())
3840 return castOp.getArg();
3841 if (
auto prev = castOp.getArg().template getDefiningOp<T>()) {
3843 if (prev.getArg().getType() == castOp.getType())
3844 return prev.getArg();
3846 castOp.getArgMutable().set(prev.getArg());
3847 return Value{castOp};
3852OpFoldResult LLVM::BitcastOp::fold(FoldAdaptor adaptor) {
3856LogicalResult LLVM::BitcastOp::verify() {
3857 auto resultType = llvm::dyn_cast<LLVMPointerType>(
3859 auto sourceType = llvm::dyn_cast<LLVMPointerType>(
3864 if (
static_cast<bool>(resultType) !=
static_cast<bool>(sourceType))
3865 return emitOpError(
"can only cast pointers from and to pointers");
3870 auto isVector = llvm::IsaPred<VectorType>;
3874 if (isVector(getResult().
getType()) && !isVector(getArg().
getType()))
3875 return emitOpError(
"cannot cast pointer to vector of pointers");
3877 if (!isVector(getResult().
getType()) && isVector(getArg().
getType()))
3878 return emitOpError(
"cannot cast vector of pointers to pointer");
3882 if (resultType.getAddressSpace() != sourceType.getAddressSpace())
3883 return emitOpError(
"cannot cast pointers of different address spaces, "
3884 "use 'llvm.addrspacecast' instead");
3889LogicalResult LLVM::PtrToAddrOp::verify() {
3896 assert(width &&
"pointers always return an index bitwidth");
3897 if (width != integerType.getWidth())
3898 return emitOpError(
"bit-width of integer result type ")
3899 << integerType <<
" must match the pointer bitwidth (" << *width
3900 <<
") specified in the datalayout";
3909OpFoldResult LLVM::AddrSpaceCastOp::fold(FoldAdaptor adaptor) {
3913Value LLVM::AddrSpaceCastOp::getViewSource() {
return getArg(); }
3921 adaptor.getDynamicIndices());
3925 if (
auto integer = llvm::dyn_cast_or_null<IntegerAttr>(
indices[0]))
3926 if (integer.getValue().isZero())
3930 bool changed =
false;
3932 for (
auto iter : llvm::enumerate(
indices)) {
3933 auto integer = llvm::dyn_cast_or_null<IntegerAttr>(iter.value());
3936 if (!
indices.isDynamicIndex(iter.index()) || !integer ||
3940 if (
Value val = llvm::dyn_cast_if_present<Value>(existing))
3941 gepArgs.emplace_back(val);
3943 gepArgs.emplace_back(cast<IntegerAttr>(existing).getInt());
3949 gepArgs.emplace_back(integer.getInt());
3957 getDynamicIndicesMutable().assign(dynamicIndices);
3958 setRawConstantIndices(rawConstantIndices);
3959 return Value{*
this};
3965Value LLVM::GEPOp::getViewSource() {
return getBase(); }
3972 auto rhs = dyn_cast_or_null<IntegerAttr>(adaptor.getRhs());
3976 if (
rhs.getValue().getZExtValue() >=
3977 getLhs().
getType().getIntOrFloatBitWidth())
3980 auto lhs = dyn_cast_or_null<IntegerAttr>(adaptor.getLhs());
3984 return IntegerAttr::get(
getType(),
lhs.getValue().shl(
rhs.getValue()));
3992 auto lhs = dyn_cast_or_null<IntegerAttr>(adaptor.getLhs());
3996 auto rhs = dyn_cast_or_null<IntegerAttr>(adaptor.getRhs());
4000 return IntegerAttr::get(
getType(),
lhs.getValue() |
rhs.getValue());
4007LogicalResult CallIntrinsicOp::verify() {
4008 if (!getIntrin().starts_with(
"llvm."))
4009 return emitOpError() <<
"intrinsic name must start with 'llvm.'";
4017 build(builder, state,
TypeRange{}, intrin, args,
4018 FastmathFlagsAttr{},
4025 mlir::LLVM::FastmathFlagsAttr fastMathFlags) {
4026 build(builder, state,
TypeRange{}, intrin, args,
4033 mlir::Type resultType, mlir::StringAttr intrin,
4035 build(builder, state, {resultType}, intrin, args, FastmathFlagsAttr{},
4043 mlir::LLVM::FastmathFlagsAttr fastMathFlags) {
4044 build(builder, state, resultTypes, intrin, args, fastMathFlags,
4049ParseResult CallIntrinsicOp::parse(
OpAsmParser &parser,
4051 StringAttr intrinAttr;
4061 result.addAttribute(CallIntrinsicOp::getIntrinAttrName(
result.name),
4069 return mlir::failure();
4072 return mlir::failure();
4077 parser, opBundleOperands, opBundleOperandTypes, opBundleTags);
4080 if (opBundleTags && !opBundleTags.empty())
4082 CallIntrinsicOp::getOpBundleTagsAttrName(
result.name).getValue(),
4086 return mlir::failure();
4091 operands, argAttrs, resultAttrs))
4095 getArgAttrsAttrName(
result.name), getResAttrsAttrName(
result.name));
4098 opBundleOperandTypes,
4099 getOpBundleSizesAttrName(
result.name)))
4102 int32_t numOpBundleOperands = 0;
4103 for (
const auto &operands : opBundleOperands)
4104 numOpBundleOperands += operands.size();
4107 CallIntrinsicOp::getOperandSegmentSizeAttr(),
4109 {static_cast<int32_t>(operands.size()), numOpBundleOperands}));
4111 return mlir::success();
4119 p <<
"(" << args <<
")";
4122 if (!getOpBundleOperands().empty()) {
4125 getOpBundleOperands().getTypes(), getOpBundleTagsAttr());
4129 {getOperandSegmentSizesAttrName(),
4130 getOpBundleSizesAttrName(), getIntrinAttrName(),
4131 getOpBundleTagsAttrName(), getArgAttrsAttrName(),
4132 getResAttrsAttrName()});
4138 p, args.
getTypes(), getArgAttrsAttr(),
4139 false, getResultTypes(), getResAttrsAttr());
4146LogicalResult LinkerOptionsOp::verify() {
4149 return emitOpError(
"must appear at the module level");
4157LogicalResult ModuleFlagsOp::verify() {
4160 return emitOpError(
"must appear at the module level");
4162 if (!isa<ModuleFlagAttr>(flag))
4163 return emitOpError(
"expected a module flag attribute");
4171void InlineAsmOp::getEffects(
4174 if (getHasSideEffects()) {
4187 getBlockAddr().getFunction());
4188 auto function = dyn_cast_or_null<LLVMFuncOp>(symbol);
4191 return emitOpError(
"must reference a function defined by 'llvm.func'");
4201BlockTagOp BlockAddressOp::getBlockTagOp() {
4206 auto funcOp = dyn_cast<LLVMFuncOp>(sym);
4209 BlockTagOp blockTagOp =
nullptr;
4210 funcOp.walk([&](LLVM::BlockTagOp labelOp) {
4211 if (labelOp.getTag() == getBlockAddr().getTag()) {
4212 blockTagOp = labelOp;
4220LogicalResult BlockAddressOp::verify() {
4221 if (!getBlockTagOp())
4223 "expects an existing block label target in the referenced function");
4230OpFoldResult BlockAddressOp::fold(FoldAdaptor) {
return getBlockAddr(); }
4237 assert(
index < getNumSuccessors() &&
"invalid successor index");
4249 rangeSegments.push_back(range.size());
4263 Block *destination = nullptr;
4264 SmallVector<OpAsmParser::UnresolvedOperand> operands;
4265 SmallVector<Type> operandTypes;
4267 if (parser.parseSuccessor(destination).failed())
4270 if (succeeded(parser.parseOptionalLParen())) {
4271 if (failed(parser.parseOperandList(
4272 operands, OpAsmParser::Delimiter::None)) ||
4273 failed(parser.parseColonTypeList(operandTypes)) ||
4274 failed(parser.parseRParen()))
4277 succOperandBlocks.push_back(destination);
4278 succOperands.emplace_back(operands);
4279 succOperandsTypes.emplace_back(operandTypes);
4282 "successor blocks")))
4293 llvm::zip(succs, succOperands),
4299 if (!succOperands.empty())
4308LogicalResult LLVM::SincosOp::verify() {
4309 auto operandType = getOperand().getType();
4310 auto resultType = getResult().getType();
4311 auto resultStructType =
4312 mlir::dyn_cast<mlir::LLVM::LLVMStructType>(resultType);
4313 if (!resultStructType || resultStructType.getBody().size() != 2 ||
4314 resultStructType.getBody()[0] != operandType ||
4315 resultStructType.getBody()[1] != operandType) {
4316 return emitOpError(
"expected result type to be an homogeneous struct with "
4317 "two elements matching the operand type, but got ")
4329 return build(builder, state, cond, {},
4341 return build(builder, state, cond,
"align",
ValueRange{
ptr, align});
4347 return build(builder, state, cond,
"separate_storage",
4357LogicalResult LLVM::masked_gather::verify() {
4358 auto ptrsVectorType = getPtrs().getType();
4359 Type expectedPtrsVectorType =
4364 if (ptrsVectorType != expectedPtrsVectorType)
4365 return emitOpError(
"expected operand #1 type to be ")
4366 << expectedPtrsVectorType;
4374LogicalResult LLVM::masked_scatter::verify() {
4375 auto ptrsVectorType = getPtrs().getType();
4376 Type expectedPtrsVectorType =
4381 if (ptrsVectorType != expectedPtrsVectorType)
4382 return emitOpError(
"expected operand #2 type to be ")
4383 << expectedPtrsVectorType;
4396 build(builder, state, resTys,
ptr, mask, passthru, argAttrs,
4404void LLVM::masked_compressstore::build(
OpBuilder &builder,
4409 build(builder, state, value,
ptr, mask, argAttrs,
4417LogicalResult InlineAsmOp::verify() {
4418 if (!getTailCallKindAttr())
4421 if (getTailCallKindAttr().getTailCallKind() == TailCallKind::MustTail)
4423 "tail call kind 'musttail' is not supported by this operation");
4433 Value divisor = getRhs();
4448 Value divisor = getRhs();
4460void LLVMDialect::initialize() {
4461 registerAttributes();
4464 addTypes<LLVMVoidType,
4467 LLVMMetadataType>();
4473#include "mlir/Dialect/LLVMIR/LLVMOps.cpp.inc"
4477#include "mlir/Dialect/LLVMIR/LLVMIntrinsicOps.cpp.inc"
4482 allowUnknownOperations();
4483 declarePromisedInterface<DialectInlinerInterface, LLVMDialect>();
4487#define GET_OP_CLASSES
4488#include "mlir/Dialect/LLVMIR/LLVMOps.cpp.inc"
4490#define GET_OP_CLASSES
4491#include "mlir/Dialect/LLVMIR/LLVMIntrinsicOps.cpp.inc"
4493LogicalResult LLVMDialect::verifyDataLayoutString(
4496 llvm::DataLayout::parse(descr);
4497 if (maybeDataLayout)
4500 std::string message;
4501 llvm::raw_string_ostream messageStream(message);
4502 llvm::logAllUnhandledErrors(maybeDataLayout.takeError(), messageStream);
4503 reportError(
"invalid data layout descriptor: " + message);
4508LogicalResult LLVMDialect::verifyOperationAttribute(
Operation *op,
4514 if (attr.
getName() != LLVM::LLVMDialect::getDataLayoutAttrName())
4516 if (
auto stringAttr = llvm::dyn_cast<StringAttr>(attr.
getValue()))
4517 return verifyDataLayoutString(
4518 stringAttr.getValue(),
4519 [op](
const Twine &message) { op->emitOpError() << message.str(); });
4522 << LLVM::LLVMDialect::getDataLayoutAttrName()
4523 <<
"' to be a string attributes";
4526LogicalResult LLVMDialect::verifyParameterAttribute(
Operation *op,
4534 StringAttr name = paramAttr.
getName();
4536 auto checkUnitAttrType = [&]() -> LogicalResult {
4537 if (!llvm::isa<UnitAttr>(paramAttr.
getValue()))
4538 return op->
emitError() << name <<
" should be a unit attribute";
4541 auto checkTypeAttrType = [&]() -> LogicalResult {
4542 if (!llvm::isa<TypeAttr>(paramAttr.
getValue()))
4543 return op->
emitError() << name <<
" should be a type attribute";
4546 auto checkIntegerAttrType = [&]() -> LogicalResult {
4547 if (!llvm::isa<IntegerAttr>(paramAttr.
getValue()))
4548 return op->
emitError() << name <<
" should be an integer attribute";
4551 auto checkPointerType = [&]() -> LogicalResult {
4552 if (!llvm::isa<LLVMPointerType>(paramType))
4554 << name <<
" attribute attached to non-pointer LLVM type";
4557 auto checkIntegerType = [&]() -> LogicalResult {
4558 if (!llvm::isa<IntegerType>(paramType))
4560 << name <<
" attribute attached to non-integer LLVM type";
4563 auto checkPointerTypeMatches = [&]() -> LogicalResult {
4564 if (
failed(checkPointerType()))
4571 if (name == LLVMDialect::getNoAliasAttrName() ||
4572 name == LLVMDialect::getReadonlyAttrName() ||
4573 name == LLVMDialect::getReadnoneAttrName() ||
4574 name == LLVMDialect::getWriteOnlyAttrName() ||
4575 name == LLVMDialect::getNestAttrName() ||
4576 name == LLVMDialect::getNoCaptureAttrName() ||
4577 name == LLVMDialect::getNoFreeAttrName() ||
4578 name == LLVMDialect::getNonNullAttrName()) {
4579 if (
failed(checkUnitAttrType()))
4581 if (verifyValueType &&
failed(checkPointerType()))
4587 if (name == LLVMDialect::getStructRetAttrName() ||
4588 name == LLVMDialect::getByValAttrName() ||
4589 name == LLVMDialect::getByRefAttrName() ||
4590 name == LLVMDialect::getElementTypeAttrName() ||
4591 name == LLVMDialect::getInAllocaAttrName() ||
4592 name == LLVMDialect::getPreallocatedAttrName()) {
4593 if (
failed(checkTypeAttrType()))
4595 if (verifyValueType &&
failed(checkPointerTypeMatches()))
4601 if (name == LLVMDialect::getSExtAttrName() ||
4602 name == LLVMDialect::getZExtAttrName()) {
4603 if (
failed(checkUnitAttrType()))
4605 if (verifyValueType &&
failed(checkIntegerType()))
4611 if (name == LLVMDialect::getAlignAttrName() ||
4612 name == LLVMDialect::getDereferenceableAttrName() ||
4613 name == LLVMDialect::getDereferenceableOrNullAttrName()) {
4614 if (
failed(checkIntegerAttrType()))
4616 if (verifyValueType &&
failed(checkPointerType()))
4622 if (name == LLVMDialect::getStackAlignmentAttrName()) {
4623 if (
failed(checkIntegerAttrType()))
4629 if (name == LLVMDialect::getNoUndefAttrName() ||
4630 name == LLVMDialect::getInRegAttrName() ||
4631 name == LLVMDialect::getReturnedAttrName())
4632 return checkUnitAttrType();
4638LogicalResult LLVMDialect::verifyRegionArgAttribute(
Operation *op,
4642 auto funcOp = dyn_cast<FunctionOpInterface>(op);
4645 Type argType = funcOp.getArgumentTypes()[argIdx];
4647 return verifyParameterAttribute(op, argType, argAttr);
4650LogicalResult LLVMDialect::verifyRegionResultAttribute(
Operation *op,
4654 auto funcOp = dyn_cast<FunctionOpInterface>(op);
4657 Type resType = funcOp.getResultTypes()[resIdx];
4661 if (llvm::isa<LLVMVoidType>(resType))
4662 return op->
emitError() <<
"cannot attach result attributes to functions "
4663 "with a void return";
4667 auto name = resAttr.
getName();
4668 if (name == LLVMDialect::getAllocAlignAttrName() ||
4669 name == LLVMDialect::getAllocatedPointerAttrName() ||
4670 name == LLVMDialect::getByValAttrName() ||
4671 name == LLVMDialect::getByRefAttrName() ||
4672 name == LLVMDialect::getInAllocaAttrName() ||
4673 name == LLVMDialect::getNestAttrName() ||
4674 name == LLVMDialect::getNoCaptureAttrName() ||
4675 name == LLVMDialect::getNoFreeAttrName() ||
4676 name == LLVMDialect::getPreallocatedAttrName() ||
4677 name == LLVMDialect::getReadnoneAttrName() ||
4678 name == LLVMDialect::getReadonlyAttrName() ||
4679 name == LLVMDialect::getReturnedAttrName() ||
4680 name == LLVMDialect::getStackAlignmentAttrName() ||
4681 name == LLVMDialect::getStructRetAttrName() ||
4682 name == LLVMDialect::getWriteOnlyAttrName())
4683 return op->
emitError() << name <<
" is not a valid result attribute";
4684 return verifyParameterAttribute(op, resType, resAttr);
4692 if (
auto symbol = dyn_cast<FlatSymbolRefAttr>(value))
4693 if (isa<LLVM::LLVMPointerType>(type))
4694 return LLVM::AddressOfOp::create(builder, loc, type, symbol);
4695 if (isa<LLVM::UndefAttr>(value))
4696 return LLVM::UndefOp::create(builder, loc, type);
4697 if (isa<LLVM::PoisonAttr>(value))
4698 return LLVM::PoisonOp::create(builder, loc, type);
4699 if (isa<LLVM::ZeroAttr>(value))
4700 return LLVM::ZeroOp::create(builder, loc, type);
4702 return LLVM::ConstantOp::materialize(builder, value, type, loc);
4710 StringRef name, StringRef value,
4711 LLVM::Linkage linkage) {
4714 "expected builder to point to a block constrained in an op");
4716 builder.getInsertionBlock()->getParentOp()->getParentOfType<ModuleOp>();
4717 assert(module &&
"builder points to an op outside of a module");
4722 auto type = LLVM::LLVMArrayType::get(IntegerType::get(ctx, 8), value.size());
4723 auto global = LLVM::GlobalOp::create(
4724 moduleBuilder, loc, type,
true, linkage, name,
4727 LLVMPointerType ptrType = LLVMPointerType::get(ctx);
4730 LLVM::AddressOfOp::create(builder, loc, ptrType, global.getSymNameAttr());
4731 return LLVM::GEPOp::create(builder, loc, ptrType, type, globalPtr,
4743 module = module->getParentOp();
4744 assert(module &&
"unexpected operation outside of a module");
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 Value getBase(Value v)
Looks through known "view-like" ops to find the base memref.
static int parseOptionalKeywordAlternative(OpAsmParser &parser, ArrayRef< StringRef > keywords)
static ArrayAttr getLLVMAlignParamForCompressExpand(OpBuilder &builder, bool isExpandLoad, uint64_t alignment=1)
static LogicalResult verifyAtomicMemOp(OpTy memOp, Type valueType, ArrayRef< AtomicOrdering > unsupportedOrderings)
Verifies the attributes and the type of atomic memory access operations.
static RetTy parseOptionalLLVMKeyword(OpAsmParser &parser, EnumTy defaultValue)
Parse an enum from the keyword, or default to the provided default value.
static LogicalResult checkGlobalXtorData(Operation *op, ArrayAttr data)
static ParseResult parseGEPIndices(OpAsmParser &parser, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &indices, DenseI32ArrayAttr &rawConstantIndices)
static LogicalResult verifyOperandBundles(OpType &op)
static ParseResult parseCmpOp(OpAsmParser &parser, OperationState &result)
static void printOneOpBundle(OpAsmPrinter &p, OperandRange operands, TypeRange operandTypes, StringRef tag)
static LogicalResult verifyComdat(Operation *op, std::optional< SymbolRefAttr > attr)
static LLVMFunctionType getLLVMFuncType(MLIRContext *context, TypeRange results, ValueRange args)
Constructs a LLVMFunctionType from MLIR results and args.
static void printSwitchOpCases(OpAsmPrinter &p, SwitchOp op, Type flagType, DenseIntElementsAttr caseValues, SuccessorRange caseDestinations, OperandRangeRange caseOperands, const TypeRangeRange &caseOperandTypes)
static ParseResult parseSwitchOpCases(OpAsmParser &parser, Type flagType, DenseIntElementsAttr &caseValues, SmallVectorImpl< Block * > &caseDestinations, SmallVectorImpl< SmallVector< OpAsmParser::UnresolvedOperand > > &caseOperands, SmallVectorImpl< SmallVector< Type > > &caseOperandTypes)
<cases> ::= [ (case (, case )* )?
static LogicalResult verifyCallOpVarCalleeType(OpTy callOp)
Verify that the parameter and return types of the variadic callee type match the callOp argument and ...
static ParseResult parseOptionalCallFuncPtr(OpAsmParser &parser, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &operands)
Parses an optional function pointer operand before the call argument list for indirect calls,...
static bool isZeroAttribute(Attribute value)
static void printGEPIndices(OpAsmPrinter &printer, LLVM::GEPOp gepOp, OperandRange indices, DenseI32ArrayAttr rawConstantIndices)
static std::optional< ParseResult > parseOpBundles(OpAsmParser &p, SmallVector< SmallVector< OpAsmParser::UnresolvedOperand > > &opBundleOperands, SmallVector< SmallVector< Type > > &opBundleOperandTypes, ArrayAttr &opBundleTags)
static LLVMStructType getValAndBoolStructType(Type valType)
Returns an LLVM struct type that contains a value type and a boolean type.
static void printOpBundles(OpAsmPrinter &p, Operation *op, OperandRangeRange opBundleOperands, TypeRangeRange opBundleOperandTypes, std::optional< ArrayAttr > opBundleTags)
static void printShuffleType(AsmPrinter &printer, Operation *op, Type v1Type, Type resType, DenseI32ArrayAttr mask)
Nothing to do when the result type is inferred.
static LogicalResult verifyBlockTags(LLVMFuncOp funcOp)
static Type buildLLVMFunctionType(OpAsmParser &parser, SMLoc loc, ArrayRef< Type > inputs, ArrayRef< Type > outputs, function_interface_impl::VariadicFlag variadicFlag)
static auto processFMFAttr(ArrayRef< NamedAttribute > attrs)
static TypeAttr getCallOpVarCalleeType(LLVMFunctionType calleeType)
Gets the variadic callee type for a LLVMFunctionType.
static Type getInsertExtractValueElementType(function_ref< InFlightDiagnostic(StringRef)> emitError, Type containerType, ArrayRef< int64_t > position)
Extract the type at position in the LLVM IR aggregate type containerType.
static ParseResult parseOneOpBundle(OpAsmParser &p, SmallVector< SmallVector< OpAsmParser::UnresolvedOperand > > &opBundleOperands, SmallVector< SmallVector< Type > > &opBundleOperandTypes, SmallVector< Attribute > &opBundleTags)
static Type getElementType(Type type)
Determine the element type of type.
static void printIndirectBrOpSucessors(OpAsmPrinter &p, IndirectBrOp op, Type flagType, SuccessorRange succs, OperandRangeRange succOperands, const TypeRangeRange &succOperandsTypes)
static ParseResult resolveOpBundleOperands(OpAsmParser &parser, SMLoc loc, OperationState &state, ArrayRef< SmallVector< OpAsmParser::UnresolvedOperand > > opBundleOperands, ArrayRef< SmallVector< Type > > opBundleOperandTypes, StringAttr opBundleSizesAttrName)
static void printLLVMLinkage(OpAsmPrinter &p, Operation *, LinkageAttr val)
static LogicalResult verifyStructArrayConstant(LLVM::ConstantOp op, LLVM::LLVMArrayType arrayType, ArrayAttr arrayAttr, int dim)
Verifies the constant array represented by arrayAttr matches the provided arrayType.
static ParseResult parseCallTypeAndResolveOperands(OpAsmParser &parser, OperationState &result, bool isDirect, ArrayRef< OpAsmParser::UnresolvedOperand > operands, SmallVectorImpl< DictionaryAttr > &argAttrs, SmallVectorImpl< DictionaryAttr > &resultAttrs)
Parses the type of a call operation and resolves the operands if the parsing succeeds.
static LogicalResult verifySymbolAttrUse(FlatSymbolRefAttr symbol, Operation *op, SymbolTableCollection &symbolTable)
Verifies symbol's use in op to ensure the symbol is a valid and fully defined llvm....
static Type extractVectorElementType(Type type)
Returns the elemental type of any LLVM-compatible vector type or self.
static bool hasScalableVectorType(Type t)
Check if the given type is a scalable vector type or a vector/array type that contains a nested scala...
static SmallVector< Type, 1 > getCallOpResultTypes(LLVMFunctionType calleeType)
Gets the MLIR Op-like result types of a LLVMFunctionType.
static OpFoldResult foldChainableCast(T castOp, typename T::FoldAdaptor adaptor)
Folds a cast op that can be chained.
static void destructureIndices(Type currType, ArrayRef< GEPArg > indices, SmallVectorImpl< int32_t > &rawConstantIndices, SmallVectorImpl< Value > &dynamicIndices)
Destructures the 'indices' parameter into 'rawConstantIndices' and 'dynamicIndices',...
static ParseResult parseCommonGlobalAndAlias(OpAsmParser &parser, OperationState &result)
Parse common attributes that might show up in the same order in both GlobalOp and AliasOp.
static Type getI1SameShape(Type type)
Returns a boolean type that has the same shape as type.
static void printCommonGlobalAndAlias(OpAsmPrinter &p, OpType op)
static ParseResult parseLLVMLinkage(OpAsmParser &p, LinkageAttr &val)
static Attribute getBoolAttribute(Type type, MLIRContext *ctx, bool value)
Returns a scalar or vector boolean attribute of the given type.
static LogicalResult verifyCallOpDebugInfo(CallOp callOp, LLVMFuncOp callee)
Verify that an inlinable callsite of a debug-info-bearing function in a debug-info-bearing function h...
static ParseResult parseShuffleType(AsmParser &parser, Type v1Type, Type &resType, DenseI32ArrayAttr mask)
Build the result type of a shuffle vector operation.
static LogicalResult verifyExtOp(ExtOp op)
Verifies that the given extension operation operates on consistent scalars or vectors,...
static constexpr const char kElemTypeAttrName[]
static ParseResult parseInsertExtractValueElementType(AsmParser &parser, Type &valueType, Type containerType, DenseI64ArrayAttr position)
Infer the value type from the container type and position.
static LogicalResult verifyStructIndices(Type baseGEPType, unsigned indexPos, GEPIndicesAdaptor< ValueRange > indices, function_ref< InFlightDiagnostic()> emitOpError)
For the given indices, check if they comply with baseGEPType, especially check against LLVMStructType...
static Attribute extractElementAt(Attribute attr, size_t index)
Extracts the element at the given index from an attribute.
static int64_t getNumElements(Type t)
Compute the total number of elements in the given type, also taking into account nested types.
static void printInsertExtractValueElementType(AsmPrinter &printer, Operation *op, Type valueType, Type containerType, DenseI64ArrayAttr position)
Nothing to print for an inferred type.
static ParseResult parseIndirectBrOpSucessors(OpAsmParser &parser, Type &flagType, SmallVectorImpl< Block * > &succOperandBlocks, SmallVectorImpl< SmallVector< OpAsmParser::UnresolvedOperand > > &succOperands, SmallVectorImpl< SmallVector< Type > > &succOperandsTypes)
#define REGISTER_ENUM_TYPE(Ty)
static std::string diag(const llvm::Value &value)
This base class exposes generic asm parser hooks, usable across the various derived parsers.
ParseResult parseSymbolName(StringAttr &result)
Parse an -identifier and store it (without the '@' symbol) in a string attribute.
@ Paren
Parens surrounding zero or more operands.
@ None
Zero or more operands with no delimiters.
@ Square
Square brackets surrounding zero or more operands.
virtual OptionalParseResult parseOptionalInteger(APInt &result)=0
Parse an optional integer value from the stream.
virtual ParseResult parseColonTypeList(SmallVectorImpl< Type > &result)=0
Parse a colon followed by a type list, which must have at least one type.
virtual Builder & getBuilder() const =0
Return a builder which provides useful access to MLIRContext, global objects like types and attribute...
virtual ParseResult parseCommaSeparatedList(Delimiter delimiter, function_ref< ParseResult()> parseElementFn, StringRef contextMessage=StringRef())=0
Parse a list of comma-separated items with an optional delimiter.
virtual ParseResult parseOptionalAttrDict(NamedAttrList &result)=0
Parse a named dictionary into 'result' if it is present.
virtual ParseResult parseOptionalKeyword(StringRef keyword)=0
Parse the given keyword if present.
MLIRContext * getContext() const
virtual ParseResult parseRParen()=0
Parse a ) token.
virtual InFlightDiagnostic emitError(SMLoc loc, const Twine &message={})=0
Emit a diagnostic at the specified location and return failure.
virtual ParseResult parseLSquare()=0
Parse a [ token.
virtual ParseResult parseRSquare()=0
Parse a ] token.
virtual ParseResult parseOptionalColonTypeList(SmallVectorImpl< Type > &result)=0
Parse an optional colon followed by a type list, which if present must have at least one type.
ParseResult parseInteger(IntT &result)
Parse an integer value from the stream.
virtual ParseResult parseOptionalRParen()=0
Parse a ) token if present.
virtual ParseResult parseCustomAttributeWithFallback(Attribute &result, Type type, function_ref< ParseResult(Attribute &result, Type type)> parseAttribute)=0
Parse a custom attribute with the provided callback, unless the next token is #, in which case the ge...
ParseResult parseString(std::string *string)
Parse a quoted string token.
virtual ParseResult parseOptionalAttrDictWithKeyword(NamedAttrList &result)=0
Parse a named dictionary into 'result' if the attributes keyword is present.
virtual SMLoc getCurrentLocation()=0
Get the location of the next token and store it into the argument.
virtual ParseResult parseOptionalComma()=0
Parse a , token if present.
virtual ParseResult parseColon()=0
Parse a : token.
virtual SMLoc getNameLoc() const =0
Return the location of the original name token.
virtual ParseResult parseOptionalRSquare()=0
Parse a ] token if present.
virtual ParseResult parseLParen()=0
Parse a ( token.
virtual ParseResult parseType(Type &result)=0
Parse a type.
virtual ParseResult parseComma()=0
Parse a , token.
virtual ParseResult parseOptionalLParen()=0
Parse a ( token if present.
ParseResult parseTypeList(SmallVectorImpl< Type > &result)
Parse a type list.
ParseResult parseKeyword(StringRef keyword)
Parse a given keyword.
virtual ParseResult parseOptionalLSquare()=0
Parse a [ token if present.
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 printSymbolName(StringRef symbolRef)
Print the given string as a symbol reference, i.e.
virtual void printString(StringRef string)
Print the given string as a quoted string, escaping any special or non-printable characters in it.
virtual void printAttribute(Attribute attr)
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.
MLIRContext * getContext() const
Return the context this attribute belongs to.
This class provides an abstraction over the different types of ranges over Blocks.
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.
Operation * getParentOp()
Returns the closest surrounding operation that contains this block.
static BoolAttr get(MLIRContext *context, bool value)
This class is a general helper class for creating context-global objects like types,...
IntegerAttr getI32IntegerAttr(int32_t value)
DenseI32ArrayAttr getDenseI32ArrayAttr(ArrayRef< int32_t > values)
IntegerAttr getI64IntegerAttr(int64_t value)
Ty getType(Args &&...args)
Get or construct an instance of the type Ty with provided arguments.
StringAttr getStringAttr(const Twine &bytes)
TypedAttr getZeroAttr(Type type)
ArrayAttr getArrayAttr(ArrayRef< Attribute > value)
MLIRContext * getContext() const
DictionaryAttr getDictionaryAttr(ArrayRef< NamedAttribute > value)
NamedAttribute getNamedAttr(StringRef name, Attribute val)
ArrayAttr getStrArrayAttr(ArrayRef< StringRef > values)
Attr getAttr(Args &&...args)
Get or construct an instance of the attribute Attr with provided arguments.
The main mechanism for performing data layout queries.
static DataLayout closest(Operation *op)
Returns the layout of the closest parent operation carrying layout info.
std::optional< uint64_t > getTypeIndexBitwidth(Type t) const
Returns the bitwidth that should be used when performing index computations for the given pointer-lik...
llvm::TypeSize getTypeSizeInBits(Type t) const
Returns the size in bits of the given type in the current scope.
static DenseElementsAttr get(ShapedType type, ArrayRef< Attribute > values)
Constructs a dense elements attribute from an array of element values.
An attribute that represents a reference to a dense integer vector or tensor object.
static DenseIntElementsAttr get(const ShapedType &type, Arg &&arg)
Get an instance of a DenseIntElementsAttr with the given arguments.
A symbol reference with a reference path containing a single element.
StringRef getValue() const
Returns the name of the held symbol reference.
StringAttr getAttr() const
Returns the name of the held symbol reference as a StringAttr.
This class represents a fused location whose metadata is known to be an instance of the given type.
This class represents a diagnostic that is inflight and set to be reported.
Diagnostic & attachNote(std::optional< Location > noteLoc=std::nullopt)
Attaches a note to this diagnostic.
Class used for building a 'llvm.getelementptr'.
Class used for convenient access and iteration over GEP indices.
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.
This class provides a mutable adaptor for a range of operands.
NamedAttribute represents a combination of a name and an Attribute value.
StringAttr getName() const
Return the name of the attribute.
Attribute getValue() const
Return the value of the attribute.
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 parseSuccessor(Block *&dest)=0
Parse a single operation successor.
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 OptionalParseResult parseOptionalOperand(UnresolvedOperand &result, bool allowResultNumber=true)=0
Parse a single operand if present.
virtual ParseResult parseSuccessorAndUseList(Block *&dest, SmallVectorImpl< Value > &operands)=0
Parse a single operation successor and its operand list.
virtual OptionalParseResult parseOptionalRegion(Region ®ion, ArrayRef< Argument > arguments={}, bool enableNameShadowing=false)=0
Parses a region if present.
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 printSuccessorAndUseList(Block *successor, ValueRange succOperands)=0
Print the successor and its operands.
void printOperands(const ContainerType &container)
Print a comma separated list of operands.
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.
virtual void printOperand(Value value)=0
Print implementations for various things an operation contains.
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.
Listener * getListener() const
Returns the current listener of this builder, or nullptr if this builder doesn't have a listener.
Block * getInsertionBlock() const
Return the block the current insertion point belongs to.
This class represents a single result from folding an operation.
This class provides the API for ops that are known to be isolated from above.
A trait used to provide symbol table functionalities to a region operation.
This class represents a contiguous range of operand ranges, e.g.
This class implements the operand iterators for the Operation class.
type_range getTypes() const
Operation is the basic unit of execution within MLIR.
bool hasTrait()
Returns true if the operation was registered with a particular trait, e.g.
Operation * getParentOp()
Returns the closest surrounding operation that contains this operation or nullptr if this is a top-le...
OperandRange operand_range
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
InFlightDiagnostic emitOpError(const Twine &message={})
Emit an error with the op name prefixed, like "'dim' op " which is convenient for verifiers.
This class implements Optional functionality for ParseResult.
ParseResult value() const
Access the internal ParseResult value.
bool has_value() const
Returns true if we contain a valid ParseResult value.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
iterator_range< OpIterator > getOps()
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
void modifyOpInPlace(Operation *root, CallableT &&callable)
This method is a utility wrapper around an in-place modification of an operation.
This class represents a specific instance of an effect.
static DerivedEffect * get()
This class models how operands are forwarded to block arguments in control flow.
This class implements the successor iterators for Block.
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,...
virtual Operation * lookupSymbolIn(Operation *symbolTableOp, StringAttr symbol)
Look up a symbol with the specified name within the specified symbol table operation,...
static StringRef getSymbolAttrName()
Return the name of the attribute used for symbol names.
static Operation * lookupNearestSymbolFrom(Operation *from, StringAttr symbol)
Returns the operation registered with the given symbol name within the closest parent operation of,...
This class provides an abstraction for a range of TypeRange.
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...
MLIRContext * getContext() const
Return the MLIRContext in which this type was uniqued.
unsigned getIntOrFloatBitWidth() const
Return the bit width of an integer or a float type, assert failure on other types.
bool isSignlessIntOrIndexOrFloat() const
Return true if this is a signless integer, index, or float type.
This class provides an abstraction over the different types of ranges over Values.
type_range getTypes() const
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.
A utility result that is used to signal how to proceed with an ongoing walk:
static WalkResult advance()
bool wasInterrupted() const
Returns true if the walk was interrupted.
static WalkResult interrupt()
static DenseArrayAttrImpl get(MLIRContext *context, ArrayRef< int32_t > content)
ArrayRef< T > asArrayRef() const
A named class for passing around the variadic flag.
The OpAsmOpInterface, see OpAsmInterface.td for more details.
void addBytecodeInterface(LLVMDialect *dialect)
Add the interfaces necessary for encoding the LLVM dialect components in bytecode.
Value createGlobalString(Location loc, OpBuilder &builder, StringRef name, StringRef value, Linkage linkage)
Create an LLVM global containing the string "value" at the module containing surrounding the insertio...
Operation * parentLLVMModule(Operation *op)
Lookup parent Module satisfying LLVM conditions on the Module Operation.
Type getVectorType(Type elementType, unsigned numElements, bool isScalable=false)
Creates an LLVM dialect-compatible vector type with the given element type and length.
bool isScalableVectorType(Type vectorType)
Returns whether a vector type is scalable or not.
bool isCompatibleVectorType(Type type)
Returns true if the given type is a vector type compatible with the LLVM dialect.
bool isCompatibleOuterType(Type type)
Returns true if the given outer type is compatible with the LLVM dialect without checking its potenti...
bool satisfiesLLVMModule(Operation *op)
LLVM requires some operations to be inside of a Module operation.
constexpr int kGEPConstantBitWidth
Bit-width of a 'GEPConstantIndex' within GEPArg.
bool isCompatibleType(Type type)
Returns true if the given type is compatible with the LLVM dialect.
bool isTypeCompatibleWithAtomicOp(Type type, const DataLayout &dataLayout)
Returns true if the given type is supported by atomic operations.
bool isCompatibleFloatingPointType(Type type)
Returns true if the given type is a floating-point type compatible with the LLVM dialect.
llvm::ElementCount getVectorNumElements(Type type)
Returns the element count of any LLVM-compatible vector type.
Speculatability
This enum is returned from the getSpeculatability method in the ConditionallySpeculatable op interfac...
constexpr auto Speculatable
constexpr auto NotSpeculatable
void printFunctionSignature(OpAsmPrinter &p, TypeRange argTypes, ArrayAttr argAttrs, bool isVariadic, TypeRange resultTypes, ArrayAttr resultAttrs, Region *body=nullptr, bool printEmptyResult=true)
Print a function signature for a call or callable operation.
ParseResult parseFunctionSignature(OpAsmParser &parser, SmallVectorImpl< Type > &argTypes, SmallVectorImpl< DictionaryAttr > &argAttrs, SmallVectorImpl< Type > &resultTypes, SmallVectorImpl< DictionaryAttr > &resultAttrs, bool mustParseEmptyResult=true)
Parses a function signature using parser.
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 walk(Operation *op, function_ref< void(Region *)> callback, WalkOrder order)
Walk all of the regions, blocks, or operations nested under (and including) the given operation.
ParseResult parseFunctionSignatureWithArguments(OpAsmParser &parser, bool allowVariadic, SmallVectorImpl< OpAsmParser::Argument > &arguments, bool &isVariadic, SmallVectorImpl< Type > &resultTypes, SmallVectorImpl< DictionaryAttr > &resultAttrs)
Parses a function signature using parser.
void printFunctionAttributes(OpAsmPrinter &p, Operation *op, ArrayRef< StringRef > elided={})
Prints the list of function prefixed with the "attributes" keyword.
void printFunctionSignature(OpAsmPrinter &p, FunctionOpInterface op, ArrayRef< Type > argTypes, bool isVariadic, ArrayRef< Type > resultTypes)
Prints the signature of the function-like operation op.
Operation::operand_range getIndices(Operation *op)
Get the indices that the given load/store operation is operating on.
Include the generated interface declarations.
bool matchPattern(Value value, const Pattern &pattern)
Entry point for matching a pattern over a Value.
detail::constant_int_value_binder m_ConstantInt(IntegerAttr::ValueType *bind_value)
Matches a constant holding a scalar/vector/tensor integer (splat) and writes the integer value to bin...
detail::DenseArrayAttrImpl< int64_t > DenseI64ArrayAttr
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
detail::constant_int_range_predicate_matcher m_IntRangeWithoutNegOneS()
Matches a constant scalar / vector splat / tensor splat integer or a signed integer range that does n...
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
detail::DenseArrayAttrImpl< int32_t > DenseI32ArrayAttr
llvm::TypeSwitch< T, ResultT > TypeSwitch
detail::constant_int_range_predicate_matcher m_IntRangeWithoutZeroS()
Matches a constant scalar / vector splat / tensor splat integer or a signed integer range that does n...
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
detail::constant_op_matcher m_Constant()
Matches a constant foldable operation.
llvm::function_ref< Fn > function_ref
detail::constant_int_range_predicate_matcher m_IntRangeWithoutZeroU()
Matches a constant scalar / vector splat / tensor splat integer or a unsigned integer range that does...
A callable is either a symbol, or an SSA value, that is referenced by a call-like operation.
This is the representation of an operand reference.
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...
OpRewritePattern(MLIRContext *context, PatternBenefit benefit=1, ArrayRef< StringRef > generatedNames={})
Patterns must specify the root operation name they match against, and can also specify the benefit of...
This represents an operation in an abstracted form, suitable for use with the builder APIs.
T & getOrAddProperties()
Get (or create) the properties of the provided type to be set on the operation on creation.
SmallVector< Value, 4 > operands
void addOperands(ValueRange newOperands)
void addAttributes(ArrayRef< NamedAttribute > newAttributes)
Add an array of named attributes.
void addAttribute(StringRef name, Attribute attr)
Add an attribute with the specified name.
void addSuccessors(Block *successor)
Adds a successor to the operation sate. successor must not be null.