|
| 1 | +//===- SymbolInterfaces.td - Interfaces for symbol ops -----*- tablegen -*-===// |
| 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 | +// |
| 9 | +// This file contains a set of interfaces and traits that can be used to define |
| 10 | +// properties of symbol and symbol table operations. |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +#ifndef MLIR_IR_SYMBOLINTERFACES |
| 15 | +#define MLIR_IR_SYMBOLINTERFACES |
| 16 | + |
| 17 | +include "mlir/IR/OpBase.td" |
| 18 | + |
| 19 | +//===----------------------------------------------------------------------===// |
| 20 | +// SymbolOpInterface |
| 21 | +//===----------------------------------------------------------------------===// |
| 22 | + |
| 23 | +def Symbol : OpInterface<"SymbolOpInterface"> { |
| 24 | + let description = [{ |
| 25 | + This interface describes an operation that may define a `Symbol`. A `Symbol` |
| 26 | + operation resides immediately within a region that defines a `SymbolTable`. |
| 27 | + See [Symbols and SymbolTables](SymbolsAndSymbolTables.md) for more details |
| 28 | + and constraints on `Symbol` operations. |
| 29 | + }]; |
| 30 | + |
| 31 | + let methods = [ |
| 32 | + InterfaceMethod<"Returns the name of this symbol.", |
| 33 | + "StringRef", "getName", (ins), [{ |
| 34 | + // Don't rely on the trait implementation as optional symbol operations |
| 35 | + // may override this. |
| 36 | + return mlir::SymbolTable::getSymbolName(op); |
| 37 | + }], /*defaultImplementation=*/[{ |
| 38 | + return mlir::SymbolTable::getSymbolName(this->getOperation()); |
| 39 | + }] |
| 40 | + >, |
| 41 | + InterfaceMethod<"Sets the name of this symbol.", |
| 42 | + "void", "setName", (ins "StringRef":$name), [{}], |
| 43 | + /*defaultImplementation=*/[{ |
| 44 | + this->getOperation()->setAttr( |
| 45 | + mlir::SymbolTable::getSymbolAttrName(), |
| 46 | + StringAttr::get(name, this->getOperation()->getContext())); |
| 47 | + }] |
| 48 | + >, |
| 49 | + InterfaceMethod<"Gets the visibility of this symbol.", |
| 50 | + "mlir::SymbolTable::Visibility", "getVisibility", (ins), [{}], |
| 51 | + /*defaultImplementation=*/[{ |
| 52 | + return mlir::SymbolTable::getSymbolVisibility(this->getOperation()); |
| 53 | + }] |
| 54 | + >, |
| 55 | + InterfaceMethod<"Sets the visibility of this symbol.", |
| 56 | + "void", "setVisibility", (ins "mlir::SymbolTable::Visibility":$vis), [{}], |
| 57 | + /*defaultImplementation=*/[{ |
| 58 | + mlir::SymbolTable::setSymbolVisibility(this->getOperation(), vis); |
| 59 | + }] |
| 60 | + >, |
| 61 | + InterfaceMethod<[{ |
| 62 | + Get all of the uses of the current symbol that are nested within the |
| 63 | + given operation 'from'. |
| 64 | + Note: See mlir::SymbolTable::getSymbolUses for more details. |
| 65 | + }], |
| 66 | + "Optional<::mlir::SymbolTable::UseRange>", "getSymbolUses", |
| 67 | + (ins "Operation *":$from), [{}], |
| 68 | + /*defaultImplementation=*/[{ |
| 69 | + return ::mlir::SymbolTable::getSymbolUses(this->getOperation(), from); |
| 70 | + }] |
| 71 | + >, |
| 72 | + InterfaceMethod<[{ |
| 73 | + Return if the current symbol is known to have no uses that are nested |
| 74 | + within the given operation 'from'. |
| 75 | + Note: See mlir::SymbolTable::symbolKnownUseEmpty for more details. |
| 76 | + }], |
| 77 | + "bool", "symbolKnownUseEmpty", (ins "Operation *":$from), [{}], |
| 78 | + /*defaultImplementation=*/[{ |
| 79 | + return ::mlir::SymbolTable::symbolKnownUseEmpty(this->getOperation(), |
| 80 | + from); |
| 81 | + }] |
| 82 | + >, |
| 83 | + InterfaceMethod<[{ |
| 84 | + Attempt to replace all uses of the current symbol with the provided |
| 85 | + symbol 'newSymbol' that are nested within the given operation 'from'. |
| 86 | + Note: See mlir::SymbolTable::replaceAllSymbolUses for more details. |
| 87 | + }], |
| 88 | + "LogicalResult", "replaceAllSymbolUses", (ins "StringRef":$newSymbol, |
| 89 | + "Operation *":$from), [{}], |
| 90 | + /*defaultImplementation=*/[{ |
| 91 | + return ::mlir::SymbolTable::replaceAllSymbolUses(this->getOperation(), |
| 92 | + newSymbol, from); |
| 93 | + }] |
| 94 | + >, |
| 95 | + InterfaceMethod<[{ |
| 96 | + Returns true if this operation optionally defines a symbol based on the |
| 97 | + presence of the symbol name. |
| 98 | + }], |
| 99 | + "bool", "isOptionalSymbol", (ins), [{}], |
| 100 | + /*defaultImplementation=*/[{ return false; }] |
| 101 | + >, |
| 102 | + InterfaceMethod<[{ |
| 103 | + Returns true if this operation can be discarded if it has no remaining |
| 104 | + symbol uses. |
| 105 | + }], |
| 106 | + "bool", "canDiscardOnUseEmpty", (ins), [{}], |
| 107 | + /*defaultImplementation=*/[{ |
| 108 | + // By default, base this on the visibility alone. A symbol can be |
| 109 | + // discarded as long as it is not public. Only public symbols may be |
| 110 | + // visible from outside of the IR. |
| 111 | + return getVisibility() != ::mlir::SymbolTable::Visibility::Public; |
| 112 | + }] |
| 113 | + >, |
| 114 | + ]; |
| 115 | + |
| 116 | + let verify = [{ |
| 117 | + // If this is an optional symbol, bail out early if possible. |
| 118 | + auto concreteOp = cast<ConcreteOp>($_op); |
| 119 | + if (concreteOp.isOptionalSymbol()) { |
| 120 | + if(!concreteOp.getAttr(::mlir::SymbolTable::getSymbolAttrName())) |
| 121 | + return success(); |
| 122 | + } |
| 123 | + return ::mlir::detail::verifySymbol($_op); |
| 124 | + }]; |
| 125 | + |
| 126 | + let extraClassDeclaration = [{ |
| 127 | + using Visibility = mlir::SymbolTable::Visibility; |
| 128 | + |
| 129 | + /// Custom classof that handles the case where the symbol is optional. |
| 130 | + static bool classof(Operation *op) { |
| 131 | + return Base::classof(op) |
| 132 | + && op->getAttr(::mlir::SymbolTable::getSymbolAttrName()); |
| 133 | + } |
| 134 | + |
| 135 | + /// Returns true if this symbol has nested visibility. |
| 136 | + bool isNested() { return getVisibility() == Visibility::Nested; } |
| 137 | + /// Returns true if this symbol has private visibility. |
| 138 | + bool isPrivate() { return getVisibility() == Visibility::Private; } |
| 139 | + /// Returns true if this symbol has public visibility. |
| 140 | + bool isPublic() { return getVisibility() == Visibility::Public; } |
| 141 | + }]; |
| 142 | + |
| 143 | + let extraTraitClassDeclaration = [{ |
| 144 | + using Visibility = mlir::SymbolTable::Visibility; |
| 145 | + }]; |
| 146 | +} |
| 147 | + |
| 148 | +//===----------------------------------------------------------------------===// |
| 149 | +// Symbol Traits |
| 150 | +//===----------------------------------------------------------------------===// |
| 151 | + |
| 152 | +// Op defines a symbol table. |
| 153 | +def SymbolTable : NativeOpTrait<"SymbolTable">; |
| 154 | + |
| 155 | +#endif // MLIR_IR_SYMBOLINTERFACES |
0 commit comments