diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/emscripten-optimizer/CMakeLists.txt | 6 | ||||
-rw-r--r-- | src/mixed_arena.h | 46 | ||||
-rw-r--r-- | src/passes/CMakeLists.txt | 17 | ||||
-rw-r--r-- | src/passes/pass.cpp (renamed from src/pass.cpp) | 0 | ||||
-rw-r--r-- | src/support/CMakeLists.txt | 10 | ||||
-rw-r--r-- | src/wasm.h | 2 |
6 files changed, 65 insertions, 16 deletions
diff --git a/src/emscripten-optimizer/CMakeLists.txt b/src/emscripten-optimizer/CMakeLists.txt new file mode 100644 index 000000000..6c302b991 --- /dev/null +++ b/src/emscripten-optimizer/CMakeLists.txt @@ -0,0 +1,6 @@ +SET(emscripten-optimizer_SOURCES + optimizer-shared.cpp + parser.cpp + simple_ast.cpp +) +ADD_LIBRARY(emscripten-optimizer STATIC ${emscripten-optimizer_SOURCES}) diff --git a/src/mixed_arena.h b/src/mixed_arena.h index 930bf3c0e..77aa052c1 100644 --- a/src/mixed_arena.h +++ b/src/mixed_arena.h @@ -61,32 +61,48 @@ struct MixedArena { size_t chunkSize = 32768; size_t index; // in last chunk + std::thread::id threadId; + // multithreaded allocation - each arena is valid on a specific thread. - // if we are on the wrong thread, we safely look in the linked + // if we are on the wrong thread, we atomically look in the linked // list of next, adding an allocator if necessary - // TODO: we don't really need locking here, atomics could suffice - std::thread::id threadId; - std::mutex mutex; - MixedArena* next; + std::atomic<MixedArena*> next; MixedArena() { threadId = std::this_thread::get_id(); - next = nullptr; + next.store(nullptr); } void* allocSpace(size_t size) { // the bump allocator data should not be modified by multiple threads at once. - if (std::this_thread::get_id() != threadId) { - // TODO use a fast double-checked locking pattern. - std::lock_guard<std::mutex> lock(mutex); + auto myId = std::this_thread::get_id(); + if (myId != threadId) { MixedArena* curr = this; - while (std::this_thread::get_id() != curr->threadId) { - if (curr->next) { - curr = curr->next; - } else { - curr->next = new MixedArena(); // will have our thread id + MixedArena* allocated = nullptr; + while (myId != curr->threadId) { + auto seen = curr->next.load(); + if (seen) { + curr = seen; + continue; + } + // there is a nullptr for next, so we may be able to place a new + // allocator for us there. but carefully, as others may do so as + // well. we may waste a few allocations here, but it doesn't matter + // as this can only happen as the chain is built up, i.e., + // O(# of cores) per allocator, and our allocatrs are long-lived. + if (!allocated) { + allocated = new MixedArena(); // has our thread id + } + if (curr->next.compare_exchange_weak(seen, allocated)) { + // we replaced it, so we are the next in the chain + // we can forget about allocated, it is owned by the chain now + allocated = nullptr; + break; } + // otherwise, the cmpxchg updated seen, and we continue to loop + curr = seen; } + if (allocated) delete allocated; return curr->allocSpace(size); } size = (size + 7) & (-8); // same alignment as malloc TODO optimize? @@ -120,7 +136,7 @@ struct MixedArena { ~MixedArena() { clear(); - if (next) delete next; + if (next.load()) delete next.load(); } }; diff --git a/src/passes/CMakeLists.txt b/src/passes/CMakeLists.txt new file mode 100644 index 000000000..460910978 --- /dev/null +++ b/src/passes/CMakeLists.txt @@ -0,0 +1,17 @@ +SET(passes_SOURCES + pass.cpp + LowerIfElse.cpp + MergeBlocks.cpp + Metrics.cpp + NameManager.cpp + OptimizeInstructions.cpp + PostEmscripten.cpp + Print.cpp + RemoveImports.cpp + RemoveUnusedBrs.cpp + RemoveUnusedNames.cpp + ReorderLocals.cpp + SimplifyLocals.cpp + Vacuum.cpp +) +ADD_LIBRARY(passes STATIC ${passes_SOURCES}) diff --git a/src/pass.cpp b/src/passes/pass.cpp index e4cc35554..e4cc35554 100644 --- a/src/pass.cpp +++ b/src/passes/pass.cpp diff --git a/src/support/CMakeLists.txt b/src/support/CMakeLists.txt new file mode 100644 index 000000000..08546e3ee --- /dev/null +++ b/src/support/CMakeLists.txt @@ -0,0 +1,10 @@ +SET(support_SOURCES + archive.cpp + bits.cpp + colors.cpp + command-line.cpp + file.cpp + safe_integer.cpp + threads.cpp +) +ADD_LIBRARY(support STATIC ${support_SOURCES}) diff --git a/src/wasm.h b/src/wasm.h index 049dc2e30..2e3a63105 100644 --- a/src/wasm.h +++ b/src/wasm.h @@ -913,7 +913,7 @@ public: // set the type of a block if you already know it void finalize(WasmType type_) { - type = type; + type = type_; } // set the type of a block based on its contents. this scans the block, so it is not fast |