MLIR  21.0.0git
MLIRGen.cpp
Go to the documentation of this file.
1 //===- MLIRGen.cpp --------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
14 #include "mlir/IR/Builders.h"
15 #include "mlir/IR/BuiltinOps.h"
16 #include "mlir/IR/Verifier.h"
22 #include "llvm/ADT/ScopedHashTable.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/ADT/TypeSwitch.h"
25 #include <optional>
26 
27 using namespace mlir;
28 using namespace mlir::pdll;
29 
30 //===----------------------------------------------------------------------===//
31 // CodeGen
32 //===----------------------------------------------------------------------===//
33 
34 namespace {
35 class CodeGen {
36 public:
37  CodeGen(MLIRContext *mlirContext, const ast::Context &context,
38  const llvm::SourceMgr &sourceMgr)
39  : builder(mlirContext), odsContext(context.getODSContext()),
40  sourceMgr(sourceMgr) {
41  // Make sure that the PDL dialect is loaded.
42  mlirContext->loadDialect<pdl::PDLDialect>();
43  }
44 
45  OwningOpRef<ModuleOp> generate(const ast::Module &module);
46 
47 private:
48  /// Generate an MLIR location from the given source location.
49  Location genLoc(llvm::SMLoc loc);
50  Location genLoc(llvm::SMRange loc) { return genLoc(loc.Start); }
51 
52  /// Generate an MLIR type from the given source type.
53  Type genType(ast::Type type);
54 
55  /// Generate MLIR for the given AST node.
56  void gen(const ast::Node *node);
57 
58  //===--------------------------------------------------------------------===//
59  // Statements
60  //===--------------------------------------------------------------------===//
61 
62  void genImpl(const ast::CompoundStmt *stmt);
63  void genImpl(const ast::EraseStmt *stmt);
64  void genImpl(const ast::LetStmt *stmt);
65  void genImpl(const ast::ReplaceStmt *stmt);
66  void genImpl(const ast::RewriteStmt *stmt);
67  void genImpl(const ast::ReturnStmt *stmt);
68 
69  //===--------------------------------------------------------------------===//
70  // Decls
71  //===--------------------------------------------------------------------===//
72 
73  void genImpl(const ast::UserConstraintDecl *decl);
74  void genImpl(const ast::UserRewriteDecl *decl);
75  void genImpl(const ast::PatternDecl *decl);
76 
77  /// Generate the set of MLIR values defined for the given variable decl, and
78  /// apply any attached constraints.
79  SmallVector<Value> genVar(const ast::VariableDecl *varDecl);
80 
81  /// Generate the value for a variable that does not have an initializer
82  /// expression, i.e. create the PDL value based on the type/constraints of the
83  /// variable.
84  Value genNonInitializerVar(const ast::VariableDecl *varDecl, Location loc);
85 
86  /// Apply the constraints of the given variable to `values`, which correspond
87  /// to the MLIR values of the variable.
88  void applyVarConstraints(const ast::VariableDecl *varDecl, ValueRange values);
89 
90  //===--------------------------------------------------------------------===//
91  // Expressions
92  //===--------------------------------------------------------------------===//
93 
94  Value genSingleExpr(const ast::Expr *expr);
95  SmallVector<Value> genExpr(const ast::Expr *expr);
96  Value genExprImpl(const ast::AttributeExpr *expr);
97  SmallVector<Value> genExprImpl(const ast::CallExpr *expr);
98  SmallVector<Value> genExprImpl(const ast::DeclRefExpr *expr);
99  Value genExprImpl(const ast::MemberAccessExpr *expr);
100  Value genExprImpl(const ast::OperationExpr *expr);
101  Value genExprImpl(const ast::RangeExpr *expr);
102  SmallVector<Value> genExprImpl(const ast::TupleExpr *expr);
103  Value genExprImpl(const ast::TypeExpr *expr);
104 
105  SmallVector<Value> genConstraintCall(const ast::UserConstraintDecl *decl,
106  Location loc, ValueRange inputs,
107  bool isNegated = false);
108  SmallVector<Value> genRewriteCall(const ast::UserRewriteDecl *decl,
109  Location loc, ValueRange inputs);
110  template <typename PDLOpT, typename T>
111  SmallVector<Value> genConstraintOrRewriteCall(const T *decl, Location loc,
112  ValueRange inputs,
113  bool isNegated = false);
114 
115  //===--------------------------------------------------------------------===//
116  // Fields
117  //===--------------------------------------------------------------------===//
118 
119  /// The MLIR builder used for building the resultant IR.
120  OpBuilder builder;
121 
122  /// A map from variable declarations to the MLIR equivalent.
123  using VariableMapTy =
124  llvm::ScopedHashTable<const ast::VariableDecl *, SmallVector<Value>>;
125  VariableMapTy variables;
126 
127  /// A reference to the ODS context.
128  const ods::Context &odsContext;
129 
130  /// The source manager of the PDLL ast.
131  const llvm::SourceMgr &sourceMgr;
132 };
133 } // namespace
134 
135 OwningOpRef<ModuleOp> CodeGen::generate(const ast::Module &module) {
136  OwningOpRef<ModuleOp> mlirModule =
137  builder.create<ModuleOp>(genLoc(module.getLoc()));
138  builder.setInsertionPointToStart(mlirModule->getBody());
139 
140  // Generate code for each of the decls within the module.
141  for (const ast::Decl *decl : module.getChildren())
142  gen(decl);
143 
144  return mlirModule;
145 }
146 
147 Location CodeGen::genLoc(llvm::SMLoc loc) {
148  unsigned fileID = sourceMgr.FindBufferContainingLoc(loc);
149 
150  // TODO: Fix performance issues in SourceMgr::getLineAndColumn so that we can
151  // use it here.
152  auto &bufferInfo = sourceMgr.getBufferInfo(fileID);
153  unsigned lineNo = bufferInfo.getLineNumber(loc.getPointer());
154  unsigned column =
155  (loc.getPointer() - bufferInfo.getPointerForLineNumber(lineNo)) + 1;
156  auto *buffer = sourceMgr.getMemoryBuffer(fileID);
157 
158  return FileLineColLoc::get(builder.getContext(),
159  buffer->getBufferIdentifier(), lineNo, column);
160 }
161 
162 Type CodeGen::genType(ast::Type type) {
163  return TypeSwitch<ast::Type, Type>(type)
164  .Case([&](ast::AttributeType astType) -> Type {
165  return builder.getType<pdl::AttributeType>();
166  })
167  .Case([&](ast::OperationType astType) -> Type {
168  return builder.getType<pdl::OperationType>();
169  })
170  .Case([&](ast::TypeType astType) -> Type {
171  return builder.getType<pdl::TypeType>();
172  })
173  .Case([&](ast::ValueType astType) -> Type {
174  return builder.getType<pdl::ValueType>();
175  })
176  .Case([&](ast::RangeType astType) -> Type {
177  return pdl::RangeType::get(genType(astType.getElementType()));
178  });
179 }
180 
181 void CodeGen::gen(const ast::Node *node) {
183  .Case<const ast::CompoundStmt, const ast::EraseStmt, const ast::LetStmt,
184  const ast::ReplaceStmt, const ast::RewriteStmt,
187  [&](auto derivedNode) { this->genImpl(derivedNode); })
188  .Case([&](const ast::Expr *expr) { genExpr(expr); });
189 }
190 
191 //===----------------------------------------------------------------------===//
192 // CodeGen: Statements
193 //===----------------------------------------------------------------------===//
194 
195 void CodeGen::genImpl(const ast::CompoundStmt *stmt) {
196  VariableMapTy::ScopeTy varScope(variables);
197  for (const ast::Stmt *childStmt : stmt->getChildren())
198  gen(childStmt);
199 }
200 
201 /// If the given builder is nested under a PDL PatternOp, build a rewrite
202 /// operation and update the builder to nest under it. This is necessary for
203 /// PDLL operation rewrite statements that are directly nested within a Pattern.
204 static void checkAndNestUnderRewriteOp(OpBuilder &builder, Value rootExpr,
205  Location loc) {
206  if (isa<pdl::PatternOp>(builder.getInsertionBlock()->getParentOp())) {
207  pdl::RewriteOp rewrite =
208  builder.create<pdl::RewriteOp>(loc, rootExpr, /*name=*/StringAttr(),
209  /*externalArgs=*/ValueRange());
210  builder.createBlock(&rewrite.getBodyRegion());
211  }
212 }
213 
214 void CodeGen::genImpl(const ast::EraseStmt *stmt) {
215  OpBuilder::InsertionGuard insertGuard(builder);
216  Value rootExpr = genSingleExpr(stmt->getRootOpExpr());
217  Location loc = genLoc(stmt->getLoc());
218 
219  // Make sure we are nested in a RewriteOp.
220  OpBuilder::InsertionGuard guard(builder);
221  checkAndNestUnderRewriteOp(builder, rootExpr, loc);
222  builder.create<pdl::EraseOp>(loc, rootExpr);
223 }
224 
225 void CodeGen::genImpl(const ast::LetStmt *stmt) { genVar(stmt->getVarDecl()); }
226 
227 void CodeGen::genImpl(const ast::ReplaceStmt *stmt) {
228  OpBuilder::InsertionGuard insertGuard(builder);
229  Value rootExpr = genSingleExpr(stmt->getRootOpExpr());
230  Location loc = genLoc(stmt->getLoc());
231 
232  // Make sure we are nested in a RewriteOp.
233  OpBuilder::InsertionGuard guard(builder);
234  checkAndNestUnderRewriteOp(builder, rootExpr, loc);
235 
236  SmallVector<Value> replValues;
237  for (ast::Expr *replExpr : stmt->getReplExprs())
238  replValues.push_back(genSingleExpr(replExpr));
239 
240  // Check to see if the statement has a replacement operation, or a range of
241  // replacement values.
242  bool usesReplOperation =
243  replValues.size() == 1 &&
244  isa<pdl::OperationType>(replValues.front().getType());
245  builder.create<pdl::ReplaceOp>(
246  loc, rootExpr, usesReplOperation ? replValues[0] : Value(),
247  usesReplOperation ? ValueRange() : ValueRange(replValues));
248 }
249 
250 void CodeGen::genImpl(const ast::RewriteStmt *stmt) {
251  OpBuilder::InsertionGuard insertGuard(builder);
252  Value rootExpr = genSingleExpr(stmt->getRootOpExpr());
253 
254  // Make sure we are nested in a RewriteOp.
255  OpBuilder::InsertionGuard guard(builder);
256  checkAndNestUnderRewriteOp(builder, rootExpr, genLoc(stmt->getLoc()));
257  gen(stmt->getRewriteBody());
258 }
259 
260 void CodeGen::genImpl(const ast::ReturnStmt *stmt) {
261  // ReturnStmt generation is handled by the respective constraint or rewrite
262  // parent node.
263 }
264 
265 //===----------------------------------------------------------------------===//
266 // CodeGen: Decls
267 //===----------------------------------------------------------------------===//
268 
269 void CodeGen::genImpl(const ast::UserConstraintDecl *decl) {
270  // All PDLL constraints get inlined when called, and the main native
271  // constraint declarations doesn't require any MLIR to be generated, only uses
272  // of it do.
273 }
274 
275 void CodeGen::genImpl(const ast::UserRewriteDecl *decl) {
276  // All PDLL rewrites get inlined when called, and the main native
277  // rewrite declarations doesn't require any MLIR to be generated, only uses
278  // of it do.
279 }
280 
281 void CodeGen::genImpl(const ast::PatternDecl *decl) {
282  const ast::Name *name = decl->getName();
283 
284  // FIXME: Properly model HasBoundedRecursion in PDL so that we don't drop it
285  // here.
286  pdl::PatternOp pattern = builder.create<pdl::PatternOp>(
287  genLoc(decl->getLoc()), decl->getBenefit(),
288  name ? std::optional<StringRef>(name->getName())
289  : std::optional<StringRef>());
290 
291  OpBuilder::InsertionGuard savedInsertPoint(builder);
292  builder.setInsertionPointToStart(pattern.getBody());
293  gen(decl->getBody());
294 }
295 
296 SmallVector<Value> CodeGen::genVar(const ast::VariableDecl *varDecl) {
297  auto it = variables.begin(varDecl);
298  if (it != variables.end())
299  return *it;
300 
301  // If the variable has an initial value, use that as the base value.
302  // Otherwise, generate a value using the constraint list.
303  SmallVector<Value> values;
304  if (const ast::Expr *initExpr = varDecl->getInitExpr())
305  values = genExpr(initExpr);
306  else
307  values.push_back(genNonInitializerVar(varDecl, genLoc(varDecl->getLoc())));
308 
309  // Apply the constraints of the values of the variable.
310  applyVarConstraints(varDecl, values);
311 
312  variables.insert(varDecl, values);
313  return values;
314 }
315 
316 Value CodeGen::genNonInitializerVar(const ast::VariableDecl *varDecl,
317  Location loc) {
318  // A functor used to generate expressions nested
319  auto getTypeConstraint = [&]() -> Value {
320  for (const ast::ConstraintRef &constraint : varDecl->getConstraints()) {
321  Value typeValue =
322  TypeSwitch<const ast::Node *, Value>(constraint.constraint)
325  [&, this](auto *cst) -> Value {
326  if (auto *typeConstraintExpr = cst->getTypeExpr())
327  return this->genSingleExpr(typeConstraintExpr);
328  return Value();
329  })
330  .Default(Value());
331  if (typeValue)
332  return typeValue;
333  }
334  return Value();
335  };
336 
337  // Generate a value based on the type of the variable.
338  ast::Type type = varDecl->getType();
339  Type mlirType = genType(type);
340  if (isa<ast::ValueType>(type))
341  return builder.create<pdl::OperandOp>(loc, mlirType, getTypeConstraint());
342  if (isa<ast::TypeType>(type))
343  return builder.create<pdl::TypeOp>(loc, mlirType, /*type=*/TypeAttr());
344  if (isa<ast::AttributeType>(type))
345  return builder.create<pdl::AttributeOp>(loc, getTypeConstraint());
346  if (ast::OperationType opType = dyn_cast<ast::OperationType>(type)) {
347  Value operands = builder.create<pdl::OperandsOp>(
348  loc, pdl::RangeType::get(builder.getType<pdl::ValueType>()),
349  /*type=*/Value());
350  Value results = builder.create<pdl::TypesOp>(
351  loc, pdl::RangeType::get(builder.getType<pdl::TypeType>()),
352  /*types=*/ArrayAttr());
353  return builder.create<pdl::OperationOp>(loc, opType.getName(), operands,
355  results);
356  }
357 
358  if (ast::RangeType rangeTy = dyn_cast<ast::RangeType>(type)) {
359  ast::Type eleTy = rangeTy.getElementType();
360  if (isa<ast::ValueType>(eleTy))
361  return builder.create<pdl::OperandsOp>(loc, mlirType,
362  getTypeConstraint());
363  if (isa<ast::TypeType>(eleTy))
364  return builder.create<pdl::TypesOp>(loc, mlirType, /*types=*/ArrayAttr());
365  }
366 
367  llvm_unreachable("invalid non-initialized variable type");
368 }
369 
370 void CodeGen::applyVarConstraints(const ast::VariableDecl *varDecl,
371  ValueRange values) {
372  // Generate calls to any user constraints that were attached via the
373  // constraint list.
374  for (const ast::ConstraintRef &ref : varDecl->getConstraints())
375  if (const auto *userCst = dyn_cast<ast::UserConstraintDecl>(ref.constraint))
376  genConstraintCall(userCst, genLoc(ref.referenceLoc), values);
377 }
378 
379 //===----------------------------------------------------------------------===//
380 // CodeGen: Expressions
381 //===----------------------------------------------------------------------===//
382 
383 Value CodeGen::genSingleExpr(const ast::Expr *expr) {
385  .Case<const ast::AttributeExpr, const ast::MemberAccessExpr,
386  const ast::OperationExpr, const ast::RangeExpr,
387  const ast::TypeExpr>(
388  [&](auto derivedNode) { return this->genExprImpl(derivedNode); })
389  .Case<const ast::CallExpr, const ast::DeclRefExpr, const ast::TupleExpr>(
390  [&](auto derivedNode) {
391  return llvm::getSingleElement(this->genExprImpl(derivedNode));
392  });
393 }
394 
395 SmallVector<Value> CodeGen::genExpr(const ast::Expr *expr) {
397  .Case<const ast::CallExpr, const ast::DeclRefExpr, const ast::TupleExpr>(
398  [&](auto derivedNode) { return this->genExprImpl(derivedNode); })
399  .Default([&](const ast::Expr *expr) -> SmallVector<Value> {
400  return {genSingleExpr(expr)};
401  });
402 }
403 
404 Value CodeGen::genExprImpl(const ast::AttributeExpr *expr) {
405  Attribute attr = parseAttribute(expr->getValue(), builder.getContext());
406  assert(attr && "invalid MLIR attribute data");
407  return builder.create<pdl::AttributeOp>(genLoc(expr->getLoc()), attr);
408 }
409 
410 SmallVector<Value> CodeGen::genExprImpl(const ast::CallExpr *expr) {
411  Location loc = genLoc(expr->getLoc());
412  SmallVector<Value> arguments;
413  for (const ast::Expr *arg : expr->getArguments())
414  arguments.push_back(genSingleExpr(arg));
415 
416  // Resolve the callable expression of this call.
417  auto *callableExpr = dyn_cast<ast::DeclRefExpr>(expr->getCallableExpr());
418  assert(callableExpr && "unhandled CallExpr callable");
419 
420  // Generate the PDL based on the type of callable.
421  const ast::Decl *callable = callableExpr->getDecl();
422  if (const auto *decl = dyn_cast<ast::UserConstraintDecl>(callable))
423  return genConstraintCall(decl, loc, arguments, expr->getIsNegated());
424  if (const auto *decl = dyn_cast<ast::UserRewriteDecl>(callable))
425  return genRewriteCall(decl, loc, arguments);
426  llvm_unreachable("unhandled CallExpr callable");
427 }
428 
429 SmallVector<Value> CodeGen::genExprImpl(const ast::DeclRefExpr *expr) {
430  if (const auto *varDecl = dyn_cast<ast::VariableDecl>(expr->getDecl()))
431  return genVar(varDecl);
432  llvm_unreachable("unknown decl reference expression");
433 }
434 
435 Value CodeGen::genExprImpl(const ast::MemberAccessExpr *expr) {
436  Location loc = genLoc(expr->getLoc());
437  StringRef name = expr->getMemberName();
438  SmallVector<Value> parentExprs = genExpr(expr->getParentExpr());
439  ast::Type parentType = expr->getParentExpr()->getType();
440 
441  // Handle operation based member access.
442  if (ast::OperationType opType = dyn_cast<ast::OperationType>(parentType)) {
443  if (isa<ast::AllResultsMemberAccessExpr>(expr)) {
444  Type mlirType = genType(expr->getType());
445  if (isa<pdl::ValueType>(mlirType))
446  return builder.create<pdl::ResultOp>(loc, mlirType, parentExprs[0],
447  builder.getI32IntegerAttr(0));
448  return builder.create<pdl::ResultsOp>(loc, mlirType, parentExprs[0]);
449  }
450 
451  const ods::Operation *odsOp = opType.getODSOperation();
452  if (!odsOp) {
453  assert(llvm::isDigit(name[0]) &&
454  "unregistered op only allows numeric indexing");
455  unsigned resultIndex;
456  name.getAsInteger(/*Radix=*/10, resultIndex);
457  IntegerAttr index = builder.getI32IntegerAttr(resultIndex);
458  return builder.create<pdl::ResultOp>(loc, genType(expr->getType()),
459  parentExprs[0], index);
460  }
461 
462  // Find the result with the member name or by index.
463  ArrayRef<ods::OperandOrResult> results = odsOp->getResults();
464  unsigned resultIndex = results.size();
465  if (llvm::isDigit(name[0])) {
466  name.getAsInteger(/*Radix=*/10, resultIndex);
467  } else {
468  auto findFn = [&](const ods::OperandOrResult &result) {
469  return result.getName() == name;
470  };
471  resultIndex = llvm::find_if(results, findFn) - results.begin();
472  }
473  assert(resultIndex < results.size() && "invalid result index");
474 
475  // Generate the result access.
476  IntegerAttr index = builder.getI32IntegerAttr(resultIndex);
477  return builder.create<pdl::ResultsOp>(loc, genType(expr->getType()),
478  parentExprs[0], index);
479  }
480 
481  // Handle tuple based member access.
482  if (auto tupleType = dyn_cast<ast::TupleType>(parentType)) {
483  auto elementNames = tupleType.getElementNames();
484 
485  // The index is either a numeric index, or a name.
486  unsigned index = 0;
487  if (llvm::isDigit(name[0]))
488  name.getAsInteger(/*Radix=*/10, index);
489  else
490  index = llvm::find(elementNames, name) - elementNames.begin();
491 
492  assert(index < parentExprs.size() && "invalid result index");
493  return parentExprs[index];
494  }
495 
496  llvm_unreachable("unhandled member access expression");
497 }
498 
499 Value CodeGen::genExprImpl(const ast::OperationExpr *expr) {
500  Location loc = genLoc(expr->getLoc());
501  std::optional<StringRef> opName = expr->getName();
502 
503  // Operands.
504  SmallVector<Value> operands;
505  for (const ast::Expr *operand : expr->getOperands())
506  operands.push_back(genSingleExpr(operand));
507 
508  // Attributes.
509  SmallVector<StringRef> attrNames;
510  SmallVector<Value> attrValues;
511  for (const ast::NamedAttributeDecl *attr : expr->getAttributes()) {
512  attrNames.push_back(attr->getName().getName());
513  attrValues.push_back(genSingleExpr(attr->getValue()));
514  }
515 
516  // Results.
517  SmallVector<Value> results;
518  for (const ast::Expr *result : expr->getResultTypes())
519  results.push_back(genSingleExpr(result));
520 
521  return builder.create<pdl::OperationOp>(loc, opName, operands, attrNames,
522  attrValues, results);
523 }
524 
525 Value CodeGen::genExprImpl(const ast::RangeExpr *expr) {
526  SmallVector<Value> elements;
527  for (const ast::Expr *element : expr->getElements())
528  llvm::append_range(elements, genExpr(element));
529 
530  return builder.create<pdl::RangeOp>(genLoc(expr->getLoc()),
531  genType(expr->getType()), elements);
532 }
533 
534 SmallVector<Value> CodeGen::genExprImpl(const ast::TupleExpr *expr) {
535  SmallVector<Value> elements;
536  for (const ast::Expr *element : expr->getElements())
537  elements.push_back(genSingleExpr(element));
538  return elements;
539 }
540 
541 Value CodeGen::genExprImpl(const ast::TypeExpr *expr) {
542  Type type = parseType(expr->getValue(), builder.getContext());
543  assert(type && "invalid MLIR type data");
544  return builder.create<pdl::TypeOp>(genLoc(expr->getLoc()),
545  builder.getType<pdl::TypeType>(),
546  TypeAttr::get(type));
547 }
548 
550 CodeGen::genConstraintCall(const ast::UserConstraintDecl *decl, Location loc,
551  ValueRange inputs, bool isNegated) {
552  // Apply any constraints defined on the arguments to the input values.
553  for (auto it : llvm::zip(decl->getInputs(), inputs))
554  applyVarConstraints(std::get<0>(it), std::get<1>(it));
555 
556  // Generate the constraint call.
557  SmallVector<Value> results =
558  genConstraintOrRewriteCall<pdl::ApplyNativeConstraintOp>(
559  decl, loc, inputs, isNegated);
560 
561  // Apply any constraints defined on the results of the constraint.
562  for (auto it : llvm::zip(decl->getResults(), results))
563  applyVarConstraints(std::get<0>(it), std::get<1>(it));
564  return results;
565 }
566 
567 SmallVector<Value> CodeGen::genRewriteCall(const ast::UserRewriteDecl *decl,
568  Location loc, ValueRange inputs) {
569  return genConstraintOrRewriteCall<pdl::ApplyNativeRewriteOp>(decl, loc,
570  inputs);
571 }
572 
573 template <typename PDLOpT, typename T>
575 CodeGen::genConstraintOrRewriteCall(const T *decl, Location loc,
576  ValueRange inputs, bool isNegated) {
577  const ast::CompoundStmt *cstBody = decl->getBody();
578 
579  // If the decl doesn't have a statement body, it is a native decl.
580  if (!cstBody) {
581  ast::Type declResultType = decl->getResultType();
582  SmallVector<Type> resultTypes;
583  if (ast::TupleType tupleType = dyn_cast<ast::TupleType>(declResultType)) {
584  for (ast::Type type : tupleType.getElementTypes())
585  resultTypes.push_back(genType(type));
586  } else {
587  resultTypes.push_back(genType(declResultType));
588  }
589  PDLOpT pdlOp = builder.create<PDLOpT>(loc, resultTypes,
590  decl->getName().getName(), inputs);
591  if (isNegated && std::is_same_v<PDLOpT, pdl::ApplyNativeConstraintOp>)
592  cast<pdl::ApplyNativeConstraintOp>(pdlOp).setIsNegated(true);
593  return pdlOp->getResults();
594  }
595 
596  // Otherwise, this is a PDLL decl.
597  VariableMapTy::ScopeTy varScope(variables);
598 
599  // Map the inputs of the call to the decl arguments.
600  // Note: This is only valid because we do not support recursion, meaning
601  // we don't need to worry about conflicting mappings here.
602  for (auto it : llvm::zip(inputs, decl->getInputs()))
603  variables.insert(std::get<1>(it), {std::get<0>(it)});
604 
605  // Visit the body of the call as normal.
606  gen(cstBody);
607 
608  // If the decl has no results, there is nothing to do.
609  if (cstBody->getChildren().empty())
610  return SmallVector<Value>();
611  auto *returnStmt = dyn_cast<ast::ReturnStmt>(cstBody->getChildren().back());
612  if (!returnStmt)
613  return SmallVector<Value>();
614 
615  // Otherwise, grab the results from the return statement.
616  return genExpr(returnStmt->getResultExpr());
617 }
618 
619 //===----------------------------------------------------------------------===//
620 // MLIRGen
621 //===----------------------------------------------------------------------===//
622 
624  MLIRContext *mlirContext, const ast::Context &context,
625  const llvm::SourceMgr &sourceMgr, const ast::Module &module) {
626  CodeGen codegen(mlirContext, context, sourceMgr);
627  OwningOpRef<ModuleOp> mlirModule = codegen.generate(module);
628  if (failed(verify(*mlirModule)))
629  return nullptr;
630  return mlirModule;
631 }
static void checkAndNestUnderRewriteOp(OpBuilder &builder, Value rootExpr, Location loc)
If the given builder is nested under a PDL PatternOp, build a rewrite operation and update the builde...
Definition: MLIRGen.cpp:204
static void rewrite(DataFlowSolver &solver, MLIRContext *context, MutableArrayRef< Region > initialRegions)
Rewrite the given regions using the computing analysis.
Definition: SCCP.cpp:67
Attributes are known-constant values of operations.
Definition: Attributes.h:25
Operation * getParentOp()
Returns the closest surrounding operation that contains this block.
Definition: Block.cpp:31
static FileLineColLoc get(StringAttr filename, unsigned line, unsigned column)
Definition: Location.cpp:161
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:76
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
void loadDialect()
Load a dialect in the context.
Definition: MLIRContext.h:107
RAII guard to reset the insertion point of the builder when destroyed.
Definition: Builders.h:346
This class helps build Operations.
Definition: Builders.h:205
Block * createBlock(Region *parent, Region::iterator insertPt={}, TypeRange argTypes={}, ArrayRef< Location > locs={})
Add new block with 'argTypes' arguments and set the insertion point to the end of it.
Definition: Builders.cpp:425
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:452
Block * getInsertionBlock() const
Return the block the current insertion point belongs to.
Definition: Builders.h:440
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:387
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
The class represents an Attribute constraint, and constrains a variable to be an Attribute.
Definition: Nodes.h:750
This expression represents a literal MLIR Attribute, and contains the textual assembly format of that...
Definition: Nodes.h:370
StringRef getValue() const
Get the raw value of this expression.
Definition: Nodes.h:376
This class represents a PDLL type that corresponds to an mlir::Attribute.
Definition: Types.h:107
This expression represents a call to a decl, such as a UserConstraintDecl/UserRewriteDecl.
Definition: Nodes.h:393
Expr * getCallableExpr() const
Return the callable of this call.
Definition: Nodes.h:400
MutableArrayRef< Expr * > getArguments()
Return the arguments of this call.
Definition: Nodes.h:403
bool getIsNegated() const
Returns whether the result of this call is to be negated.
Definition: Nodes.h:407
This statement represents a compound statement, which contains a collection of other statements.
Definition: Nodes.h:179
MutableArrayRef< Stmt * > getChildren()
Return the children of this compound statement.
Definition: Nodes.h:185
This class represents the main context of the PDLL AST.
Definition: Context.h:25
This expression represents a reference to a Decl node.
Definition: Nodes.h:433
Decl * getDecl() const
Get the decl referenced by this expression.
Definition: Nodes.h:438
This class represents the base Decl node.
Definition: Nodes.h:669
This statement represents the erase statement in PDLL.
Definition: Nodes.h:255
This class represents a base AST Expression node.
Definition: Nodes.h:348
Type getType() const
Return the type of this expression.
Definition: Nodes.h:351
This statement represents a let statement in PDLL.
Definition: Nodes.h:211
VariableDecl * getVarDecl() const
Return the variable defined by this statement.
Definition: Nodes.h:216
This expression represents a named member or field access of a given parent expression.
Definition: Nodes.h:454
const Expr * getParentExpr() const
Get the parent expression of this access.
Definition: Nodes.h:461
StringRef getMemberName() const
Return the name of the member being accessed.
Definition: Nodes.h:464
This class represents a top-level AST module.
Definition: Nodes.h:1297
MutableArrayRef< Decl * > getChildren()
Return the children of this module.
Definition: Nodes.h:1302
This Decl represents a NamedAttribute, and contains a string name and attribute value.
Definition: Nodes.h:998
This class represents a base AST node.
Definition: Nodes.h:108
This expression represents the structural form of an MLIR Operation.
Definition: Nodes.h:512
MutableArrayRef< Expr * > getResultTypes()
Return the result types of this operation.
Definition: Nodes.h:540
MutableArrayRef< NamedAttributeDecl * > getAttributes()
Return the attributes of this operation.
Definition: Nodes.h:548
MutableArrayRef< Expr * > getOperands()
Return the operands of this operation.
Definition: Nodes.h:532
std::optional< StringRef > getName() const
Return the name of the operation, or std::nullopt if there isn't one.
Definition: Nodes.cpp:327
This class represents a PDLL type that corresponds to an mlir::Operation.
Definition: Types.h:134
This Decl represents a single Pattern.
Definition: Nodes.h:1043
const CompoundStmt * getBody() const
Return the body of this pattern.
Definition: Nodes.h:1057
static PatternDecl * create(Context &ctx, SMRange location, const Name *name, std::optional< uint16_t > benefit, bool hasBoundedRecursion, const CompoundStmt *body)
Definition: Nodes.cpp:513
std::optional< uint16_t > getBenefit() const
Return the benefit of this pattern if specified, or std::nullopt.
Definition: Nodes.h:1051
This expression builds a range from a set of element values (which may be ranges themselves).
Definition: Nodes.h:586
MutableArrayRef< Expr * > getElements()
Return the element expressions of this range.
Definition: Nodes.h:592
RangeType getType() const
Return the range result type of this expression.
Definition: Nodes.h:600
This class represents a PDLL type that corresponds to a range of elements with a given element type.
Definition: Types.h:159
Type getElementType() const
Return the element type of this range.
Definition: Types.cpp:100
This statement represents the replace statement in PDLL.
Definition: Nodes.h:271
MutableArrayRef< Expr * > getReplExprs()
Return the replacement values of this statement.
Definition: Nodes.h:277
This statement represents a return from a "callable" like decl, e.g.
Definition: Nodes.h:324
This statement represents an operation rewrite that contains a block of nested rewrite commands.
Definition: Nodes.h:302
CompoundStmt * getRewriteBody() const
Return the compound rewrite body.
Definition: Nodes.h:308
This class represents a base AST Statement node.
Definition: Nodes.h:164
This expression builds a tuple from a set of element values.
Definition: Nodes.h:619
MutableArrayRef< Expr * > getElements()
Return the element expressions of this tuple.
Definition: Nodes.h:625
This class represents a PDLL tuple type, i.e.
Definition: Types.h:222
This expression represents a literal MLIR Type, and contains the textual assembly format of that type...
Definition: Nodes.h:648
StringRef getValue() const
Get the raw value of this expression.
Definition: Nodes.h:654
This class represents a PDLL type that corresponds to an mlir::Type.
Definition: Types.h:249
This decl represents a user defined constraint.
Definition: Nodes.h:888
MutableArrayRef< VariableDecl * > getInputs()
Return the input arguments of this constraint.
Definition: Nodes.h:914
MutableArrayRef< VariableDecl * > getResults()
Return the explicit results of the constraint declaration.
Definition: Nodes.h:927
This decl represents a user defined rewrite.
Definition: Nodes.h:1098
The class represents a Value constraint, and constrains a variable to be a Value.
Definition: Nodes.h:830
The class represents a ValueRange constraint, and constrains a variable to be a ValueRange.
Definition: Nodes.h:853
This class represents a PDLL type that corresponds to an mlir::Value.
Definition: Types.h:262
This Decl represents the definition of a PDLL variable.
Definition: Nodes.h:1248
Expr * getInitExpr() const
Return the initializer expression of this statement, or nullptr if there was no initializer.
Definition: Nodes.h:1264
MutableArrayRef< ConstraintRef > getConstraints()
Return the constraints of this variable.
Definition: Nodes.h:1255
Type getType() const
Return the type of the decl.
Definition: Nodes.h:1270
This class contains all of the registered ODS operation classes.
Definition: Context.h:32
This class provides an ODS representation of a specific operation operand or result.
Definition: Operation.h:74
This class provides an ODS representation of a specific operation.
Definition: Operation.h:125
ArrayRef< OperandOrResult > getResults() const
Returns the results of this operation.
Definition: Operation.h:168
OwningOpRef< ModuleOp > codegenPDLLToMLIR(MLIRContext *mlirContext, const ast::Context &context, const llvm::SourceMgr &sourceMgr, const ast::Module &module)
Given a PDLL module, generate an MLIR PDL pattern module within the given MLIR context.
Definition: MLIRGen.cpp:623
Include the generated interface declarations.
Attribute parseAttribute(llvm::StringRef attrStr, MLIRContext *context, Type type={}, size_t *numRead=nullptr, bool isKnownNullTerminated=false)
This parses a single MLIR attribute to an MLIR context if it was valid.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
Type parseType(llvm::StringRef typeStr, MLIRContext *context, size_t *numRead=nullptr, bool isKnownNullTerminated=false)
This parses a single MLIR type to an MLIR context if it was valid.
LogicalResult verify(Operation *op, bool verifyRecursively=true)
Perform (potentially expensive) checks of invariants, used to detect compiler bugs,...
Definition: Verifier.cpp:423
This class represents a reference to a constraint, and contains a constraint and the location of the ...
Definition: Nodes.h:716
This class provides a convenient API for interacting with source names.
Definition: Nodes.h:37
StringRef getName() const
Return the raw string name.
Definition: Nodes.h:41