Skip to content

Commit 2da4ce8

Browse files
authored
Revert "[mlir] Fix DistinctAttributeUniquer deleting attribute storage when crash reproduction is enabled" (#133000)
Reverts #128566. See as well the discussion in #132935.
1 parent b022f67 commit 2da4ce8

File tree

6 files changed

+4
-106
lines changed

6 files changed

+4
-106
lines changed

mlir/include/mlir/IR/MLIRContext.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,6 @@ class MLIRContext {
153153
disableMultithreading(!enable);
154154
}
155155

156-
/// Set the flag specifying if thread-local storage should be used by storage
157-
/// allocators in this context. Note that disabling mutlithreading implies
158-
/// thread-local storage is also disabled.
159-
void disableThreadLocalStorage(bool disable = true);
160-
void enableThreadLocalStorage(bool enable = true) {
161-
disableThreadLocalStorage(!enable);
162-
}
163-
164156
/// Set a new thread pool to be used in this context. This method requires
165157
/// that multithreading is disabled for this context prior to the call. This
166158
/// allows to share a thread pool across multiple contexts, as well as

mlir/lib/IR/AttributeDetail.h

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "llvm/ADT/APFloat.h"
2525
#include "llvm/ADT/PointerIntPair.h"
2626
#include "llvm/Support/TrailingObjects.h"
27-
#include <mutex>
2827

2928
namespace mlir {
3029
namespace detail {
@@ -402,8 +401,7 @@ class DistinctAttributeUniquer {
402401
/// is freed after the destruction of the distinct attribute allocator.
403402
class DistinctAttributeAllocator {
404403
public:
405-
DistinctAttributeAllocator(bool threadingIsEnabled)
406-
: threadingIsEnabled(threadingIsEnabled), useThreadLocalAllocator(true) {};
404+
DistinctAttributeAllocator() = default;
407405

408406
DistinctAttributeAllocator(DistinctAttributeAllocator &&) = delete;
409407
DistinctAttributeAllocator(const DistinctAttributeAllocator &) = delete;
@@ -413,49 +411,12 @@ class DistinctAttributeAllocator {
413411
/// Allocates a distinct attribute storage using a thread local bump pointer
414412
/// allocator to enable synchronization free parallel allocations.
415413
DistinctAttrStorage *allocate(Attribute referencedAttr) {
416-
if (!useThreadLocalAllocator && threadingIsEnabled) {
417-
std::scoped_lock<std::mutex> lock(allocatorMutex);
418-
return allocateImpl(referencedAttr);
419-
}
420-
return allocateImpl(referencedAttr);
421-
}
422-
423-
/// Sets a flag that stores if multithreading is enabled. The flag is used to
424-
/// decide if locking is needed when using a non thread-safe allocator.
425-
void disableMultiThreading(bool disable = true) {
426-
threadingIsEnabled = !disable;
427-
}
428-
429-
/// Sets a flag to disable using thread local bump pointer allocators and use
430-
/// a single thread-safe allocator. Use this to persist allocated storage
431-
/// beyond the lifetime of a child thread calling this function while ensuring
432-
/// thread-safe allocation.
433-
void disableThreadLocalStorage(bool disable = true) {
434-
useThreadLocalAllocator = !disable;
435-
}
436-
437-
private:
438-
DistinctAttrStorage *allocateImpl(Attribute referencedAttr) {
439-
return new (getAllocatorInUse().Allocate<DistinctAttrStorage>())
414+
return new (allocatorCache.get().Allocate<DistinctAttrStorage>())
440415
DistinctAttrStorage(referencedAttr);
441416
}
442417

443-
/// If threading is disabled on the owning MLIR context, a normal non
444-
/// thread-local, non-thread safe bump pointer allocator is used instead to
445-
/// prevent use-after-free errors whenever attribute storage created on a
446-
/// crash recover thread is accessed after the thread joins.
447-
llvm::BumpPtrAllocator &getAllocatorInUse() {
448-
if (useThreadLocalAllocator)
449-
return allocatorCache.get();
450-
return allocator;
451-
}
452-
418+
private:
453419
ThreadLocalCache<llvm::BumpPtrAllocator> allocatorCache;
454-
llvm::BumpPtrAllocator allocator;
455-
std::mutex allocatorMutex;
456-
457-
bool threadingIsEnabled : 1;
458-
bool useThreadLocalAllocator : 1;
459420
};
460421
} // namespace detail
461422
} // namespace mlir

mlir/lib/IR/MLIRContext.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,7 @@ class MLIRContextImpl {
268268

269269
public:
270270
MLIRContextImpl(bool threadingIsEnabled)
271-
: threadingIsEnabled(threadingIsEnabled),
272-
distinctAttributeAllocator(threadingIsEnabled) {
271+
: threadingIsEnabled(threadingIsEnabled) {
273272
if (threadingIsEnabled) {
274273
ownedThreadPool = std::make_unique<llvm::DefaultThreadPool>();
275274
threadPool = ownedThreadPool.get();
@@ -597,7 +596,6 @@ void MLIRContext::disableMultithreading(bool disable) {
597596
// Update the threading mode for each of the uniquers.
598597
impl->affineUniquer.disableMultithreading(disable);
599598
impl->attributeUniquer.disableMultithreading(disable);
600-
impl->distinctAttributeAllocator.disableMultiThreading(disable);
601599
impl->typeUniquer.disableMultithreading(disable);
602600

603601
// Destroy thread pool (stop all threads) if it is no longer needed, or create
@@ -719,10 +717,6 @@ bool MLIRContext::isOperationRegistered(StringRef name) {
719717
return RegisteredOperationName::lookup(name, this).has_value();
720718
}
721719

722-
void MLIRContext::disableThreadLocalStorage(bool disable) {
723-
getImpl().distinctAttributeAllocator.disableThreadLocalStorage(disable);
724-
}
725-
726720
void Dialect::addType(TypeID typeID, AbstractType &&typeInfo) {
727721
auto &impl = context->getImpl();
728722
assert(impl.multiThreadedExecutionContext == 0 &&

mlir/lib/Pass/PassCrashRecovery.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -414,15 +414,6 @@ struct FileReproducerStream : public mlir::ReproducerStream {
414414

415415
LogicalResult PassManager::runWithCrashRecovery(Operation *op,
416416
AnalysisManager am) {
417-
// Notify the context to disable the use of thread-local storage while the
418-
// pass manager is running in a crash recovery context thread. Re-enable the
419-
// thread local storage upon function exit. This is required to persist any
420-
// attribute storage allocated during passes beyond the lifetime of the
421-
// recovery context thread.
422-
MLIRContext *ctx = getContext();
423-
ctx->disableThreadLocalStorage();
424-
auto guard =
425-
llvm::make_scope_exit([ctx]() { ctx->enableThreadLocalStorage(); });
426417
crashReproGenerator->initialize(getPasses(), op, verifyPasses);
427418

428419
// Safely invoke the passes within a recovery context.

mlir/test/Dialect/LLVMIR/add-debuginfo-func-scope-with-crash-reproduction.mlir

Lines changed: 0 additions & 22 deletions
This file was deleted.

mlir/test/IR/test-builtin-distinct-attrs-with-crash-reproduction.mlir

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)