Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/Instrumentation/LowerAllowCheckPass.cpp
35266 views
//===- LowerAllowCheckPass.cpp ----------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/Transforms/Instrumentation/LowerAllowCheckPass.h"910#include "llvm/ADT/SmallVector.h"11#include "llvm/ADT/Statistic.h"12#include "llvm/ADT/StringRef.h"13#include "llvm/Analysis/OptimizationRemarkEmitter.h"14#include "llvm/Analysis/ProfileSummaryInfo.h"15#include "llvm/IR/Constant.h"16#include "llvm/IR/Constants.h"17#include "llvm/IR/DiagnosticInfo.h"18#include "llvm/IR/Instructions.h"19#include "llvm/IR/IntrinsicInst.h"20#include "llvm/IR/Intrinsics.h"21#include "llvm/IR/Metadata.h"22#include "llvm/IR/Module.h"23#include "llvm/Support/RandomNumberGenerator.h"24#include <memory>25#include <random>2627using namespace llvm;2829#define DEBUG_TYPE "lower-allow-check"3031static cl::opt<int>32HotPercentileCutoff("lower-allow-check-percentile-cutoff-hot",33cl::desc("Hot percentile cuttoff."));3435static cl::opt<float>36RandomRate("lower-allow-check-random-rate",37cl::desc("Probability value in the range [0.0, 1.0] of "38"unconditional pseudo-random checks."));3940STATISTIC(NumChecksTotal, "Number of checks");41STATISTIC(NumChecksRemoved, "Number of removed checks");4243struct RemarkInfo {44ore::NV Kind;45ore::NV F;46ore::NV BB;47explicit RemarkInfo(IntrinsicInst *II)48: Kind("Kind", II->getArgOperand(0)),49F("Function", II->getParent()->getParent()),50BB("Block", II->getParent()->getName()) {}51};5253static void emitRemark(IntrinsicInst *II, OptimizationRemarkEmitter &ORE,54bool Removed) {55if (Removed) {56ORE.emit([&]() {57RemarkInfo Info(II);58return OptimizationRemark(DEBUG_TYPE, "Removed", II)59<< "Removed check: Kind=" << Info.Kind << " F=" << Info.F60<< " BB=" << Info.BB;61});62} else {63ORE.emit([&]() {64RemarkInfo Info(II);65return OptimizationRemarkMissed(DEBUG_TYPE, "Allowed", II)66<< "Allowed check: Kind=" << Info.Kind << " F=" << Info.F67<< " BB=" << Info.BB;68});69}70}7172static bool removeUbsanTraps(Function &F, const BlockFrequencyInfo &BFI,73const ProfileSummaryInfo *PSI,74OptimizationRemarkEmitter &ORE) {75SmallVector<std::pair<IntrinsicInst *, bool>, 16> ReplaceWithValue;76std::unique_ptr<RandomNumberGenerator> Rng;7778auto ShouldRemove = [&](bool IsHot) {79if (!RandomRate.getNumOccurrences())80return IsHot;81if (!Rng)82Rng = F.getParent()->createRNG(F.getName());83std::bernoulli_distribution D(RandomRate);84return !D(*Rng);85};8687for (BasicBlock &BB : F) {88for (Instruction &I : BB) {89IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I);90if (!II)91continue;92auto ID = II->getIntrinsicID();93switch (ID) {94case Intrinsic::allow_ubsan_check:95case Intrinsic::allow_runtime_check: {96++NumChecksTotal;9798bool IsHot = false;99if (PSI) {100uint64_t Count = BFI.getBlockProfileCount(&BB).value_or(0);101IsHot = PSI->isHotCountNthPercentile(HotPercentileCutoff, Count);102}103104bool ToRemove = ShouldRemove(IsHot);105ReplaceWithValue.push_back({106II,107ToRemove,108});109if (ToRemove)110++NumChecksRemoved;111emitRemark(II, ORE, ToRemove);112break;113}114default:115break;116}117}118}119120for (auto [I, V] : ReplaceWithValue) {121I->replaceAllUsesWith(ConstantInt::getBool(I->getType(), !V));122I->eraseFromParent();123}124125return !ReplaceWithValue.empty();126}127128PreservedAnalyses LowerAllowCheckPass::run(Function &F,129FunctionAnalysisManager &AM) {130if (F.isDeclaration())131return PreservedAnalyses::all();132auto &MAMProxy = AM.getResult<ModuleAnalysisManagerFunctionProxy>(F);133ProfileSummaryInfo *PSI =134MAMProxy.getCachedResult<ProfileSummaryAnalysis>(*F.getParent());135BlockFrequencyInfo &BFI = AM.getResult<BlockFrequencyAnalysis>(F);136OptimizationRemarkEmitter &ORE =137AM.getResult<OptimizationRemarkEmitterAnalysis>(F);138139return removeUbsanTraps(F, BFI, PSI, ORE) ? PreservedAnalyses::none()140: PreservedAnalyses::all();141}142143bool LowerAllowCheckPass::IsRequested() {144return RandomRate.getNumOccurrences() ||145HotPercentileCutoff.getNumOccurrences();146}147148149