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