Changeset View
Changeset View
Standalone View
Standalone View
mlir/lib/TableGen/Interfaces.cpp
- This file was moved from mlir/lib/TableGen/OpInterfaces.cpp.
//===- OpInterfaces.cpp - OpInterfaces class ------------------------------===// | //===- Interfaces.cpp - Interface classes ---------------------------------===// | ||||
// | // | ||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||
// See https://llvm.org/LICENSE.txt for license information. | // See https://llvm.org/LICENSE.txt for license information. | ||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||
// | // | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
// | |||||
// OpInterfaces wrapper to simplify using TableGen OpInterfaces. | |||||
// | |||||
//===----------------------------------------------------------------------===// | |||||
#include "mlir/TableGen/OpInterfaces.h" | #include "mlir/TableGen/Interfaces.h" | ||||
#include "llvm/ADT/StringExtras.h" | #include "llvm/ADT/StringExtras.h" | ||||
#include "llvm/Support/FormatVariadic.h" | #include "llvm/Support/FormatVariadic.h" | ||||
#include "llvm/TableGen/Error.h" | #include "llvm/TableGen/Error.h" | ||||
#include "llvm/TableGen/Record.h" | #include "llvm/TableGen/Record.h" | ||||
using namespace mlir; | using namespace mlir; | ||||
using namespace mlir::tblgen; | using namespace mlir::tblgen; | ||||
OpInterfaceMethod::OpInterfaceMethod(const llvm::Record *def) : def(def) { | //===----------------------------------------------------------------------===// | ||||
// InterfaceMethod | |||||
//===----------------------------------------------------------------------===// | |||||
InterfaceMethod::InterfaceMethod(const llvm::Record *def) : def(def) { | |||||
llvm::DagInit *args = def->getValueAsDag("arguments"); | llvm::DagInit *args = def->getValueAsDag("arguments"); | ||||
for (unsigned i = 0, e = args->getNumArgs(); i != e; ++i) { | for (unsigned i = 0, e = args->getNumArgs(); i != e; ++i) { | ||||
arguments.push_back( | arguments.push_back( | ||||
{llvm::cast<llvm::StringInit>(args->getArg(i))->getValue(), | {llvm::cast<llvm::StringInit>(args->getArg(i))->getValue(), | ||||
args->getArgNameStr(i)}); | args->getArgNameStr(i)}); | ||||
} | } | ||||
} | } | ||||
StringRef OpInterfaceMethod::getReturnType() const { | StringRef InterfaceMethod::getReturnType() const { | ||||
return def->getValueAsString("returnType"); | return def->getValueAsString("returnType"); | ||||
} | } | ||||
// Return the name of this method. | // Return the name of this method. | ||||
StringRef OpInterfaceMethod::getName() const { | StringRef InterfaceMethod::getName() const { | ||||
return def->getValueAsString("name"); | return def->getValueAsString("name"); | ||||
} | } | ||||
// Return if this method is static. | // Return if this method is static. | ||||
bool OpInterfaceMethod::isStatic() const { | bool InterfaceMethod::isStatic() const { | ||||
return def->isSubClassOf("StaticInterfaceMethod"); | return def->isSubClassOf("StaticInterfaceMethod"); | ||||
} | } | ||||
// Return the body for this method if it has one. | // Return the body for this method if it has one. | ||||
llvm::Optional<StringRef> OpInterfaceMethod::getBody() const { | llvm::Optional<StringRef> InterfaceMethod::getBody() const { | ||||
auto value = def->getValueAsString("body"); | auto value = def->getValueAsString("body"); | ||||
return value.empty() ? llvm::Optional<StringRef>() : value; | return value.empty() ? llvm::Optional<StringRef>() : value; | ||||
} | } | ||||
// Return the default implementation for this method if it has one. | // Return the default implementation for this method if it has one. | ||||
llvm::Optional<StringRef> OpInterfaceMethod::getDefaultImplementation() const { | llvm::Optional<StringRef> InterfaceMethod::getDefaultImplementation() const { | ||||
auto value = def->getValueAsString("defaultBody"); | auto value = def->getValueAsString("defaultBody"); | ||||
return value.empty() ? llvm::Optional<StringRef>() : value; | return value.empty() ? llvm::Optional<StringRef>() : value; | ||||
} | } | ||||
// Return the description of this method if it has one. | // Return the description of this method if it has one. | ||||
llvm::Optional<StringRef> OpInterfaceMethod::getDescription() const { | llvm::Optional<StringRef> InterfaceMethod::getDescription() const { | ||||
auto value = def->getValueAsString("description"); | auto value = def->getValueAsString("description"); | ||||
return value.empty() ? llvm::Optional<StringRef>() : value; | return value.empty() ? llvm::Optional<StringRef>() : value; | ||||
} | } | ||||
ArrayRef<OpInterfaceMethod::Argument> OpInterfaceMethod::getArguments() const { | ArrayRef<InterfaceMethod::Argument> InterfaceMethod::getArguments() const { | ||||
return arguments; | return arguments; | ||||
} | } | ||||
bool OpInterfaceMethod::arg_empty() const { return arguments.empty(); } | bool InterfaceMethod::arg_empty() const { return arguments.empty(); } | ||||
//===----------------------------------------------------------------------===// | |||||
// Interface | |||||
//===----------------------------------------------------------------------===// | |||||
Interface::Interface(const llvm::Record *def) : def(def) { | |||||
assert(def->isSubClassOf("Interface") && | |||||
"must be subclass of TableGen 'Interface' class"); | |||||
OpInterface::OpInterface(const llvm::Record *def) : def(def) { | |||||
auto *listInit = dyn_cast<llvm::ListInit>(def->getValueInit("methods")); | auto *listInit = dyn_cast<llvm::ListInit>(def->getValueInit("methods")); | ||||
for (llvm::Init *init : listInit->getValues()) | for (llvm::Init *init : listInit->getValues()) | ||||
methods.emplace_back(cast<llvm::DefInit>(init)->getDef()); | methods.emplace_back(cast<llvm::DefInit>(init)->getDef()); | ||||
} | } | ||||
// Return the name of this interface. | // Return the name of this interface. | ||||
StringRef OpInterface::getName() const { | StringRef Interface::getName() const { | ||||
return def->getValueAsString("cppClassName"); | return def->getValueAsString("cppClassName"); | ||||
} | } | ||||
// Return the methods of this interface. | // Return the methods of this interface. | ||||
ArrayRef<OpInterfaceMethod> OpInterface::getMethods() const { return methods; } | ArrayRef<InterfaceMethod> Interface::getMethods() const { return methods; } | ||||
// Return the description of this method if it has one. | // Return the description of this method if it has one. | ||||
llvm::Optional<StringRef> OpInterface::getDescription() const { | llvm::Optional<StringRef> Interface::getDescription() const { | ||||
auto value = def->getValueAsString("description"); | auto value = def->getValueAsString("description"); | ||||
return value.empty() ? llvm::Optional<StringRef>() : value; | return value.empty() ? llvm::Optional<StringRef>() : value; | ||||
} | } | ||||
// Return the interfaces extra class declaration code. | // Return the interfaces extra class declaration code. | ||||
llvm::Optional<StringRef> OpInterface::getExtraClassDeclaration() const { | llvm::Optional<StringRef> Interface::getExtraClassDeclaration() const { | ||||
auto value = def->getValueAsString("extraClassDeclaration"); | auto value = def->getValueAsString("extraClassDeclaration"); | ||||
return value.empty() ? llvm::Optional<StringRef>() : value; | return value.empty() ? llvm::Optional<StringRef>() : value; | ||||
} | } | ||||
// Return the traits extra class declaration code. | // Return the traits extra class declaration code. | ||||
llvm::Optional<StringRef> OpInterface::getExtraTraitClassDeclaration() const { | llvm::Optional<StringRef> Interface::getExtraTraitClassDeclaration() const { | ||||
auto value = def->getValueAsString("extraTraitClassDeclaration"); | auto value = def->getValueAsString("extraTraitClassDeclaration"); | ||||
return value.empty() ? llvm::Optional<StringRef>() : value; | return value.empty() ? llvm::Optional<StringRef>() : value; | ||||
} | } | ||||
// Return the body for this method if it has one. | // Return the body for this method if it has one. | ||||
llvm::Optional<StringRef> OpInterface::getVerify() const { | llvm::Optional<StringRef> Interface::getVerify() const { | ||||
// Only OpInterface supports the verify method. | |||||
if (!isa<OpInterface>(this)) | |||||
return llvm::None; | |||||
auto value = def->getValueAsString("verify"); | auto value = def->getValueAsString("verify"); | ||||
return value.empty() ? llvm::Optional<StringRef>() : value; | return value.empty() ? llvm::Optional<StringRef>() : value; | ||||
} | } | ||||
//===----------------------------------------------------------------------===// | |||||
// AttrInterface | |||||
//===----------------------------------------------------------------------===// | |||||
bool AttrInterface::classof(const Interface *interface) { | |||||
return interface->getDef().isSubClassOf("AttrInterface"); | |||||
} | |||||
//===----------------------------------------------------------------------===// | |||||
// OpInterface | |||||
//===----------------------------------------------------------------------===// | |||||
bool OpInterface::classof(const Interface *interface) { | |||||
return interface->getDef().isSubClassOf("OpInterface"); | |||||
} | |||||
//===----------------------------------------------------------------------===// | |||||
// TypeInterface | |||||
//===----------------------------------------------------------------------===// | |||||
bool TypeInterface::classof(const Interface *interface) { | |||||
return interface->getDef().isSubClassOf("TypeInterface"); | |||||
} |