Skip to content

Commit 30aff7a

Browse files
dbudiichios202
authored andcommitted
MLIR-QUERY: backwardSlice, forwardSlice & QueryOptions added
1 parent 0eac094 commit 30aff7a

File tree

5 files changed

+47
-9
lines changed

5 files changed

+47
-9
lines changed

mlir/lib/Query/Matcher/Parser.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,19 @@ bool Parser::parseIdentifierPrefixImpl(VariantValue *value) {
293293

294294
if (tokenizer->nextTokenKind() != TokenKind::OpenParen) {
295295
// Parse as a named value.
296-
auto namedValue =
297-
namedValues ? namedValues->lookup(nameToken.text) : VariantValue();
296+
if (auto namedValue = namedValues ? namedValues->lookup(nameToken.text)
297+
: VariantValue()) {
298298

299-
if (!namedValue.isMatcher()) {
300-
error->addError(tokenizer->peekNextToken().range,
301-
ErrorType::ParserNotAMatcher);
302-
return false;
299+
if (tokenizer->nextTokenKind() != TokenKind::Period) {
300+
*value = namedValue;
301+
return true;
302+
}
303+
304+
if (!namedValue.isMatcher()) {
305+
error->addError(tokenizer->peekNextToken().range,
306+
ErrorType::ParserNotAMatcher);
307+
return false;
308+
}
303309
}
304310

305311
if (tokenizer->nextTokenKind() == TokenKind::NewLine) {

mlir/lib/Query/Query.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ LogicalResult QuitQuery::run(llvm::raw_ostream &os, QuerySession &qs) const {
127127
return mlir::success();
128128
}
129129

130+
LogicalResult LetQuery::run(llvm::raw_ostream &os, QuerySession &qs) const {
131+
if (value.hasValue()) {
132+
qs.namedValues[name] = value;
133+
} else {
134+
qs.namedValues.erase(name);
135+
}
136+
return mlir::success();
137+
}
138+
130139
LogicalResult MatchQuery::run(llvm::raw_ostream &os, QuerySession &qs) const {
131140
Operation *rootOp = qs.getRootOp();
132141

mlir/lib/Query/QueryParser.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ QueryRef QueryParser::doParse() {
166166
.Case("", ParsedQueryKind::NoOp)
167167
.Case("#", ParsedQueryKind::Comment, /*isCompletion=*/false)
168168
.Case("help", ParsedQueryKind::Help)
169+
.Case("l", ParsedQueryKind::Let, /*isCompletion=*/false)
170+
.Case("let", ParsedQueryKind::Let, /*isCompletion=*/false)
169171
.Case("m", ParsedQueryKind::Match, /*isCompletion=*/false)
170172
.Case("set", ParsedQueryKind::Set)
171173
.Case("match", ParsedQueryKind::Match)
@@ -188,6 +190,27 @@ QueryRef QueryParser::doParse() {
188190
case ParsedQueryKind::Quit:
189191
return endQuery(new QuitQuery);
190192

193+
case ParsedQueryKind::Let: {
194+
llvm::StringRef name = lexWord();
195+
196+
if (name.empty()) {
197+
return new InvalidQuery("expected variable name");
198+
}
199+
200+
if (completionPos) {
201+
return completeMatcherExpression();
202+
}
203+
204+
matcher::internal::Diagnostics diag;
205+
matcher::VariantValue value;
206+
if (!matcher::internal::Parser::parseExpression(
207+
line, qs.getRegistryData(), &qs.namedValues, &value, &diag)) {
208+
return makeInvalidQueryFromDiagnostics(diag);
209+
}
210+
QueryRef query = new LetQuery(name, value);
211+
query->remainingContent = line;
212+
return query;
213+
}
191214
case ParsedQueryKind::Match: {
192215
if (completionPos) {
193216
return completeMatcherExpression();
@@ -204,6 +227,7 @@ QueryRef QueryParser::doParse() {
204227
}
205228
auto actualSource = origMatcherSource.substr(0, origMatcherSource.size() -
206229
matcherSource.size());
230+
207231
QueryRef query = new MatchQuery(actualSource, *matcher);
208232
query->remainingContent = matcherSource;
209233
return query;

mlir/test/mlir-query/function-extraction.mlir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// CHECK: %[[MUL0:.*]] = arith.mulf {{.*}} : f32
55
// CHECK: %[[MUL1:.*]] = arith.mulf {{.*}}, %[[MUL0]] : f32
66
// CHECK: %[[MUL2:.*]] = arith.mulf {{.*}} : f32
7-
// CHECK-NEXT: return %[[MUL0]], %[[MUL1]], %[[MUL2]] : f32, f32, f32
7+
// CHECK-NEXT: return %[[MUL0]], %[[MUL1]], %[[MUL2]] : f32, f32, f32S
88

99
func.func @mixedOperations(%a: f32, %b: f32, %c: f32) -> f32 {
1010
%sum0 = arith.addf %a, %b : f32
@@ -16,4 +16,4 @@ func.func @mixedOperations(%a: f32, %b: f32, %c: f32) -> f32 {
1616
%sum2 = arith.addf %mul1, %b : f32
1717
%mul2 = arith.mulf %sub2, %sum2 : f32
1818
return %mul2 : f32
19-
}
19+
}

mlir/tools/mlir-query/mlir-query.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
// of the registered queries.
1111
//
1212
//===----------------------------------------------------------------------===//
13-
1413
#include "mlir/IR/Dialect.h"
1514
#include "mlir/IR/MLIRContext.h"
1615
#include "mlir/IR/Matchers.h"

0 commit comments

Comments
 (0)