|
| 1 | +//===- RemoveTrapsPass.cpp --------------------------------------*- 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 | +#include "llvm/Transforms/Instrumentation/RemoveTrapsPass.h" |
| 10 | + |
| 11 | +#include "llvm/ADT/SmallVector.h" |
| 12 | +#include "llvm/ADT/Statistic.h" |
| 13 | +#include "llvm/Analysis/ProfileSummaryInfo.h" |
| 14 | +#include "llvm/IR/Instructions.h" |
| 15 | +#include "llvm/IR/IntrinsicInst.h" |
| 16 | +#include "llvm/IR/Intrinsics.h" |
| 17 | +#include "llvm/Support/RandomNumberGenerator.h" |
| 18 | +#include <memory> |
| 19 | +#include <random> |
| 20 | + |
| 21 | +using namespace llvm; |
| 22 | + |
| 23 | +#define DEBUG_TYPE "remove-traps" |
| 24 | + |
| 25 | +static cl::opt<int> HotPercentileCutoff( |
| 26 | + "remove-traps-percentile-cutoff-hot", cl::init(0), |
| 27 | + cl::desc("Alternative hot percentile cuttoff. By default " |
| 28 | + "`-profile-summary-cutoff-hot` is used.")); |
| 29 | + |
| 30 | +static cl::opt<float> |
| 31 | + RandomRate("remove-traps-random-rate", cl::init(0.0), |
| 32 | + cl::desc("Probability value in the range [0.0, 1.0] of " |
| 33 | + "unconditional pseudo-random checks removal.")); |
| 34 | + |
| 35 | +STATISTIC(NumChecksTotal, "Number of checks"); |
| 36 | +STATISTIC(NumChecksRemoved, "Number of removed checks"); |
| 37 | + |
| 38 | +static bool removeUbsanTraps(Function &F, const BlockFrequencyInfo &BFI, |
| 39 | + const ProfileSummaryInfo *PSI) { |
| 40 | + SmallVector<IntrinsicInst *, 16> Remove; |
| 41 | + std::unique_ptr<RandomNumberGenerator> Rng; |
| 42 | + |
| 43 | + auto ShouldRemove = [&](bool IsHot) { |
| 44 | + if (!RandomRate.getNumOccurrences()) |
| 45 | + return IsHot; |
| 46 | + if (!Rng) |
| 47 | + Rng = F.getParent()->createRNG(F.getName()); |
| 48 | + std::bernoulli_distribution D(RandomRate); |
| 49 | + return D(*Rng); |
| 50 | + }; |
| 51 | + |
| 52 | + for (BasicBlock &BB : F) { |
| 53 | + for (Instruction &I : BB) { |
| 54 | + IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I); |
| 55 | + if (!II) |
| 56 | + continue; |
| 57 | + auto ID = II->getIntrinsicID(); |
| 58 | + switch (ID) { |
| 59 | + case Intrinsic::ubsantrap: { |
| 60 | + ++NumChecksTotal; |
| 61 | + |
| 62 | + bool IsHot = false; |
| 63 | + if (PSI) { |
| 64 | + uint64_t Count = 0; |
| 65 | + for (const auto *PR : predecessors(&BB)) |
| 66 | + Count += BFI.getBlockProfileCount(PR).value_or(0); |
| 67 | + |
| 68 | + IsHot = |
| 69 | + HotPercentileCutoff.getNumOccurrences() |
| 70 | + ? (HotPercentileCutoff > 0 && |
| 71 | + PSI->isHotCountNthPercentile(HotPercentileCutoff, Count)) |
| 72 | + : PSI->isHotCount(Count); |
| 73 | + } |
| 74 | + |
| 75 | + if (ShouldRemove(IsHot)) { |
| 76 | + Remove.push_back(II); |
| 77 | + ++NumChecksRemoved; |
| 78 | + } |
| 79 | + break; |
| 80 | + } |
| 81 | + default: |
| 82 | + break; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + for (IntrinsicInst *I : Remove) |
| 88 | + I->eraseFromParent(); |
| 89 | + |
| 90 | + return !Remove.empty(); |
| 91 | +} |
| 92 | + |
| 93 | +PreservedAnalyses RemoveTrapsPass::run(Function &F, |
| 94 | + FunctionAnalysisManager &AM) { |
| 95 | + if (F.isDeclaration()) |
| 96 | + return PreservedAnalyses::all(); |
| 97 | + auto &MAMProxy = AM.getResult<ModuleAnalysisManagerFunctionProxy>(F); |
| 98 | + ProfileSummaryInfo *PSI = |
| 99 | + MAMProxy.getCachedResult<ProfileSummaryAnalysis>(*F.getParent()); |
| 100 | + BlockFrequencyInfo &BFI = AM.getResult<BlockFrequencyAnalysis>(F); |
| 101 | + |
| 102 | + return removeUbsanTraps(F, BFI, PSI) ? PreservedAnalyses::none() |
| 103 | + : PreservedAnalyses::all(); |
| 104 | +} |
0 commit comments