Index: llvm/lib/Transforms/Scalar/JumpThreading.cpp =================================================================== --- llvm/lib/Transforms/Scalar/JumpThreading.cpp +++ llvm/lib/Transforms/Scalar/JumpThreading.cpp @@ -2728,6 +2728,9 @@ auto *BI = BranchInst::Create(NewBB, BB, SI->getCondition(), Pred); BI->applyMergedLocation(PredTerm->getDebugLoc(), SI->getDebugLoc()); BI->copyMetadata(*SI, {LLVMContext::MD_prof}); + // Also preserve the metadata from the branch + BI->copyMetadata(*PredTerm, + {LLVMContext::MD_loop, LLVMContext::MD_annotation}); SIUse->setIncomingValue(Idx, SI->getFalseValue()); SIUse->addIncoming(SI->getTrueValue(), NewBB); Index: llvm/test/Transforms/JumpThreading/preserve-select-unfold.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/JumpThreading/preserve-select-unfold.ll @@ -0,0 +1,40 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals +; RUN: opt -passes=jump-threading -S < %s | FileCheck %s +; Ensure that we preserve llvm.loop information +define spir_kernel void @test(i1 %Input) { +; CHECK-LABEL: @test( +; CHECK-NEXT: entry: +; CHECK-NEXT: br label [[A:%.*]] +; CHECK: select.unfold: +; CHECK-NEXT: br label [[A]], !llvm.loop [[LOOP0:![0-9]+]] +; CHECK: A: +; CHECK-NEXT: [[CURRENT_STATE_0_I_I:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ 0, [[M:%.*]] ], [ 2, [[SELECT_UNFOLD:%.*]] ] +; CHECK-NEXT: [[B:%.*]] = icmp eq i32 [[CURRENT_STATE_0_I_I]], 0 +; CHECK-NEXT: br i1 [[B]], label [[M]], label [[B2:%.*]] +; CHECK: B2: +; CHECK-NEXT: ret void +; CHECK: M: +; CHECK-NEXT: br i1 [[INPUT:%.*]], label [[SELECT_UNFOLD]], label [[A]], !llvm.loop [[LOOP0]] +; +entry: + br label %A + +A: + %current_state.0.i.i = phi i32 [ 0, %entry ], [ %X, %M ] + %B = icmp eq i32 %current_state.0.i.i, 0 + br i1 %B, label %M, label %B2 + +B2: + ret void + +M: + %X = select i1 %Input, i32 2, i32 0 + br label %A, !llvm.loop !0 +} + +!0 = distinct !{!0, !1} +!1 = !{!"llvm.loop.unroll.disable"} +;. +; CHECK: [[LOOP0]] = distinct !{!0, !1} +; CHECK: [[META1:![0-9]+]] = !{!"llvm.loop.unroll.disable"} +;.