1//===- ControlFlowToLLVM.cpp - ControlFlow to LLVM dialect conversion -----===//
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 implements a pass to convert MLIR standard and builtin dialects
10// into the LLVM IR dialect.
11//
12//===----------------------------------------------------------------------===//
13
14#include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h"
15
16#include "mlir/Analysis/DataLayoutAnalysis.h"
17#include "mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h"
18#include "mlir/Conversion/LLVMCommon/ConversionTarget.h"
19#include "mlir/Conversion/LLVMCommon/Pattern.h"
20#include "mlir/Conversion/LLVMCommon/PrintCallHelper.h"
21#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
22#include "mlir/Dialect/LLVMIR/FunctionCallUtils.h"
23#include "mlir/Dialect/LLVMIR/LLVMCallOps.h"
24#include "mlir/Dialect/LLVMIR/LLVMModuleOps.h"
25#include "mlir/Dialect/LLVMIR/LLVMTerminatorOps.h"
26#include "mlir/IR/BuiltinOps.h"
27#include "mlir/IR/PatternMatch.h"
28#include "mlir/Pass/Pass.h"
29#include "mlir/Transforms/DialectConversion.h"
30
31namespace mlir {
32#define GEN_PASS_DEF_CONVERTCONTROLFLOWTOLLVMPASS
33#include "mlir/Conversion/Passes.h.inc"
34} // namespace mlir
35
36using namespace mlir;
37
38#define PASS_NAME "convert-cf-to-llvm"
39
40namespace {
41/// Lower `cf.assert`. The default lowering calls the `abort` function if the
42/// assertion is violated and has no effect otherwise. The failure message is
43/// ignored by the default lowering but should be propagated by any custom
44/// lowering.
45struct AssertOpLowering : public ConvertOpToLLVMPattern<cf::AssertOp> {
46 explicit AssertOpLowering(const LLVMTypeConverter &typeConverter,
47 bool abortOnFailedAssert = true,
48 SymbolTableCollection *symbolTables = nullptr)
49 : ConvertOpToLLVMPattern<cf::AssertOp>(typeConverter, /*benefit=*/1),
50 abortOnFailedAssert(abortOnFailedAssert), symbolTables(symbolTables) {}
51
52 LogicalResult
53 matchAndRewrite(cf::AssertOp op, OpAdaptor adaptor,
54 ConversionPatternRewriter &rewriter) const override {
55 auto loc = op.getLoc();
56 auto module = op->getParentOfType<ModuleOp>();
57
58 // Split block at `assert` operation.
59 Block *opBlock = rewriter.getInsertionBlock();
60 auto opPosition = rewriter.getInsertionPoint();
61 Block *continuationBlock = rewriter.splitBlock(block: opBlock, before: opPosition);
62
63 // Failed block: Generate IR to print the message and call `abort`.
64 Block *failureBlock = rewriter.createBlock(parent: opBlock->getParent());
65 auto createResult = LLVM::createPrintStrCall(
66 builder&: rewriter, loc, moduleOp: module, symbolName: "assert_msg", string: op.getMsg(), typeConverter: *getTypeConverter(),
67 /*addNewLine=*/addNewline: false,
68 /*runtimeFunctionName=*/"puts", symbolTables);
69 if (createResult.failed())
70 return failure();
71
72 if (abortOnFailedAssert) {
73 // Insert the `abort` declaration if necessary.
74 auto abortFunc = module.lookupSymbol<LLVM::LLVMFuncOp>(name: "abort");
75 if (!abortFunc) {
76 OpBuilder::InsertionGuard guard(rewriter);
77 rewriter.setInsertionPointToStart(module.getBody());
78 auto abortFuncTy = LLVM::LLVMFunctionType::get(result: getVoidType(), arguments: {});
79 abortFunc = LLVM::LLVMFuncOp::create(builder&: rewriter, location: rewriter.getUnknownLoc(),
80 name: "abort", type: abortFuncTy);
81 }
82 LLVM::CallOp::create(builder&: rewriter, location: loc, func: abortFunc, args: ValueRange());
83 LLVM::UnreachableOp::create(builder&: rewriter, location: loc);
84 } else {
85 LLVM::BrOp::create(builder&: rewriter, location: loc, operands: ValueRange(), dest: continuationBlock);
86 }
87
88 // Generate assertion test.
89 rewriter.setInsertionPointToEnd(opBlock);
90 rewriter.replaceOpWithNewOp<LLVM::CondBrOp>(
91 op, args: adaptor.getArg(), args&: continuationBlock, args&: failureBlock);
92
93 return success();
94 }
95
96private:
97 /// If set to `false`, messages are printed but program execution continues.
98 /// This is useful for testing asserts.
99 bool abortOnFailedAssert = true;
100
101 SymbolTableCollection *symbolTables = nullptr;
102};
103
104/// Helper function for converting branch ops. This function converts the
105/// signature of the given block. If the new block signature is different from
106/// `expectedTypes`, returns "failure".
107static FailureOr<Block *> getConvertedBlock(ConversionPatternRewriter &rewriter,
108 const TypeConverter *converter,
109 Operation *branchOp, Block *block,
110 TypeRange expectedTypes) {
111 assert(converter && "expected non-null type converter");
112 assert(!block->isEntryBlock() && "entry blocks have no predecessors");
113
114 // There is nothing to do if the types already match.
115 if (block->getArgumentTypes() == expectedTypes)
116 return block;
117
118 // Compute the new block argument types and convert the block.
119 std::optional<TypeConverter::SignatureConversion> conversion =
120 converter->convertBlockSignature(block);
121 if (!conversion)
122 return rewriter.notifyMatchFailure(arg&: branchOp,
123 msg: "could not compute block signature");
124 if (expectedTypes != conversion->getConvertedTypes())
125 return rewriter.notifyMatchFailure(
126 arg&: branchOp,
127 msg: "mismatch between adaptor operand types and computed block signature");
128 return rewriter.applySignatureConversion(block, conversion&: *conversion, converter);
129}
130
131/// Flatten the given value ranges into a single vector of values.
132static SmallVector<Value> flattenValues(ArrayRef<ValueRange> values) {
133 SmallVector<Value> result;
134 for (const ValueRange &vals : values)
135 llvm::append_range(C&: result, R: vals);
136 return result;
137}
138
139/// Set attributes on an operation using its inherent/discardable split.
140static void setConvertedAttrs(Operation *op, DictionaryAttr attrs) {
141 SmallVector<NamedAttribute> discardableAttrs;
142 for (NamedAttribute attr : attrs) {
143 if (op->getInherentAttr(name: attr.getName()).has_value())
144 op->setInherentAttr(name: attr.getName(), value: attr.getValue());
145 else
146 discardableAttrs.push_back(Elt: attr);
147 }
148 op->setDiscardableAttrs(discardableAttrs);
149}
150
151/// Convert the destination block signature (if necessary) and lower the branch
152/// op to llvm.br.
153struct BranchOpLowering : public ConvertOpToLLVMPattern<cf::BranchOp> {
154 using ConvertOpToLLVMPattern<cf::BranchOp>::ConvertOpToLLVMPattern;
155 using Adaptor = ConvertOpToLLVMPattern<cf::BranchOp>::OneToNOpAdaptor;
156
157 LogicalResult
158 matchAndRewrite(cf::BranchOp op, Adaptor adaptor,
159 ConversionPatternRewriter &rewriter) const override {
160 SmallVector<Value> flattenedAdaptor = flattenValues(values: adaptor.getOperands());
161 FailureOr<Block *> convertedBlock =
162 getConvertedBlock(rewriter, converter: getTypeConverter(), branchOp: op, block: op.getSuccessor(),
163 expectedTypes: TypeRange(ValueRange(flattenedAdaptor)));
164 if (failed(Result: convertedBlock))
165 return failure();
166 DictionaryAttr attrs = op->getDiscardableAttrDictionary();
167 auto loopAnnotation = op->getAttrOfType<LLVM::LoopAnnotationAttr>(
168 name: LLVM::getLoopAnnotationAttrName());
169 Operation *newOp = rewriter.replaceOpWithNewOp<LLVM::BrOp>(
170 op, args&: flattenedAdaptor, args&: loopAnnotation, args&: *convertedBlock);
171 // TODO: We should not just forward all attributes like that. But there are
172 // existing Flang tests that depend on this behavior.
173 setConvertedAttrs(op: newOp, attrs);
174 return success();
175 }
176};
177
178/// Convert the destination block signatures (if necessary) and lower the
179/// branch op to llvm.cond_br.
180struct CondBranchOpLowering : public ConvertOpToLLVMPattern<cf::CondBranchOp> {
181 using ConvertOpToLLVMPattern<cf::CondBranchOp>::ConvertOpToLLVMPattern;
182 using Adaptor = ConvertOpToLLVMPattern<cf::CondBranchOp>::OneToNOpAdaptor;
183
184 LogicalResult
185 matchAndRewrite(cf::CondBranchOp op, Adaptor adaptor,
186 ConversionPatternRewriter &rewriter) const override {
187 SmallVector<Value> flattenedAdaptorTrue =
188 flattenValues(values: adaptor.getTrueDestOperands());
189 SmallVector<Value> flattenedAdaptorFalse =
190 flattenValues(values: adaptor.getFalseDestOperands());
191 if (!llvm::hasSingleElement(C: adaptor.getCondition()))
192 return rewriter.notifyMatchFailure(arg&: op,
193 msg: "expected single element condition");
194 FailureOr<Block *> convertedTrueBlock =
195 getConvertedBlock(rewriter, converter: getTypeConverter(), branchOp: op, block: op.getTrueDest(),
196 expectedTypes: TypeRange(ValueRange(flattenedAdaptorTrue)));
197 if (failed(Result: convertedTrueBlock))
198 return failure();
199 FailureOr<Block *> convertedFalseBlock =
200 getConvertedBlock(rewriter, converter: getTypeConverter(), branchOp: op, block: op.getFalseDest(),
201 expectedTypes: TypeRange(ValueRange(flattenedAdaptorFalse)));
202 if (failed(Result: convertedFalseBlock))
203 return failure();
204 DictionaryAttr attrs = op->getDiscardableAttrDictionary();
205 auto loopAnnotation = op->getAttrOfType<LLVM::LoopAnnotationAttr>(
206 name: LLVM::getLoopAnnotationAttrName());
207 auto newOp = rewriter.replaceOpWithNewOp<LLVM::CondBrOp>(
208 op, args: llvm::getSingleElement(C: adaptor.getCondition()),
209 args&: flattenedAdaptorTrue, args&: flattenedAdaptorFalse, args: op.getBranchWeightsAttr(),
210 args&: loopAnnotation, args&: *convertedTrueBlock, args&: *convertedFalseBlock);
211 // TODO: We should not just forward all attributes like that. But there are
212 // existing Flang tests that depend on this behavior.
213 setConvertedAttrs(op: newOp, attrs);
214 return success();
215 }
216};
217
218/// Convert the destination block signatures (if necessary) and lower the
219/// switch op to llvm.switch.
220struct SwitchOpLowering : public ConvertOpToLLVMPattern<cf::SwitchOp> {
221 using ConvertOpToLLVMPattern<cf::SwitchOp>::ConvertOpToLLVMPattern;
222
223 LogicalResult
224 matchAndRewrite(cf::SwitchOp op, cf::SwitchOp::Adaptor adaptor,
225 ConversionPatternRewriter &rewriter) const override {
226 // Get or convert default block.
227 FailureOr<Block *> convertedDefaultBlock = getConvertedBlock(
228 rewriter, converter: getTypeConverter(), branchOp: op, block: op.getDefaultDestination(),
229 expectedTypes: TypeRange(adaptor.getDefaultOperands()));
230 if (failed(Result: convertedDefaultBlock))
231 return failure();
232
233 // Get or convert all case blocks.
234 SmallVector<Block *> caseDestinations;
235 SmallVector<ValueRange> caseOperands = adaptor.getCaseOperands();
236 for (auto it : llvm::enumerate(First: op.getCaseDestinations())) {
237 Block *b = it.value();
238 FailureOr<Block *> convertedBlock =
239 getConvertedBlock(rewriter, converter: getTypeConverter(), branchOp: op, block: b,
240 expectedTypes: TypeRange(caseOperands[it.index()]));
241 if (failed(Result: convertedBlock))
242 return failure();
243 caseDestinations.push_back(Elt: *convertedBlock);
244 }
245
246 rewriter.replaceOpWithNewOp<LLVM::SwitchOp>(
247 op, args: adaptor.getFlag(), args&: *convertedDefaultBlock,
248 args: adaptor.getDefaultOperands(), args: adaptor.getCaseValuesAttr(),
249 args&: caseDestinations, args&: caseOperands);
250 return success();
251 }
252};
253
254} // namespace
255
256void mlir::cf::populateControlFlowToLLVMConversionPatterns(
257 const LLVMTypeConverter &converter, RewritePatternSet &patterns) {
258 // clang-format off
259 patterns.add<
260 BranchOpLowering,
261 CondBranchOpLowering,
262 SwitchOpLowering>(arg: converter);
263 // clang-format on
264}
265
266void mlir::cf::populateAssertToLLVMConversionPattern(
267 const LLVMTypeConverter &converter, RewritePatternSet &patterns,
268 bool abortOnFailure, SymbolTableCollection *symbolTables) {
269 patterns.add<AssertOpLowering>(arg: converter, args&: abortOnFailure, args&: symbolTables);
270}
271
272//===----------------------------------------------------------------------===//
273// Pass Definition
274//===----------------------------------------------------------------------===//
275
276namespace {
277/// A pass converting MLIR operations into the LLVM IR dialect.
278struct ConvertControlFlowToLLVM
279 : public impl::ConvertControlFlowToLLVMPassBase<ConvertControlFlowToLLVM> {
280
281 using Base::Base;
282
283 /// Run the dialect converter on the module.
284 void runOnOperation() override {
285 MLIRContext *ctx = &getContext();
286 LLVMConversionTarget target(*ctx);
287 // This pass lowers only CF dialect ops, but it also modifies block
288 // signatures inside other ops. These ops should be treated as legal. They
289 // are lowered by other passes.
290 target.markUnknownOpDynamicallyLegal(fn: [&](Operation *op) {
291 return op->getDialect() !=
292 ctx->getLoadedDialect<cf::ControlFlowDialect>();
293 });
294
295 const auto &dataLayoutAnalysis = getAnalysis<DataLayoutAnalysis>();
296 LowerToLLVMOptions options(ctx,
297 dataLayoutAnalysis.getAtOrAbove(operation: getOperation()));
298 if (indexBitwidth != kDeriveIndexBitwidthFromDataLayout)
299 options.overrideIndexBitwidth(bitwidth: indexBitwidth);
300
301 LLVMTypeConverter converter(ctx, options, &dataLayoutAnalysis);
302 RewritePatternSet patterns(ctx);
303 mlir::cf::populateControlFlowToLLVMConversionPatterns(converter, patterns);
304 mlir::cf::populateAssertToLLVMConversionPattern(converter, patterns);
305
306 if (failed(Result: applyPartialConversion(op: getOperation(), target,
307 patterns: std::move(patterns))))
308 signalPassFailure();
309 }
310};
311} // namespace
312
313//===----------------------------------------------------------------------===//
314// ConvertToLLVMPatternInterface implementation
315//===----------------------------------------------------------------------===//
316
317namespace {
318/// Implement the interface to convert MemRef to LLVM.
319struct ControlFlowToLLVMDialectInterface
320 : public ConvertToLLVMPatternInterface {
321 ControlFlowToLLVMDialectInterface(Dialect *dialect)
322 : ConvertToLLVMPatternInterface(dialect) {}
323
324 void loadDependentDialects(MLIRContext *context) const final {
325 context->loadDialect<LLVM::LLVMDialect>();
326 }
327
328 /// Hook for derived dialect interface to provide conversion patterns
329 /// and mark dialect legal for the conversion target.
330 void populateConvertToLLVMConversionPatterns(
331 ConversionTarget &target, LLVMTypeConverter &typeConverter,
332 RewritePatternSet &patterns) const final {
333 mlir::cf::populateControlFlowToLLVMConversionPatterns(converter: typeConverter,
334 patterns);
335 mlir::cf::populateAssertToLLVMConversionPattern(converter: typeConverter, patterns);
336 }
337};
338} // namespace
339
340void mlir::cf::registerConvertControlFlowToLLVMInterface(
341 DialectRegistry &registry) {
342 registry.addExtension(extensionFn: +[](MLIRContext *ctx, cf::ControlFlowDialect *dialect) {
343 dialect->addInterfaces<ControlFlowToLLVMDialectInterface>();
344 });
345}
346