2424#include " llvm/ADT/APFloat.h"
2525#include " llvm/ADT/PointerIntPair.h"
2626#include " llvm/Support/TrailingObjects.h"
27- #include < mutex>
2827
2928namespace mlir {
3029namespace detail {
@@ -402,8 +401,7 @@ class DistinctAttributeUniquer {
402401// / is freed after the destruction of the distinct attribute allocator.
403402class DistinctAttributeAllocator {
404403public:
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
0 commit comments