[llvm] [UBSAN] Emit optimization remarks (PR #88304)
Vitaly Buka via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 10 12:41:30 PDT 2024
https://github.com/vitalybuka updated https://github.com/llvm/llvm-project/pull/88304
>From bc01fb28bd5377bca37788a15c852d4a1078c820 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Wed, 10 Apr 2024 11:22:04 -0700
Subject: [PATCH 1/2] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20in?=
=?UTF-8?q?itial=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.4
---
llvm/lib/IR/DiagnosticInfo.cpp | 6 +++-
.../Instrumentation/LowerAllowCheckPass.cpp | 32 +++++++++++++++++--
2 files changed, 34 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/IR/DiagnosticInfo.cpp b/llvm/lib/IR/DiagnosticInfo.cpp
index 342c4cbbc39d65..31971b179fb4be 100644
--- a/llvm/lib/IR/DiagnosticInfo.cpp
+++ b/llvm/lib/IR/DiagnosticInfo.cpp
@@ -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)
diff --git a/llvm/lib/Transforms/Instrumentation/LowerAllowCheckPass.cpp b/llvm/lib/Transforms/Instrumentation/LowerAllowCheckPass.cpp
index cdc8318f088c27..6c5a20227d0a36 100644
--- a/llvm/lib/Transforms/Instrumentation/LowerAllowCheckPass.cpp
+++ b/llvm/lib/Transforms/Instrumentation/LowerAllowCheckPass.cpp
@@ -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>
@@ -35,8 +40,26 @@ static cl::opt<float>
STATISTIC(NumChecksTotal, "Number of checks");
STATISTIC(NumChecksRemoved, "Number of removed checks");
+static void emitRemark(IntrinsicInst *II, OptimizationRemarkEmitter &ORE,
+ bool Removed) {
+ ore::NV Kind("Kind", II->getArgOperand(0));
+ ore::NV BB("Block", II->getParent()->getName());
+ if (Removed) {
+ ORE.emit([&]() {
+ return OptimizationRemark(DEBUG_TYPE, "Removed", II)
+ << "Removed check: Kind=" << Kind << " BB=" << BB;
+ });
+ } else {
+ ORE.emit([&]() {
+ return OptimizationRemarkMissed(DEBUG_TYPE, "Allowed", II)
+ << "Allowed check: Kind=" << Kind << " BB=" << 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;
@@ -75,6 +98,7 @@ static bool removeUbsanTraps(Function &F, const BlockFrequencyInfo &BFI,
});
if (ToRemove)
++NumChecksRemoved;
+ emitRemark(II, ORE, ToRemove);
break;
}
default:
@@ -99,9 +123,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() {
>From e84163b9f2e5489c9f154693b91602626ad7487d Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Wed, 10 Apr 2024 12:41:15 -0700
Subject: [PATCH 2/2] update
Created using spr 1.3.4
---
.../Instrumentation/LowerAllowCheckPass.cpp | 22 ++++++++++++++-----
1 file changed, 16 insertions(+), 6 deletions(-)
diff --git a/llvm/lib/Transforms/Instrumentation/LowerAllowCheckPass.cpp b/llvm/lib/Transforms/Instrumentation/LowerAllowCheckPass.cpp
index 6c5a20227d0a36..465fa41b6c6630 100644
--- a/llvm/lib/Transforms/Instrumentation/LowerAllowCheckPass.cpp
+++ b/llvm/lib/Transforms/Instrumentation/LowerAllowCheckPass.cpp
@@ -40,19 +40,31 @@ 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) {
- ore::NV Kind("Kind", II->getArgOperand(0));
- ore::NV BB("Block", II->getParent()->getName());
if (Removed) {
ORE.emit([&]() {
+ RemarkInfo Info(II);
return OptimizationRemark(DEBUG_TYPE, "Removed", II)
- << "Removed check: Kind=" << Kind << " BB=" << BB;
+ << "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=" << Kind << " BB=" << BB;
+ << "Allowed check: Kind=" << Info.Kind << " F=" << Info.F
+ << " BB=" << Info.BB;
});
}
}
@@ -63,8 +75,6 @@ static bool removeUbsanTraps(Function &F, const BlockFrequencyInfo &BFI,
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;
More information about the llvm-commits
mailing list