/mlir/include/mlir/IR/Module.h

https://github.com/llvm/llvm-project · C Header · 150 lines · 78 code · 27 blank · 45 comment · 2 complexity · 6cc11c2cece8cf6c71613fa9d70311ed MD5 · raw file

  1. //===- Module.h - MLIR Module Class -----------------------------*- C++ -*-===//
  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. //
  9. // Module is the top-level container for code in an MLIR program.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef MLIR_IR_MODULE_H
  13. #define MLIR_IR_MODULE_H
  14. #include "mlir/IR/OwningOpRefBase.h"
  15. #include "mlir/IR/SymbolTable.h"
  16. #include "llvm/Support/PointerLikeTypeTraits.h"
  17. namespace mlir {
  18. class ModuleTerminatorOp;
  19. //===----------------------------------------------------------------------===//
  20. // Module Operation.
  21. //===----------------------------------------------------------------------===//
  22. /// ModuleOp represents a module, or an operation containing one region with a
  23. /// single block containing opaque operations. The region of a module is not
  24. /// allowed to implicitly capture global values, and all external references
  25. /// must use symbolic references via attributes(e.g. via a string name).
  26. class ModuleOp
  27. : public Op<
  28. ModuleOp, OpTrait::ZeroOperands, OpTrait::ZeroResult,
  29. OpTrait::IsIsolatedFromAbove, OpTrait::AffineScope,
  30. OpTrait::SymbolTable,
  31. OpTrait::SingleBlockImplicitTerminator<ModuleTerminatorOp>::Impl,
  32. SymbolOpInterface::Trait, OpTrait::NoRegionArguments> {
  33. public:
  34. using Op::Op;
  35. using Op::print;
  36. static StringRef getOperationName() { return "module"; }
  37. static void build(OpBuilder &builder, OperationState &result,
  38. Optional<StringRef> name = llvm::None);
  39. /// Construct a module from the given location with an optional name.
  40. static ModuleOp create(Location loc, Optional<StringRef> name = llvm::None);
  41. /// Operation hooks.
  42. static ParseResult parse(OpAsmParser &parser, OperationState &result);
  43. void print(OpAsmPrinter &p);
  44. LogicalResult verify();
  45. /// Return body of this module.
  46. Region &getBodyRegion();
  47. Block *getBody();
  48. /// Return the name of this module if present.
  49. Optional<StringRef> getName();
  50. /// Print the this module in the custom top-level form.
  51. void print(raw_ostream &os, OpPrintingFlags flags = llvm::None);
  52. void print(raw_ostream &os, AsmState &state,
  53. OpPrintingFlags flags = llvm::None);
  54. void dump();
  55. //===--------------------------------------------------------------------===//
  56. // Body Management.
  57. //===--------------------------------------------------------------------===//
  58. /// Iteration over the operations in the module.
  59. using iterator = Block::iterator;
  60. iterator begin() { return getBody()->begin(); }
  61. iterator end() { return getBody()->end(); }
  62. Operation &front() { return *begin(); }
  63. /// This returns a range of operations of the given type 'T' held within the
  64. /// module.
  65. template <typename T> iterator_range<Block::op_iterator<T>> getOps() {
  66. return getBody()->getOps<T>();
  67. }
  68. /// Insert the operation into the back of the body, before the terminator.
  69. void push_back(Operation *op) {
  70. insert(Block::iterator(getBody()->getTerminator()), op);
  71. }
  72. /// Insert the operation at the given insertion point. Note: The operation is
  73. /// never inserted after the terminator, even if the insertion point is end().
  74. void insert(Operation *insertPt, Operation *op) {
  75. insert(Block::iterator(insertPt), op);
  76. }
  77. void insert(Block::iterator insertPt, Operation *op) {
  78. auto *body = getBody();
  79. if (insertPt == body->end())
  80. insertPt = Block::iterator(body->getTerminator());
  81. body->getOperations().insert(insertPt, op);
  82. }
  83. //===--------------------------------------------------------------------===//
  84. // SymbolOpInterface Methods
  85. //===--------------------------------------------------------------------===//
  86. /// A ModuleOp may optionally define a symbol.
  87. bool isOptionalSymbol() { return true; }
  88. };
  89. /// The ModuleTerminatorOp is a special terminator operation for the body of a
  90. /// ModuleOp, it has no semantic meaning beyond keeping the body of a ModuleOp
  91. /// well-formed.
  92. ///
  93. /// This operation does _not_ have a custom syntax. However, ModuleOp will omit
  94. /// the terminator in their custom syntax for brevity.
  95. class ModuleTerminatorOp
  96. : public Op<ModuleTerminatorOp, OpTrait::ZeroOperands, OpTrait::ZeroResult,
  97. OpTrait::HasParent<ModuleOp>::Impl, OpTrait::IsTerminator> {
  98. public:
  99. using Op::Op;
  100. static StringRef getOperationName() { return "module_terminator"; }
  101. static void build(OpBuilder &, OperationState &) {}
  102. };
  103. /// This class acts as an owning reference to a module, and will automatically
  104. /// destroy the held module on destruction if the held module is valid.
  105. class OwningModuleRef : public OwningOpRefBase<ModuleOp> {
  106. public:
  107. using OwningOpRefBase<ModuleOp>::OwningOpRefBase;
  108. };
  109. } // end namespace mlir
  110. namespace llvm {
  111. /// Allow stealing the low bits of ModuleOp.
  112. template <> struct PointerLikeTypeTraits<mlir::ModuleOp> {
  113. public:
  114. static inline void *getAsVoidPointer(mlir::ModuleOp I) {
  115. return const_cast<void *>(I.getAsOpaquePointer());
  116. }
  117. static inline mlir::ModuleOp getFromVoidPointer(void *P) {
  118. return mlir::ModuleOp::getFromOpaquePointer(P);
  119. }
  120. static constexpr int NumLowBitsAvailable = 3;
  121. };
  122. } // end namespace llvm
  123. #endif // MLIR_IR_MODULE_H