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;
75 clonedOps.emplace_back(builder.clone(*slicedOp, mapper));
76 clonedVals.insert(clonedVals.end(), clonedOp->
result_begin(),
80 func::ReturnOp::create(builder, loc, clonedVals);
83 size_t currentIndex = 0;
84 while (currentIndex < funcOp.getNumArguments()) {
86 if (funcOp.getArgument(currentIndex).use_empty())
87 if (succeeded(funcOp.eraseArgument(currentIndex)))
99 return mlir::failure();
103 return mlir::success();
107 os <<
"Available commands:\n\n"
108 " match MATCHER, m MATCHER "
109 "Match the mlir against the given matcher.\n"
111 "Terminates the query session.\n\n";
112 return mlir::success();
117 return mlir::success();
130 std::vector<Operation *> flattenedMatches =
135 return mlir::failure();
136 os <<
"\n" << *
function <<
"\n\n";
138 return mlir::success();
142 for (
auto &results : matches) {
143 os <<
"Match #" << ++matchCount <<
":\n\n";
144 for (
Operation *op : results.matchedOps) {
145 if (op == results.rootOp) {
152 os << matchCount << (matchCount == 1 ?
" match.\n\n" :
" matches.\n\n");
153 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.
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
Finds and collects matches from 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)
LogicalResult verify(Operation *op, bool verifyRecursively=true)
Perform (potentially expensive) checks of invariants, used to detect compiler bugs,...
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