MLIR 24.0.0git
LLVMCallOps.cpp
Go to the documentation of this file.
1//===- LLVMCallOps.cpp - LLVM dialect call ops ----------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the ops of op partition `Call` of the LLVM dialect.
10//
11//===----------------------------------------------------------------------===//
12
14
15#include "IR/LLVMOps.h"
19#include "mlir/IR/Builders.h"
21#include "llvm/ADT/STLExtras.h"
22
23#include <optional>
24
25using namespace mlir;
26using namespace mlir::LLVM;
27
30 llvm::make_filter_range(attrs, [&](NamedAttribute attr) {
31 if (attr.getName() == "fastmathFlags") {
32 auto defAttr =
33 FastmathFlagsAttr::get(attr.getValue().getContext(), {});
34 return defAttr != attr.getValue();
35 }
36 return true;
37 }));
38 return filteredAttrs;
39}
40
41//===----------------------------------------------------------------------===//
42// Operand bundle helpers.
43//===----------------------------------------------------------------------===//
44
46 TypeRange operandTypes, StringRef tag) {
47 p.printString(tag);
48 p << "(";
49
50 if (!operands.empty()) {
51 p.printOperands(operands);
52 p << " : ";
53 llvm::interleaveComma(operandTypes, p);
54 }
55
56 p << ")";
57}
58
60 OperandRangeRange opBundleOperands,
61 TypeRangeRange opBundleOperandTypes,
62 std::optional<ArrayAttr> opBundleTags) {
63 if (opBundleOperands.empty())
64 return;
65 assert(opBundleTags && "expect operand bundle tags");
66
67 p << "[";
68 llvm::interleaveComma(
69 llvm::zip(opBundleOperands, opBundleOperandTypes, *opBundleTags), p,
70 [&p](auto bundle) {
71 auto bundleTag = cast<StringAttr>(std::get<2>(bundle)).getValue();
72 printOneOpBundle(p, std::get<0>(bundle), std::get<1>(bundle),
73 bundleTag);
74 });
75 p << "]";
76}
77
78static ParseResult parseOneOpBundle(
79 OpAsmParser &p,
81 SmallVector<SmallVector<Type>> &opBundleOperandTypes,
82 SmallVector<Attribute> &opBundleTags) {
83 SMLoc currentParserLoc = p.getCurrentLocation();
86 std::string tag;
87
88 if (p.parseString(&tag))
89 return p.emitError(currentParserLoc, "expect operand bundle tag");
90
91 if (p.parseLParen())
92 return failure();
93
94 if (p.parseOptionalRParen()) {
95 if (p.parseOperandList(operands) || p.parseColon() ||
96 p.parseTypeList(types) || p.parseRParen())
97 return failure();
98 }
99
100 opBundleOperands.push_back(std::move(operands));
101 opBundleOperandTypes.push_back(std::move(types));
102 opBundleTags.push_back(StringAttr::get(p.getContext(), tag));
103
104 return success();
105}
106
107std::optional<ParseResult> mlir::LLVM::parseOpBundles(
108 OpAsmParser &p,
110 SmallVector<SmallVector<Type>> &opBundleOperandTypes,
111 ArrayAttr &opBundleTags) {
112 if (p.parseOptionalLSquare())
113 return std::nullopt;
114
115 if (succeeded(p.parseOptionalRSquare()))
116 return success();
117
118 SmallVector<Attribute> opBundleTagAttrs;
119 auto bundleParser = [&] {
120 return parseOneOpBundle(p, opBundleOperands, opBundleOperandTypes,
121 opBundleTagAttrs);
122 };
123 if (p.parseCommaSeparatedList(bundleParser))
124 return failure();
125
126 if (p.parseRSquare())
127 return failure();
128
129 opBundleTags = ArrayAttr::get(p.getContext(), opBundleTagAttrs);
130
131 return success();
132}
133
134//===----------------------------------------------------------------------===//
135// CallOp
136//===----------------------------------------------------------------------===//
137
138/// Gets the MLIR Op-like result types of a LLVMFunctionType.
139static SmallVector<Type, 1> getCallOpResultTypes(LLVMFunctionType calleeType) {
140 SmallVector<Type, 1> results;
141 Type resultType = calleeType.getReturnType();
142 if (!isa<LLVM::LLVMVoidType>(resultType))
143 results.push_back(resultType);
144 return results;
145}
146
147/// Gets the variadic callee type for a LLVMFunctionType.
148static TypeAttr getCallOpVarCalleeType(LLVMFunctionType calleeType) {
149 return calleeType.isVarArg() ? TypeAttr::get(calleeType) : nullptr;
150}
151
152/// Constructs a LLVMFunctionType from MLIR `results` and `args`.
153static LLVMFunctionType getLLVMFuncType(MLIRContext *context, TypeRange results,
154 ValueRange args) {
155 Type resultType;
156 if (results.empty())
157 resultType = LLVMVoidType::get(context);
158 else
159 resultType = results.front();
160 return LLVMFunctionType::get(resultType, llvm::to_vector(args.getTypes()),
161 /*isVarArg=*/false);
162}
163
164void CallOp::build(OpBuilder &builder, OperationState &state, TypeRange results,
165 StringRef callee, ValueRange args) {
166 build(builder, state, results, builder.getStringAttr(callee), args);
167}
168
169void CallOp::build(OpBuilder &builder, OperationState &state, TypeRange results,
170 StringAttr callee, ValueRange args) {
171 build(builder, state, results, SymbolRefAttr::get(callee), args);
172}
173
174void CallOp::build(OpBuilder &builder, OperationState &state, TypeRange results,
175 FlatSymbolRefAttr callee, ValueRange args) {
176 assert(callee && "expected non-null callee in direct call builder");
177 build(builder, state, results,
178 /*var_callee_type=*/nullptr, callee, args, /*fastmathFlags=*/nullptr,
179 /*CConv=*/nullptr, /*TailCallKind=*/nullptr,
180 /*memory_effects=*/nullptr,
181 /*convergent=*/nullptr, /*no_unwind=*/nullptr, /*will_return=*/nullptr,
182 /*noreturn=*/nullptr, /*returns_twice=*/nullptr, /*hot=*/nullptr,
183 /*cold=*/nullptr, /*noduplicate=*/nullptr,
184 /*no_caller_saved_registers=*/nullptr, /*nocallback=*/nullptr,
185 /*modular_format=*/nullptr, /*nobuiltins=*/nullptr,
186 /*allocsize=*/nullptr, /*optsize=*/nullptr, /*minsize=*/nullptr,
187 /*builtin=*/nullptr, /*nobuiltin=*/nullptr,
188 /*save_reg_params=*/nullptr,
189 /*zero_call_used_regs=*/nullptr, /*trap_func_name=*/nullptr,
190 /*default_func_attrs=*/nullptr,
191 /*uniform_work_group_size=*/nullptr,
192 /*op_bundle_operands=*/{}, /*op_bundle_tags=*/{},
193 /*arg_attrs=*/nullptr, /*res_attrs=*/nullptr,
194 /*access_groups=*/nullptr, /*alias_scopes=*/nullptr,
195 /*noalias_scopes=*/nullptr, /*tbaa=*/nullptr,
196 /*no_inline=*/nullptr, /*always_inline=*/nullptr,
197 /*inline_hint=*/nullptr);
198}
199
200void CallOp::build(OpBuilder &builder, OperationState &state,
201 LLVMFunctionType calleeType, StringRef callee,
202 ValueRange args) {
203 build(builder, state, calleeType, builder.getStringAttr(callee), args);
204}
205
206void CallOp::build(OpBuilder &builder, OperationState &state,
207 LLVMFunctionType calleeType, StringAttr callee,
208 ValueRange args) {
209 build(builder, state, calleeType, SymbolRefAttr::get(callee), args);
210}
211
212void CallOp::build(OpBuilder &builder, OperationState &state,
213 LLVMFunctionType calleeType, FlatSymbolRefAttr callee,
214 ValueRange args) {
215 build(builder, state, getCallOpResultTypes(calleeType),
216 getCallOpVarCalleeType(calleeType), callee, args,
217 /*fastmathFlags=*/nullptr,
218 /*CConv=*/nullptr,
219 /*TailCallKind=*/nullptr, /*memory_effects=*/nullptr,
220 /*convergent=*/nullptr,
221 /*no_unwind=*/nullptr, /*will_return=*/nullptr,
222 /*noreturn=*/nullptr,
223 /*returns_twice=*/nullptr, /*hot=*/nullptr,
224 /*cold=*/nullptr, /*noduplicate=*/nullptr,
225 /*no_caller_saved_registers=*/nullptr, /*nocallback=*/nullptr,
226 /*modular_format=*/nullptr, /*nobuiltins=*/nullptr,
227 /*allocsize=*/nullptr, /*optsize=*/nullptr, /*minsize=*/nullptr,
228 /*builtin=*/nullptr, /*nobuiltin=*/nullptr,
229 /*save_reg_params=*/nullptr,
230 /*zero_call_used_regs=*/nullptr, /*trap_func_name=*/nullptr,
231 /*default_func_attrs=*/nullptr,
232 /*uniform_work_group_size=*/nullptr,
233 /*op_bundle_operands=*/{}, /*op_bundle_tags=*/{},
234 /*arg_attrs=*/nullptr, /*res_attrs=*/nullptr,
235 /*access_groups=*/nullptr,
236 /*alias_scopes=*/nullptr, /*noalias_scopes=*/nullptr, /*tbaa=*/nullptr,
237 /*no_inline=*/nullptr, /*always_inline=*/nullptr,
238 /*inline_hint=*/nullptr);
239}
240
241void CallOp::build(OpBuilder &builder, OperationState &state,
242 LLVMFunctionType calleeType, ValueRange args) {
243 build(builder, state, getCallOpResultTypes(calleeType),
244 getCallOpVarCalleeType(calleeType),
245 /*callee=*/nullptr, args,
246 /*fastmathFlags=*/nullptr,
247 /*CConv=*/nullptr, /*TailCallKind=*/nullptr, /*memory_effects=*/nullptr,
248 /*convergent=*/nullptr, /*no_unwind=*/nullptr, /*will_return=*/nullptr,
249 /*noreturn=*/nullptr,
250 /*returns_twice=*/nullptr, /*hot=*/nullptr,
251 /*cold=*/nullptr, /*noduplicate=*/nullptr,
252 /*no_caller_saved_registers=*/nullptr, /*nocallback=*/nullptr,
253 /*modular_format=*/nullptr, /*nobuiltins=*/nullptr,
254 /*allocsize=*/nullptr, /*optsize=*/nullptr, /*minsize=*/nullptr,
255 /*builtin=*/nullptr, /*nobuiltin=*/nullptr,
256 /*save_reg_params=*/nullptr,
257 /*zero_call_used_regs=*/nullptr, /*trap_func_name=*/nullptr,
258 /*default_func_attrs=*/nullptr,
259 /*uniform_work_group_size=*/nullptr,
260 /*op_bundle_operands=*/{}, /*op_bundle_tags=*/{},
261 /*arg_attrs=*/nullptr, /*res_attrs=*/nullptr,
262 /*access_groups=*/nullptr, /*alias_scopes=*/nullptr,
263 /*noalias_scopes=*/nullptr, /*tbaa=*/nullptr,
264 /*no_inline=*/nullptr, /*always_inline=*/nullptr,
265 /*inline_hint=*/nullptr);
266}
267
268void CallOp::build(OpBuilder &builder, OperationState &state, LLVMFuncOp func,
269 ValueRange args) {
270 auto calleeType = func.getFunctionType();
271 build(builder, state, getCallOpResultTypes(calleeType),
272 getCallOpVarCalleeType(calleeType), SymbolRefAttr::get(func), args,
273 /*fastmathFlags=*/nullptr,
274 /*CConv=*/nullptr, /*TailCallKind=*/nullptr, /*memory_effects=*/nullptr,
275 /*convergent=*/nullptr, /*no_unwind=*/nullptr, /*will_return=*/nullptr,
276 /*noreturn=*/nullptr,
277 /*returns_twice=*/nullptr, /*hot=*/nullptr,
278 /*cold=*/nullptr, /*noduplicate=*/nullptr,
279 /*no_caller_saved_registers=*/nullptr, /*nocallback=*/nullptr,
280 /*modular_format=*/nullptr, /*nobuiltins=*/nullptr,
281 /*allocsize=*/nullptr, /*optsize=*/nullptr, /*minsize=*/nullptr,
282 /*builtin=*/nullptr, /*nobuiltin=*/nullptr,
283 /*save_reg_params=*/nullptr,
284 /*zero_call_used_regs=*/nullptr, /*trap_func_name=*/nullptr,
285 /*default_func_attrs=*/nullptr,
286 /*uniform_work_group_size=*/nullptr,
287 /*op_bundle_operands=*/{}, /*op_bundle_tags=*/{},
288 /*access_groups=*/nullptr, /*alias_scopes=*/nullptr,
289 /*arg_attrs=*/nullptr, /*res_attrs=*/nullptr,
290 /*noalias_scopes=*/nullptr, /*tbaa=*/nullptr,
291 /*no_inline=*/nullptr, /*always_inline=*/nullptr,
292 /*inline_hint=*/nullptr);
293}
294
295CallInterfaceCallable CallOp::getCallableForCallee() {
296 // Direct call.
297 if (FlatSymbolRefAttr calleeAttr = getCalleeAttr())
298 return calleeAttr;
299 // Indirect call, callee Value is the first operand.
300 return getOperand(0);
301}
302
303void CallOp::setCalleeFromCallable(CallInterfaceCallable callee) {
304 // Direct call.
305 if (FlatSymbolRefAttr calleeAttr = getCalleeAttr()) {
306 auto symRef = cast<SymbolRefAttr>(callee);
307 return setCalleeAttr(cast<FlatSymbolRefAttr>(symRef));
308 }
309 // Indirect call, callee Value is the first operand.
310 return setOperand(0, cast<Value>(callee));
311}
312
313/// Return the number of leading callee operands of `callOp` that the operation
314/// consumes instead of passing them to the callee.
315template <typename OpTy>
316static unsigned getNumConsumedCalleeOperands(OpTy callOp) {
317 // The first operand is the callee if no callee attribute is present.
318 if (callOp.getCallee().has_value())
319 return 0;
320 return 1;
321}
322
323/// Return the operands of `callOp` that are passed to the callee, including the
324/// variadic arguments in case of a call to a variadic callee.
325template <typename OpTy>
327 return callOp.getCalleeOperands().drop_front(
329}
330
331/// Return the operands of `callOp` that correspond to the declared parameters
332/// of the callee, i.e., its `CallOpInterface` argument operands.
333///
334/// The variadic arguments of a call to a variadic callee are *not* included:
335/// they do not correspond to any argument of the callee. The callee does not
336/// receive them as block arguments but reads them with `llvm.intr.vastart` and
337/// friends, so in terms of `CallOpInterface` they are consumed operands rather
338/// than forwarded ones.
339template <typename OpTy>
342 if (std::optional<LLVMFunctionType> varCalleeType = callOp.getVarCalleeType())
343 return operands.take_front(varCalleeType->getNumParams());
344 return operands;
345}
346
347Operation::operand_range CallOp::getArgOperands() {
348 return getArgOperandsImpl(*this);
349}
350
351MutableOperandRange CallOp::getArgOperandsMutable() {
352 // Slice the generated range to retain its segment-size metadata. A raw
353 // range would not update operandSegmentSizes when arguments are erased.
354 return getCalleeOperandsMutable().slice(getNumConsumedCalleeOperands(*this),
355 getArgOperandsImpl(*this).size());
356}
357
358/// Verify that an inlinable callsite of a debug-info-bearing function in a
359/// debug-info-bearing function has a debug location attached to it. This
360/// mirrors an LLVM IR verifier.
361template <typename OpTy>
362static LogicalResult verifyCallOpDebugInfo(OpTy callOp, LLVMFuncOp callee) {
363 if (callee.isExternal())
364 return success();
365 auto parentFunc = callOp->template getParentOfType<FunctionOpInterface>();
366 if (!parentFunc)
367 return success();
368
369 auto hasSubprogram = [](Operation *op) {
370 return op->getLoc()
372 nullptr;
373 };
374 if (!hasSubprogram(parentFunc) || !hasSubprogram(callee))
375 return success();
376 bool containsLoc = !isa<UnknownLoc>(callOp->getLoc());
377 if (!containsLoc)
378 return callOp.emitError()
379 << "inlinable function call in a function with a DISubprogram "
380 "location must have a debug location";
381 return success();
382}
383
384/// Verify that the parameter and return types of the variadic callee type match
385/// the `callOp` argument and result types.
386template <typename OpTy>
387static LogicalResult verifyCallOpVarCalleeType(OpTy callOp) {
388 // An indirect call stores the callee in its first callee operand.
389 if (!callOp.getCallee().has_value() && callOp.getCalleeOperands().empty())
390 return callOp.emitOpError(
391 "must have either a `callee` attribute or at least an operand");
392
393 std::optional<LLVMFunctionType> varCalleeType = callOp.getVarCalleeType();
394 if (!varCalleeType)
395 return success();
396
397 // Verify the variadic callee type is a variadic function type.
398 if (!varCalleeType->isVarArg())
399 return callOp.emitOpError(
400 "expected var_callee_type to be a variadic function type");
401
402 // Note: `getArgOperands` is derived from `var_callee_type`, so the raw callee
403 // operands are used here instead.
405
406 // Verify the variadic callee type has at most as many parameters as the call
407 // has argument operands.
408 if (varCalleeType->getNumParams() > passedOperands.size())
409 return callOp.emitOpError("expected var_callee_type to have at most ")
410 << passedOperands.size() << " parameters";
411
412 // Verify the variadic callee type matches the call argument types.
413 for (auto [paramType, operand] :
414 llvm::zip(varCalleeType->getParams(), passedOperands))
415 if (paramType != operand.getType())
416 return callOp.emitOpError()
417 << "var_callee_type parameter type mismatch: " << paramType
418 << " != " << operand.getType();
419
420 // Verify the variadic callee type matches the call result type.
421 if (!callOp.getNumResults()) {
422 if (!isa<LLVMVoidType>(varCalleeType->getReturnType()))
423 return callOp.emitOpError("expected var_callee_type to return void");
424 } else {
425 if (callOp.getResult().getType() != varCalleeType->getReturnType())
426 return callOp.emitOpError("var_callee_type return type mismatch: ")
427 << varCalleeType->getReturnType()
428 << " != " << callOp.getResult().getType();
429 }
430 return success();
431}
432
433LogicalResult CallOp::verify() { return verifyOperandBundles(*this); }
434
435template <typename OpTy>
436static LogicalResult
438 if (failed(verifyCallOpVarCalleeType(callOp)))
439 return failure();
440 // Type for the callee, we'll get it differently depending if it is a direct
441 // or indirect call.
442 Type fnType;
443
444 // If this is an indirect call, the callee attribute is missing.
445 FlatSymbolRefAttr calleeName = callOp.getCalleeAttr();
446 if (!calleeName) {
447 // Note: `verifyCallOpVarCalleeType` has already checked that there is a
448 // callee operand.
449 auto ptrType =
450 llvm::dyn_cast<LLVMPointerType>(callOp.getOperand(0).getType());
451 if (!ptrType)
452 return callOp.emitOpError("indirect call expects a pointer as callee: ")
453 << callOp.getOperand(0).getType();
454
455 // Nothing else to verify: an indirect callee cannot be resolved.
456 return success();
457 } else {
458 Operation *callee =
459 symbolTable.lookupNearestSymbolFrom(callOp, calleeName.getAttr());
460 if (!callee)
461 return callOp.emitOpError()
462 << "'" << calleeName.getValue()
463 << "' does not reference a symbol in the current scope";
464 if (auto fn = dyn_cast<LLVMFuncOp>(callee)) {
465 if (failed(verifyCallOpDebugInfo(callOp, fn)))
466 return failure();
467 fnType = fn.getFunctionType();
468 } else if (auto ifunc = dyn_cast<IFuncOp>(callee)) {
469 fnType = ifunc.getIFuncType();
470 } else if (isa<AliasOp>(callee)) {
471 // Aliases can alias functions, so calling through an alias is valid.
472 // The function type is determined by the call's operands and result
473 // types.
474 fnType = callOp.getCalleeFunctionType();
475 } else {
476 return callOp.emitOpError()
477 << "'" << calleeName.getValue()
478 << "' does not reference a valid LLVM function, IFunc, or alias";
479 }
480 }
481
482 LLVMFunctionType funcType = llvm::dyn_cast<LLVMFunctionType>(fnType);
483 if (!funcType)
484 return callOp.emitOpError("callee does not have a functional type: ")
485 << fnType;
486
487 if (funcType.isVarArg() && !callOp.getVarCalleeType())
488 return callOp.emitOpError()
489 << "missing var_callee_type attribute for vararg call";
490
491 // Verify the result types. These checks are more specific than what
492 // `verifyCallOpInterface` can report, so they are run first.
493 if (callOp.getNumResults() == 0 &&
494 !llvm::isa<LLVM::LLVMVoidType>(funcType.getReturnType()))
495 return callOp.emitOpError() << "expected function call to produce a value";
496
497 if (callOp.getNumResults() != 0 &&
498 llvm::isa<LLVM::LLVMVoidType>(funcType.getReturnType()))
499 return callOp.emitOpError()
500 << "calling function with void result must not produce values";
501
502 if (callOp.getNumResults() > 1)
503 return callOp.emitOpError()
504 << "expected LLVM function call to produce 0 or 1 result";
505
506 if (callOp.getNumResults() &&
507 callOp.getResult().getType() != funcType.getReturnType())
508 return callOp.emitOpError()
509 << "result type mismatch: " << callOp.getResult().getType()
510 << " != " << funcType.getReturnType();
511
512 // Verify that the operand types match the callee. Note that this does not
513 // need to special-case a variadic callee: the variadic arguments are not
514 // argument operands.
515 SmallVector<Type, 1> calleeResultTypes;
516 if (!llvm::isa<LLVM::LLVMVoidType>(funcType.getReturnType()))
517 calleeResultTypes.push_back(funcType.getReturnType());
519 callOp, funcType.getParams(), calleeResultTypes);
520}
521
522LogicalResult CallOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
523 return verifyCallOpSymbolUses(*this, symbolTable);
524}
525
526void CallOp::print(OpAsmPrinter &p) {
527 auto callee = getCallee();
528 bool isDirect = callee.has_value();
529
530 p << ' ';
531
532 // Print calling convention.
533 if (getCConv() != LLVM::CConv::C)
534 p << stringifyCConv(getCConv()) << ' ';
535
536 if (getTailCallKind() != LLVM::TailCallKind::None)
537 p << tailcallkind::stringifyTailCallKind(getTailCallKind()) << ' ';
538
539 // Print the direct callee if present as a function attribute, or an indirect
540 // callee (first operand) otherwise.
541 if (isDirect)
542 p.printSymbolName(callee.value());
543 else
544 p << getOperand(0);
545
546 auto args = getCalleeOperands().drop_front(isDirect ? 0 : 1);
547 p << '(' << args << ')';
548
549 // Print the variadic callee type if the call is variadic.
550 if (std::optional<LLVMFunctionType> varCalleeType = getVarCalleeType())
551 p << " vararg(" << *varCalleeType << ")";
552
553 if (!getOpBundleOperands().empty()) {
554 p << " ";
555 printOpBundles(p, *this, getOpBundleOperands(),
556 getOpBundleOperands().getTypes(), getOpBundleTags());
557 }
558
560 {getCalleeAttrName(), getTailCallKindAttrName(),
561 getVarCalleeTypeAttrName(), getCConvAttrName(),
562 getOperandSegmentSizesAttrName(),
563 getOpBundleSizesAttrName(),
564 getOpBundleTagsAttrName(), getArgAttrsAttrName(),
565 getResAttrsAttrName()});
566
567 p << " : ";
568 if (!isDirect)
569 p << getOperand(0).getType() << ", ";
570
571 // Reconstruct the MLIR function type from operand and result types.
573 p, args.getTypes(), getArgAttrsAttr(),
574 /*isVariadic=*/false, getResultTypes(), getResAttrsAttr());
575}
576
577/// Parses the type of a call operation and resolves the operands if the parsing
578/// succeeds. Returns failure otherwise.
580 OpAsmParser &parser, OperationState &result, bool isDirect,
583 SmallVectorImpl<DictionaryAttr> &resultAttrs) {
584 SMLoc trailingTypesLoc = parser.getCurrentLocation();
585 SmallVector<Type> types;
586 if (parser.parseColon())
587 return failure();
588 if (!isDirect) {
589 types.emplace_back();
590 if (parser.parseType(types.back()))
591 return failure();
592 if (parser.parseOptionalComma())
593 return parser.emitError(
594 trailingTypesLoc, "expected indirect call to have 2 trailing types");
595 }
596 SmallVector<Type> argTypes;
597 SmallVector<Type> resTypes;
598 if (call_interface_impl::parseFunctionSignature(parser, argTypes, argAttrs,
599 resTypes, resultAttrs)) {
600 if (isDirect)
601 return parser.emitError(trailingTypesLoc,
602 "expected direct call to have 1 trailing types");
603 return parser.emitError(trailingTypesLoc,
604 "expected trailing function type");
605 }
606
607 if (resTypes.size() > 1)
608 return parser.emitError(trailingTypesLoc,
609 "expected function with 0 or 1 result");
610 if (resTypes.size() == 1 && llvm::isa<LLVM::LLVMVoidType>(resTypes[0]))
611 return parser.emitError(trailingTypesLoc,
612 "expected a non-void result type");
613
614 // The head element of the types list matches the callee type for
615 // indirect calls, while the types list is emtpy for direct calls.
616 // Append the function input types to resolve the call operation
617 // operands.
618 llvm::append_range(types, argTypes);
619 if (parser.resolveOperands(operands, types, parser.getNameLoc(),
620 result.operands))
621 return failure();
622 if (!resTypes.empty())
623 result.addTypes(resTypes);
624
625 return success();
626}
627
628/// Parses an optional function pointer operand before the call argument list
629/// for indirect calls, or stops parsing at the function identifier otherwise.
630static ParseResult parseOptionalCallFuncPtr(
631 OpAsmParser &parser,
633 OpAsmParser::UnresolvedOperand funcPtrOperand;
634 OptionalParseResult parseResult = parser.parseOptionalOperand(funcPtrOperand);
635 if (parseResult.has_value()) {
636 if (failed(*parseResult))
637 return *parseResult;
638 operands.push_back(funcPtrOperand);
639 }
640 return success();
641}
642
643static ParseResult resolveOpBundleOperands(
644 OpAsmParser &parser, SMLoc loc, OperationState &state,
646 ArrayRef<SmallVector<Type>> opBundleOperandTypes,
647 StringAttr opBundleSizesAttrName) {
648 unsigned opBundleIndex = 0;
649 for (const auto &[operands, types] :
650 llvm::zip_equal(opBundleOperands, opBundleOperandTypes)) {
651 if (operands.size() != types.size())
652 return parser.emitError(loc, "expected ")
653 << operands.size()
654 << " types for operand bundle operands for operand bundle #"
655 << opBundleIndex << ", but actually got " << types.size();
656 if (parser.resolveOperands(operands, types, loc, state.operands))
657 return failure();
658 }
659
660 SmallVector<int32_t> opBundleSizes;
661 opBundleSizes.reserve(opBundleOperands.size());
662 for (const auto &operands : opBundleOperands)
663 opBundleSizes.push_back(operands.size());
664
665 state.addAttribute(
666 opBundleSizesAttrName,
667 DenseI32ArrayAttr::get(parser.getContext(), opBundleSizes));
668
669 return success();
670}
671
672// <operation> ::= `llvm.call` (cconv)? (tailcallkind)? (function-id | ssa-use)
673// `(` ssa-use-list `)`
674// ( `vararg(` var-callee-type `)` )?
675// ( `[` op-bundles-list `]` )?
676// attribute-dict? `:` (type `,`)? function-type
677ParseResult CallOp::parse(OpAsmParser &parser, OperationState &result) {
678 SymbolRefAttr funcAttr;
679 TypeAttr varCalleeType;
682 SmallVector<SmallVector<Type>> opBundleOperandTypes;
683 ArrayAttr opBundleTags;
684
685 // Default to C Calling Convention if no keyword is provided.
686 result.addAttribute(
687 getCConvAttrName(result.name),
688 CConvAttr::get(parser.getContext(),
689 parseOptionalLLVMKeyword<CConv>(parser, LLVM::CConv::C)));
690
691 result.addAttribute(
692 getTailCallKindAttrName(result.name),
693 TailCallKindAttr::get(parser.getContext(),
695 parser, LLVM::TailCallKind::None)));
696
697 // Parse a function pointer for indirect calls.
698 if (parseOptionalCallFuncPtr(parser, operands))
699 return failure();
700 bool isDirect = operands.empty();
701
702 // Parse a function identifier for direct calls.
703 if (isDirect)
704 if (parser.parseAttribute(funcAttr, "callee", result.attributes))
705 return failure();
706
707 // Parse the function arguments.
709 return failure();
710
711 bool isVarArg = parser.parseOptionalKeyword("vararg").succeeded();
712 if (isVarArg) {
713 StringAttr varCalleeTypeAttrName =
714 CallOp::getVarCalleeTypeAttrName(result.name);
715 if (parser.parseLParen().failed() ||
716 parser
717 .parseAttribute(varCalleeType, varCalleeTypeAttrName,
718 result.attributes)
719 .failed() ||
720 parser.parseRParen().failed())
721 return failure();
722 }
723
724 SMLoc opBundlesLoc = parser.getCurrentLocation();
725 if (std::optional<ParseResult> result = parseOpBundles(
726 parser, opBundleOperands, opBundleOperandTypes, opBundleTags);
727 result && failed(*result))
728 return failure();
729 if (opBundleTags && !opBundleTags.empty())
730 result.addAttribute(CallOp::getOpBundleTagsAttrName(result.name).getValue(),
731 opBundleTags);
732
733 if (parser.parseOptionalAttrDict(result.attributes))
734 return failure();
735
736 // Parse the trailing type list and resolve the operands.
738 SmallVector<DictionaryAttr> resultAttrs;
739 if (parseCallTypeAndResolveOperands(parser, result, isDirect, operands,
740 argAttrs, resultAttrs))
741 return failure();
743 parser.getBuilder(), result, argAttrs, resultAttrs,
744 getArgAttrsAttrName(result.name), getResAttrsAttrName(result.name));
745 if (resolveOpBundleOperands(parser, opBundlesLoc, result, opBundleOperands,
746 opBundleOperandTypes,
747 getOpBundleSizesAttrName(result.name)))
748 return failure();
749
750 int32_t numOpBundleOperands = 0;
751 for (const auto &operands : opBundleOperands)
752 numOpBundleOperands += operands.size();
753
754 result.addAttribute(
755 CallOp::getOperandSegmentSizeAttr(),
757 {static_cast<int32_t>(operands.size()), numOpBundleOperands}));
758 return success();
759}
760
761LLVMFunctionType CallOp::getCalleeFunctionType() {
762 if (std::optional<LLVMFunctionType> varCalleeType = getVarCalleeType())
763 return *varCalleeType;
764 return getLLVMFuncType(getContext(), getResultTypes(), getArgOperands());
765}
766
767///===---------------------------------------------------------------------===//
768/// LLVM::InvokeOp
769///===---------------------------------------------------------------------===//
770
771void InvokeOp::build(OpBuilder &builder, OperationState &state, LLVMFuncOp func,
772 ValueRange ops, Block *normal, ValueRange normalOps,
773 Block *unwind, ValueRange unwindOps) {
774 auto calleeType = func.getFunctionType();
775 build(builder, state, getCallOpResultTypes(calleeType),
776 getCallOpVarCalleeType(calleeType), SymbolRefAttr::get(func), ops,
777 /*arg_attrs=*/nullptr, /*res_attrs=*/nullptr, normalOps, unwindOps,
778 nullptr, nullptr, /*default_func_attrs=*/nullptr,
779 /*uniform_work_group_size=*/nullptr, {}, {}, normal, unwind);
780}
781
782void InvokeOp::build(OpBuilder &builder, OperationState &state, TypeRange tys,
783 FlatSymbolRefAttr callee, ValueRange ops, Block *normal,
784 ValueRange normalOps, Block *unwind,
785 ValueRange unwindOps) {
786 build(builder, state, tys,
787 /*var_callee_type=*/nullptr, callee, ops, /*arg_attrs=*/nullptr,
788 /*res_attrs=*/nullptr, normalOps, unwindOps, nullptr, nullptr,
789 /*default_func_attrs=*/nullptr,
790 /*uniform_work_group_size=*/nullptr, {}, {}, normal, unwind);
791}
792
793void InvokeOp::build(OpBuilder &builder, OperationState &state,
794 LLVMFunctionType calleeType, FlatSymbolRefAttr callee,
795 ValueRange ops, Block *normal, ValueRange normalOps,
796 Block *unwind, ValueRange unwindOps) {
797 build(builder, state, getCallOpResultTypes(calleeType),
798 getCallOpVarCalleeType(calleeType), callee, ops,
799 /*arg_attrs=*/nullptr, /*res_attrs=*/nullptr, normalOps, unwindOps,
800 nullptr, nullptr, /*default_func_attrs=*/nullptr,
801 /*uniform_work_group_size=*/nullptr, {}, {}, normal, unwind);
802}
803
804SuccessorOperands InvokeOp::getSuccessorOperands(unsigned index) {
805 assert(index < getNumSuccessors() && "invalid successor index");
806 return SuccessorOperands(index == 0 ? getNormalDestOperandsMutable()
807 : getUnwindDestOperandsMutable());
808}
809
810CallInterfaceCallable InvokeOp::getCallableForCallee() {
811 // Direct call.
812 if (FlatSymbolRefAttr calleeAttr = getCalleeAttr())
813 return calleeAttr;
814 // Indirect call, callee Value is the first operand.
815 return getOperand(0);
816}
817
818void InvokeOp::setCalleeFromCallable(CallInterfaceCallable callee) {
819 // Direct call.
820 if (FlatSymbolRefAttr calleeAttr = getCalleeAttr()) {
821 auto symRef = cast<SymbolRefAttr>(callee);
822 return setCalleeAttr(cast<FlatSymbolRefAttr>(symRef));
823 }
824 // Indirect call, callee Value is the first operand.
825 return setOperand(0, cast<Value>(callee));
826}
827
828Operation::operand_range InvokeOp::getArgOperands() {
829 return getArgOperandsImpl(*this);
830}
831
832MutableOperandRange InvokeOp::getArgOperandsMutable() {
833 // Slice the generated range to retain its segment-size metadata. A raw
834 // range would not update operandSegmentSizes when arguments are erased.
835 return getCalleeOperandsMutable().slice(getNumConsumedCalleeOperands(*this),
836 getArgOperandsImpl(*this).size());
837}
838
839LogicalResult InvokeOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
840 return verifyCallOpSymbolUses(*this, symbolTable);
841}
842
843LogicalResult InvokeOp::verify() {
844 Block *unwindDest = getUnwindDest();
845 if (unwindDest->empty())
846 return emitError("must have at least one operation in unwind destination");
847
848 // In unwind destination, first operation must be LandingpadOp
849 if (!isa<LandingpadOp>(unwindDest->front()))
850 return emitError("first operation in unwind destination should be a "
851 "llvm.landingpad operation");
852
853 if (failed(verifyOperandBundles(*this)))
854 return failure();
855
856 return success();
857}
858
859void InvokeOp::print(OpAsmPrinter &p) {
860 auto callee = getCallee();
861 bool isDirect = callee.has_value();
862
863 p << ' ';
864
865 // Print calling convention.
866 if (getCConv() != LLVM::CConv::C)
867 p << stringifyCConv(getCConv()) << ' ';
868
869 // Either function name or pointer
870 if (isDirect)
871 p.printSymbolName(callee.value());
872 else
873 p << getOperand(0);
874
875 p << '(' << getCalleeOperands().drop_front(isDirect ? 0 : 1) << ')';
876 p << " to ";
877 p.printSuccessorAndUseList(getNormalDest(), getNormalDestOperands());
878 p << " unwind ";
879 p.printSuccessorAndUseList(getUnwindDest(), getUnwindDestOperands());
880
881 // Print the variadic callee type if the invoke is variadic.
882 if (std::optional<LLVMFunctionType> varCalleeType = getVarCalleeType())
883 p << " vararg(" << *varCalleeType << ")";
884
885 if (!getOpBundleOperands().empty()) {
886 p << " ";
887 printOpBundles(p, *this, getOpBundleOperands(),
888 getOpBundleOperands().getTypes(), getOpBundleTags());
889 }
890
892 {getCalleeAttrName(), getOperandSegmentSizeAttr(),
893 getCConvAttrName(), getVarCalleeTypeAttrName(),
894 getOpBundleSizesAttrName(),
895 getOpBundleTagsAttrName(), getArgAttrsAttrName(),
896 getResAttrsAttrName()});
897
898 p << " : ";
899 if (!isDirect)
900 p << getOperand(0).getType() << ", ";
902 p, getCalleeOperands().drop_front(isDirect ? 0 : 1).getTypes(),
903 getArgAttrsAttr(),
904 /*isVariadic=*/false, getResultTypes(), getResAttrsAttr());
905}
906
907// <operation> ::= `llvm.invoke` (cconv)? (function-id | ssa-use)
908// `(` ssa-use-list `)`
909// `to` bb-id (`[` ssa-use-and-type-list `]`)?
910// `unwind` bb-id (`[` ssa-use-and-type-list `]`)?
911// ( `vararg(` var-callee-type `)` )?
912// ( `[` op-bundles-list `]` )?
913// attribute-dict? `:` (type `,`)?
914// function-type-with-argument-attributes
915ParseResult InvokeOp::parse(OpAsmParser &parser, OperationState &result) {
917 SymbolRefAttr funcAttr;
918 TypeAttr varCalleeType;
920 SmallVector<SmallVector<Type>> opBundleOperandTypes;
921 ArrayAttr opBundleTags;
922 Block *normalDest, *unwindDest;
923 SmallVector<Value, 4> normalOperands, unwindOperands;
924 Builder &builder = parser.getBuilder();
925
926 // Default to C Calling Convention if no keyword is provided.
927 result.addAttribute(
928 getCConvAttrName(result.name),
929 CConvAttr::get(parser.getContext(),
930 parseOptionalLLVMKeyword<CConv>(parser, LLVM::CConv::C)));
931
932 // Parse a function pointer for indirect calls.
933 if (parseOptionalCallFuncPtr(parser, operands))
934 return failure();
935 bool isDirect = operands.empty();
936
937 // Parse a function identifier for direct calls.
938 if (isDirect && parser.parseAttribute(funcAttr, "callee", result.attributes))
939 return failure();
940
941 // Parse the function arguments.
942 if (parser.parseOperandList(operands, OpAsmParser::Delimiter::Paren) ||
943 parser.parseKeyword("to") ||
944 parser.parseSuccessorAndUseList(normalDest, normalOperands) ||
945 parser.parseKeyword("unwind") ||
946 parser.parseSuccessorAndUseList(unwindDest, unwindOperands))
947 return failure();
948
949 bool isVarArg = parser.parseOptionalKeyword("vararg").succeeded();
950 if (isVarArg) {
951 StringAttr varCalleeTypeAttrName =
952 InvokeOp::getVarCalleeTypeAttrName(result.name);
953 if (parser.parseLParen().failed() ||
954 parser
955 .parseAttribute(varCalleeType, varCalleeTypeAttrName,
956 result.attributes)
957 .failed() ||
958 parser.parseRParen().failed())
959 return failure();
960 }
961
962 SMLoc opBundlesLoc = parser.getCurrentLocation();
963 if (std::optional<ParseResult> result = parseOpBundles(
964 parser, opBundleOperands, opBundleOperandTypes, opBundleTags);
965 result && failed(*result))
966 return failure();
967 if (opBundleTags && !opBundleTags.empty())
968 result.addAttribute(
969 InvokeOp::getOpBundleTagsAttrName(result.name).getValue(),
970 opBundleTags);
971
972 if (parser.parseOptionalAttrDict(result.attributes))
973 return failure();
974
975 // Parse the trailing type list and resolve the function operands.
977 SmallVector<DictionaryAttr> resultAttrs;
978 if (parseCallTypeAndResolveOperands(parser, result, isDirect, operands,
979 argAttrs, resultAttrs))
980 return failure();
982 parser.getBuilder(), result, argAttrs, resultAttrs,
983 getArgAttrsAttrName(result.name), getResAttrsAttrName(result.name));
984
985 result.addSuccessors({normalDest, unwindDest});
986 result.addOperands(normalOperands);
987 result.addOperands(unwindOperands);
988
989 if (resolveOpBundleOperands(parser, opBundlesLoc, result, opBundleOperands,
990 opBundleOperandTypes,
991 getOpBundleSizesAttrName(result.name)))
992 return failure();
993
994 int32_t numOpBundleOperands = 0;
995 for (const auto &operands : opBundleOperands)
996 numOpBundleOperands += operands.size();
997
998 result.addAttribute(
999 InvokeOp::getOperandSegmentSizeAttr(),
1000 builder.getDenseI32ArrayAttr({static_cast<int32_t>(operands.size()),
1001 static_cast<int32_t>(normalOperands.size()),
1002 static_cast<int32_t>(unwindOperands.size()),
1003 numOpBundleOperands}));
1004 return success();
1005}
1006
1007LLVMFunctionType InvokeOp::getCalleeFunctionType() {
1008 if (std::optional<LLVMFunctionType> varCalleeType = getVarCalleeType())
1009 return *varCalleeType;
1010 return getLLVMFuncType(getContext(), getResultTypes(), getArgOperands());
1011}
1012
1013///===----------------------------------------------------------------------===//
1014/// Verifying/Printing/Parsing for LLVM::LandingpadOp.
1015///===----------------------------------------------------------------------===//
1016
1017LogicalResult LandingpadOp::verify() {
1018 Value value;
1019 if (LLVMFuncOp func = (*this)->getParentOfType<LLVMFuncOp>()) {
1020 if (!func.getPersonality())
1021 return emitError(
1022 "llvm.landingpad needs to be in a function with a personality");
1023 }
1024
1025 // Consistency of llvm.landingpad result types is checked in
1026 // LLVMFuncOp::verify().
1027
1028 if (!getCleanup() && getOperands().empty())
1029 return emitError("landingpad instruction expects at least one clause or "
1030 "cleanup attribute");
1031
1032 for (unsigned idx = 0, ie = getNumOperands(); idx < ie; idx++) {
1033 value = getOperand(idx);
1034 bool isFilter = llvm::isa<LLVMArrayType>(value.getType());
1035 if (isFilter) {
1036 // FIXME: Verify filter clauses when arrays are appropriately handled
1037 } else {
1038 // catch - global addresses only.
1039 // Bitcast ops should have global addresses as their args.
1040 if (auto bcOp = value.getDefiningOp<BitcastOp>()) {
1041 if (auto addrOp = bcOp.getArg().getDefiningOp<AddressOfOp>())
1042 continue;
1043 return emitError("constant clauses expected").attachNote(bcOp.getLoc())
1044 << "global addresses expected as operand to "
1045 "bitcast used in clauses for landingpad";
1046 }
1047 // ZeroOp and AddressOfOp allowed
1048 if (value.getDefiningOp<ZeroOp>())
1049 continue;
1050 if (value.getDefiningOp<AddressOfOp>())
1051 continue;
1052 return emitError("clause #")
1053 << idx << " is not a known constant - null, addressof, bitcast";
1054 }
1055 }
1056 return success();
1057}
1058
1059void LandingpadOp::print(OpAsmPrinter &p) {
1060 p << (getCleanup() ? " cleanup " : " ");
1061
1062 // Clauses
1063 for (auto value : getOperands()) {
1064 // Similar to llvm - if clause is an array type then it is filter
1065 // clause else catch clause
1066 bool isArrayTy = llvm::isa<LLVMArrayType>(value.getType());
1067 p << '(' << (isArrayTy ? "filter " : "catch ") << value << " : "
1068 << value.getType() << ") ";
1069 }
1070
1071 p.printOptionalAttrDict(getAttrsForPrinting(*this).getAttrs(), {"cleanup"});
1072
1073 p << ": " << getType();
1074}
1075
1076// <operation> ::= `llvm.landingpad` `cleanup`?
1077// ((`catch` | `filter`) operand-type ssa-use)* attribute-dict?
1078ParseResult LandingpadOp::parse(OpAsmParser &parser, OperationState &result) {
1079 // Check for cleanup
1080 if (succeeded(parser.parseOptionalKeyword("cleanup")))
1081 result.addAttribute("cleanup", parser.getBuilder().getUnitAttr());
1082
1083 // Parse clauses with types
1084 while (succeeded(parser.parseOptionalLParen()) &&
1085 (succeeded(parser.parseOptionalKeyword("filter")) ||
1086 succeeded(parser.parseOptionalKeyword("catch")))) {
1088 Type ty;
1089 if (parser.parseOperand(operand) || parser.parseColon() ||
1090 parser.parseType(ty) ||
1091 parser.resolveOperand(operand, ty, result.operands) ||
1092 parser.parseRParen())
1093 return failure();
1094 }
1095
1096 Type type;
1097 if (parser.parseColon() || parser.parseType(type))
1098 return failure();
1099
1100 result.addTypes(type);
1101 return success();
1102}
1103
1104//===----------------------------------------------------------------------===//
1105// CallIntrinsicOp
1106//===----------------------------------------------------------------------===//
1107
1108LogicalResult CallIntrinsicOp::verify() {
1109 if (!getIntrin().starts_with("llvm."))
1110 return emitOpError() << "intrinsic name must start with 'llvm.'";
1111 if (failed(verifyOperandBundles(*this)))
1112 return failure();
1113 return success();
1114}
1115
1116void CallIntrinsicOp::build(OpBuilder &builder, OperationState &state,
1117 mlir::StringAttr intrin, mlir::ValueRange args) {
1118 build(builder, state, /*resultTypes=*/TypeRange{}, intrin, args,
1119 FastmathFlagsAttr{},
1120 /*op_bundle_operands=*/{}, /*op_bundle_tags=*/{}, /*arg_attrs=*/{},
1121 /*res_attrs=*/{});
1122}
1123
1124void CallIntrinsicOp::build(OpBuilder &builder, OperationState &state,
1125 mlir::StringAttr intrin, mlir::ValueRange args,
1126 mlir::LLVM::FastmathFlagsAttr fastMathFlags) {
1127 build(builder, state, /*resultTypes=*/TypeRange{}, intrin, args,
1128 fastMathFlags,
1129 /*op_bundle_operands=*/{}, /*op_bundle_tags=*/{}, /*arg_attrs=*/{},
1130 /*res_attrs=*/{});
1131}
1132
1133void CallIntrinsicOp::build(OpBuilder &builder, OperationState &state,
1134 mlir::Type resultType, mlir::StringAttr intrin,
1135 mlir::ValueRange args) {
1136 build(builder, state, {resultType}, intrin, args, FastmathFlagsAttr{},
1137 /*op_bundle_operands=*/{}, /*op_bundle_tags=*/{}, /*arg_attrs=*/{},
1138 /*res_attrs=*/{});
1139}
1140
1141void CallIntrinsicOp::build(OpBuilder &builder, OperationState &state,
1142 mlir::TypeRange resultTypes,
1143 mlir::StringAttr intrin, mlir::ValueRange args,
1144 mlir::LLVM::FastmathFlagsAttr fastMathFlags) {
1145 build(builder, state, resultTypes, intrin, args, fastMathFlags,
1146 /*op_bundle_operands=*/{}, /*op_bundle_tags=*/{}, /*arg_attrs=*/{},
1147 /*res_attrs=*/{});
1148}
1149
1150ParseResult CallIntrinsicOp::parse(OpAsmParser &parser,
1152 StringAttr intrinAttr;
1155 SmallVector<SmallVector<Type>> opBundleOperandTypes;
1156 ArrayAttr opBundleTags;
1157
1158 // Parse intrinsic name.
1160 intrinAttr, parser.getBuilder().getType<NoneType>()))
1161 return failure();
1162 result.addAttribute(CallIntrinsicOp::getIntrinAttrName(result.name),
1163 intrinAttr);
1164
1165 if (parser.parseLParen())
1166 return failure();
1167
1168 // Parse the function arguments.
1169 if (parser.parseOperandList(operands))
1170 return mlir::failure();
1171
1172 if (parser.parseRParen())
1173 return mlir::failure();
1174
1175 // Handle bundles.
1176 SMLoc opBundlesLoc = parser.getCurrentLocation();
1177 if (std::optional<ParseResult> result = parseOpBundles(
1178 parser, opBundleOperands, opBundleOperandTypes, opBundleTags);
1179 result && failed(*result))
1180 return failure();
1181 if (opBundleTags && !opBundleTags.empty())
1182 result.addAttribute(
1183 CallIntrinsicOp::getOpBundleTagsAttrName(result.name).getValue(),
1184 opBundleTags);
1185
1186 if (parser.parseOptionalAttrDict(result.attributes))
1187 return mlir::failure();
1188
1190 SmallVector<DictionaryAttr> resultAttrs;
1191 if (parseCallTypeAndResolveOperands(parser, result, /*isDirect=*/true,
1192 operands, argAttrs, resultAttrs))
1193 return failure();
1195 parser.getBuilder(), result, argAttrs, resultAttrs,
1196 getArgAttrsAttrName(result.name), getResAttrsAttrName(result.name));
1197
1198 if (resolveOpBundleOperands(parser, opBundlesLoc, result, opBundleOperands,
1199 opBundleOperandTypes,
1200 getOpBundleSizesAttrName(result.name)))
1201 return failure();
1202
1203 int32_t numOpBundleOperands = 0;
1204 for (const auto &operands : opBundleOperands)
1205 numOpBundleOperands += operands.size();
1206
1207 result.addAttribute(
1208 CallIntrinsicOp::getOperandSegmentSizeAttr(),
1210 {static_cast<int32_t>(operands.size()), numOpBundleOperands}));
1211
1212 return mlir::success();
1213}
1214
1215void CallIntrinsicOp::print(OpAsmPrinter &p) {
1216 p << ' ';
1217 p.printAttributeWithoutType(getIntrinAttr());
1218
1219 OperandRange args = getArgs();
1220 p << "(" << args << ")";
1221
1222 // Operand bundles.
1223 if (!getOpBundleOperands().empty()) {
1224 p << ' ';
1225 printOpBundles(p, *this, getOpBundleOperands(),
1226 getOpBundleOperands().getTypes(), getOpBundleTagsAttr());
1227 }
1228
1230 {getOperandSegmentSizesAttrName(),
1231 getOpBundleSizesAttrName(), getIntrinAttrName(),
1232 getOpBundleTagsAttrName(), getArgAttrsAttrName(),
1233 getResAttrsAttrName()});
1234
1235 p << " : ";
1236
1237 // Reconstruct the MLIR function type from operand and result types.
1239 p, args.getTypes(), getArgAttrsAttr(),
1240 /*isVariadic=*/false, getResultTypes(), getResAttrsAttr());
1241}
1242
1243//===----------------------------------------------------------------------===//
1244// InlineAsmOp
1245//===----------------------------------------------------------------------===//
1246
1247void InlineAsmOp::getEffects(
1249 &effects) {
1250 if (getHasSideEffects()) {
1251 effects.emplace_back(MemoryEffects::Write::get());
1252 effects.emplace_back(MemoryEffects::Read::get());
1253 }
1254}
1255
1256//===----------------------------------------------------------------------===//
1257// InlineAsmOp
1258//===----------------------------------------------------------------------===//
1259
1260LogicalResult InlineAsmOp::verify() {
1261 if (!getTailCallKindAttr())
1262 return success();
1263
1264 if (getTailCallKindAttr().getTailCallKind() == TailCallKind::MustTail)
1265 return emitOpError(
1266 "tail call kind 'musttail' is not supported by this operation");
1267
1268 return success();
1269}
1270
1271SmallVector<Value> mlir::LLVM::CallOp::getAccessedOperands() {
1272 // Note: This must not use `getArgOperands`, which excludes the variadic
1273 // arguments of a call to a variadic callee. Those are passed to the callee
1274 // and may well be accessed by it.
1275 Operation::operand_range operands = getCalleeOperands();
1276 // In an indirect call, the first callee operand is the callee itself.
1277 if (!getCallee().has_value() && !operands.empty())
1278 operands = operands.drop_front();
1279 return llvm::filter_to_vector(
1280 operands, [](Value arg) { return isa<LLVMPointerType>(arg.getType()); });
1281}
1282
1283#define GET_OP_CLASSES
1284#include "mlir/Dialect/LLVMIR/LLVMCallOps.cpp.inc"
return success()
getNumOperands() - 1))) return failure()
Given a list of lists of parsed operands, populates uniqueOperands with unique operands.
static LogicalResult verifyCallOpSymbolUses(OpTy callOp, SymbolTableCollection &symbolTable)
static void printOneOpBundle(OpAsmPrinter &p, OperandRange operands, TypeRange operandTypes, StringRef tag)
static unsigned getNumConsumedCalleeOperands(OpTy callOp)
Return the number of leading callee operands of callOp that the operation consumes instead of passing...
static LLVMFunctionType getLLVMFuncType(MLIRContext *context, TypeRange results, ValueRange args)
Constructs a LLVMFunctionType from MLIR results and args.
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 Operation::operand_range getOperandsPassedToCallee(OpTy callOp)
Return the operands of callOp that are passed to the callee, including the variadic arguments in case...
static LogicalResult verifyCallOpDebugInfo(OpTy callOp, LLVMFuncOp callee)
Verify that an inlinable callsite of a debug-info-bearing function in a debug-info-bearing function h...
static auto processFMFAttr(ArrayRef< NamedAttribute > attrs)
static TypeAttr getCallOpVarCalleeType(LLVMFunctionType calleeType)
Gets the variadic callee type for a LLVMFunctionType.
static ParseResult parseOneOpBundle(OpAsmParser &p, SmallVector< SmallVector< OpAsmParser::UnresolvedOperand > > &opBundleOperands, SmallVector< SmallVector< Type > > &opBundleOperandTypes, SmallVector< Attribute > &opBundleTags)
static ParseResult resolveOpBundleOperands(OpAsmParser &parser, SMLoc loc, OperationState &state, ArrayRef< SmallVector< OpAsmParser::UnresolvedOperand > > opBundleOperands, ArrayRef< SmallVector< Type > > opBundleOperandTypes, StringAttr opBundleSizesAttrName)
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 SmallVector< Type, 1 > getCallOpResultTypes(LLVMFunctionType calleeType)
Gets the MLIR Op-like result types of a LLVMFunctionType.
static Operation::operand_range getArgOperandsImpl(OpTy callOp)
Return the operands of callOp that correspond to the declared parameters of the callee,...
ArrayAttr()
b getContext())
@ Paren
Parens surrounding zero or more operands.
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 parseRSquare()=0
Parse a ] token.
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 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 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.
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.
MLIRContext * getContext() const
Return the context this attribute belongs to.
Block represents an ordered list of Operations.
Definition Block.h:34
bool empty()
Definition Block.h:173
Operation & front()
Definition Block.h:178
This class is a general helper class for creating context-global objects like types,...
Definition Builders.h:51
UnitAttr getUnitAttr()
Definition Builders.cpp:106
DenseI32ArrayAttr getDenseI32ArrayAttr(ArrayRef< int32_t > values)
Definition Builders.cpp:171
Ty getType(Args &&...args)
Get or construct an instance of the type Ty with provided arguments.
Definition Builders.h:94
StringAttr getStringAttr(const Twine &bytes)
Definition Builders.cpp:271
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.
Definition Location.h:151
Diagnostic & attachNote(std::optional< Location > noteLoc=std::nullopt)
Attaches a note to this diagnostic.
T findInstanceOf()
Return an instance of the given location type if one is nested under the current location.
Definition Location.h:45
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:71
This class provides a mutable adaptor for a range of operands.
Definition ValueRange.h:119
MutableOperandRange slice(unsigned subStart, unsigned subLen, std::optional< OperandSegment > segment=std::nullopt) const
Slice this range into a sub range, with the additional operand segment.
ArrayRef< NamedAttribute > getAttrs() const
Return all of the attributes on this operation.
NamedAttribute represents a combination of a name and an Attribute value.
Definition Attributes.h:166
StringAttr getName() const
Return the name of the attribute.
Attribute getValue() const
Return the value of the attribute.
Definition Attributes.h:181
The OpAsmParser has methods for interacting with the asm parser: parsing things from it,...
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.
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(OperandRange operands)
Print a comma separated range of operation operands out of line to avoid instantiating the range iter...
virtual void printOptionalAttrDict(ArrayRef< NamedAttribute > attrs, ArrayRef< StringRef > elidedAttrs={})=0
If the specified operation has attributes, print out an attribute dictionary with their values.
This class helps build Operations.
Definition Builders.h:210
This class represents a contiguous range of operand ranges, e.g.
Definition ValueRange.h:85
This class implements the operand iterators for the Operation class.
Definition ValueRange.h:44
type_range getTypes() const
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
Location getLoc()
The source location the operation was defined or derived from.
Definition Operation.h:240
OperandRange operand_range
Definition Operation.h:398
This class implements Optional functionality for ParseResult.
bool has_value() const
Returns true if we contain a valid ParseResult value.
This class represents a specific instance of an effect.
This class models how operands are forwarded to block arguments in control flow.
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,...
This class provides an abstraction for a range of TypeRange.
Definition TypeRange.h:107
This class provides an abstraction over the various different ranges of value types.
Definition TypeRange.h:40
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
This class provides an abstraction over the different types of ranges over Values.
Definition ValueRange.h:392
type_range getTypes() const
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Type getType() const
Return the type of this value.
Definition Value.h:105
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
Definition Value.cpp:18
static DenseArrayAttrImpl get(MLIRContext *context, ArrayRef< int32_t > content)
NamedAttrList getAttrsForPrinting(Operation *op)
Returns the attributes of op to print: its discardable attributes and all its inherent attributes.
void printOpBundles(mlir::OpAsmPrinter &p, mlir::Operation *op, mlir::OperandRangeRange opBundleOperands, mlir::TypeRangeRange opBundleOperandTypes, std::optional< mlir::ArrayAttr > opBundleTags)
RetTy parseOptionalLLVMKeyword(OpAsmParser &parser, EnumTy defaultValue)
Parse an enum from the keyword, or default to the provided default value.
Definition LLVMOps.h:155
LogicalResult verifyOperandBundles(OpType &op)
Verifies the operand bundles of op.
Definition LLVMOps.h:170
std::optional< mlir::ParseResult > parseOpBundles(mlir::OpAsmParser &p, mlir::SmallVector< mlir::SmallVector< mlir::OpAsmParser::UnresolvedOperand > > &opBundleOperands, mlir::SmallVector< mlir::SmallVector< mlir::Type > > &opBundleOperandTypes, mlir::ArrayAttr &opBundleTags)
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.
LogicalResult verifyCallOpInterface(CallOpInterface call, TypeRange argumentTypes, TypeRange resultTypes)
Verify that the forwarded operands and results of call are in a 1:1 relationship with the given argum...
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,...
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:886
Include the generated interface declarations.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
Definition Utils.cpp:311
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
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.
This represents an operation in an abstracted form, suitable for use with the builder APIs.
SmallVector< Value, 4 > operands
void addAttribute(StringRef name, Attribute attr)
Add an attribute with the specified name.