Skip to content

Commit c7274fc

Browse files
authored
[AllocToken] Introduce AllocToken instrumentation pass (#156838)
Introduce `AllocToken`, an instrumentation pass designed to provide tokens to memory allocators enabling various heap organization strategies, such as heap partitioning. Initially, the pass instruments functions marked with a new attribute `sanitize_alloc_token` by rewriting allocation calls to include a token ID, appended as a function argument with the default ABI. The design aims to provide a flexible framework for implementing different token generation schemes. It currently supports the following token modes: - TypeHash (default): token IDs based on a hash of the allocated type - Random: statically-assigned pseudo-random token IDs - Increment: incrementing token IDs per TU For the `TypeHash` mode introduce support for `!alloc_token` metadata: the metadata can be attached to allocation calls to provide richer semantic information to be consumed by the AllocToken pass. Optimization remarks can be enabled to show where no metadata was available. An alternative "fast ABI" is provided, where instead of passing the token ID as an argument (e.g., `__alloc_token_malloc(size, id)`), the token ID is directly encoded into the name of the called function (e.g., `__alloc_token_0_malloc(size)`). Where the maximum tokens is small, this offers more efficient instrumentation by avoiding the overhead of passing an additional argument at each allocation site. Link: https://discourse.llvm.org/t/rfc-a-framework-for-allocator-partitioning-hints/87434 [1] --- This change is part of the following series: 1. llvm/llvm-project#160131 2. llvm/llvm-project#156838 3. llvm/llvm-project#162098 4. llvm/llvm-project#162099 5. llvm/llvm-project#156839 6. llvm/llvm-project#156840 7. llvm/llvm-project#156841 8. llvm/llvm-project#156842
1 parent 646bc09 commit c7274fc

File tree

15 files changed

+1038
-0
lines changed

15 files changed

+1038
-0
lines changed

llvm/docs/ReleaseNotes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ Changes to Sanitizers
177177
Other Changes
178178
-------------
179179

180+
* Introduces the `AllocToken` pass, an instrumentation pass providing tokens to
181+
memory allocators enabling various heap organization strategies, such as heap
182+
partitioning.
183+
180184
External Open Source Projects Using LLVM {{env.config.release}}
181185
===============================================================
182186

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===- AllocToken.h - Allocation token instrumentation --------------------===//
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+
// This file declares the AllocTokenPass, an instrumentation pass that
10+
// replaces allocation calls with ones including an allocation token.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_TRANSFORMS_INSTRUMENTATION_ALLOCTOKEN_H
15+
#define LLVM_TRANSFORMS_INSTRUMENTATION_ALLOCTOKEN_H
16+
17+
#include "llvm/IR/Analysis.h"
18+
#include "llvm/IR/PassManager.h"
19+
#include <optional>
20+
21+
namespace llvm {
22+
23+
class Module;
24+
25+
struct AllocTokenOptions {
26+
std::optional<uint64_t> MaxTokens;
27+
bool FastABI = false;
28+
bool Extended = false;
29+
AllocTokenOptions() = default;
30+
};
31+
32+
/// A module pass that rewrites heap allocations to use token-enabled
33+
/// allocation functions based on various source-level properties.
34+
class AllocTokenPass : public PassInfoMixin<AllocTokenPass> {
35+
public:
36+
LLVM_ABI explicit AllocTokenPass(AllocTokenOptions Opts = {});
37+
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
38+
static bool isRequired() { return true; }
39+
40+
private:
41+
const AllocTokenOptions Options;
42+
};
43+
44+
} // namespace llvm
45+
46+
#endif // LLVM_TRANSFORMS_INSTRUMENTATION_ALLOCTOKEN_H

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@
240240
#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
241241
#include "llvm/Transforms/InstCombine/InstCombine.h"
242242
#include "llvm/Transforms/Instrumentation/AddressSanitizer.h"
243+
#include "llvm/Transforms/Instrumentation/AllocToken.h"
243244
#include "llvm/Transforms/Instrumentation/BoundsChecking.h"
244245
#include "llvm/Transforms/Instrumentation/CGProfile.h"
245246
#include "llvm/Transforms/Instrumentation/ControlHeightReduction.h"

llvm/lib/Passes/PassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ 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())
128129
MODULE_PASS("pgo-icall-prom", PGOIndirectCallPromotion())
129130
MODULE_PASS("pgo-instr-gen", PGOInstrumentationGen())
130131
MODULE_PASS("pgo-instr-use", PGOInstrumentationUse())

0 commit comments

Comments
 (0)