Skip to content

Commit

Permalink
[mlir] Fix crash when adding nested dialect extensions
Browse files Browse the repository at this point in the history
A dialect extension can add additional dialect extensions in its `apply` function. This used to crash when the vector of `extensions` was internally reallocated while it is being iterated over.

Differential Revision: https://reviews.llvm.org/D158838
  • Loading branch information
matthias-springer authored and tru committed Aug 31, 2023
1 parent 94f348b commit cc7e24c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
13 changes: 8 additions & 5 deletions mlir/lib/IR/Dialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ DialectInterfaceCollectionBase::DialectInterfaceCollectionBase(
MLIRContext *ctx, TypeID interfaceKind, StringRef interfaceName) {
for (auto *dialect : ctx->getLoadedDialects()) {
#ifndef NDEBUG
dialect->handleUseOfUndefinedPromisedInterface(interfaceKind, interfaceName);
dialect->handleUseOfUndefinedPromisedInterface(interfaceKind,
interfaceName);
#endif
if (auto *interface = dialect->getRegisteredInterface(interfaceKind)) {
interfaces.insert(interface);
Expand Down Expand Up @@ -243,8 +244,9 @@ void DialectRegistry::applyExtensions(Dialect *dialect) const {
extension.apply(ctx, requiredDialects);
};

for (const auto &extension : extensions)
applyExtension(*extension);
// Note: Additional extensions may be added while applying an extension.
for (int i = 0; i < static_cast<int>(extensions.size()); ++i)
applyExtension(*extensions[i]);
}

void DialectRegistry::applyExtensions(MLIRContext *ctx) const {
Expand All @@ -264,8 +266,9 @@ void DialectRegistry::applyExtensions(MLIRContext *ctx) const {
extension.apply(ctx, requiredDialects);
};

for (const auto &extension : extensions)
applyExtension(*extension);
// Note: Additional extensions may be added while applying an extension.
for (int i = 0; i < static_cast<int>(extensions.size()); ++i)
applyExtension(*extensions[i]);
}

bool DialectRegistry::isSubsetOf(const DialectRegistry &rhs) const {
Expand Down
46 changes: 46 additions & 0 deletions mlir/unittests/IR/DialectTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,50 @@ TEST(Dialect, RepeatedDelayedRegistration) {
EXPECT_TRUE(testDialectInterface != nullptr);
}

namespace {
/// A dummy extension that increases a counter when being applied and
/// recursively adds additional extensions.
struct DummyExtension : DialectExtension<DummyExtension, TestDialect> {
DummyExtension(int *counter, int numRecursive)
: DialectExtension(), counter(counter), numRecursive(numRecursive) {}

void apply(MLIRContext *ctx, TestDialect *dialect) const final {
++(*counter);
DialectRegistry nestedRegistry;
for (int i = 0; i < numRecursive; ++i)
nestedRegistry.addExtension(
std::make_unique<DummyExtension>(counter, /*numRecursive=*/0));
// Adding additional extensions may trigger a reallocation of the
// `extensions` vector in the dialect registry.
ctx->appendDialectRegistry(nestedRegistry);
}

private:
int *counter;
int numRecursive;
};
} // namespace

TEST(Dialect, NestedDialectExtension) {
DialectRegistry registry;
registry.insert<TestDialect>();

// Add an extension that adds 100 more extensions.
int counter1 = 0;
registry.addExtension(std::make_unique<DummyExtension>(&counter1, 100));
// Add one more extension. This should not crash.
int counter2 = 0;
registry.addExtension(std::make_unique<DummyExtension>(&counter2, 0));

// Load dialect and apply extensions.
MLIRContext context(registry);
Dialect *testDialect = context.getOrLoadDialect<TestDialect>();
ASSERT_TRUE(testDialect != nullptr);

// Extensions may be applied multiple times. Make sure that each expected
// extension was applied at least once.
EXPECT_GE(counter1, 101);
EXPECT_GE(counter2, 1);
}

} // namespace

0 comments on commit cc7e24c

Please sign in to comment.