Skip to content

Commit 0bb5449

Browse files
committed
[𝘀𝗽𝗿] changes to main this commit is based on
Created using spr 1.3.8-beta.1 [skip ci]
1 parent 0fc05aa commit 0bb5449

File tree

16 files changed

+304
-76
lines changed

16 files changed

+304
-76
lines changed

llvm/include/llvm/IR/Intrinsics.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2853,7 +2853,15 @@ def int_ptrauth_blend :
28532853
def int_ptrauth_sign_generic :
28542854
DefaultAttrsIntrinsic<[llvm_i64_ty], [llvm_i64_ty, llvm_i64_ty], [IntrNoMem]>;
28552855

2856+
//===----------------- AllocToken Intrinsics ------------------------------===//
2857+
2858+
// Return the token ID for the given !alloc_token metadata.
2859+
def int_alloc_token_id :
2860+
DefaultAttrsIntrinsic<[llvm_anyint_ty], [llvm_metadata_ty],
2861+
[IntrNoMem, NoUndef<RetIndex>]>;
2862+
28562863
//===----------------------------------------------------------------------===//
2864+
28572865
//===------- Convergence Intrinsics ---------------------------------------===//
28582866

28592867
def int_experimental_convergence_entry
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//===- llvm/Support/AllocToken.h - Allocation Token Calculation -----*- C++ -*//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Definition of AllocToken modes and shared calculation of stateless token IDs.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_SUPPORT_ALLOCTOKEN_H
14+
#define LLVM_SUPPORT_ALLOCTOKEN_H
15+
16+
#include "llvm/ADT/SmallString.h"
17+
#include <cstdint>
18+
#include <optional>
19+
20+
namespace llvm {
21+
22+
/// Modes for generating allocation token IDs.
23+
enum class AllocTokenMode {
24+
/// Incrementally increasing token ID.
25+
Increment,
26+
27+
/// Simple mode that returns a statically-assigned random token ID.
28+
Random,
29+
30+
/// Token ID based on allocated type hash.
31+
TypeHash,
32+
33+
/// Token ID based on allocated type hash, where the top half ID-space is
34+
/// reserved for types that contain pointers and the bottom half for types
35+
/// that do not contain pointers.
36+
TypeHashPointerSplit,
37+
};
38+
39+
/// The default allocation token mode.
40+
inline constexpr AllocTokenMode DefaultAllocTokenMode =
41+
AllocTokenMode::TypeHashPointerSplit;
42+
43+
/// Metadata about an allocation used to generate a token ID.
44+
struct AllocTokenMetadata {
45+
SmallString<64> TypeName;
46+
bool ContainsPointer;
47+
};
48+
49+
/// Calculates stable allocation token ID. Returns std::nullopt for stateful
50+
/// modes that are only available in the AllocToken pass.
51+
///
52+
/// \param Mode The token generation mode.
53+
/// \param Metadata The metadata about the allocation.
54+
/// \param MaxTokens The maximum number of tokens (must not be 0)
55+
/// \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);
59+
60+
} // end namespace llvm
61+
62+
#endif // LLVM_SUPPORT_ALLOCTOKEN_H

llvm/include/llvm/Transforms/Instrumentation/AllocToken.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616

1717
#include "llvm/IR/Analysis.h"
1818
#include "llvm/IR/PassManager.h"
19+
#include "llvm/Support/AllocToken.h"
1920
#include <optional>
2021

2122
namespace llvm {
2223

2324
class Module;
2425

2526
struct AllocTokenOptions {
27+
AllocTokenMode Mode = DefaultAllocTokenMode;
2628
std::optional<uint64_t> MaxTokens;
2729
bool FastABI = false;
2830
bool Extended = false;

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,38 @@ Expected<MemorySanitizerOptions> parseMSanPassOptions(StringRef Params) {
10951095
return Result;
10961096
}
10971097

1098+
Expected<AllocTokenOptions> parseAllocTokenPassOptions(StringRef Params) {
1099+
AllocTokenOptions Result;
1100+
while (!Params.empty()) {
1101+
StringRef ParamName;
1102+
std::tie(ParamName, Params) = Params.split(';');
1103+
1104+
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)
1113+
Result.Mode = *Mode;
1114+
else
1115+
return make_error<StringError>(
1116+
formatv("invalid argument to AllocToken pass mode "
1117+
"parameter: '{}'",
1118+
ParamName)
1119+
.str(),
1120+
inconvertibleErrorCode());
1121+
} else {
1122+
return make_error<StringError>(
1123+
formatv("invalid AllocToken pass parameter '{}'", ParamName).str(),
1124+
inconvertibleErrorCode());
1125+
}
1126+
}
1127+
return Result;
1128+
}
1129+
10981130
/// Parser of parameters for SimplifyCFG pass.
10991131
Expected<SimplifyCFGOptions> parseSimplifyCFGOptions(StringRef Params) {
11001132
SimplifyCFGOptions Result;

llvm/lib/Passes/PassRegistry.def

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ MODULE_PASS("openmp-opt", OpenMPOptPass())
125125
MODULE_PASS("openmp-opt-postlink",
126126
OpenMPOptPass(ThinOrFullLTOPhase::FullLTOPostLink))
127127
MODULE_PASS("partial-inliner", PartialInlinerPass())
128-
MODULE_PASS("alloc-token", AllocTokenPass())
129128
MODULE_PASS("pgo-icall-prom", PGOIndirectCallPromotion())
130129
MODULE_PASS("pgo-instr-gen", PGOInstrumentationGen())
131130
MODULE_PASS("pgo-instr-use", PGOInstrumentationUse())
@@ -181,6 +180,10 @@ MODULE_PASS("wholeprogramdevirt", WholeProgramDevirtPass())
181180
#ifndef MODULE_PASS_WITH_PARAMS
182181
#define MODULE_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS)
183182
#endif
183+
MODULE_PASS_WITH_PARAMS(
184+
"alloc-token", "AllocTokenPass",
185+
[](AllocTokenOptions Opts) { return AllocTokenPass(Opts); },
186+
parseAllocTokenPassOptions, "mode=<mode>")
184187
MODULE_PASS_WITH_PARAMS(
185188
"asan", "AddressSanitizerPass",
186189
[](AddressSanitizerOptions Opts) { return AddressSanitizerPass(Opts); },

llvm/lib/Support/AllocToken.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===- AllocToken.cpp - Allocation Token Calculation ----------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Definition of AllocToken modes and shared calculation of stateless token IDs.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "llvm/Support/AllocToken.h"
14+
#include "llvm/Support/ErrorHandling.h"
15+
#include "llvm/Support/SipHash.h"
16+
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");
22+
23+
switch (Mode) {
24+
case AllocTokenMode::Increment:
25+
case AllocTokenMode::Random:
26+
// Stateful modes cannot be implemented as a pure function.
27+
return std::nullopt;
28+
29+
case AllocTokenMode::TypeHash: {
30+
return getStableSipHash(Metadata.TypeName) % MaxTokens;
31+
}
32+
33+
case AllocTokenMode::TypeHashPointerSplit: {
34+
if (MaxTokens == 1)
35+
return 0;
36+
const uint64_t HalfTokens = MaxTokens / 2;
37+
uint64_t Hash = getStableSipHash(Metadata.TypeName) % HalfTokens;
38+
if (Metadata.ContainsPointer)
39+
Hash += HalfTokens;
40+
return Hash;
41+
}
42+
}
43+
44+
llvm_unreachable("");
45+
}
46+
} // namespace llvm

llvm/lib/Support/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ add_llvm_component_library(LLVMSupport
147147
ARMBuildAttributes.cpp
148148
AArch64AttributeParser.cpp
149149
AArch64BuildAttributes.cpp
150+
AllocToken.cpp
150151
ARMAttributeParser.cpp
151152
ARMWinEH.cpp
152153
Allocator.cpp

0 commit comments

Comments
 (0)