Skip to content

Commit f1eaf21

Browse files
melverLukacma
authored andcommitted
[AllocToken] Make token mode a pass parameter (llvm#163634)
Refactor the AllocToken pass to accept the mode via pass options rather than LLVM cl::opt. This is both cleaner, but also required to make the mode frontend-driven and avoid potential inconsistencies.
1 parent 49b266b commit f1eaf21

File tree

14 files changed

+61
-26
lines changed

14 files changed

+61
-26
lines changed

llvm/include/llvm/Support/AllocToken.h

Lines changed: 10 additions & 0 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

@@ -36,6 +37,15 @@ enum class AllocTokenMode {
3637
TypeHashPointerSplit,
3738
};
3839

40+
/// The default allocation token mode.
41+
inline constexpr AllocTokenMode DefaultAllocTokenMode =
42+
AllocTokenMode::TypeHashPointerSplit;
43+
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+
3949
/// Metadata about an allocation used to generate a token ID.
4050
struct AllocTokenMetadata {
4151
SmallString<64> TypeName;

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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,31 @@ 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+
if (auto Mode = getAllocTokenModeFromString(ParamName))
1106+
Result.Mode = *Mode;
1107+
else
1108+
return make_error<StringError>(
1109+
formatv("invalid argument to AllocToken pass mode "
1110+
"parameter: '{}'",
1111+
ParamName)
1112+
.str(),
1113+
inconvertibleErrorCode());
1114+
} else {
1115+
return make_error<StringError>(
1116+
formatv("invalid AllocToken pass parameter '{}'", ParamName).str(),
1117+
inconvertibleErrorCode());
1118+
}
1119+
}
1120+
return Result;
1121+
}
1122+
10981123
/// Parser of parameters for SimplifyCFG pass.
10991124
Expected<SimplifyCFGOptions> parseSimplifyCFGOptions(StringRef Params) {
11001125
SimplifyCFGOptions Result;

llvm/lib/Passes/PassRegistry.def

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

llvm/lib/Support/AllocToken.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,22 @@
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

1718
using namespace llvm;
1819

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+
1930
static uint64_t getStableHash(const AllocTokenMetadata &Metadata,
2031
uint64_t MaxTokens) {
2132
return getStableSipHash(Metadata.TypeName) % MaxTokens;

llvm/lib/Transforms/Instrumentation/AllocToken.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,6 @@ namespace {
6363

6464
//===--- Command-line options ---------------------------------------------===//
6565

66-
cl::opt<TokenMode> ClMode(
67-
"alloc-token-mode", cl::Hidden, cl::desc("Token assignment mode"),
68-
cl::init(TokenMode::TypeHashPointerSplit),
69-
cl::values(
70-
clEnumValN(TokenMode::Increment, "increment",
71-
"Incrementally increasing token ID"),
72-
clEnumValN(TokenMode::Random, "random",
73-
"Statically-assigned random token ID"),
74-
clEnumValN(TokenMode::TypeHash, "typehash",
75-
"Token ID based on allocated type hash"),
76-
clEnumValN(
77-
TokenMode::TypeHashPointerSplit, "typehashpointersplit",
78-
"Token ID based on allocated type hash, where the top half "
79-
"ID-space is reserved for types that contain pointers and the "
80-
"bottom half for types that do not contain pointers. ")));
81-
8266
cl::opt<std::string> ClFuncPrefix("alloc-token-prefix",
8367
cl::desc("The allocation function prefix"),
8468
cl::Hidden, cl::init("__alloc_token_"));
@@ -265,7 +249,7 @@ class AllocToken {
265249
: Options(transformOptionsFromCl(std::move(Opts))), Mod(M),
266250
FAM(MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager()),
267251
Mode(IncrementMode(*IntPtrTy, *Options.MaxTokens)) {
268-
switch (ClMode.getValue()) {
252+
switch (Options.Mode) {
269253
case TokenMode::Increment:
270254
break;
271255
case TokenMode::Random:

llvm/test/Instrumentation/AllocToken/basic.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2-
; RUN: opt < %s -passes=inferattrs,alloc-token -alloc-token-mode=increment -S | FileCheck %s
2+
; RUN: opt < %s -passes='inferattrs,alloc-token<mode=increment>' -S | FileCheck %s
33

44
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
55

llvm/test/Instrumentation/AllocToken/basic32.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2-
; RUN: opt < %s -passes=inferattrs,alloc-token -alloc-token-mode=increment -S | FileCheck %s
2+
; RUN: opt < %s -passes='inferattrs,alloc-token<mode=increment>' -S | FileCheck %s
33

44
target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32-S128"
55

llvm/test/Instrumentation/AllocToken/fast.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2-
; RUN: opt < %s -passes=inferattrs,alloc-token -alloc-token-mode=increment -alloc-token-fast-abi -alloc-token-max=3 -S | FileCheck %s
2+
; RUN: opt < %s -passes='inferattrs,alloc-token<mode=increment>' -alloc-token-fast-abi -alloc-token-max=3 -S | FileCheck %s
33

44
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
55

llvm/test/Instrumentation/AllocToken/intrinsic.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
22
; Test that the alloc-token pass lowers the intrinsic to a constant token ID.
33
;
4-
; RUN: opt < %s -passes=alloc-token -alloc-token-mode=typehashpointersplit -alloc-token-max=2 -S | FileCheck %s
4+
; RUN: opt < %s -passes='alloc-token<mode=typehashpointersplit>' -alloc-token-max=2 -S | FileCheck %s
55

66
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
77
target triple = "x86_64-unknown-linux-gnu"

0 commit comments

Comments
 (0)