Skip to content

Commit e8ce5f1

Browse files
committed
[bolt] Use llvm::sys::RWMutex instead of std::shared_timed_mutex
This has the following advantages: - std::shared_timed_mutex is macOS 10.12+ only. llvm::sys::RWMutex automatically switches to a different implementation internally when targeting older macOS versions. - bolt only needs std::shared_mutex, not std::shared_timed_mutex. llvm::sys::RWMutex automatically uses std::shared_mutex internally where available. std::shared_mutex and RWMutex have the same API, so no code changes other than types and includes are needed. Differential Revision: https://reviews.llvm.org/D138423
1 parent 99b3849 commit e8ce5f1

File tree

10 files changed

+29
-26
lines changed

10 files changed

+29
-26
lines changed

bolt/include/bolt/Core/BinaryContext.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@
3737
#include "llvm/MC/MCSymbol.h"
3838
#include "llvm/MC/TargetRegistry.h"
3939
#include "llvm/Support/ErrorOr.h"
40+
#include "llvm/Support/RWMutex.h"
4041
#include "llvm/Support/raw_ostream.h"
4142
#include <functional>
4243
#include <list>
4344
#include <map>
4445
#include <set>
45-
#include <shared_mutex>
4646
#include <string>
4747
#include <system_error>
4848
#include <type_traits>
@@ -190,7 +190,7 @@ class BinaryContext {
190190
std::map<uint64_t, BinaryFunction> BinaryFunctions;
191191

192192
/// A mutex that is used to control parallel accesses to BinaryFunctions
193-
mutable std::shared_timed_mutex BinaryFunctionsMutex;
193+
mutable llvm::sys::RWMutex BinaryFunctionsMutex;
194194

195195
/// Functions injected by BOLT
196196
std::vector<BinaryFunction *> InjectedBinaryFunctions;
@@ -420,7 +420,7 @@ class BinaryContext {
420420
std::unordered_map<const MCSymbol *, BinaryFunction *> SymbolToFunctionMap;
421421

422422
/// A mutex that is used to control parallel accesses to SymbolToFunctionMap
423-
mutable std::shared_timed_mutex SymbolToFunctionMapMutex;
423+
mutable llvm::sys::RWMutex SymbolToFunctionMapMutex;
424424

425425
/// Look up the symbol entry that contains the given \p Address (based on
426426
/// the start address and size for each symbol). Returns a pointer to
@@ -556,9 +556,9 @@ class BinaryContext {
556556
std::unique_ptr<MCContext> Ctx;
557557

558558
/// A mutex that is used to control parallel accesses to Ctx
559-
mutable std::shared_timed_mutex CtxMutex;
560-
std::unique_lock<std::shared_timed_mutex> scopeLock() const {
561-
return std::unique_lock<std::shared_timed_mutex>(CtxMutex);
559+
mutable llvm::sys::RWMutex CtxMutex;
560+
std::unique_lock<llvm::sys::RWMutex> scopeLock() const {
561+
return std::unique_lock<llvm::sys::RWMutex>(CtxMutex);
562562
}
563563

564564
std::unique_ptr<DWARFContext> DwCtx;

bolt/include/bolt/Core/BinaryFunction.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "llvm/MC/MCInst.h"
4646
#include "llvm/MC/MCSymbol.h"
4747
#include "llvm/Object/ObjectFile.h"
48+
#include "llvm/Support/RWMutex.h"
4849
#include "llvm/Support/raw_ostream.h"
4950
#include <algorithm>
5051
#include <iterator>
@@ -1166,7 +1167,7 @@ class BinaryFunction {
11661167

11671168
MCSymbol *&FunctionEndLabel = FunctionEndLabels[LabelIndex];
11681169
if (!FunctionEndLabel) {
1169-
std::unique_lock<std::shared_timed_mutex> Lock(BC.CtxMutex);
1170+
std::unique_lock<llvm::sys::RWMutex> Lock(BC.CtxMutex);
11701171
if (Fragment == FragmentNum::main())
11711172
FunctionEndLabel = BC.Ctx->createNamedTempSymbol("func_end");
11721173
else
@@ -1490,7 +1491,7 @@ class BinaryFunction {
14901491
std::unique_ptr<BinaryBasicBlock>
14911492
createBasicBlock(MCSymbol *Label = nullptr) {
14921493
if (!Label) {
1493-
std::unique_lock<std::shared_timed_mutex> Lock(BC.CtxMutex);
1494+
std::unique_lock<llvm::sys::RWMutex> Lock(BC.CtxMutex);
14941495
Label = BC.Ctx->createNamedTempSymbol("BB");
14951496
}
14961497
auto BB =

bolt/include/bolt/Passes/Aligner.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define BOLT_PASSES_ALIGNER_H
1717

1818
#include "bolt/Passes/BinaryPasses.h"
19+
#include "llvm/Support/RWMutex.h"
1920

2021
namespace llvm {
2122
namespace bolt {
@@ -24,7 +25,7 @@ class AlignerPass : public BinaryFunctionPass {
2425
private:
2526
/// Stats for usage of max bytes for basic block alignment.
2627
std::vector<uint32_t> AlignHistogram;
27-
std::shared_timed_mutex AlignHistogramMtx;
28+
llvm::sys::RWMutex AlignHistogramMtx;
2829

2930
/// Stats: execution count of blocks that were aligned.
3031
std::atomic<uint64_t> AlignedBlocksCount{0};

bolt/include/bolt/Passes/Instrumentation.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "bolt/Passes/BinaryPasses.h"
2121
#include "bolt/Passes/InstrumentationSummary.h"
22+
#include "llvm/Support/RWMutex.h"
2223

2324
namespace llvm {
2425
namespace bolt {
@@ -109,7 +110,7 @@ class Instrumentation : public BinaryFunctionPass {
109110
/// strtab indices in StringTable for each function name
110111
std::unordered_map<const BinaryFunction *, uint32_t> FuncToStringIdx;
111112

112-
mutable std::shared_timed_mutex FDMutex;
113+
mutable llvm::sys::RWMutex FDMutex;
113114

114115
/// The data generated during Instrumentation pass that needs to
115116
/// be passed to the Instrument runtime library.

bolt/lib/Core/BinaryBasicBlock.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ void BinaryBasicBlock::addBranchInstruction(const BinaryBasicBlock *Successor) {
491491
assert(isSuccessor(Successor));
492492
BinaryContext &BC = Function->getBinaryContext();
493493
MCInst NewInst;
494-
std::unique_lock<std::shared_timed_mutex> Lock(BC.CtxMutex);
494+
std::unique_lock<llvm::sys::RWMutex> Lock(BC.CtxMutex);
495495
BC.MIB->createUncondBranch(NewInst, Successor->getLabel(), BC.Ctx.get());
496496
Instructions.emplace_back(std::move(NewInst));
497497
}

bolt/lib/Core/BinaryContext.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,9 +1316,8 @@ void BinaryContext::foldFunction(BinaryFunction &ChildBF,
13161316
assert(!ChildBF.isMultiEntry() && !ParentBF.isMultiEntry() &&
13171317
"cannot merge functions with multiple entry points");
13181318

1319-
std::unique_lock<std::shared_timed_mutex> WriteCtxLock(CtxMutex,
1320-
std::defer_lock);
1321-
std::unique_lock<std::shared_timed_mutex> WriteSymbolMapLock(
1319+
std::unique_lock<llvm::sys::RWMutex> WriteCtxLock(CtxMutex, std::defer_lock);
1320+
std::unique_lock<llvm::sys::RWMutex> WriteSymbolMapLock(
13221321
SymbolToFunctionMapMutex, std::defer_lock);
13231322

13241323
const StringRef ChildName = ChildBF.getOneName();
@@ -1343,10 +1342,10 @@ void BinaryContext::foldFunction(BinaryFunction &ChildBF,
13431342
// continue to exist and either one can be executed.
13441343
ChildBF.mergeProfileDataInto(ParentBF);
13451344

1346-
std::shared_lock<std::shared_timed_mutex> ReadBfsLock(BinaryFunctionsMutex,
1347-
std::defer_lock);
1348-
std::unique_lock<std::shared_timed_mutex> WriteBfsLock(BinaryFunctionsMutex,
1349-
std::defer_lock);
1345+
std::shared_lock<llvm::sys::RWMutex> ReadBfsLock(BinaryFunctionsMutex,
1346+
std::defer_lock);
1347+
std::unique_lock<llvm::sys::RWMutex> WriteBfsLock(BinaryFunctionsMutex,
1348+
std::defer_lock);
13501349
// Remove ChildBF from the global set of functions in relocs mode.
13511350
ReadBfsLock.lock();
13521351
auto FI = BinaryFunctions.find(ChildBF.getAddress());
@@ -2157,7 +2156,7 @@ void BinaryContext::markAmbiguousRelocations(BinaryData &BD,
21572156

21582157
BinaryFunction *BinaryContext::getFunctionForSymbol(const MCSymbol *Symbol,
21592158
uint64_t *EntryDesc) {
2160-
std::shared_lock<std::shared_timed_mutex> Lock(SymbolToFunctionMapMutex);
2159+
std::shared_lock<llvm::sys::RWMutex> Lock(SymbolToFunctionMapMutex);
21612160
auto BFI = SymbolToFunctionMap.find(Symbol);
21622161
if (BFI == SymbolToFunctionMap.end())
21632162
return nullptr;

bolt/lib/Core/Exceptions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ void BinaryFunction::updateEHRanges() {
406406
const MCSymbol *EHSymbol;
407407
MCInst EHLabel;
408408
{
409-
std::unique_lock<std::shared_timed_mutex> Lock(BC.CtxMutex);
409+
std::unique_lock<llvm::sys::RWMutex> Lock(BC.CtxMutex);
410410
EHSymbol = BC.Ctx->createNamedTempSymbol("EH");
411411
BC.MIB->createEHLabel(EHLabel, EHSymbol, BC.Ctx.get());
412412
}

bolt/lib/Core/ParallelUtilities.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
#include "bolt/Core/ParallelUtilities.h"
1414
#include "bolt/Core/BinaryContext.h"
1515
#include "bolt/Core/BinaryFunction.h"
16+
#include "llvm/Support/RWMutex.h"
1617
#include "llvm/Support/ThreadPool.h"
1718
#include "llvm/Support/Timer.h"
1819
#include <mutex>
19-
#include <shared_mutex>
2020

2121
#define DEBUG_TYPE "par-utils"
2222

@@ -170,13 +170,13 @@ void runOnEachFunctionWithUniqueAllocId(
170170
if (BC.getBinaryFunctions().size() == 0)
171171
return;
172172

173-
std::shared_timed_mutex MainLock;
173+
llvm::sys::RWMutex MainLock;
174174
auto runBlock = [&](std::map<uint64_t, BinaryFunction>::iterator BlockBegin,
175175
std::map<uint64_t, BinaryFunction>::iterator BlockEnd,
176176
MCPlusBuilder::AllocatorIdTy AllocId) {
177177
Timer T(LogName, LogName);
178178
LLVM_DEBUG(T.startTimer());
179-
std::shared_lock<std::shared_timed_mutex> Lock(MainLock);
179+
std::shared_lock<llvm::sys::RWMutex> Lock(MainLock);
180180
for (auto It = BlockBegin; It != BlockEnd; ++It) {
181181
BinaryFunction &BF = It->second;
182182
if (SkipPredicate && SkipPredicate(BF))
@@ -192,7 +192,7 @@ void runOnEachFunctionWithUniqueAllocId(
192192
return;
193193
}
194194
// This lock is used to postpone task execution
195-
std::unique_lock<std::shared_timed_mutex> Lock(MainLock);
195+
std::unique_lock<llvm::sys::RWMutex> Lock(MainLock);
196196

197197
// Estimate the overall runtime cost using the scheduling policy
198198
const unsigned TotalCost = estimateTotalCost(BC, SkipPredicate, SchedPolicy);

bolt/lib/Passes/Aligner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ void AlignerPass::alignBlocks(BinaryFunction &Function,
143143

144144
// Update stats.
145145
LLVM_DEBUG(
146-
std::unique_lock<std::shared_timed_mutex> Lock(AlignHistogramMtx);
146+
std::unique_lock<llvm::sys::RWMutex> Lock(AlignHistogramMtx);
147147
AlignHistogram[BytesToUse]++;
148148
AlignedBlocksCount += BB->getKnownExecutionCount();
149149
);

bolt/lib/Passes/Instrumentation.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "bolt/RuntimeLibs/InstrumentationRuntimeLibrary.h"
1616
#include "bolt/Utils/Utils.h"
1717
#include "llvm/Support/CommandLine.h"
18+
#include "llvm/Support/RWMutex.h"
1819
#include <stack>
1920

2021
#define DEBUG_TYPE "bolt-instrumentation"
@@ -298,7 +299,7 @@ void Instrumentation::instrumentFunction(BinaryFunction &Function,
298299

299300
FunctionDescription *FuncDesc = nullptr;
300301
{
301-
std::unique_lock<std::shared_timed_mutex> L(FDMutex);
302+
std::unique_lock<llvm::sys::RWMutex> L(FDMutex);
302303
Summary->FunctionDescriptions.emplace_back();
303304
FuncDesc = &Summary->FunctionDescriptions.back();
304305
}

0 commit comments

Comments
 (0)