Skip to content

Commit ab09d6f

Browse files
joker-ephmemfrob
authored andcommitted
Free memory leak on duplicate interface registration
I guess this is why we should use unique_ptr as much as possible. Also fix the InterfaceAttachmentTest.cpp test. Differential Revision: https://reviews.llvm.org/D110984
1 parent 3458e8b commit ab09d6f

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

mlir/lib/Support/InterfaceSupport.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ void detail::InterfaceMap::insert(
2828
});
2929
if (it != interfaces.end() && it->first == id) {
3030
LLVM_DEBUG(llvm::dbgs() << "Ignoring repeated interface registration");
31+
free(element.second);
3132
continue;
3233
}
3334
interfaces.insert(it, element);

mlir/unittests/IR/InterfaceAttachmentTest.cpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "../../test/lib/Dialect/Test/TestAttributes.h"
2121
#include "../../test/lib/Dialect/Test/TestDialect.h"
2222
#include "../../test/lib/Dialect/Test/TestTypes.h"
23+
#include "mlir/IR/OwningOpRef.h"
2324

2425
using namespace mlir;
2526
using namespace test;
@@ -291,24 +292,25 @@ TEST(InterfaceAttachment, Operation) {
291292
MLIRContext context;
292293

293294
// Initially, the operation doesn't have the interface.
294-
auto moduleOp = ModuleOp::create(UnknownLoc::get(&context));
295-
ASSERT_FALSE(isa<TestExternalOpInterface>(moduleOp.getOperation()));
295+
OwningOpRef<ModuleOp> moduleOp = ModuleOp::create(UnknownLoc::get(&context));
296+
ASSERT_FALSE(isa<TestExternalOpInterface>(moduleOp->getOperation()));
296297

297298
// We can attach an external interface and now the operaiton has it.
298299
ModuleOp::attachInterface<TestExternalOpModel>(context);
299-
auto iface = dyn_cast<TestExternalOpInterface>(moduleOp.getOperation());
300+
auto iface = dyn_cast<TestExternalOpInterface>(moduleOp->getOperation());
300301
ASSERT_TRUE(iface != nullptr);
301302
EXPECT_EQ(iface.getNameLengthPlusArg(10), 24u);
302303
EXPECT_EQ(iface.getNameLengthTimesArg(3), 42u);
303304
EXPECT_EQ(iface.getNameLengthPlusArgTwice(18), 50u);
304305
EXPECT_EQ(iface.getNameLengthMinusArg(5), 9u);
305306

306307
// Default implementation can be overridden.
307-
auto funcOp = FuncOp::create(UnknownLoc::get(&context), "function",
308-
FunctionType::get(&context, {}, {}));
309-
ASSERT_FALSE(isa<TestExternalOpInterface>(funcOp.getOperation()));
308+
OwningOpRef<FuncOp> funcOp =
309+
FuncOp::create(UnknownLoc::get(&context), "function",
310+
FunctionType::get(&context, {}, {}));
311+
ASSERT_FALSE(isa<TestExternalOpInterface>(funcOp->getOperation()));
310312
FuncOp::attachInterface<TestExternalOpOverridingModel>(context);
311-
iface = dyn_cast<TestExternalOpInterface>(funcOp.getOperation());
313+
iface = dyn_cast<TestExternalOpInterface>(funcOp->getOperation());
312314
ASSERT_TRUE(iface != nullptr);
313315
EXPECT_EQ(iface.getNameLengthPlusArg(10), 22u);
314316
EXPECT_EQ(iface.getNameLengthTimesArg(0), 42u);
@@ -317,8 +319,9 @@ TEST(InterfaceAttachment, Operation) {
317319

318320
// Another context doesn't have the interfaces registered.
319321
MLIRContext other;
320-
auto otherModuleOp = ModuleOp::create(UnknownLoc::get(&other));
321-
ASSERT_FALSE(isa<TestExternalOpInterface>(otherModuleOp.getOperation()));
322+
OwningOpRef<ModuleOp> otherModuleOp =
323+
ModuleOp::create(UnknownLoc::get(&other));
324+
ASSERT_FALSE(isa<TestExternalOpInterface>(otherModuleOp->getOperation()));
322325
}
323326

324327
template <class ConcreteOp>
@@ -346,16 +349,16 @@ TEST(InterfaceAttachment, OperationDelayedContextConstruct) {
346349
MLIRContext context(registry);
347350
context.loadDialect<test::TestDialect>();
348351

349-
ModuleOp module = ModuleOp::create(UnknownLoc::get(&context));
350-
OpBuilder builder(module);
352+
OwningOpRef<ModuleOp> module = ModuleOp::create(UnknownLoc::get(&context));
353+
OpBuilder builder(module->getBody(), module->getBody()->begin());
351354
auto opJ =
352355
builder.create<test::OpJ>(builder.getUnknownLoc(), builder.getI32Type());
353356
auto opH =
354357
builder.create<test::OpH>(builder.getUnknownLoc(), opJ.getResult());
355358
auto opI =
356359
builder.create<test::OpI>(builder.getUnknownLoc(), opJ.getResult());
357360

358-
EXPECT_TRUE(isa<TestExternalOpInterface>(module.getOperation()));
361+
EXPECT_TRUE(isa<TestExternalOpInterface>(module->getOperation()));
359362
EXPECT_TRUE(isa<TestExternalOpInterface>(opJ.getOperation()));
360363
EXPECT_TRUE(isa<TestExternalOpInterface>(opH.getOperation()));
361364
EXPECT_FALSE(isa<TestExternalOpInterface>(opI.getOperation()));
@@ -373,23 +376,23 @@ TEST(InterfaceAttachment, OperationDelayedContextAppend) {
373376
MLIRContext context;
374377
context.loadDialect<test::TestDialect>();
375378

376-
ModuleOp module = ModuleOp::create(UnknownLoc::get(&context));
377-
OpBuilder builder(module);
379+
OwningOpRef<ModuleOp> module = ModuleOp::create(UnknownLoc::get(&context));
380+
OpBuilder builder(module->getBody(), module->getBody()->begin());
378381
auto opJ =
379382
builder.create<test::OpJ>(builder.getUnknownLoc(), builder.getI32Type());
380383
auto opH =
381384
builder.create<test::OpH>(builder.getUnknownLoc(), opJ.getResult());
382385
auto opI =
383386
builder.create<test::OpI>(builder.getUnknownLoc(), opJ.getResult());
384387

385-
EXPECT_FALSE(isa<TestExternalOpInterface>(module.getOperation()));
388+
EXPECT_FALSE(isa<TestExternalOpInterface>(module->getOperation()));
386389
EXPECT_FALSE(isa<TestExternalOpInterface>(opJ.getOperation()));
387390
EXPECT_FALSE(isa<TestExternalOpInterface>(opH.getOperation()));
388391
EXPECT_FALSE(isa<TestExternalOpInterface>(opI.getOperation()));
389392

390393
context.appendDialectRegistry(registry);
391394

392-
EXPECT_TRUE(isa<TestExternalOpInterface>(module.getOperation()));
395+
EXPECT_TRUE(isa<TestExternalOpInterface>(module->getOperation()));
393396
EXPECT_TRUE(isa<TestExternalOpInterface>(opJ.getOperation()));
394397
EXPECT_TRUE(isa<TestExternalOpInterface>(opH.getOperation()));
395398
EXPECT_FALSE(isa<TestExternalOpInterface>(opI.getOperation()));

0 commit comments

Comments
 (0)