From 294f3ce5dde916c358d8f672b4a1c706c0387154 Mon Sep 17 00:00:00 2001 From: Paul Kirth Date: Wed, 12 Jun 2024 12:52:28 -0700 Subject: =?UTF-8?q?Reapply=20"[llvm][IR]=20Extend=20BranchWeightMetadata?= =?UTF-8?q?=20to=20track=20provenance=20o=E2=80=A6=20(#95281)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …f weights" #95136 Reverts #95060, and relands #86609, with the unintended code generation changes addressed. This patch implements the changes to LLVM IR discussed in https://discourse.llvm.org/t/rfc-update-branch-weights-metadata-to-allow-tracking-branch-weight-origins/75032 In this patch, we add an optional field to MD_prof meatdata nodes for branch weights, which can be used to distinguish weights added from llvm.expect* intrinsics from those added via other methods, e.g. from profiles or inserted by the compiler. One of the major motivations, is for use with MisExpect diagnostics, which need to know if branch_weight metadata originates from an llvm.expect intrinsic. Without that information, we end up checking branch weights multiple times in the case if ThinLTO + SampleProfiling, leading to some inaccuracy in how we report MisExpect related diagnostics to users. Since we change the format of MD_prof metadata in a fundamental way, we need to update code handling branch weights in a number of places. We also update the lang ref for branch weights to reflect the change. --- llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'llvm/lib/Bitcode/Reader/BitcodeReader.cpp') diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp index c667913005cd..4ad3a2eaceea 100644 --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -57,6 +57,7 @@ #include "llvm/IR/Module.h" #include "llvm/IR/ModuleSummaryIndex.h" #include "llvm/IR/Operator.h" +#include "llvm/IR/ProfDataUtils.h" #include "llvm/IR/Type.h" #include "llvm/IR/Value.h" #include "llvm/IR/Verifier.h" @@ -6951,8 +6952,10 @@ Error BitcodeReader::materialize(GlobalValue *GV) { else continue; // ignore and continue. + unsigned Offset = getBranchWeightOffset(MD); + // If branch weight doesn't match, just strip branch weight. - if (MD->getNumOperands() != 1 + ExpectedNumOperands) + if (MD->getNumOperands() != Offset + ExpectedNumOperands) I.setMetadata(LLVMContext::MD_prof, nullptr); } } -- cgit v1.2.3