Files
clang-p2996/mlir/lib/Query/Matcher/RegistryManager.h
Devajith 02d9f4d1f1 [mlir][mlir-query] Introduce mlir-query tool with autocomplete support
This commit adds the initial version of the mlir-query tool, which leverages the pre-existing matchers defined in mlir/include/mlir/IR/Matchers.h

The tool provides the following set of basic queries:

hasOpAttrName(string)
hasOpName(string)
isConstantOp()
isNegInfFloat()
isNegZeroFloat()
isNonZero()
isOne()
isOneFloat()
isPosInfFloat()
isPosZeroFloat()
isZero()
isZeroFloat()

Reviewed By: jpienaar

Differential Revision: https://reviews.llvm.org/D155127
2023-10-13 14:03:27 -07:00

71 lines
2.4 KiB
C++

//===--- RegistryManager.h - Matcher registry -------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// RegistryManager to manage registry of all known matchers.
//
// The registry provides a generic interface to construct any matcher by name.
//
//===----------------------------------------------------------------------===//
#ifndef MLIR_TOOLS_MLIRQUERY_MATCHER_REGISTRYMANAGER_H
#define MLIR_TOOLS_MLIRQUERY_MATCHER_REGISTRYMANAGER_H
#include "Diagnostics.h"
#include "mlir/Query/Matcher/Marshallers.h"
#include "mlir/Query/Matcher/Registry.h"
#include "mlir/Query/Matcher/VariantValue.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include <string>
namespace mlir::query::matcher {
using MatcherCtor = const internal::MatcherDescriptor *;
struct MatcherCompletion {
MatcherCompletion() = default;
MatcherCompletion(llvm::StringRef typedText, llvm::StringRef matcherDecl)
: typedText(typedText.str()), matcherDecl(matcherDecl.str()) {}
bool operator==(const MatcherCompletion &other) const {
return typedText == other.typedText && matcherDecl == other.matcherDecl;
}
// The text to type to select this matcher.
std::string typedText;
// The "declaration" of the matcher, with type information.
std::string matcherDecl;
};
class RegistryManager {
public:
RegistryManager() = delete;
static std::optional<MatcherCtor>
lookupMatcherCtor(llvm::StringRef matcherName,
const Registry &matcherRegistry);
static std::vector<ArgKind> getAcceptedCompletionTypes(
llvm::ArrayRef<std::pair<MatcherCtor, unsigned>> context);
static std::vector<MatcherCompletion>
getMatcherCompletions(ArrayRef<ArgKind> acceptedTypes,
const Registry &matcherRegistry);
static VariantMatcher constructMatcher(MatcherCtor ctor,
internal::SourceRange nameRange,
ArrayRef<ParserValue> args,
internal::Diagnostics *error);
};
} // namespace mlir::query::matcher
#endif // MLIR_TOOLS_MLIRQUERY_MATCHER_REGISTRYMANAGER_H