MLIR 22.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
26namespace {
27
28struct PoisonOpLowering : public ConvertOpToLLVMPattern<ub::PoisonOp> {
30
31 LogicalResult
32 matchAndRewrite(ub::PoisonOp op, OpAdaptor adaptor,
33 ConversionPatternRewriter &rewriter) const override;
34};
35
36} // namespace
37
38//===----------------------------------------------------------------------===//
39// PoisonOpLowering
40//===----------------------------------------------------------------------===//
41
42LogicalResult
43PoisonOpLowering::matchAndRewrite(ub::PoisonOp op, OpAdaptor adaptor,
44 ConversionPatternRewriter &rewriter) const {
45 if (!isa<ub::PoisonAttr>(op.getValue())) {
46 return rewriter.notifyMatchFailure(op, [&](Diagnostic &diag) {
47 diag << "pattern can only convert op with '"
48 << ub::PoisonAttr::getMnemonic() << "' poison value";
49 });
50 }
51
52 Type resType = getTypeConverter()->convertType(op.getType());
53 if (!resType) {
54 return rewriter.notifyMatchFailure(op, [&](Diagnostic &diag) {
55 diag << "failed to convert result type " << op.getType();
56 });
57 }
58
59 rewriter.replaceOpWithNewOp<LLVM::PoisonOp>(op, resType);
60 return success();
61}
62
63//===----------------------------------------------------------------------===//
64// Pass Definition
65//===----------------------------------------------------------------------===//
66
67namespace {
68struct UBToLLVMConversionPass
69 : public impl::UBToLLVMConversionPassBase<UBToLLVMConversionPass> {
70 using Base::Base;
71
72 void runOnOperation() override {
73 LLVMConversionTarget target(getContext());
74 RewritePatternSet patterns(&getContext());
75
76 LowerToLLVMOptions options(&getContext());
77 if (indexBitwidth != kDeriveIndexBitwidthFromDataLayout)
78 options.overrideIndexBitwidth(indexBitwidth);
79
80 LLVMTypeConverter converter(&getContext(), options);
82
83 if (failed(applyPartialConversion(getOperation(), target,
84 std::move(patterns))))
85 signalPassFailure();
86 }
87};
88} // namespace
89
90//===----------------------------------------------------------------------===//
91// Pattern Population
92//===----------------------------------------------------------------------===//
93
95 const LLVMTypeConverter &converter, RewritePatternSet &patterns) {
96 patterns.add<PoisonOpLowering>(converter);
97}
98
99//===----------------------------------------------------------------------===//
100// ConvertToLLVMPatternInterface implementation
101//===----------------------------------------------------------------------===//
102
103namespace {
104/// Implement the interface to convert UB to LLVM.
105struct UBToLLVMDialectInterface : public ConvertToLLVMPatternInterface {
107 void loadDependentDialects(MLIRContext *context) const final {
108 context->loadDialect<LLVM::LLVMDialect>();
109 }
110
111 /// Hook for derived dialect interface to provide conversion patterns
112 /// and mark dialect legal for the conversion target.
113 void populateConvertToLLVMConversionPatterns(
115 RewritePatternSet &patterns) const final {
117 }
118};
119} // namespace
120
122 registry.addExtension(+[](MLIRContext *ctx, ub::UBDialect *dialect) {
123 dialect->addInterfaces<UBToLLVMDialectInterface>();
124 });
125}
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:207
ConvertOpToLLVMPattern(const LLVMTypeConverter &typeConverter, PatternBenefit benefit=1)
Definition Pattern.h:213
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
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:561
void registerConvertUBToLLVMInterface(DialectRegistry &registry)
Definition UBToLLVM.cpp:121
void populateUBToLLVMConversionPatterns(const LLVMTypeConverter &converter, RewritePatternSet &patterns)
Definition UBToLLVM.cpp:94
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