Skip to content

[UBSAN] Emit optimization remarks #88304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion llvm/lib/IR/DiagnosticInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,12 @@ DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key,
else if (isa<Constant>(V)) {
raw_string_ostream OS(Val);
V->printAsOperand(OS, /*PrintType=*/false);
} else if (auto *I = dyn_cast<Instruction>(V))
} else if (auto *I = dyn_cast<Instruction>(V)) {
Val = I->getOpcodeName();
} else if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
if (auto *S = dyn_cast<MDString>(MD->getMetadata()))
Val = S->getString();
}
}

DiagnosticInfoOptimizationBase::Argument::Argument(StringRef Key, const Type *T)
Expand Down
46 changes: 41 additions & 5 deletions llvm/lib/Transforms/Instrumentation/LowerAllowCheckPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@

#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
#include "llvm/Analysis/ProfileSummaryInfo.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/Metadata.h"
#include "llvm/Support/RandomNumberGenerator.h"
#include <memory>
#include <random>
Expand All @@ -35,13 +40,41 @@ static cl::opt<float>
STATISTIC(NumChecksTotal, "Number of checks");
STATISTIC(NumChecksRemoved, "Number of removed checks");

struct RemarkInfo {
ore::NV Kind;
ore::NV F;
ore::NV BB;
explicit RemarkInfo(IntrinsicInst *II)
: Kind("Kind", II->getArgOperand(0)),
F("Function", II->getParent()->getParent()),
BB("Block", II->getParent()->getName()) {}
};

static void emitRemark(IntrinsicInst *II, OptimizationRemarkEmitter &ORE,
bool Removed) {
if (Removed) {
ORE.emit([&]() {
RemarkInfo Info(II);
return OptimizationRemark(DEBUG_TYPE, "Removed", II)
<< "Removed check: Kind=" << Info.Kind << " F=" << Info.F
<< " BB=" << Info.BB;
});
} else {
ORE.emit([&]() {
RemarkInfo Info(II);
return OptimizationRemarkMissed(DEBUG_TYPE, "Allowed", II)
<< "Allowed check: Kind=" << Info.Kind << " F=" << Info.F
<< " BB=" << Info.BB;
});
}
}

static bool removeUbsanTraps(Function &F, const BlockFrequencyInfo &BFI,
const ProfileSummaryInfo *PSI) {
const ProfileSummaryInfo *PSI,
OptimizationRemarkEmitter &ORE) {
SmallVector<std::pair<IntrinsicInst *, bool>, 16> ReplaceWithValue;
std::unique_ptr<RandomNumberGenerator> Rng;

// TODO:
// https://github.com/llvm/llvm-project/pull/84858#discussion_r1520603139
auto ShouldRemove = [&](bool IsHot) {
if (!RandomRate.getNumOccurrences())
return IsHot;
Expand Down Expand Up @@ -75,6 +108,7 @@ static bool removeUbsanTraps(Function &F, const BlockFrequencyInfo &BFI,
});
if (ToRemove)
++NumChecksRemoved;
emitRemark(II, ORE, ToRemove);
break;
}
default:
Expand All @@ -99,9 +133,11 @@ PreservedAnalyses LowerAllowCheckPass::run(Function &F,
ProfileSummaryInfo *PSI =
MAMProxy.getCachedResult<ProfileSummaryAnalysis>(*F.getParent());
BlockFrequencyInfo &BFI = AM.getResult<BlockFrequencyAnalysis>(F);
OptimizationRemarkEmitter &ORE =
AM.getResult<OptimizationRemarkEmitterAnalysis>(F);

return removeUbsanTraps(F, BFI, PSI) ? PreservedAnalyses::none()
: PreservedAnalyses::all();
return removeUbsanTraps(F, BFI, PSI, ORE) ? PreservedAnalyses::none()
: PreservedAnalyses::all();
}

bool LowerAllowCheckPass::IsRequested() {
Expand Down
24 changes: 24 additions & 0 deletions llvm/test/Transforms/lower-builtin-allow-check-remarks.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
; RUN: opt < %s -passes='require<profile-summary>,function(lower-allow-check)' -lower-allow-check-random-rate=1 -pass-remarks=lower-allow-check -pass-remarks-missed=lower-allow-check -S 2>&1 | FileCheck %s
; RUN: opt < %s -passes='require<profile-summary>,function(lower-allow-check)' -lower-allow-check-random-rate=0 -pass-remarks=lower-allow-check -pass-remarks-missed=lower-allow-check -S 2>&1 | FileCheck %s --check-prefixes=REMOVE

; CHECK: remark: <unknown>:0:0: Allowed check: Kind=test_check F=test_runtime BB=entry1
; CHECK: remark: <unknown>:0:0: Allowed check: Kind=7 F=test_ubsan BB=entry2

; REMOVE: remark: <unknown>:0:0: Removed check: Kind=test_check F=test_runtime BB=entry1
; REMOVE: remark: <unknown>:0:0: Removed check: Kind=7 F=test_ubsan BB=entry2

target triple = "x86_64-pc-linux-gnu"

define i1 @test_runtime() local_unnamed_addr {
entry1:
%allow = call i1 @llvm.allow.runtime.check(metadata !"test_check")
ret i1 %allow
}

declare i1 @llvm.allow.runtime.check(metadata) nounwind

define i1 @test_ubsan() local_unnamed_addr {
entry2:
%allow = call i1 @llvm.allow.ubsan.check(i8 7)
ret i1 %allow
}