1//===- LLVMTerminatorOps.cpp - LLVM dialect terminator 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 `Terminator` of the LLVM dialect.
10//
11//===----------------------------------------------------------------------===//
12
13#include "mlir/Dialect/LLVMIR/LLVMTerminatorOps.h"
14
15#include "IR/LLVMOps.h"
16#include "mlir/Dialect/LLVMIR/LLVMCallOps.h"
17#include "mlir/Dialect/LLVMIR/LLVMConstantOps.h"
18#include "mlir/Dialect/LLVMIR/LLVMModuleOps.h"
19#include "mlir/IR/Builders.h"
20#include "mlir/IR/DialectImplementation.h"
21#include "mlir/IR/PatternMatch.h"
22#include "llvm/ADT/STLExtras.h"
23
24#include <optional>
25
26using namespace mlir;
27using namespace mlir::LLVM;
28
29//===----------------------------------------------------------------------===//
30// LLVM::BrOp
31//===----------------------------------------------------------------------===//
32
33LogicalResult BrOp::canonicalize(BrOp op, PatternRewriter &rewriter) {
34 // Merge a branch to a block that has a single predecessor, the same
35 // transform as cf::BranchOp; the successor block is inlined into the
36 // branch block and the branch is erased.
37 Block *succ = op.getDest();
38 Block *opParent = op->getBlock();
39 if (succ == opParent || !llvm::hasSingleElement(C: succ->getPredecessors()))
40 return failure();
41
42 // If any branch operand is itself a block argument of the successor,
43 // merging would replace uses of that argument with itself and leave
44 // dangling uses after the successor block is erased.
45 for (Value operand : op.getDestOperands())
46 if (auto arg = dyn_cast<BlockArgument>(Val&: operand))
47 if (arg.getOwner() == succ)
48 return failure();
49
50 // The successor must be mergeable. A landing pad must stay the first
51 // operation of its block, a block tag means the block address is taken,
52 // and merging would silently drop a loop annotation on the branch.
53 if (!succ->empty()) {
54 if (isa<LandingpadOp>(Val: succ->front()))
55 return failure();
56 if (llvm::any_of(Range: succ->without_terminator(),
57 P: [](Operation &inner) { return isa<BlockTagOp>(Val: inner); }))
58 return failure();
59 }
60 if (op.getLoopAnnotationAttr())
61 return failure();
62
63 SmallVector<Value> brOperands(op.getDestOperands());
64 rewriter.eraseOp(op);
65 rewriter.mergeBlocks(source: succ, dest: opParent, argValues: brOperands);
66 return success();
67}
68
69SuccessorOperands BrOp::getSuccessorOperands(unsigned index) {
70 assert(index == 0 && "invalid successor index");
71 return SuccessorOperands(getDestOperandsMutable());
72}
73
74//===----------------------------------------------------------------------===//
75// LLVM::CondBrOp
76//===----------------------------------------------------------------------===//
77
78SuccessorOperands CondBrOp::getSuccessorOperands(unsigned index) {
79 assert(index < getNumSuccessors() && "invalid successor index");
80 return SuccessorOperands(index == 0 ? getTrueDestOperandsMutable()
81 : getFalseDestOperandsMutable());
82}
83
84void CondBrOp::build(OpBuilder &builder, OperationState &result,
85 Value condition, Block *trueDest, ValueRange trueOperands,
86 Block *falseDest, ValueRange falseOperands,
87 std::optional<std::pair<uint32_t, uint32_t>> weights) {
88 DenseI32ArrayAttr weightsAttr;
89 if (weights)
90 weightsAttr =
91 builder.getDenseI32ArrayAttr(values: {static_cast<int32_t>(weights->first),
92 static_cast<int32_t>(weights->second)});
93
94 build(odsBuilder&: builder, odsState&: result, condition, trueDestOperands: trueOperands, falseDestOperands: falseOperands, branch_weights: weightsAttr,
95 /*loop_annotation=*/{}, trueDest, falseDest);
96}
97
98//===----------------------------------------------------------------------===//
99// LLVM::SwitchOp
100//===----------------------------------------------------------------------===//
101
102void SwitchOp::build(OpBuilder &builder, OperationState &result, Value value,
103 Block *defaultDestination, ValueRange defaultOperands,
104 DenseIntElementsAttr caseValues,
105 BlockRange caseDestinations,
106 ArrayRef<ValueRange> caseOperands,
107 ArrayRef<int32_t> branchWeights) {
108 DenseI32ArrayAttr weightsAttr;
109 if (!branchWeights.empty())
110 weightsAttr = builder.getDenseI32ArrayAttr(values: branchWeights);
111
112 build(odsBuilder&: builder, odsState&: result, value, defaultOperands, caseOperands, case_values: caseValues,
113 branch_weights: weightsAttr, defaultDestination, caseDestinations);
114}
115
116void SwitchOp::build(OpBuilder &builder, OperationState &result, Value value,
117 Block *defaultDestination, ValueRange defaultOperands,
118 ArrayRef<APInt> caseValues, BlockRange caseDestinations,
119 ArrayRef<ValueRange> caseOperands,
120 ArrayRef<int32_t> branchWeights) {
121 DenseIntElementsAttr caseValuesAttr;
122 if (!caseValues.empty()) {
123 ShapedType caseValueType = VectorType::get(
124 shape: static_cast<int64_t>(caseValues.size()), elementType: value.getType());
125 caseValuesAttr = DenseIntElementsAttr::get(type: caseValueType, arg&: caseValues);
126 }
127
128 build(builder, result, value, defaultDestination, defaultOperands,
129 caseValues: caseValuesAttr, caseDestinations, caseOperands, branchWeights);
130}
131
132void SwitchOp::build(OpBuilder &builder, OperationState &result, Value value,
133 Block *defaultDestination, ValueRange defaultOperands,
134 ArrayRef<int32_t> caseValues, BlockRange caseDestinations,
135 ArrayRef<ValueRange> caseOperands,
136 ArrayRef<int32_t> branchWeights) {
137 DenseIntElementsAttr caseValuesAttr;
138 if (!caseValues.empty()) {
139 ShapedType caseValueType = VectorType::get(
140 shape: static_cast<int64_t>(caseValues.size()), elementType: value.getType());
141 caseValuesAttr = DenseIntElementsAttr::get(type: caseValueType, arg&: caseValues);
142 }
143
144 build(builder, result, value, defaultDestination, defaultOperands,
145 caseValues: caseValuesAttr, caseDestinations, caseOperands, branchWeights);
146}
147
148/// <cases> ::= `[` (case (`,` case )* )? `]`
149/// <case> ::= integer `:` bb-id (`(` ssa-use-and-type-list `)`)?
150ParseResult mlir::LLVM::parseSwitchOpCases(
151 OpAsmParser &parser, Type flagType, DenseIntElementsAttr &caseValues,
152 SmallVectorImpl<Block *> &caseDestinations,
153 SmallVectorImpl<SmallVector<OpAsmParser::UnresolvedOperand>> &caseOperands,
154 SmallVectorImpl<SmallVector<Type>> &caseOperandTypes) {
155 if (failed(Result: parser.parseLSquare()))
156 return failure();
157 if (succeeded(Result: parser.parseOptionalRSquare()))
158 return success();
159 SmallVector<APInt> values;
160 unsigned bitWidth = flagType.getIntOrFloatBitWidth();
161 auto parseCase = [&]() {
162 int64_t value = 0;
163 if (failed(Result: parser.parseInteger(result&: value)))
164 return failure();
165 values.push_back(Elt: APInt(bitWidth, value, /*isSigned=*/true));
166
167 Block *destination;
168 SmallVector<OpAsmParser::UnresolvedOperand> operands;
169 SmallVector<Type> operandTypes;
170 if (parser.parseColon() || parser.parseSuccessor(dest&: destination))
171 return failure();
172 if (!parser.parseOptionalLParen()) {
173 if (parser.parseOperandList(result&: operands, delimiter: OpAsmParser::Delimiter::None) ||
174 parser.parseColonTypeList(result&: operandTypes) || parser.parseRParen())
175 return failure();
176 }
177 caseDestinations.push_back(Elt: destination);
178 caseOperands.emplace_back(Args&: operands);
179 caseOperandTypes.emplace_back(Args&: operandTypes);
180 return success();
181 };
182 if (failed(Result: parser.parseCommaSeparatedList(parseElementFn: parseCase)))
183 return failure();
184
185 ShapedType caseValueType =
186 VectorType::get(shape: static_cast<int64_t>(values.size()), elementType: flagType);
187 caseValues = DenseIntElementsAttr::get(type: caseValueType, arg&: values);
188 return parser.parseRSquare();
189}
190
191void mlir::LLVM::printSwitchOpCases(OpAsmPrinter &p, SwitchOp op, Type flagType,
192 DenseIntElementsAttr caseValues,
193 SuccessorRange caseDestinations,
194 OperandRangeRange caseOperands,
195 const TypeRangeRange &caseOperandTypes) {
196 p << '[';
197 p.printNewline();
198 if (!caseValues) {
199 p << ']';
200 return;
201 }
202
203 size_t index = 0;
204 llvm::interleave(
205 c: llvm::zip(t&: caseValues, u&: caseDestinations),
206 each_fn: [&](auto i) {
207 p << " ";
208 p << std::get<0>(i);
209 p << ": ";
210 p.printSuccessorAndUseList(successor: std::get<1>(i), succOperands: caseOperands[index++]);
211 },
212 between_fn: [&] {
213 p << ',';
214 p.printNewline();
215 });
216 p.printNewline();
217 p << ']';
218}
219
220LogicalResult SwitchOp::verify() {
221 if ((!getCaseValues() && !getCaseDestinations().empty()) ||
222 (getCaseValues() &&
223 getCaseValues()->size() !=
224 static_cast<int64_t>(getCaseDestinations().size())))
225 return emitOpError(message: "expects number of case values to match number of "
226 "case destinations");
227 if (getCaseValues() &&
228 getValue().getType() != getCaseValues()->getElementType())
229 return emitError(message: "expects case value type to match condition value type");
230 return success();
231}
232
233SuccessorOperands SwitchOp::getSuccessorOperands(unsigned index) {
234 assert(index < getNumSuccessors() && "invalid successor index");
235 return SuccessorOperands(index == 0 ? getDefaultOperandsMutable()
236 : getCaseOperandsMutable(index: index - 1));
237}
238
239//===----------------------------------------------------------------------===//
240// ReturnOp
241//===----------------------------------------------------------------------===//
242
243LogicalResult ReturnOp::verify() {
244 auto parent = (*this)->getParentOfType<LLVMFuncOp>();
245 if (!parent)
246 return success();
247
248 Type expectedType = parent.getFunctionType().getReturnType();
249 if (llvm::isa<LLVMVoidType>(Val: expectedType)) {
250 if (!getArg())
251 return success();
252 InFlightDiagnostic diag = emitOpError(message: "expected no operands");
253 diag.attachNote(noteLoc: parent->getLoc()) << "when returning from function";
254 return diag;
255 }
256 if (!getArg()) {
257 if (llvm::isa<LLVMVoidType>(Val: expectedType))
258 return success();
259 InFlightDiagnostic diag = emitOpError(message: "expected 1 operand");
260 diag.attachNote(noteLoc: parent->getLoc()) << "when returning from function";
261 return diag;
262 }
263 if (expectedType != getArg().getType()) {
264 InFlightDiagnostic diag = emitOpError(message: "mismatching result types");
265 diag.attachNote(noteLoc: parent->getLoc()) << "when returning from function";
266 return diag;
267 }
268 return success();
269}
270
271//===----------------------------------------------------------------------===//
272// LLVM::IndirectBrOp
273//===----------------------------------------------------------------------===//
274
275SuccessorOperands IndirectBrOp::getSuccessorOperands(unsigned index) {
276 assert(index < getNumSuccessors() && "invalid successor index");
277 return SuccessorOperands(getSuccOperandsMutable()[index]);
278}
279
280void IndirectBrOp::build(OpBuilder &odsBuilder, OperationState &odsState,
281 Value addr, ArrayRef<ValueRange> succOperands,
282 BlockRange successors) {
283 odsState.addOperands(newOperands: addr);
284 for (ValueRange range : succOperands)
285 odsState.addOperands(newOperands: range);
286 SmallVector<int32_t> rangeSegments;
287 for (ValueRange range : succOperands)
288 rangeSegments.push_back(Elt: range.size());
289 odsState.getOrAddProperties<Properties>().indbr_operand_segments =
290 odsBuilder.getDenseI32ArrayAttr(values: rangeSegments);
291 odsState.addSuccessors(newSuccessors: successors);
292}
293
294ParseResult mlir::LLVM::parseIndirectBrOpSucessors(
295 OpAsmParser &parser, Type &flagType,
296 SmallVectorImpl<Block *> &succOperandBlocks,
297 SmallVectorImpl<SmallVector<OpAsmParser::UnresolvedOperand>> &succOperands,
298 SmallVectorImpl<SmallVector<Type>> &succOperandsTypes) {
299 if (failed(Result: parser.parseCommaSeparatedList(
300 delimiter: OpAsmParser::Delimiter::Square,
301 parseElementFn: [&]() {
302 Block *destination = nullptr;
303 SmallVector<OpAsmParser::UnresolvedOperand> operands;
304 SmallVector<Type> operandTypes;
305
306 if (parser.parseSuccessor(dest&: destination).failed())
307 return failure();
308
309 if (succeeded(Result: parser.parseOptionalLParen())) {
310 if (failed(Result: parser.parseOperandList(
311 result&: operands, delimiter: OpAsmParser::Delimiter::None)) ||
312 failed(Result: parser.parseColonTypeList(result&: operandTypes)) ||
313 failed(Result: parser.parseRParen()))
314 return failure();
315 }
316 succOperandBlocks.push_back(Elt: destination);
317 succOperands.emplace_back(Args&: operands);
318 succOperandsTypes.emplace_back(Args&: operandTypes);
319 return success();
320 },
321 contextMessage: "successor blocks")))
322 return failure();
323 return success();
324}
325
326void mlir::LLVM::printIndirectBrOpSucessors(
327 OpAsmPrinter &p, IndirectBrOp op, Type flagType, SuccessorRange succs,
328 OperandRangeRange succOperands, const TypeRangeRange &succOperandsTypes) {
329 p << "[";
330 llvm::interleave(
331 c: llvm::zip(t&: succs, u&: succOperands),
332 each_fn: [&](auto i) {
333 p.printNewline();
334 p.printSuccessorAndUseList(successor: std::get<0>(i), succOperands: std::get<1>(i));
335 },
336 between_fn: [&] { p << ','; });
337 if (!succOperands.empty())
338 p.printNewline();
339 p << "]";
340}
341
342#define GET_OP_CLASSES
343#include "mlir/Dialect/LLVMIR/LLVMTerminatorOps.cpp.inc"
344