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
13#include "mlir/Dialect/LLVMIR/LLVMCallOps.h"
14
15#include "IR/LLVMOps.h"
16#include "mlir/Dialect/LLVMIR/LLVMConstantOps.h"
17#include "mlir/Dialect/LLVMIR/LLVMConversionOps.h"
18#include "mlir/Dialect/LLVMIR/LLVMModuleOps.h"
19#include "mlir/IR/Builders.h"
20#include "mlir/IR/DialectImplementation.h"
21#include "llvm/ADT/STLExtras.h"
22
23#include <optional>
24
25using namespace mlir;
26using namespace mlir::LLVM;
27
28static auto processFMFAttr(ArrayRef<NamedAttribute> attrs) {
29 SmallVector<NamedAttribute, 8> filteredAttrs(
30 llvm::make_filter_range(Range&: attrs, Pred: [&](NamedAttribute attr) {
31 if (attr.getName() == "fastmathFlags") {
32 auto defAttr =
33 FastmathFlagsAttr::get(context: attr.getValue().getContext(), value: {});
34 return defAttr != attr.getValue();
35 }
36 return true;
37 }));
38 return filteredAttrs;
39}
40
41//===----------------------------------------------------------------------===//
42// Operand bundle helpers.
43//===----------------------------------------------------------------------===//
44
45static void printOneOpBundle(OpAsmPrinter &p, OperandRange operands,
46 TypeRange operandTypes, StringRef tag) {
47 p.printString(string: tag);
48 p << "(";
49
50 if (!operands.empty()) {
51 p.printOperands(operands);
52 p << " : ";
53 llvm::interleaveComma(c: operandTypes, os&: p);
54 }
55
56 p << ")";
57}
58
59void mlir::LLVM::printOpBundles(OpAsmPrinter &p, Operation *op,
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 c: llvm::zip(t&: opBundleOperands, u&: opBundleOperandTypes, args&: *opBundleTags), os&: p,
70 each_fn: [&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,
80 SmallVector<SmallVector<OpAsmParser::UnresolvedOperand>> &opBundleOperands,
81 SmallVector<SmallVector<Type>> &opBundleOperandTypes,
82 SmallVector<Attribute> &opBundleTags) {
83 SMLoc currentParserLoc = p.getCurrentLocation();
84 SmallVector<OpAsmParser::UnresolvedOperand> operands;
85 SmallVector<Type> types;
86 std::string tag;
87
88 if (p.parseString(string: &tag))
89 return p.emitError(loc: currentParserLoc, message: "expect operand bundle tag");
90
91 if (p.parseLParen())
92 return failure();
93
94 if (p.parseOptionalRParen()) {
95 if (p.parseOperandList(result&: operands) || p.parseColon() ||
96 p.parseTypeList(result&: types) || p.parseRParen())
97 return failure();
98 }
99
100 opBundleOperands.push_back(Elt: std::move(operands));
101 opBundleOperandTypes.push_back(Elt: std::move(types));
102 opBundleTags.push_back(Elt: StringAttr::get(context: p.getContext(), bytes: tag));
103
104 return success();
105}
106
107std::optional<ParseResult> mlir::LLVM::parseOpBundles(
108 OpAsmParser &p,
109 SmallVector<SmallVector<OpAsmParser::UnresolvedOperand>> &opBundleOperands,
110 SmallVector<SmallVector<Type>> &opBundleOperandTypes,
111 ArrayAttr &opBundleTags) {
112 if (p.parseOptionalLSquare())
113 return std::nullopt;
114
115 if (succeeded(Result: p.parseOptionalRSquare()))
116 return success();
117
118 SmallVector<Attribute> opBundleTagAttrs;
119 auto bundleParser = [&] {
120 return parseOneOpBundle(p, opBundleOperands, opBundleOperandTypes,
121 opBundleTags&: opBundleTagAttrs);
122 };
123 if (p.parseCommaSeparatedList(parseElementFn: bundleParser))
124 return failure();
125
126 if (p.parseRSquare())
127 return failure();
128
129 opBundleTags = ArrayAttr::get(context: p.getContext(), value: 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>(Val: resultType))
143 results.push_back(Elt: 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(type: 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(ctx: context);
158 else
159 resultType = results.front();
160 return LLVMFunctionType::get(result: resultType, arguments: llvm::to_vector(Range: args.getTypes()),
161 /*isVarArg=*/false);
162}
163
164void CallOp::build(OpBuilder &builder, OperationState &state, TypeRange results,
165 StringRef callee, ValueRange args) {
166 build(odsBuilder&: builder, odsState&: state, results, callee: builder.getStringAttr(bytes: callee), args);
167}
168
169void CallOp::build(OpBuilder &builder, OperationState &state, TypeRange results,
170 StringAttr callee, ValueRange args) {
171 build(odsBuilder&: builder, odsState&: state, results, callee: SymbolRefAttr::get(value: 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(odsBuilder&: builder, odsState&: state, resultTypes: results,
178 /*var_callee_type=*/nullptr, callee, callee_operands: 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=*/no_inline: nullptr, /*alias_scopes=*/always_inline: nullptr,
195 /*noalias_scopes=*/inline_hint: nullptr, /*tbaa=*/access_groups: nullptr,
196 /*no_inline=*/alias_scopes: nullptr, /*always_inline=*/noalias_scopes: nullptr,
197 /*inline_hint=*/tbaa: nullptr);
198}
199
200void CallOp::build(OpBuilder &builder, OperationState &state,
201 LLVMFunctionType calleeType, StringRef callee,
202 ValueRange args) {
203 build(odsBuilder&: builder, odsState&: state, calleeType, callee: builder.getStringAttr(bytes: callee), args);
204}
205
206void CallOp::build(OpBuilder &builder, OperationState &state,
207 LLVMFunctionType calleeType, StringAttr callee,
208 ValueRange args) {
209 build(odsBuilder&: builder, odsState&: state, calleeType, callee: SymbolRefAttr::get(value: callee), args);
210}
211
212void CallOp::build(OpBuilder &builder, OperationState &state,
213 LLVMFunctionType calleeType, FlatSymbolRefAttr callee,
214 ValueRange args) {
215 build(odsBuilder&: builder, odsState&: state, resultTypes: getCallOpResultTypes(calleeType),
216 var_callee_type: getCallOpVarCalleeType(calleeType), callee, callee_operands: 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=*/no_inline: nullptr,
236 /*alias_scopes=*/always_inline: nullptr, /*noalias_scopes=*/inline_hint: nullptr, /*tbaa=*/access_groups: nullptr,
237 /*no_inline=*/alias_scopes: nullptr, /*always_inline=*/noalias_scopes: nullptr,
238 /*inline_hint=*/tbaa: nullptr);
239}
240
241void CallOp::build(OpBuilder &builder, OperationState &state,
242 LLVMFunctionType calleeType, ValueRange args) {
243 build(odsBuilder&: builder, odsState&: state, resultTypes: getCallOpResultTypes(calleeType),
244 var_callee_type: getCallOpVarCalleeType(calleeType),
245 /*callee=*/nullptr, callee_operands: 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=*/no_inline: nullptr, /*alias_scopes=*/always_inline: nullptr,
263 /*noalias_scopes=*/inline_hint: nullptr, /*tbaa=*/access_groups: nullptr,
264 /*no_inline=*/alias_scopes: nullptr, /*always_inline=*/noalias_scopes: nullptr,
265 /*inline_hint=*/tbaa: nullptr);
266}
267
268void CallOp::build(OpBuilder &builder, OperationState &state, LLVMFuncOp func,
269 ValueRange args) {
270 auto calleeType = func.getFunctionType();
271 build(odsBuilder&: builder, odsState&: state, resultTypes: getCallOpResultTypes(calleeType),
272 var_callee_type: getCallOpVarCalleeType(calleeType), callee: SymbolRefAttr::get(symbol: func), callee_operands: 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=*/arg_attrs: nullptr, /*alias_scopes=*/res_attrs: nullptr,
289 /*arg_attrs=*/no_inline: nullptr, /*res_attrs=*/always_inline: nullptr,
290 /*noalias_scopes=*/inline_hint: nullptr, /*tbaa=*/access_groups: nullptr,
291 /*no_inline=*/alias_scopes: nullptr, /*always_inline=*/noalias_scopes: nullptr,
292 /*inline_hint=*/tbaa: 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(i: 0);
301}
302
303void CallOp::setCalleeFromCallable(CallInterfaceCallable callee) {
304 // Direct call.
305 if (FlatSymbolRefAttr calleeAttr = getCalleeAttr()) {
306 auto symRef = cast<SymbolRefAttr>(Val&: callee);
307 return setCalleeAttr(cast<FlatSymbolRefAttr>(Val&: symRef));
308 }
309 // Indirect call, callee Value is the first operand.
310 return setOperand(i: 0, value: cast<Value>(Val&: 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>
326static Operation::operand_range getOperandsPassedToCallee(OpTy callOp) {
327 return callOp.getCalleeOperands().drop_front(
328 getNumConsumedCalleeOperands(callOp));
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>
340static Operation::operand_range getArgOperandsImpl(OpTy callOp) {
341 Operation::operand_range operands = getOperandsPassedToCallee(callOp);
342 if (std::optional<LLVMFunctionType> varCalleeType = callOp.getVarCalleeType())
343 return operands.take_front(n: varCalleeType->getNumParams());
344 return operands;
345}
346
347Operation::operand_range CallOp::getArgOperands() {
348 return getArgOperandsImpl(callOp: *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(subStart: getNumConsumedCalleeOperands(callOp: *this),
355 subLen: getArgOperandsImpl(callOp: *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()
371 ->findInstanceOf<FusedLocWith<LLVM::DISubprogramAttr>>() !=
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.
404 Operation::operand_range passedOperands = getOperandsPassedToCallee(callOp);
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(t: varCalleeType->getParams(), u&: 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>(Val: 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(op&: *this); }
434
435template <typename OpTy>
436static LogicalResult
437verifyCallOpSymbolUses(OpTy callOp, SymbolTableCollection &symbolTable) {
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>(Val: callee)) {
465 if (failed(verifyCallOpDebugInfo(callOp, fn)))
466 return failure();
467 fnType = fn.getFunctionType();
468 } else if (auto ifunc = dyn_cast<IFuncOp>(Val: callee)) {
469 fnType = ifunc.getIFuncType();
470 } else if (isa<AliasOp>(Val: 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>(Val&: 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>(Val: funcType.getReturnType()))
495 return callOp.emitOpError() << "expected function call to produce a value";
496
497 if (callOp.getNumResults() != 0 &&
498 llvm::isa<LLVM::LLVMVoidType>(Val: 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>(Val: funcType.getReturnType()))
517 calleeResultTypes.push_back(Elt: funcType.getReturnType());
518 return call_interface_impl::verifyCallOpInterface(
519 callOp, funcType.getParams(), calleeResultTypes);
520}
521
522LogicalResult CallOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
523 return verifyCallOpSymbolUses(callOp: *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(symbolRef: callee.value());
543 else
544 p << getOperand(i: 0);
545
546 auto args = getCalleeOperands().drop_front(n: 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, op: *this, opBundleOperands: getOpBundleOperands(),
556 opBundleOperandTypes: getOpBundleOperands().getTypes(), opBundleTags: getOpBundleTags());
557 }
558
559 p.printOptionalAttrDict(attrs: processFMFAttr(attrs: getAttrsForPrinting(op: *this).getAttrs()),
560 elidedAttrs: {getCalleeAttrName(), getTailCallKindAttrName(),
561 getVarCalleeTypeAttrName(), getCConvAttrName(),
562 getOperandSegmentSizesAttrName(),
563 getOpBundleSizesAttrName(),
564 getOpBundleTagsAttrName(), getArgAttrsAttrName(),
565 getResAttrsAttrName()});
566
567 p << " : ";
568 if (!isDirect)
569 p << getOperand(i: 0).getType() << ", ";
570
571 // Reconstruct the MLIR function type from operand and result types.
572 call_interface_impl::printFunctionSignature(
573 p, argTypes: args.getTypes(), argAttrs: getArgAttrsAttr(),
574 /*isVariadic=*/false, resultTypes: getResultTypes(), resultAttrs: getResAttrsAttr());
575}
576
577/// Parses the type of a call operation and resolves the operands if the parsing
578/// succeeds. Returns failure otherwise.
579static ParseResult parseCallTypeAndResolveOperands(
580 OpAsmParser &parser, OperationState &result, bool isDirect,
581 ArrayRef<OpAsmParser::UnresolvedOperand> operands,
582 SmallVectorImpl<DictionaryAttr> &argAttrs,
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(result&: types.back()))
591 return failure();
592 if (parser.parseOptionalComma())
593 return parser.emitError(
594 loc: trailingTypesLoc, message: "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 resultTypes&: resTypes, resultAttrs)) {
600 if (isDirect)
601 return parser.emitError(loc: trailingTypesLoc,
602 message: "expected direct call to have 1 trailing types");
603 return parser.emitError(loc: trailingTypesLoc,
604 message: "expected trailing function type");
605 }
606
607 if (resTypes.size() > 1)
608 return parser.emitError(loc: trailingTypesLoc,
609 message: "expected function with 0 or 1 result");
610 if (resTypes.size() == 1 && llvm::isa<LLVM::LLVMVoidType>(Val: resTypes[0]))
611 return parser.emitError(loc: trailingTypesLoc,
612 message: "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(C&: types, R&: argTypes);
619 if (parser.resolveOperands(operands, types, loc: parser.getNameLoc(),
620 result&: result.operands))
621 return failure();
622 if (!resTypes.empty())
623 result.addTypes(newTypes: 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,
632 SmallVectorImpl<OpAsmParser::UnresolvedOperand> &operands) {
633 OpAsmParser::UnresolvedOperand funcPtrOperand;
634 OptionalParseResult parseResult = parser.parseOptionalOperand(result&: funcPtrOperand);
635 if (parseResult.has_value()) {
636 if (failed(Result: *parseResult))
637 return *parseResult;
638 operands.push_back(Elt: funcPtrOperand);
639 }
640 return success();
641}
642
643static ParseResult resolveOpBundleOperands(
644 OpAsmParser &parser, SMLoc loc, OperationState &state,
645 ArrayRef<SmallVector<OpAsmParser::UnresolvedOperand>> opBundleOperands,
646 ArrayRef<SmallVector<Type>> opBundleOperandTypes,
647 StringAttr opBundleSizesAttrName) {
648 unsigned opBundleIndex = 0;
649 for (const auto &[operands, types] :
650 llvm::zip_equal(t&: opBundleOperands, u&: opBundleOperandTypes)) {
651 if (operands.size() != types.size())
652 return parser.emitError(loc, message: "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, result&: state.operands))
657 return failure();
658 }
659
660 SmallVector<int32_t> opBundleSizes;
661 opBundleSizes.reserve(N: opBundleOperands.size());
662 for (const auto &operands : opBundleOperands)
663 opBundleSizes.push_back(Elt: operands.size());
664
665 state.addAttribute(
666 name: opBundleSizesAttrName,
667 attr: DenseI32ArrayAttr::get(context: parser.getContext(), content: 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;
680 SmallVector<OpAsmParser::UnresolvedOperand> operands;
681 SmallVector<SmallVector<OpAsmParser::UnresolvedOperand>> opBundleOperands;
682 SmallVector<SmallVector<Type>> opBundleOperandTypes;
683 ArrayAttr opBundleTags;
684
685 // Default to C Calling Convention if no keyword is provided.
686 result.addAttribute(
687 name: getCConvAttrName(name: result.name),
688 attr: CConvAttr::get(context: parser.getContext(),
689 CallingConv: parseOptionalLLVMKeyword<CConv>(parser, defaultValue: LLVM::CConv::C)));
690
691 result.addAttribute(
692 name: getTailCallKindAttrName(name: result.name),
693 attr: TailCallKindAttr::get(context: parser.getContext(),
694 tailCallKind: parseOptionalLLVMKeyword<TailCallKind>(
695 parser, defaultValue: 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(result&: funcAttr, attrName: "callee", attrs&: result.attributes))
705 return failure();
706
707 // Parse the function arguments.
708 if (parser.parseOperandList(result&: operands, delimiter: OpAsmParser::Delimiter::Paren))
709 return failure();
710
711 bool isVarArg = parser.parseOptionalKeyword(keyword: "vararg").succeeded();
712 if (isVarArg) {
713 StringAttr varCalleeTypeAttrName =
714 CallOp::getVarCalleeTypeAttrName(name: result.name);
715 if (parser.parseLParen().failed() ||
716 parser
717 .parseAttribute(result&: varCalleeType, attrName: varCalleeTypeAttrName,
718 attrs&: 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 p&: parser, opBundleOperands, opBundleOperandTypes, opBundleTags);
727 result && failed(Result: *result))
728 return failure();
729 if (opBundleTags && !opBundleTags.empty())
730 result.addAttribute(name: CallOp::getOpBundleTagsAttrName(name: result.name).getValue(),
731 attr: opBundleTags);
732
733 if (parser.parseOptionalAttrDict(result&: result.attributes))
734 return failure();
735
736 // Parse the trailing type list and resolve the operands.
737 SmallVector<DictionaryAttr> argAttrs;
738 SmallVector<DictionaryAttr> resultAttrs;
739 if (parseCallTypeAndResolveOperands(parser, result, isDirect, operands,
740 argAttrs, resultAttrs))
741 return failure();
742 call_interface_impl::addArgAndResultAttrs(
743 builder&: parser.getBuilder(), result, argAttrs, resultAttrs,
744 argAttrsName: getArgAttrsAttrName(name: result.name), resAttrsName: getResAttrsAttrName(name: result.name));
745 if (resolveOpBundleOperands(parser, loc: opBundlesLoc, state&: result, opBundleOperands,
746 opBundleOperandTypes,
747 opBundleSizesAttrName: getOpBundleSizesAttrName(name: 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 name: CallOp::getOperandSegmentSizeAttr(),
756 attr: parser.getBuilder().getDenseI32ArrayAttr(
757 values: {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(context: getContext(), results: getResultTypes(), args: 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(odsBuilder&: builder, odsState&: state, resultTypes: getCallOpResultTypes(calleeType),
776 var_callee_type: getCallOpVarCalleeType(calleeType), callee: SymbolRefAttr::get(symbol: func), callee_operands: ops,
777 /*arg_attrs=*/nullptr, /*res_attrs=*/nullptr, normalDestOperands: normalOps, unwindDestOperands: unwindOps,
778 branch_weights: nullptr, CConv: nullptr, /*default_func_attrs=*/nullptr,
779 /*uniform_work_group_size=*/nullptr, op_bundle_operands: {}, op_bundle_tags: {}, normalDest: normal, unwindDest: 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(odsBuilder&: builder, odsState&: state, resultTypes: tys,
787 /*var_callee_type=*/nullptr, callee, callee_operands: ops, /*arg_attrs=*/nullptr,
788 /*res_attrs=*/nullptr, normalDestOperands: normalOps, unwindDestOperands: unwindOps, branch_weights: nullptr, CConv: nullptr,
789 /*default_func_attrs=*/nullptr,
790 /*uniform_work_group_size=*/nullptr, op_bundle_operands: {}, op_bundle_tags: {}, normalDest: normal, unwindDest: 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(odsBuilder&: builder, odsState&: state, resultTypes: getCallOpResultTypes(calleeType),
798 var_callee_type: getCallOpVarCalleeType(calleeType), callee, callee_operands: ops,
799 /*arg_attrs=*/nullptr, /*res_attrs=*/nullptr, normalDestOperands: normalOps, unwindDestOperands: unwindOps,
800 branch_weights: nullptr, CConv: nullptr, /*default_func_attrs=*/nullptr,
801 /*uniform_work_group_size=*/nullptr, op_bundle_operands: {}, op_bundle_tags: {}, normalDest: normal, unwindDest: 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(i: 0);
816}
817
818void InvokeOp::setCalleeFromCallable(CallInterfaceCallable callee) {
819 // Direct call.
820 if (FlatSymbolRefAttr calleeAttr = getCalleeAttr()) {
821 auto symRef = cast<SymbolRefAttr>(Val&: callee);
822 return setCalleeAttr(cast<FlatSymbolRefAttr>(Val&: symRef));
823 }
824 // Indirect call, callee Value is the first operand.
825 return setOperand(i: 0, value: cast<Value>(Val&: callee));
826}
827
828Operation::operand_range InvokeOp::getArgOperands() {
829 return getArgOperandsImpl(callOp: *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(subStart: getNumConsumedCalleeOperands(callOp: *this),
836 subLen: getArgOperandsImpl(callOp: *this).size());
837}
838
839LogicalResult InvokeOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
840 return verifyCallOpSymbolUses(callOp: *this, symbolTable);
841}
842
843LogicalResult InvokeOp::verify() {
844 Block *unwindDest = getUnwindDest();
845 if (unwindDest->empty())
846 return emitError(message: "must have at least one operation in unwind destination");
847
848 // In unwind destination, first operation must be LandingpadOp
849 if (!isa<LandingpadOp>(Val: unwindDest->front()))
850 return emitError(message: "first operation in unwind destination should be a "
851 "llvm.landingpad operation");
852
853 if (failed(Result: verifyOperandBundles(op&: *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(symbolRef: callee.value());
872 else
873 p << getOperand(i: 0);
874
875 p << '(' << getCalleeOperands().drop_front(n: isDirect ? 0 : 1) << ')';
876 p << " to ";
877 p.printSuccessorAndUseList(successor: getNormalDest(), succOperands: getNormalDestOperands());
878 p << " unwind ";
879 p.printSuccessorAndUseList(successor: getUnwindDest(), succOperands: 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, op: *this, opBundleOperands: getOpBundleOperands(),
888 opBundleOperandTypes: getOpBundleOperands().getTypes(), opBundleTags: getOpBundleTags());
889 }
890
891 p.printOptionalAttrDict(attrs: getAttrsForPrinting(op: *this).getAttrs(),
892 elidedAttrs: {getCalleeAttrName(), getOperandSegmentSizeAttr(),
893 getCConvAttrName(), getVarCalleeTypeAttrName(),
894 getOpBundleSizesAttrName(),
895 getOpBundleTagsAttrName(), getArgAttrsAttrName(),
896 getResAttrsAttrName()});
897
898 p << " : ";
899 if (!isDirect)
900 p << getOperand(i: 0).getType() << ", ";
901 call_interface_impl::printFunctionSignature(
902 p, argTypes: getCalleeOperands().drop_front(n: isDirect ? 0 : 1).getTypes(),
903 argAttrs: getArgAttrsAttr(),
904 /*isVariadic=*/false, resultTypes: getResultTypes(), resultAttrs: 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) {
916 SmallVector<OpAsmParser::UnresolvedOperand, 8> operands;
917 SymbolRefAttr funcAttr;
918 TypeAttr varCalleeType;
919 SmallVector<SmallVector<OpAsmParser::UnresolvedOperand>> opBundleOperands;
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 name: getCConvAttrName(name: result.name),
929 attr: CConvAttr::get(context: parser.getContext(),
930 CallingConv: parseOptionalLLVMKeyword<CConv>(parser, defaultValue: 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(result&: funcAttr, attrName: "callee", attrs&: result.attributes))
939 return failure();
940
941 // Parse the function arguments.
942 if (parser.parseOperandList(result&: operands, delimiter: OpAsmParser::Delimiter::Paren) ||
943 parser.parseKeyword(keyword: "to") ||
944 parser.parseSuccessorAndUseList(dest&: normalDest, operands&: normalOperands) ||
945 parser.parseKeyword(keyword: "unwind") ||
946 parser.parseSuccessorAndUseList(dest&: unwindDest, operands&: unwindOperands))
947 return failure();
948
949 bool isVarArg = parser.parseOptionalKeyword(keyword: "vararg").succeeded();
950 if (isVarArg) {
951 StringAttr varCalleeTypeAttrName =
952 InvokeOp::getVarCalleeTypeAttrName(name: result.name);
953 if (parser.parseLParen().failed() ||
954 parser
955 .parseAttribute(result&: varCalleeType, attrName: varCalleeTypeAttrName,
956 attrs&: 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 p&: parser, opBundleOperands, opBundleOperandTypes, opBundleTags);
965 result && failed(Result: *result))
966 return failure();
967 if (opBundleTags && !opBundleTags.empty())
968 result.addAttribute(
969 name: InvokeOp::getOpBundleTagsAttrName(name: result.name).getValue(),
970 attr: opBundleTags);
971
972 if (parser.parseOptionalAttrDict(result&: result.attributes))
973 return failure();
974
975 // Parse the trailing type list and resolve the function operands.
976 SmallVector<DictionaryAttr> argAttrs;
977 SmallVector<DictionaryAttr> resultAttrs;
978 if (parseCallTypeAndResolveOperands(parser, result, isDirect, operands,
979 argAttrs, resultAttrs))
980 return failure();
981 call_interface_impl::addArgAndResultAttrs(
982 builder&: parser.getBuilder(), result, argAttrs, resultAttrs,
983 argAttrsName: getArgAttrsAttrName(name: result.name), resAttrsName: getResAttrsAttrName(name: result.name));
984
985 result.addSuccessors(newSuccessors: {normalDest, unwindDest});
986 result.addOperands(newOperands: normalOperands);
987 result.addOperands(newOperands: unwindOperands);
988
989 if (resolveOpBundleOperands(parser, loc: opBundlesLoc, state&: result, opBundleOperands,
990 opBundleOperandTypes,
991 opBundleSizesAttrName: getOpBundleSizesAttrName(name: 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 name: InvokeOp::getOperandSegmentSizeAttr(),
1000 attr: builder.getDenseI32ArrayAttr(values: {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(context: getContext(), results: getResultTypes(), args: 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 message: "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(message: "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(i: idx);
1034 bool isFilter = llvm::isa<LLVMArrayType>(Val: 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(message: "constant clauses expected").attachNote(noteLoc: 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(message: "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>(Val: value.getType());
1067 p << '(' << (isArrayTy ? "filter " : "catch ") << value << " : "
1068 << value.getType() << ") ";
1069 }
1070
1071 p.printOptionalAttrDict(attrs: getAttrsForPrinting(op: *this).getAttrs(), elidedAttrs: {"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(Result: parser.parseOptionalKeyword(keyword: "cleanup")))
1081 result.addAttribute(name: "cleanup", attr: parser.getBuilder().getUnitAttr());
1082
1083 // Parse clauses with types
1084 while (succeeded(Result: parser.parseOptionalLParen()) &&
1085 (succeeded(Result: parser.parseOptionalKeyword(keyword: "filter")) ||
1086 succeeded(Result: parser.parseOptionalKeyword(keyword: "catch")))) {
1087 OpAsmParser::UnresolvedOperand operand;
1088 Type ty;
1089 if (parser.parseOperand(result&: operand) || parser.parseColon() ||
1090 parser.parseType(result&: ty) ||
1091 parser.resolveOperand(operand, type: ty, result&: result.operands) ||
1092 parser.parseRParen())
1093 return failure();
1094 }
1095
1096 Type type;
1097 if (parser.parseColon() || parser.parseType(result&: type))
1098 return failure();
1099
1100 result.addTypes(newTypes: type);
1101 return success();
1102}
1103
1104//===----------------------------------------------------------------------===//
1105// CallIntrinsicOp
1106//===----------------------------------------------------------------------===//
1107
1108LogicalResult CallIntrinsicOp::verify() {
1109 if (!getIntrin().starts_with(Prefix: "llvm."))
1110 return emitOpError() << "intrinsic name must start with 'llvm.'";
1111 if (failed(Result: verifyOperandBundles(op&: *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(odsBuilder&: builder, odsState&: state, /*resultTypes=*/TypeRange{}, intrin, args,
1119 fastmathFlags: 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(odsBuilder&: builder, odsState&: state, /*resultTypes=*/TypeRange{}, intrin, args,
1128 fastmathFlags: 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(odsBuilder&: builder, odsState&: state, results: {resultType}, intrin, args, fastmathFlags: 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(odsBuilder&: builder, odsState&: state, resultTypes, intrin, args, fastmathFlags: fastMathFlags,
1146 /*op_bundle_operands=*/{}, /*op_bundle_tags=*/{}, /*arg_attrs=*/{},
1147 /*res_attrs=*/{});
1148}
1149
1150ParseResult CallIntrinsicOp::parse(OpAsmParser &parser,
1151 OperationState &result) {
1152 StringAttr intrinAttr;
1153 SmallVector<OpAsmParser::UnresolvedOperand, 4> operands;
1154 SmallVector<SmallVector<OpAsmParser::UnresolvedOperand>> opBundleOperands;
1155 SmallVector<SmallVector<Type>> opBundleOperandTypes;
1156 ArrayAttr opBundleTags;
1157
1158 // Parse intrinsic name.
1159 if (parser.parseCustomAttributeWithFallback(
1160 result&: intrinAttr, type: parser.getBuilder().getType<NoneType>()))
1161 return failure();
1162 result.addAttribute(name: CallIntrinsicOp::getIntrinAttrName(name: result.name),
1163 attr: intrinAttr);
1164
1165 if (parser.parseLParen())
1166 return failure();
1167
1168 // Parse the function arguments.
1169 if (parser.parseOperandList(result&: 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 p&: parser, opBundleOperands, opBundleOperandTypes, opBundleTags);
1179 result && failed(Result: *result))
1180 return failure();
1181 if (opBundleTags && !opBundleTags.empty())
1182 result.addAttribute(
1183 name: CallIntrinsicOp::getOpBundleTagsAttrName(name: result.name).getValue(),
1184 attr: opBundleTags);
1185
1186 if (parser.parseOptionalAttrDict(result&: result.attributes))
1187 return mlir::failure();
1188
1189 SmallVector<DictionaryAttr> argAttrs;
1190 SmallVector<DictionaryAttr> resultAttrs;
1191 if (parseCallTypeAndResolveOperands(parser, result, /*isDirect=*/true,
1192 operands, argAttrs, resultAttrs))
1193 return failure();
1194 call_interface_impl::addArgAndResultAttrs(
1195 builder&: parser.getBuilder(), result, argAttrs, resultAttrs,
1196 argAttrsName: getArgAttrsAttrName(name: result.name), resAttrsName: getResAttrsAttrName(name: result.name));
1197
1198 if (resolveOpBundleOperands(parser, loc: opBundlesLoc, state&: result, opBundleOperands,
1199 opBundleOperandTypes,
1200 opBundleSizesAttrName: getOpBundleSizesAttrName(name: 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 name: CallIntrinsicOp::getOperandSegmentSizeAttr(),
1209 attr: parser.getBuilder().getDenseI32ArrayAttr(
1210 values: {static_cast<int32_t>(operands.size()), numOpBundleOperands}));
1211
1212 return mlir::success();
1213}
1214
1215void CallIntrinsicOp::print(OpAsmPrinter &p) {
1216 p << ' ';
1217 p.printAttributeWithoutType(attr: getIntrinAttr());
1218
1219 OperandRange args = getArgs();
1220 p << "(" << args << ")";
1221
1222 // Operand bundles.
1223 if (!getOpBundleOperands().empty()) {
1224 p << ' ';
1225 printOpBundles(p, op: *this, opBundleOperands: getOpBundleOperands(),
1226 opBundleOperandTypes: getOpBundleOperands().getTypes(), opBundleTags: getOpBundleTagsAttr());
1227 }
1228
1229 p.printOptionalAttrDict(attrs: processFMFAttr(attrs: getAttrsForPrinting(op: *this).getAttrs()),
1230 elidedAttrs: {getOperandSegmentSizesAttrName(),
1231 getOpBundleSizesAttrName(), getIntrinAttrName(),
1232 getOpBundleTagsAttrName(), getArgAttrsAttrName(),
1233 getResAttrsAttrName()});
1234
1235 p << " : ";
1236
1237 // Reconstruct the MLIR function type from operand and result types.
1238 call_interface_impl::printFunctionSignature(
1239 p, argTypes: args.getTypes(), argAttrs: getArgAttrsAttr(),
1240 /*isVariadic=*/false, resultTypes: getResultTypes(), resultAttrs: getResAttrsAttr());
1241}
1242
1243//===----------------------------------------------------------------------===//
1244// InlineAsmOp
1245//===----------------------------------------------------------------------===//
1246
1247void InlineAsmOp::getEffects(
1248 SmallVectorImpl<SideEffects::EffectInstance<MemoryEffects::Effect>>
1249 &effects) {
1250 if (getHasSideEffects()) {
1251 effects.emplace_back(Args: MemoryEffects::Write::get());
1252 effects.emplace_back(Args: 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 message: "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 C&: operands, Pred: [](Value arg) { return isa<LLVMPointerType>(Val: arg.getType()); });
1281}
1282
1283#define GET_OP_CLASSES
1284#include "mlir/Dialect/LLVMIR/LLVMCallOps.cpp.inc"
1285