[llvm-branch-commits] [llvm] [ir] MD_prof is not UB-implying (PR #152420)
Mircea Trofin via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Aug 6 17:55:52 PDT 2025
https://github.com/mtrofin created https://github.com/llvm/llvm-project/pull/152420
None
>From 31a3f5c0ec50fc2347207a3bba0d75daaf3f2817 Mon Sep 17 00:00:00 2001
From: Mircea Trofin <mtrofin at google.com>
Date: Wed, 6 Aug 2025 17:43:35 -0700
Subject: [PATCH] [ir] MD_prof is not UB-implying
---
llvm/lib/IR/Instruction.cpp | 10 ++--
.../Transforms/LICM/hoist-phi-metadata.ll | 46 +++++++++++++++++++
2 files changed, 53 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/IR/Instruction.cpp b/llvm/lib/IR/Instruction.cpp
index cc18e9c642799..55a411a61457b 100644
--- a/llvm/lib/IR/Instruction.cpp
+++ b/llvm/lib/IR/Instruction.cpp
@@ -557,9 +557,13 @@ void Instruction::dropUBImplyingAttrsAndMetadata() {
// !range, !nonnull and !align produce poison, so they are safe to speculate.
// !noundef and various AA metadata must be dropped, as it generally produces
// immediate undefined behavior.
- unsigned KnownIDs[] = {LLVMContext::MD_annotation, LLVMContext::MD_range,
- LLVMContext::MD_nonnull, LLVMContext::MD_align};
- dropUBImplyingAttrsAndUnknownMetadata(KnownIDs);
+ static const unsigned KnownIDs[] = {
+ LLVMContext::MD_annotation, LLVMContext::MD_range,
+ LLVMContext::MD_nonnull, LLVMContext::MD_align, LLVMContext::MD_prof};
+ ArrayRef<unsigned> K(KnownIDs);
+ if (ProfcheckDisableMetadataFixes)
+ K = K.drop_back();
+ dropUBImplyingAttrsAndUnknownMetadata(K);
}
bool Instruction::hasUBImplyingAttrs() const {
diff --git a/llvm/test/Transforms/LICM/hoist-phi-metadata.ll b/llvm/test/Transforms/LICM/hoist-phi-metadata.ll
index e98de9c79ea8c..6034d12d931c2 100644
--- a/llvm/test/Transforms/LICM/hoist-phi-metadata.ll
+++ b/llvm/test/Transforms/LICM/hoist-phi-metadata.ll
@@ -45,6 +45,46 @@ end:
ret void
}
+declare i32 @getv()
+
+; indirect.goto.dest2 should get hoisted, and that should not result
+; in a loss of profiling info
+define i32 @test19(i1 %cond, i1 %cond2, ptr %address, i32 %v1) nounwind {
+; CHECK-LABEL: define i32 @test19
+; CHECK-SAME: (i1 [[COND:%.*]], i1 [[COND2:%.*]], ptr [[ADDRESS:%.*]], i32 [[V1:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[INDIRECT_GOTO_DEST:%.*]] = select i1 [[COND]], ptr blockaddress(@test19, [[EXIT:%.*]]), ptr [[ADDRESS]], !prof [[PROF9:![0-9]+]]
+; CHECK-NEXT: [[INDIRECT_GOTO_DEST2:%.*]] = select i1 [[COND2]], ptr blockaddress(@test19, [[EXIT]]), ptr [[ADDRESS]], !prof [[PROF10:![0-9]+]]
+; CHECK-NEXT: br label [[L0:%.*]]
+; CHECK: L0:
+; CHECK-NEXT: [[V2:%.*]] = call i32 @getv()
+; CHECK-NEXT: [[SINKABLE:%.*]] = mul i32 [[V1]], [[V2]]
+; CHECK-NEXT: [[SINKABLE2:%.*]] = add i32 [[V1]], [[V2]]
+; CHECK-NEXT: indirectbr ptr [[INDIRECT_GOTO_DEST]], [label [[L1:%.*]], label %exit]
+; CHECK: L1:
+; CHECK-NEXT: indirectbr ptr [[INDIRECT_GOTO_DEST2]], [label [[L0]], label %exit]
+; CHECK: exit:
+; CHECK-NEXT: [[R:%.*]] = phi i32 [ [[SINKABLE]], [[L0]] ], [ [[SINKABLE2]], [[L1]] ]
+; CHECK-NEXT: ret i32 [[R]]
+;
+entry:
+ br label %L0
+L0:
+ %indirect.goto.dest = select i1 %cond, ptr blockaddress(@test19, %exit), ptr %address, !prof !10
+ %v2 = call i32 @getv()
+ %sinkable = mul i32 %v1, %v2
+ %sinkable2 = add i32 %v1, %v2
+ indirectbr ptr %indirect.goto.dest, [label %L1, label %exit]
+
+L1:
+ %indirect.goto.dest2 = select i1 %cond2, ptr blockaddress(@test19, %exit), ptr %address, !prof !11
+ indirectbr ptr %indirect.goto.dest2, [label %L0, label %exit]
+
+exit:
+ %r = phi i32 [%sinkable, %L0], [%sinkable2, %L1]
+ ret i32 %r
+}
+
!llvm.module.flags = !{!2, !3}
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1)
@@ -57,6 +97,10 @@ end:
!7 = !DILocation(line: 3, column: 22, scope: !4)
!8 = !{!"branch_weights", i32 5, i32 7}
!9 = !{!"branch_weights", i32 13, i32 11}
+!10 = !{!"branch_weights", i32 101, i32 189}
+!11 = !{!"branch_weights", i32 67, i32 1}
+;.
+; CHECK: attributes #[[ATTR0]] = { nounwind }
;.
; CHECK: [[META0:![0-9]+]] = !{i32 7, !"Dwarf Version", i32 5}
; CHECK: [[META1:![0-9]+]] = !{i32 2, !"Debug Info Version", i32 3}
@@ -67,4 +111,6 @@ end:
; CHECK: [[PROF6]] = !{!"branch_weights", i32 5, i32 7}
; CHECK: [[DBG7]] = !DILocation(line: 3, column: 22, scope: [[META3]])
; CHECK: [[PROF8]] = !{!"branch_weights", i32 13, i32 11}
+; CHECK: [[PROF9]] = !{!"branch_weights", i32 101, i32 189}
+; CHECK: [[PROF10]] = !{!"branch_weights", i32 67, i32 1}
;.
More information about the llvm-branch-commits
mailing list