15 #include "llvm/ADT/SetVector.h"
16 #include "llvm/Support/SourceMgr.h"
17 #include "llvm/Support/raw_ostream.h"
25 std::vector<llvm::LineEditor::Completion>
34 llvm::StringRef functionName) {
39 std::vector<Operation *> slice;
40 std::vector<Value> values;
41 std::vector<Type> outputTypes;
43 for (
auto *op : ops) {
45 if (!isa<func::ReturnOp>(op))
49 llvm::append_range(outputTypes, op->getResults().getTypes());
52 llvm::append_range(values, op->getOperands());
56 FunctionType funcType =
58 auto loc = builder.getUnknownLoc();
59 func::FuncOp funcOp = func::FuncOp::create(loc, functionName, funcType);
61 builder.setInsertionPointToEnd(funcOp.addEntryBlock());
66 mapper.
map(arg.value(), funcOp.getArgument(arg.index()));
69 std::vector<Operation *> clonedOps;
70 std::vector<Value> clonedVals;
73 clonedOps.emplace_back(builder.clone(*slicedOp, mapper));
74 clonedVals.insert(clonedVals.end(), clonedOp->
result_begin(),
78 builder.
create<func::ReturnOp>(loc, clonedVals);
81 size_t currentIndex = 0;
82 while (currentIndex < funcOp.getNumArguments()) {
84 if (funcOp.getArgument(currentIndex).use_empty())
85 if (succeeded(funcOp.eraseArgument(currentIndex)))
97 return mlir::failure();
101 return mlir::success();
105 os <<
"Available commands:\n\n"
106 " match MATCHER, m MATCHER "
107 "Match the mlir against the given matcher.\n"
109 "Terminates the query session.\n\n";
110 return mlir::success();
115 return mlir::success();
128 std::vector<Operation *> flattenedMatches =
132 os <<
"\n" << *
function <<
"\n\n";
134 return mlir::success();
138 for (
auto &results : matches) {
139 os <<
"Match #" << ++matchCount <<
":\n\n";
140 for (
auto op : results.matchedOps) {
141 if (op == results.rootOp) {
148 os << matchCount << (matchCount == 1 ?
" match.\n\n" :
" matches.\n\n");
149 return mlir::success();
This is a utility class for mapping one set of IR entities to another.
void map(Value from, Value to)
Inserts a new mapping for 'from' to 'to'.
MLIRContext is the top-level object for a collection of MLIR operations.
void loadDialect()
Load a dialect in the context.
This class helps build Operations.
Operation is the basic unit of execution within MLIR.
result_iterator result_begin()
MLIRContext * getContext()
Return the context this operation is associated with.
static Operation * create(Location location, OperationName name, TypeRange resultTypes, ValueRange operands, NamedAttrList &&attributes, OpaqueProperties properties, BlockRange successors, unsigned numRegions)
Create a new Operation with the specific fields.
result_iterator result_end()
This class provides an abstraction over the various different ranges of value types.
This class provides an abstraction over the different types of ranges over Values.
static QueryRef parse(llvm::StringRef line, const QuerySession &qs)
static std::vector< llvm::LineEditor::Completion > complete(llvm::StringRef line, size_t pos, const QuerySession &qs)
StringRef getFunctionName() const
bool hasFunctionName() const
A class that provides utilities to find operations in the IR.
void printMatch(llvm::raw_ostream &os, QuerySession &qs, Operation *op) const
Prints the matched operation.
std::vector< MatchResult > collectMatches(Operation *root, DynMatcher matcher) const
Traverses the IR and returns a vector of MatchResult for each match of the matcher.
std::vector< Operation * > flattenMatchedOps(std::vector< MatchResult > &matches) const
Flattens a vector of MatchResult into a vector of operations.
constexpr void enumerate(std::tuple< Tys... > &tuple, CallbackT &&callback)
QueryRef parse(llvm::StringRef line, const QuerySession &qs)
static Operation * extractFunction(std::vector< Operation * > &ops, MLIRContext *context, llvm::StringRef functionName)
llvm::IntrusiveRefCntPtr< Query > QueryRef
std::vector< llvm::LineEditor::Completion > complete(llvm::StringRef line, size_t pos, const QuerySession &qs)
llvm::LogicalResult run(llvm::raw_ostream &os, QuerySession &qs) const override
llvm::LogicalResult run(llvm::raw_ostream &os, QuerySession &qs) const override
llvm::LogicalResult run(llvm::raw_ostream &os, QuerySession &qs) const override
const matcher::DynMatcher matcher
llvm::LogicalResult run(llvm::raw_ostream &os, QuerySession &qs) const override
llvm::LogicalResult run(llvm::raw_ostream &os, QuerySession &qs) const override