16 #include "llvm/ADT/SetVector.h"
17 #include "llvm/Support/SourceMgr.h"
18 #include "llvm/Support/raw_ostream.h"
26 std::vector<llvm::LineEditor::Completion>
35 llvm::StringRef functionName) {
40 std::vector<Operation *> slice;
41 std::vector<Value> values;
42 std::vector<Type> outputTypes;
44 for (
auto *op : ops) {
46 if (!isa<func::ReturnOp>(op))
50 llvm::append_range(outputTypes, op->getResults().getTypes());
53 llvm::append_range(values, op->getOperands());
57 FunctionType funcType =
59 auto loc = builder.getUnknownLoc();
60 func::FuncOp funcOp = func::FuncOp::create(loc, functionName, funcType);
62 builder.setInsertionPointToEnd(funcOp.addEntryBlock());
67 mapper.
map(arg.value(), funcOp.getArgument(arg.index()));
70 std::vector<Operation *> clonedOps;
71 std::vector<Value> clonedVals;
76 clonedOps.emplace_back(builder.clone(*slicedOp, mapper));
77 clonedVals.insert(clonedVals.end(), clonedOp->
result_begin(),
81 builder.
create<func::ReturnOp>(loc, clonedVals);
84 size_t currentIndex = 0;
85 while (currentIndex < funcOp.getNumArguments()) {
87 if (funcOp.getArgument(currentIndex).use_empty())
88 if (succeeded(funcOp.eraseArgument(currentIndex)))
100 return mlir::failure();
104 return mlir::success();
108 os <<
"Available commands:\n\n"
109 " match MATCHER, m MATCHER "
110 "Match the mlir against the given matcher.\n"
112 "Terminates the query session.\n\n";
113 return mlir::success();
118 return mlir::success();
131 std::vector<Operation *> flattenedMatches =
135 if (failed(
verify(
function)))
136 return mlir::failure();
137 os <<
"\n" << *
function <<
"\n\n";
139 return mlir::success();
143 for (
auto &results : matches) {
144 os <<
"Match #" << ++matchCount <<
":\n\n";
145 for (
auto op : results.matchedOps) {
146 if (op == results.rootOp) {
153 os << matchCount << (matchCount == 1 ?
" match.\n\n" :
" matches.\n\n");
154 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
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