28 #include "llvm/ADT/StringRef.h"
32 #define GEN_PASS_DEF_CONVERTCONTROLFLOWTOLLVMPASS
33 #include "mlir/Conversion/Passes.h.inc"
38 #define PASS_NAME "convert-cf-to-llvm"
47 bool abortOnFailedAssert =
true)
49 abortOnFailedAssert(abortOnFailedAssert) {}
52 matchAndRewrite(cf::AssertOp op, OpAdaptor adaptor,
54 auto loc = op.getLoc();
55 auto module = op->getParentOfType<ModuleOp>();
65 rewriter, loc, module,
"assert_msg", op.getMsg(), *getTypeConverter(),
68 if (createResult.failed())
71 if (abortOnFailedAssert) {
73 auto abortFunc = module.lookupSymbol<LLVM::LLVMFuncOp>(
"abort");
79 "abort", abortFuncTy);
81 rewriter.
create<LLVM::CallOp>(loc, abortFunc, std::nullopt);
82 rewriter.
create<LLVM::UnreachableOp>(loc);
90 op, adaptor.getArg(), continuationBlock, failureBlock);
98 bool abortOnFailedAssert =
true;
108 assert(converter &&
"expected non-null type converter");
109 assert(!block->
isEntryBlock() &&
"entry blocks have no predecessors");
116 std::optional<TypeConverter::SignatureConversion> conversion =
120 "could not compute block signature");
121 if (expectedTypes != conversion->getConvertedTypes())
124 "mismatch between adaptor operand types and computed block signature");
134 matchAndRewrite(cf::BranchOp op,
typename cf::BranchOp::Adaptor adaptor,
136 FailureOr<Block *> convertedBlock =
137 getConvertedBlock(rewriter, getTypeConverter(), op, op.getSuccessor(),
139 if (failed(convertedBlock))
142 op, adaptor.getOperands(), *convertedBlock);
145 newOp->
setAttrs(op->getAttrDictionary());
156 matchAndRewrite(cf::CondBranchOp op,
157 typename cf::CondBranchOp::Adaptor adaptor,
159 FailureOr<Block *> convertedTrueBlock =
160 getConvertedBlock(rewriter, getTypeConverter(), op, op.getTrueDest(),
161 TypeRange(adaptor.getTrueDestOperands()));
162 if (failed(convertedTrueBlock))
164 FailureOr<Block *> convertedFalseBlock =
165 getConvertedBlock(rewriter, getTypeConverter(), op, op.getFalseDest(),
166 TypeRange(adaptor.getFalseDestOperands()));
167 if (failed(convertedFalseBlock))
170 op, adaptor.getCondition(), *convertedTrueBlock,
171 adaptor.getTrueDestOperands(), *convertedFalseBlock,
172 adaptor.getFalseDestOperands());
175 newOp->
setAttrs(op->getAttrDictionary());
186 matchAndRewrite(cf::SwitchOp op,
typename cf::SwitchOp::Adaptor adaptor,
189 FailureOr<Block *> convertedDefaultBlock = getConvertedBlock(
190 rewriter, getTypeConverter(), op, op.getDefaultDestination(),
191 TypeRange(adaptor.getDefaultOperands()));
192 if (failed(convertedDefaultBlock))
199 Block *b = it.value();
200 FailureOr<Block *> convertedBlock =
201 getConvertedBlock(rewriter, getTypeConverter(), op, b,
203 if (failed(convertedBlock))
205 caseDestinations.push_back(*convertedBlock);
209 op, adaptor.getFlag(), *convertedDefaultBlock,
210 adaptor.getDefaultOperands(), adaptor.getCaseValuesAttr(),
211 caseDestinations, caseOperands);
223 CondBranchOpLowering,
224 SwitchOpLowering>(converter);
230 bool abortOnFailure) {
240 struct ConvertControlFlowToLLVM
241 :
public impl::ConvertControlFlowToLLVMPassBase<ConvertControlFlowToLLVM> {
246 void runOnOperation()
override {
252 target.markUnknownOpDynamicallyLegal([&](
Operation *op) {
259 options.overrideIndexBitwidth(indexBitwidth);
279 struct ControlFlowToLLVMDialectInterface
282 void loadDependentDialects(
MLIRContext *context)
const final {
283 context->loadDialect<LLVM::LLVMDialect>();
288 void populateConvertToLLVMConversionPatterns(
301 dialect->addInterfaces<ControlFlowToLLVMDialectInterface>();
static MLIRContext * getContext(OpFoldResult val)
static llvm::ManagedStatic< PassManagerOptions > options
Block represents an ordered list of Operations.
ValueTypeRange< BlockArgListType > getArgumentTypes()
Return a range containing the types of the arguments for this block.
bool isEntryBlock()
Return if this block is the entry block in the parent region.
This class implements a pattern rewriter for use with ConversionPatterns.
Block * applySignatureConversion(Block *block, TypeConverter::SignatureConversion &conversion, const TypeConverter *converter=nullptr)
Apply a signature conversion to given block.
This class describes a specific conversion target.
Utility class for operation conversions targeting the LLVM dialect that match exactly one source oper...
Base class for dialect interfaces providing translation to LLVM IR.
ConvertToLLVMPatternInterface(Dialect *dialect)
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
bool addExtension(TypeID extensionID, std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
Derived class that automatically populates legalization information for different LLVM ops.
Conversion from types to the LLVM IR dialect.
Options to control the LLVM lowering.
MLIRContext is the top-level object for a collection of MLIR operations.
Dialect * getLoadedDialect(StringRef name)
Get a registered IR dialect with the given namespace.
RAII guard to reset the insertion point of the builder when destroyed.
Block::iterator getInsertionPoint() const
Returns the current insertion point of the builder.
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
void setInsertionPointToEnd(Block *block)
Sets the insertion point to the end of the specified block.
Block * createBlock(Region *parent, Region::iterator insertPt={}, TypeRange argTypes=std::nullopt, ArrayRef< Location > locs=std::nullopt)
Add new block with 'argTypes' arguments and set the insertion point to the end of it.
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Block * getInsertionBlock() const
Return the block the current insertion point belongs to.
Operation is the basic unit of execution within MLIR.
Dialect * getDialect()
Return the dialect this operation is associated with, or nullptr if the associated dialect is not loa...
void setAttrs(DictionaryAttr newAttrs)
Set the attributes from a dictionary on this operation.
std::enable_if_t<!std::is_convertible< CallbackT, Twine >::value, LogicalResult > notifyMatchFailure(Location loc, CallbackT &&reasonCallback)
Used to notify the listener that the IR failed to be rewritten because of a match failure,...
Block * splitBlock(Block *block, Block::iterator before)
Split the operations starting at "before" (inclusive) out of the given block into a new block,...
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
std::optional< SignatureConversion > convertBlockSignature(Block *block) const
This function converts the type signature of the given block, by invoking 'convertSignatureArg' for e...
This class provides an abstraction over the various different ranges of value types.
This class provides an abstraction over the different types of ranges over Values.
LogicalResult createPrintStrCall(OpBuilder &builder, Location loc, ModuleOp moduleOp, StringRef symbolName, StringRef string, const LLVMTypeConverter &typeConverter, bool addNewline=true, std::optional< StringRef > runtimeFunctionName={})
Generate IR that prints the given string to stdout.
void populateAssertToLLVMConversionPattern(const LLVMTypeConverter &converter, RewritePatternSet &patterns, bool abortOnFailure=true)
Populate the cf.assert to LLVM conversion pattern.
void registerConvertControlFlowToLLVMInterface(DialectRegistry ®istry)
void populateControlFlowToLLVMConversionPatterns(const LLVMTypeConverter &converter, RewritePatternSet &patterns)
Collect the patterns to convert from the ControlFlow dialect to LLVM.
constexpr void enumerate(std::tuple< Tys... > &tuple, CallbackT &&callback)
Include the generated interface declarations.
static constexpr unsigned kDeriveIndexBitwidthFromDataLayout
Value to pass as bitwidth for the index type when the converter is expected to derive the bitwidth fr...
const FrozenRewritePatternSet & patterns
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
LogicalResult applyPartialConversion(ArrayRef< Operation * > ops, const ConversionTarget &target, const FrozenRewritePatternSet &patterns, ConversionConfig config=ConversionConfig())
Below we define several entry points for operation conversion.