[mlir] Add workaround for false positive in -Wfree-nonheap-object
authorFangrui Song <i@maskray.me>
Wed, 21 Jul 2021 23:16:20 +0000 (16:16 -0700)
committerFangrui Song <i@maskray.me>
Wed, 21 Jul 2021 23:16:20 +0000 (16:16 -0700)
Restore 499571ea835daf786626a0db1e12f890b6cd8f8d
reverted by 0082764605cc0e7e0363a41ffa77d214c3157aa6.

A compiler slightly older than
"[clang][Sema] removes -Wfree-nonheap-object reference param false positive"
may report the false positive.
We need to retain the workaround a bit longer so that such compilers
can be used to compile MLIR in a warning-free way.

mlir/lib/IR/OperationSupport.cpp

index b1feab3..cfa1302 100644 (file)
@@ -237,7 +237,9 @@ detail::OperandStorage::~OperandStorage() {
   if (isDynamicStorage()) {
     TrailingOperandStorage &storage = getDynamicStorage();
     storage.~TrailingOperandStorage();
-    free(&storage);
+    // Work around -Wfree-nonheap-object false positive fixed by D102728.
+    auto *mem = &storage;
+    free(mem);
   } else {
     getInlineStorage().~TrailingOperandStorage();
   }
@@ -371,8 +373,11 @@ MutableArrayRef<OpOperand> detail::OperandStorage::resize(Operation *owner,
     new (&newOperands[numOperands]) OpOperand(owner);
 
   // If the current storage is also dynamic, free it.
-  if (isDynamicStorage())
-    free(&storage);
+  if (isDynamicStorage()) {
+    // Work around -Wfree-nonheap-object false positive fixed by D102728.
+    auto *mem = &storage;
+    free(mem);
+  }
 
   // Update the storage representation to use the new dynamic storage.
   dynamicStorage.setPointerAndInt(newStorage, true);