Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 6e64fd8

Browse files
committed
Revert "[SimplifyCFG] propagate branch metadata when creating select"
MemorySanitizer: use-of-uninitialized-value 0x4910e47 in count /mnt/b/sanitizer-buildbot2/sanitizer-x86_64-linux-bootstrap/build/llvm/include/llvm/Support/MathExtras.h:159:12 0x4910e47 in countLeadingZeros<unsigned long> /mnt/b/sanitizer-buildbot2/sanitizer-x86_64-linux-bootstrap/build/llvm/include/llvm/Support/MathExtras.h:183 0x4910e47 in FitWeights /mnt/b/sanitizer-buildbot2/sanitizer-x86_64-linux-bootstrap/build/llvm/lib/Transforms/Utils/SimplifyCFG.cpp:855 0x4910e47 in SimplifyCondBranchToCondBranch /mnt/b/sanitizer-buildbot2/sanitizer-x86_64-linux-bootstrap/build/llvm/lib/Transforms/Utils/SimplifyCFG.cpp:2895 This reverts commit 609f4dd. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@268577 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent a8d416d commit 6e64fd8

File tree

2 files changed

+22
-67
lines changed

2 files changed

+22
-67
lines changed

lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ static void GetBranchWeights(TerminatorInst *TI,
848848
}
849849
}
850850

851-
/// Scale each weight so they all fit in uint32_t.
851+
/// Keep halving the weights until all can fit in uint32_t.
852852
static void FitWeights(MutableArrayRef<uint64_t> Weights) {
853853
uint64_t Max = *std::max_element(Weights.begin(), Weights.end());
854854
if (Max > UINT_MAX) {
@@ -2840,27 +2840,28 @@ static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI,
28402840
PBI->setSuccessor(1, OtherDest);
28412841

28422842
// Update branch weight for PBI.
2843-
MDBuilder MDB(BI->getContext());
28442843
uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
2845-
uint64_t PredCommon, PredOther, SuccCommon, SuccOther;
2846-
bool HasWeights = PBI->extractProfMetadata(PredTrueWeight, PredFalseWeight);
2847-
if (HasWeights)
2848-
HasWeights = BI->extractProfMetadata(SuccTrueWeight, SuccFalseWeight);
2849-
if (HasWeights) {
2850-
PredCommon = PBIOp ? PredFalseWeight : PredTrueWeight;
2851-
PredOther = PBIOp ? PredTrueWeight : PredFalseWeight;
2852-
SuccCommon = BIOp ? SuccFalseWeight : SuccTrueWeight;
2853-
SuccOther = BIOp ? SuccTrueWeight : SuccFalseWeight;
2844+
bool PredHasWeights =
2845+
PBI->extractProfMetadata(PredTrueWeight, PredFalseWeight);
2846+
bool SuccHasWeights =
2847+
BI->extractProfMetadata(SuccTrueWeight, SuccFalseWeight);
2848+
if (PredHasWeights && SuccHasWeights) {
2849+
uint64_t PredCommon = PBIOp ? PredFalseWeight : PredTrueWeight;
2850+
uint64_t PredOther = PBIOp ?PredTrueWeight : PredFalseWeight;
2851+
uint64_t SuccCommon = BIOp ? SuccFalseWeight : SuccTrueWeight;
2852+
uint64_t SuccOther = BIOp ? SuccTrueWeight : SuccFalseWeight;
28542853
// The weight to CommonDest should be PredCommon * SuccTotal +
28552854
// PredOther * SuccCommon.
28562855
// The weight to OtherDest should be PredOther * SuccOther.
28572856
uint64_t NewWeights[2] = {PredCommon * (SuccCommon + SuccOther) +
28582857
PredOther * SuccCommon,
28592858
PredOther * SuccOther};
2859+
// Halve the weights if any of them cannot fit in an uint32_t
28602860
FitWeights(NewWeights);
28612861

28622862
PBI->setMetadata(LLVMContext::MD_prof,
2863-
MDB.createBranchWeights(NewWeights[0], NewWeights[1]));
2863+
MDBuilder(BI->getContext())
2864+
.createBranchWeights(NewWeights[0], NewWeights[1]));
28642865
}
28652866

28662867
// OtherDest may have phi nodes. If so, add an entry from PBI's
@@ -2879,24 +2880,9 @@ static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI,
28792880
Value *PBIV = PN->getIncomingValue(PBBIdx);
28802881
if (BIV != PBIV) {
28812882
// Insert a select in PBI to pick the right value.
2882-
SelectInst *NV = cast<SelectInst>
2883+
Value *NV = cast<SelectInst>
28832884
(Builder.CreateSelect(PBICond, PBIV, BIV, PBIV->getName() + ".mux"));
28842885
PN->setIncomingValue(PBBIdx, NV);
2885-
// Although the select has the same condition as PBI, the original branch
2886-
// weights for PBI do not apply to the new select because the select's
2887-
// 'logical' edges are incoming edges of the phi that is eliminated, not
2888-
// the outgoing edges of PBI.
2889-
if (HasWeights) {
2890-
// The weight to PredCommonDest should be PredCommon * SuccTotal.
2891-
// The weight to PredOtherDest should be PredOther * SuccCommon.
2892-
uint64_t NewWeights[2] = {PredCommon * (SuccCommon + SuccOther),
2893-
PredOther * SuccCommon};
2894-
2895-
FitWeights(NewWeights);
2896-
2897-
NV->setMetadata(LLVMContext::MD_prof,
2898-
MDB.createBranchWeights(NewWeights[0], NewWeights[1]));
2899-
}
29002886
}
29012887
}
29022888

test/Transforms/SimplifyCFG/preserve-branchweights.ll

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -412,48 +412,22 @@ return:
412412
ret i32 %retval.0
413413
}
414414

415-
; The selects should have freshly calculated branch weights.
415+
; The 1st select should have branch weights equal to the 1st branch.
416+
; The 2nd select should have freshly calculated branch weights.
416417

417418
define i32 @SimplifyCondBranchToCondBranch(i1 %cmpa, i1 %cmpb) {
418419
; CHECK-LABEL: @SimplifyCondBranchToCondBranch(
419420
; CHECK-NEXT: block1:
420-
; CHECK-NEXT: [[BRMERGE:%.*]] = or i1 %cmpa, %cmpb
421-
; CHECK-NEXT: [[DOTMUX:%.*]] = select i1 %cmpa, i32 0, i32 2, !prof !12
422-
; CHECK-NEXT: [[OUTVAL:%.*]] = select i1 [[BRMERGE]], i32 [[DOTMUX]], i32 1, !prof !13
421+
; CHECK-NEXT: [[BRMERGE:%.*]] = or i1 %cmpb, %cmpa
422+
; CHECK-NEXT: [[DOTMUX:%.*]] = select i1 %cmpb, i32 0, i32 2
423+
; CHECK-NEXT: [[OUTVAL:%.*]] = select i1 [[BRMERGE]], i32 [[DOTMUX]], i32 1, !prof !12
423424
; CHECK-NEXT: ret i32 [[OUTVAL]]
424425
;
425426
block1:
426-
br i1 %cmpa, label %block3, label %block2, !prof !13
427+
br i1 %cmpb, label %block3, label %block2, !prof !0
427428

428429
block2:
429-
br i1 %cmpb, label %block3, label %exit, !prof !14
430-
431-
block3:
432-
%cowval = phi i32 [ 2, %block2 ], [ 0, %block1 ]
433-
br label %exit
434-
435-
exit:
436-
%outval = phi i32 [ %cowval, %block3 ], [ 1, %block2 ]
437-
ret i32 %outval
438-
}
439-
440-
; Swap the operands of the compares to verify that the weights update correctly.
441-
442-
define i32 @SimplifyCondBranchToCondBranchSwap(i1 %cmpa, i1 %cmpb) {
443-
; CHECK-LABEL: @SimplifyCondBranchToCondBranchSwap(
444-
; CHECK-NEXT: block1:
445-
; CHECK-NEXT: [[CMPA_NOT:%.*]] = xor i1 %cmpa, true
446-
; CHECK-NEXT: [[CMPB_NOT:%.*]] = xor i1 %cmpb, true
447-
; CHECK-NEXT: [[BRMERGE:%.*]] = or i1 [[CMPA_NOT]], [[CMPB_NOT]]
448-
; CHECK-NEXT: [[DOTMUX:%.*]] = select i1 [[CMPA_NOT]], i32 0, i32 2, !prof !14
449-
; CHECK-NEXT: [[OUTVAL:%.*]] = select i1 [[BRMERGE]], i32 [[DOTMUX]], i32 1, !prof !15
450-
; CHECK-NEXT: ret i32 [[OUTVAL]]
451-
;
452-
block1:
453-
br i1 %cmpa, label %block2, label %block3, !prof !13
454-
455-
block2:
456-
br i1 %cmpb, label %exit, label %block3, !prof !14
430+
br i1 %cmpa, label %block3, label %exit, !prof !2
457431

458432
block3:
459433
%cowval = phi i32 [ 2, %block2 ], [ 0, %block1 ]
@@ -478,8 +452,6 @@ exit:
478452
!10 = !{!"branch_weights", i32 672646, i32 21604207}
479453
!11 = !{!"branch_weights", i32 6960, i32 21597248}
480454
!12 = !{!"these_are_not_the_branch_weights_you_are_looking_for", i32 3, i32 5}
481-
!13 = !{!"branch_weights", i32 2, i32 3}
482-
!14 = !{!"branch_weights", i32 4, i32 7}
483455

484456
; CHECK: !0 = !{!"branch_weights", i32 5, i32 11}
485457
; CHECK: !1 = !{!"branch_weights", i32 1, i32 5}
@@ -495,8 +467,5 @@ exit:
495467
;; treat the weight as an unsigned integer.
496468
; CHECK: !10 = !{!"branch_weights", i32 112017436, i32 -735157296}
497469
; CHECK: !11 = !{!"branch_weights", i32 3, i32 5}
498-
; CHECK: !12 = !{!"branch_weights", i32 22, i32 12}
499-
; CHECK: !13 = !{!"branch_weights", i32 34, i32 21}
500-
; CHECK: !14 = !{!"branch_weights", i32 33, i32 14}
501-
; CHECK: !15 = !{!"branch_weights", i32 47, i32 8}
470+
; CHECK: !12 = !{!"branch_weights", i32 14, i32 10}
502471

0 commit comments

Comments
 (0)