Skip to content

Commit 19e3791

Browse files
committed
[𝘀𝗽𝗿] changes introduced through rebase
Created using spr 1.3.8-beta.1 [skip ci]
1 parent b68690b commit 19e3791

File tree

4 files changed

+40
-28
lines changed

4 files changed

+40
-28
lines changed

llvm/include/llvm/Support/AllocToken.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define LLVM_SUPPORT_ALLOCTOKEN_H
1515

1616
#include "llvm/ADT/SmallString.h"
17+
#include "llvm/ADT/StringRef.h"
1718
#include <cstdint>
1819
#include <optional>
1920

@@ -40,6 +41,11 @@ enum class AllocTokenMode {
4041
inline constexpr AllocTokenMode DefaultAllocTokenMode =
4142
AllocTokenMode::TypeHashPointerSplit;
4243

44+
/// Returns the AllocTokenMode from its canonical string name; if an invalid
45+
/// name was provided returns nullopt.
46+
LLVM_ABI std::optional<AllocTokenMode>
47+
getAllocTokenModeFromString(StringRef Name);
48+
4349
/// Metadata about an allocation used to generate a token ID.
4450
struct AllocTokenMetadata {
4551
SmallString<64> TypeName;
@@ -53,9 +59,9 @@ struct AllocTokenMetadata {
5359
/// \param Metadata The metadata about the allocation.
5460
/// \param MaxTokens The maximum number of tokens (must not be 0)
5561
/// \return The calculated allocation token ID, or std::nullopt.
56-
std::optional<uint64_t> getAllocTokenHash(AllocTokenMode Mode,
57-
const AllocTokenMetadata &Metadata,
58-
uint64_t MaxTokens);
62+
LLVM_ABI std::optional<uint64_t>
63+
getAllocToken(AllocTokenMode Mode, const AllocTokenMetadata &Metadata,
64+
uint64_t MaxTokens);
5965

6066
} // end namespace llvm
6167

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,14 +1102,7 @@ Expected<AllocTokenOptions> parseAllocTokenPassOptions(StringRef Params) {
11021102
std::tie(ParamName, Params) = Params.split(';');
11031103

11041104
if (ParamName.consume_front("mode=")) {
1105-
auto Mode = StringSwitch<std::optional<AllocTokenMode>>(ParamName)
1106-
.Case("increment", AllocTokenMode::Increment)
1107-
.Case("random", AllocTokenMode::Random)
1108-
.Case("typehash", AllocTokenMode::TypeHash)
1109-
.Case("typehashpointersplit",
1110-
AllocTokenMode::TypeHashPointerSplit)
1111-
.Default(std::nullopt);
1112-
if (Mode)
1105+
if (auto Mode = getAllocTokenModeFromString(ParamName))
11131106
Result.Mode = *Mode;
11141107
else
11151108
return make_error<StringError>(

llvm/lib/Support/AllocToken.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,46 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "llvm/Support/AllocToken.h"
14+
#include "llvm/ADT/StringSwitch.h"
1415
#include "llvm/Support/ErrorHandling.h"
1516
#include "llvm/Support/SipHash.h"
1617

17-
namespace llvm {
18-
std::optional<uint64_t> getAllocTokenHash(AllocTokenMode Mode,
19-
const AllocTokenMetadata &Metadata,
20-
uint64_t MaxTokens) {
21-
assert(MaxTokens && "Must provide concrete max tokens");
18+
using namespace llvm;
19+
20+
std::optional<AllocTokenMode>
21+
llvm::getAllocTokenModeFromString(StringRef Name) {
22+
return StringSwitch<std::optional<AllocTokenMode>>(Name)
23+
.Case("increment", AllocTokenMode::Increment)
24+
.Case("random", AllocTokenMode::Random)
25+
.Case("typehash", AllocTokenMode::TypeHash)
26+
.Case("typehashpointersplit", AllocTokenMode::TypeHashPointerSplit)
27+
.Default(std::nullopt);
28+
}
29+
30+
static uint64_t getStableHash(const AllocTokenMetadata &Metadata,
31+
uint64_t MaxTokens) {
32+
return getStableSipHash(Metadata.TypeName) % MaxTokens;
33+
}
34+
35+
std::optional<uint64_t> llvm::getAllocToken(AllocTokenMode Mode,
36+
const AllocTokenMetadata &Metadata,
37+
uint64_t MaxTokens) {
38+
assert(MaxTokens && "Must provide non-zero max tokens");
2239

2340
switch (Mode) {
2441
case AllocTokenMode::Increment:
2542
case AllocTokenMode::Random:
2643
// Stateful modes cannot be implemented as a pure function.
2744
return std::nullopt;
2845

29-
case AllocTokenMode::TypeHash: {
30-
return getStableSipHash(Metadata.TypeName) % MaxTokens;
31-
}
46+
case AllocTokenMode::TypeHash:
47+
return getStableHash(Metadata, MaxTokens);
3248

3349
case AllocTokenMode::TypeHashPointerSplit: {
3450
if (MaxTokens == 1)
3551
return 0;
3652
const uint64_t HalfTokens = MaxTokens / 2;
37-
uint64_t Hash = getStableSipHash(Metadata.TypeName) % HalfTokens;
53+
uint64_t Hash = getStableHash(Metadata, HalfTokens);
3854
if (Metadata.ContainsPointer)
3955
Hash += HalfTokens;
4056
return Hash;
@@ -43,4 +59,3 @@ std::optional<uint64_t> getAllocTokenHash(AllocTokenMode Mode,
4359

4460
llvm_unreachable("");
4561
}
46-
} // namespace llvm

llvm/lib/Transforms/Instrumentation/AllocToken.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@ class TypeHashMode : public ModeBase {
189189
if (MDNode *N = getAllocTokenMetadata(CB)) {
190190
MDString *S = cast<MDString>(N->getOperand(0));
191191
AllocTokenMetadata Metadata{S->getString(), containsPointer(N)};
192-
if (auto Token =
193-
getAllocTokenHash(TokenMode::TypeHash, Metadata, MaxTokens))
192+
if (auto Token = getAllocToken(TokenMode::TypeHash, Metadata, MaxTokens))
194193
return *Token;
195194
}
196195
// Fallback.
@@ -222,8 +221,8 @@ class TypeHashPointerSplitMode : public TypeHashMode {
222221
if (MDNode *N = getAllocTokenMetadata(CB)) {
223222
MDString *S = cast<MDString>(N->getOperand(0));
224223
AllocTokenMetadata Metadata{S->getString(), containsPointer(N)};
225-
if (auto Token = getAllocTokenHash(TokenMode::TypeHashPointerSplit,
226-
Metadata, MaxTokens))
224+
if (auto Token = getAllocToken(TokenMode::TypeHashPointerSplit, Metadata,
225+
MaxTokens))
227226
return *Token;
228227
}
229228
// Pick the fallback token (ClFallbackToken), which by default is 0, meaning
@@ -357,9 +356,8 @@ bool AllocToken::instrumentFunction(Function &F) {
357356
}
358357

359358
if (!IntrinsicInsts.empty()) {
360-
for (auto *II : IntrinsicInsts) {
359+
for (auto *II : IntrinsicInsts)
361360
replaceIntrinsicInst(II, ORE);
362-
}
363361
Modified = true;
364362
NumFunctionsModified++;
365363
}
@@ -381,7 +379,7 @@ AllocToken::shouldInstrumentCall(const CallBase &CB,
381379
if (TLI.getLibFunc(*Callee, Func)) {
382380
if (isInstrumentableLibFunc(Func, CB, TLI))
383381
return Func;
384-
} else if (Options.Extended && getAllocTokenMetadata(CB)) {
382+
} else if (Options.Extended && CB.getMetadata(LLVMContext::MD_alloc_token)) {
385383
return NotLibFunc;
386384
}
387385

0 commit comments

Comments
 (0)