MLIR 24.0.0git
UBToLLVM.cpp
Go to the documentation of this file.
1//===- UBToLLVM.cpp - UB 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
10
18#include "mlir/Pass/Pass.h"
19
20namespace mlir {
21#define GEN_PASS_DEF_UBTOLLVMCONVERSIONPASS
22#include "mlir/Conversion/Passes.h.inc"
23} // namespace mlir
24
25using namespace mlir;
26
27//===----------------------------------------------------------------------===//
28// PoisonOpLowering
29//===----------------------------------------------------------------------===//
30
31namespace {
32struct PoisonOpLowering : public ConvertOpToLLVMPattern<ub::PoisonOp> {
34
35 LogicalResult
36 matchAndRewrite(ub::PoisonOp op, OpAdaptor adaptor,
37 ConversionPatternRewriter &rewriter) const override;
38};
39} // namespace
40
41LogicalResult
42PoisonOpLowering::matchAndRewrite(ub::PoisonOp op, OpAdaptor adaptor,
43 ConversionPatternRewriter &rewriter) const {
44 if (!isa<ub::PoisonAttr>(op.getValue())) {
45 return rewriter.notifyMatchFailure(op, [&](Diagnostic &diag) {
46 diag << "pattern can only convert op with '"
47 << ub::PoisonAttr::getMnemonic() << "' poison value";
48 });
49 }
50
51 Type resType = getTypeConverter()->convertType(op.getType());
52 if (!resType) {
53 return rewriter.notifyMatchFailure(op, [&](Diagnostic &diag) {
54 diag << "failed to convert result type " << op.getType();
55 });
56 }
57
58 rewriter.replaceOpWithNewOp<LLVM::PoisonOp>(op, resType);
59 return success();
60}
61
62//===----------------------------------------------------------------------===//
63// UnreachableOpLowering
64//===----------------------------------------------------------------------===//
65
66namespace {
67struct UnreachableOpLowering
68 : public ConvertOpToLLVMPattern<ub::UnreachableOp> {
70
71 LogicalResult
72 matchAndRewrite(ub::UnreachableOp op, OpAdaptor adaptor,
73 ConversionPatternRewriter &rewriter) const override;
74};
75} // namespace
76LogicalResult
77
78UnreachableOpLowering::matchAndRewrite(
79 ub::UnreachableOp op, OpAdaptor adaptor,
80 ConversionPatternRewriter &rewriter) const {
81 rewriter.replaceOpWithNewOp<LLVM::UnreachableOp>(op);
82 return success();
83}
84
85//===----------------------------------------------------------------------===//
86// Pass Definition
87//===----------------------------------------------------------------------===//
88
89namespace {
90struct UBToLLVMConversionPass
91 : public impl::UBToLLVMConversionPassBase<UBToLLVMConversionPass> {
92 using Base::Base;
93
94 void runOnOperation() override {
95 LLVMConversionTarget target(getContext());
96 RewritePatternSet patterns(&getContext());
97
98 LowerToLLVMOptions options(&getContext());
99 if (indexBitwidth != kDeriveIndexBitwidthFromDataLayout)
100 options.overrideIndexBitwidth(indexBitwidth);
101
102 LLVMTypeConverter converter(&getContext(), options);
104
105 if (failed(applyPartialConversion(getOperation(), target,
106 std::move(patterns))))
107 signalPassFailure();
108 }
109};
110} // namespace
111
112//===----------------------------------------------------------------------===//
113// Pattern Population
114//===----------------------------------------------------------------------===//
115
117 const LLVMTypeConverter &converter, RewritePatternSet &patterns) {
118 patterns.add<PoisonOpLowering, UnreachableOpLowering>(converter);
119}
120
121//===----------------------------------------------------------------------===//
122// ConvertToLLVMPatternInterface implementation
123//===----------------------------------------------------------------------===//
124
125namespace {
126/// Implement the interface to convert UB to LLVM.
127struct UBToLLVMDialectInterface : public ConvertToLLVMPatternInterface {
128 UBToLLVMDialectInterface(Dialect *dialect)
129 : ConvertToLLVMPatternInterface(dialect) {}
130
131 void loadDependentDialects(MLIRContext *context) const final {
132 context->loadDialect<LLVM::LLVMDialect>();
133 }
134
135 /// Hook for derived dialect interface to provide conversion patterns
136 /// and mark dialect legal for the conversion target.
137 void populateConvertToLLVMConversionPatterns(
138 ConversionTarget &target, LLVMTypeConverter &typeConverter,
139 RewritePatternSet &patterns) const final {
140 ub::populateUBToLLVMConversionPatterns(typeConverter, patterns);
141 }
142};
143} // namespace
144
146 registry.addExtension(+[](MLIRContext *ctx, ub::UBDialect *dialect) {
147 dialect->addInterfaces<UBToLLVMDialectInterface>();
148 });
149}
return success()
b getContext())
static llvm::ManagedStatic< PassManagerOptions > options
Utility class for operation conversions targeting the LLVM dialect that match exactly one source oper...
Definition Pattern.h:233
ConvertOpToLLVMPattern(const LLVMTypeConverter &typeConverter, PatternBenefit benefit=1)
Definition Pattern.h:239
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.
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
Definition Dialect.h:38
Conversion from types to the LLVM IR dialect.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:71
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:886
void registerConvertUBToLLVMInterface(DialectRegistry &registry)
Definition UBToLLVM.cpp:145
void populateUBToLLVMConversionPatterns(const LLVMTypeConverter &converter, RewritePatternSet &patterns)
Definition UBToLLVM.cpp:116
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...