[all-commits] [llvm/llvm-project] 4433e5: [mlir] Fix circular dialect initialization

Matthias Springer via All-commits all-commits at lists.llvm.org
Thu Oct 27 02:50:55 PDT 2022


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 4433e52e69b1ce19b1d3c756e6d3262170ad4a30
      https://github.com/llvm/llvm-project/commit/4433e52e69b1ce19b1d3c756e6d3262170ad4a30
  Author: Matthias Springer <springerm at google.com>
  Date:   2022-10-27 (Thu, 27 Oct 2022)

  Changed paths:
    M mlir/include/mlir/IR/MLIRContext.h
    M mlir/lib/IR/MLIRContext.cpp
    M mlir/tools/mlir-tblgen/DialectGen.cpp

  Log Message:
  -----------
  [mlir] Fix circular dialect initialization

This change fixes a bug where a dialect is initialized multiple times. This triggers an assertion when the ops of the dialect are registered (`error: operation named ... is already registered`).

This bug can be triggered as follows:

1. Dialect A depends on dialect B (as per ADialect.td).

2. Somewhere there is an extension of dialect B that depends on dialect A (e.g., it defines external models create ops from dialect A). E.g.:
```
registry.addExtension(+[](MLIRContext *ctx, BDialect *dialect) {
  BDialectOp::attachInterface ...
  ctx->loadDialect<ADialect>();
});
```

3. When dialect A is loaded, its `initialize` function is called twice:

```
     ADialect::ADialect()
        |     |
        |     v
        |   ADialect::initialize()
        v
     getOrLoadDialect<BDialect>()
        |
        v
     (load extension of BDialect)
        |
        v
     ctx->loadDialect<ADialect>()  // user wrote this in the extension
        |
        v
     getOrLoadDialect<ADialect>()  // the dialect is not "fully" loaded yet
        |
        v
     ADialect::ADialect()
        |
        v
     ADialect::initialize()
```

An example of a dialect extension that depends on other dialects is `Dialect/Tensor/Transforms/BufferizableOpInterfaceImpl.cpp`. That particular dialect extension does not trigger this bug. (It would trigger this bug if the SCF dialect would depend on the Tensor dialect.)

This change introduces a new dialect state: dialects that are currently being loaded. Same as dialects that were already fully loaded (and initialized), dialects that are in the process of being loaded are not loaded a second time.

Differential Revision: https://reviews.llvm.org/D136685




More information about the All-commits mailing list