15 #include "llvm/Support/SourceMgr.h"
16 #include "llvm/Support/raw_ostream.h"
24 std::vector<llvm::LineEditor::Completion>
30 const std::string &binding) {
33 qs.
getBufferId(), fileLoc.getLine(), fileLoc.getColumn());
35 "\"" + binding +
"\" binds here");
42 llvm::StringRef functionName) {
47 std::vector<Operation *> slice;
48 std::vector<Value> values;
49 std::vector<Type> outputTypes;
51 for (
auto *op : ops) {
53 if (!isa<func::ReturnOp>(op))
57 outputTypes.insert(outputTypes.end(), op->getResults().getTypes().begin(),
58 op->getResults().getTypes().end());
61 values.insert(values.end(), op->getOperands().begin(),
62 op->getOperands().end());
66 FunctionType funcType =
68 auto loc = builder.getUnknownLoc();
69 func::FuncOp funcOp = func::FuncOp::create(loc, functionName, funcType);
71 builder.setInsertionPointToEnd(funcOp.addEntryBlock());
76 mapper.
map(arg.value(), funcOp.getArgument(arg.index()));
79 std::vector<Operation *> clonedOps;
80 std::vector<Value> clonedVals;
83 clonedOps.emplace_back(builder.clone(*slicedOp, mapper));
84 clonedVals.insert(clonedVals.end(), clonedOp->
result_begin(),
88 builder.
create<func::ReturnOp>(loc, clonedVals);
91 size_t currentIndex = 0;
92 while (currentIndex < funcOp.getNumArguments()) {
93 if (funcOp.getArgument(currentIndex).use_empty())
94 funcOp.eraseArgument(currentIndex);
106 return mlir::failure();
110 return mlir::success();
114 os <<
"Available commands:\n\n"
115 " match MATCHER, m MATCHER "
116 "Match the mlir against the given matcher.\n"
118 "Terminates the query session.\n\n";
119 return mlir::success();
124 return mlir::success();
130 std::vector<Operation *> matches =
139 os <<
"\n" << *
function <<
"\n\n";
141 return mlir::success();
146 os <<
"Match #" << ++matchCount <<
":\n\n";
150 os << matchCount << (matchCount == 1 ?
" match.\n\n" :
" matches.\n\n");
152 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'.
T findInstanceOf()
Return an instance of the given location type if one is nested under the current location.
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.
Location getLoc()
The source location the operation was defined or derived from.
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)
llvm::SourceMgr & getSourceManager() const
StringRef getFunctionName() const
bool hasFunctionName() const
static std::vector< Operation * > getMatches(Operation *root, DynMatcher matcher)
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)
static void printMatch(llvm::raw_ostream &os, QuerySession &qs, Operation *op, const std::string &binding)
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