MLIR 24.0.0git
UBOps.cpp
Go to the documentation of this file.
1//===- UBOps.cpp - UB Dialect Operations ----------------------------------===//
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
12
13#include "mlir/IR/Builders.h"
14
15#include "mlir/Dialect/UB/IR/UBOpsDialect.cpp.inc"
16
17using namespace mlir;
18using namespace mlir::ub;
19
20namespace {
21/// This class defines the interface for handling inlining with UB
22/// operations.
23struct UBInlinerInterface : public DialectInlinerInterface {
24 using DialectInlinerInterface::DialectInlinerInterface;
25
26 /// All UB ops can be inlined.
27 bool isLegalToInline(Operation *, Region *, bool, IRMapping &) const final {
28 return true;
29 }
30
31 /// `ub.unreachable` does not return to the caller, so it stays in place when
32 /// the inlined region has more than one block.
33 void handleTerminator(Operation *op, Block *newDest) const final {
34 assert(isa<UnreachableOp>(op) && "unexpected UB terminator");
35 }
36
37 /// The single-block fast path erases the terminator and continues the
38 /// inlined block with the operations after the call, which is wrong for
39 /// `ub.unreachable`. This hook is only asked when the inlined block ends in a
40 /// UB terminator, and `ub.unreachable` is the only one.
41 bool allowSingleBlockOptimization(Block *) const final { return false; }
42};
43} // namespace
44
45//===----------------------------------------------------------------------===//
46// UBDialect
47//===----------------------------------------------------------------------===//
48
49void UBDialect::initialize() {
50 addInterfaces<UBInlinerInterface>();
51 declarePromisedInterface<ConvertToLLVMPatternInterface, UBDialect>();
52}
53
54Operation *UBDialect::materializeConstant(OpBuilder &builder, Attribute value,
55 Type type, Location loc) {
56 if (auto attr = dyn_cast<PoisonAttr>(value))
57 return PoisonOp::create(builder, loc, type, attr);
58
59 return nullptr;
60}
61
62OpFoldResult PoisonOp::fold(FoldAdaptor /*adaptor*/) { return getValue(); }
63
64#include "mlir/Dialect/UB/IR/UBOpsInterfaces.cpp.inc"
65
66#define GET_ATTRDEF_CLASSES
67#include "mlir/Dialect/UB/IR/UBOpsAttributes.cpp.inc"
68
69#define GET_OP_CLASSES
70#include "mlir/Dialect/UB/IR/UBOps.cpp.inc"
static bool isLegalToInline(InlinerInterface &interface, Region *src, Region *insertRegion, bool shouldCloneInlinedRegion, IRMapping &valueMapping)
Utility to check that all of the operations within 'src' can be inlined.
Attributes are known-constant values of operations.
Definition Attributes.h:25
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
This class helps build Operations.
Definition Builders.h:210
This class represents a single result from folding an operation.
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
Include the generated interface declarations.