MLIR  20.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 
16 #include "mlir/IR/TypeUtilities.h"
17 #include "mlir/Pass/Pass.h"
18 
19 namespace mlir {
20 #define GEN_PASS_DEF_UBTOLLVMCONVERSIONPASS
21 #include "mlir/Conversion/Passes.h.inc"
22 } // namespace mlir
23 
24 using namespace mlir;
25 
26 namespace {
27 
28 struct 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 
42 LogicalResult
43 PoisonOpLowering::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 
67 namespace {
68 struct UBToLLVMConversionPass
69  : public impl::UBToLLVMConversionPassBase<UBToLLVMConversionPass> {
70  using Base::Base;
71 
72  void runOnOperation() override {
75 
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 
103 namespace {
104 /// Implement the interface to convert UB to LLVM.
105 struct 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(
114  ConversionTarget &target, LLVMTypeConverter &typeConverter,
115  RewritePatternSet &patterns) const final {
117  }
118 };
119 } // namespace
120 
122  registry.addExtension(+[](MLIRContext *ctx, ub::UBDialect *dialect) {
123  dialect->addInterfaces<UBToLLVMDialectInterface>();
124  });
125 }
static MLIRContext * getContext(OpFoldResult val)
static std::string diag(const llvm::Value &value)
static llvm::ManagedStatic< PassManagerOptions > options
This class implements a pattern rewriter for use with ConversionPatterns.
This class describes a specific conversion target.
Utility class for operation conversions targeting the LLVM dialect that match exactly one source oper...
Definition: Pattern.h:143
ConvertOpToLLVMPattern(const LLVMTypeConverter &typeConverter, PatternBenefit benefit=1)
Definition: Pattern.h:149
Base class for dialect interfaces providing translation to LLVM IR.
ConvertToLLVMPatternInterface(Dialect *dialect)
This class contains all of the information necessary to report a diagnostic to the DiagnosticEngine.
Definition: Diagnostics.h:155
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.
Definition: TypeConverter.h:35
Options to control the LLVM lowering.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
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,...
Definition: PatternMatch.h:724
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
Definition: PatternMatch.h:542
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
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
LogicalResult applyPartialConversion(ArrayRef< Operation * > ops, const ConversionTarget &target, const FrozenRewritePatternSet &patterns, ConversionConfig config=ConversionConfig())
Below we define several entry points for operation conversion.