diff options
author | Snehasish Kumar <snehasishk@google.com> | 2025-04-17 08:22:19 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-04-17 08:22:19 -0700 |
commit | 2007dcfeb86fe272fcd16283f9bca45292e3c630 (patch) | |
tree | 8fc9c8ff2fc7a1f81ba3252340506a1ba7ecbf7e /llvm/lib/Transforms/Utils/Local.cpp | |
parent | 1467b3b0a86fcd4a6b3225fbafd959ee781b5b5c (diff) | |
download | llvm-2007dcfeb86fe272fcd16283f9bca45292e3c630.zip llvm-2007dcfeb86fe272fcd16283f9bca45292e3c630.tar.gz llvm-2007dcfeb86fe272fcd16283f9bca45292e3c630.tar.bz2 |
Reapply [Metadata] Preserve MD_prof when merging instructions when one is missing. (#135418)
Preserve branch weight metadata when merging instructions if one of the
instructions is missing metadata. This is similar in behaviour to what
we do today for other types of metadata such as mmra, memprof and
callsite metadata.
Also add a legality check when merging prof metadata based on
instruction type. Without this check GVN PRE optimizations result in
prof metadata on phi nodes which break the module verifier.
Build failure caught by
https://lab.llvm.org/buildbot/#/builders/113/builds/6621
```
!9185 = !{!"branch_weights", i32 3912, i32 802}
Wrong number of operands
!9185 = !{!"branch_weights", i32 3912, i32 802}
fatal error: error in backend: Broken module found, compilation aborted!
```
Reverts #134200 with additional changes.
Diffstat (limited to 'llvm/lib/Transforms/Utils/Local.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/Local.cpp | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp index 2f3ea22..b3204da 100644 --- a/llvm/lib/Transforms/Utils/Local.cpp +++ b/llvm/lib/Transforms/Utils/Local.cpp @@ -3353,9 +3353,10 @@ static void combineMetadata(Instruction *K, const Instruction *J, case LLVMContext::MD_invariant_group: // Preserve !invariant.group in K. break; - // Keep empty cases for mmra, memprof, and callsite to prevent them from - // being removed as unknown metadata. The actual merging is handled + // Keep empty cases for prof, mmra, memprof, and callsite to prevent them + // from being removed as unknown metadata. The actual merging is handled // separately below. + case LLVMContext::MD_prof: case LLVMContext::MD_mmra: case LLVMContext::MD_memprof: case LLVMContext::MD_callsite: @@ -3384,10 +3385,6 @@ static void combineMetadata(Instruction *K, const Instruction *J, if (!AAOnly) K->setMetadata(Kind, JMD); break; - case LLVMContext::MD_prof: - if (!AAOnly && DoesKMove) - K->setMetadata(Kind, MDNode::getMergedProfMetadata(KMD, JMD, K, J)); - break; case LLVMContext::MD_noalias_addrspace: if (DoesKMove) K->setMetadata(Kind, @@ -3434,6 +3431,16 @@ static void combineMetadata(Instruction *K, const Instruction *J, K->setMetadata(LLVMContext::MD_callsite, MDNode::getMergedCallsiteMetadata(KCallSite, JCallSite)); } + + // Merge prof metadata. + // Handle separately to support cases where only one instruction has the + // metadata. + auto *JProf = J->getMetadata(LLVMContext::MD_prof); + auto *KProf = K->getMetadata(LLVMContext::MD_prof); + if (!AAOnly && (JProf || KProf)) { + K->setMetadata(LLVMContext::MD_prof, + MDNode::getMergedProfMetadata(KProf, JProf, K, J)); + } } void llvm::combineMetadataForCSE(Instruction *K, const Instruction *J, |