Skip to content

Commit 3901f13

Browse files
authored
[sroa][profcheck] Propagate profile in unfoldGEPSelect (#163318)
Also applied `ProfcheckDisableMetadataFixes`​ to previous (PR #163317) PR.
1 parent 2424118 commit 3901f13

File tree

3 files changed

+53
-13
lines changed

3 files changed

+53
-13
lines changed

llvm/lib/Transforms/Scalar/SROA.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,13 @@ STATISTIC(
118118
STATISTIC(NumDeleted, "Number of instructions deleted");
119119
STATISTIC(NumVectorized, "Number of vectorized aggregates");
120120

121+
namespace llvm {
121122
/// Disable running mem2reg during SROA in order to test or debug SROA.
122123
static cl::opt<bool> SROASkipMem2Reg("sroa-skip-mem2reg", cl::init(false),
123124
cl::Hidden);
125+
extern cl::opt<bool> ProfcheckDisableMetadataFixes;
126+
} // namespace llvm
127+
124128
namespace {
125129

126130
class AllocaSliceRewriter;
@@ -1777,7 +1781,8 @@ static void speculateSelectInstLoads(SelectInst &SI, LoadInst &LI,
17771781
}
17781782

17791783
Value *V = IRB.CreateSelect(SI.getCondition(), TL, FL,
1780-
LI.getName() + ".sroa.speculated", &SI);
1784+
LI.getName() + ".sroa.speculated",
1785+
ProfcheckDisableMetadataFixes ? nullptr : &SI);
17811786

17821787
LLVM_DEBUG(dbgs() << " speculated to: " << *V << "\n");
17831788
LI.replaceAllUsesWith(V);
@@ -4360,10 +4365,13 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
43604365
};
43614366

43624367
Value *Cond, *True, *False;
4368+
Instruction *MDFrom = nullptr;
43634369
if (auto *SI = dyn_cast<SelectInst>(Sel)) {
43644370
Cond = SI->getCondition();
43654371
True = SI->getTrueValue();
43664372
False = SI->getFalseValue();
4373+
if (!ProfcheckDisableMetadataFixes)
4374+
MDFrom = SI;
43674375
} else {
43684376
Cond = Sel->getOperand(0);
43694377
True = ConstantInt::get(Sel->getType(), 1);
@@ -4383,8 +4391,12 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
43834391
IRB.CreateGEP(Ty, FalseOps[0], ArrayRef(FalseOps).drop_front(),
43844392
False->getName() + ".sroa.gep", NW);
43854393

4386-
Value *NSel =
4387-
IRB.CreateSelect(Cond, NTrue, NFalse, Sel->getName() + ".sroa.sel");
4394+
Value *NSel = MDFrom
4395+
? IRB.CreateSelect(Cond, NTrue, NFalse,
4396+
Sel->getName() + ".sroa.sel", MDFrom)
4397+
: IRB.CreateSelectWithUnknownProfile(
4398+
Cond, NTrue, NFalse, DEBUG_TYPE,
4399+
Sel->getName() + ".sroa.sel");
43884400
Visited.erase(&GEPI);
43894401
GEPI.replaceAllUsesWith(NSel);
43904402
GEPI.eraseFromParent();

llvm/test/Transforms/SROA/phi-gep.ll

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals
22
; RUN: opt -S -passes='sroa<preserve-cfg>' < %s | FileCheck %s --check-prefixes=CHECK,CHECK-PRESERVE-CFG
33
; RUN: opt -S -passes='sroa<modify-cfg>' < %s | FileCheck %s --check-prefixes=CHECK,CHECK-MODIFY-CFG
44

55
%pair = type { i32, i32 }
66

7+
;.
8+
; CHECK: @g = global %pair zeroinitializer, align 4
9+
;.
710
define i32 @test_sroa_phi_gep(i1 %cond) {
811
; CHECK-LABEL: @test_sroa_phi_gep(
912
; CHECK-NEXT: entry:
@@ -334,18 +337,18 @@ exit:
334337
unreachable
335338
}
336339

337-
define void @test_sroa_gep_phi_select_same_block(i1 %c1, i1 %c2, ptr %ptr) {
340+
define void @test_sroa_gep_phi_select_same_block(i1 %c1, i1 %c2, ptr %ptr) !prof !0 {
338341
; CHECK-LABEL: @test_sroa_gep_phi_select_same_block(
339342
; CHECK-NEXT: entry:
340343
; CHECK-NEXT: [[ALLOCA:%.*]] = alloca [[PAIR:%.*]], align 8
341344
; CHECK-NEXT: br label [[WHILE_BODY:%.*]]
342345
; CHECK: while.body:
343346
; CHECK-NEXT: [[PHI:%.*]] = phi ptr [ [[ALLOCA]], [[ENTRY:%.*]] ], [ [[SELECT:%.*]], [[WHILE_BODY]] ]
344-
; CHECK-NEXT: [[SELECT]] = select i1 [[C1:%.*]], ptr [[PHI]], ptr [[PTR:%.*]]
347+
; CHECK-NEXT: [[SELECT]] = select i1 [[C1:%.*]], ptr [[PHI]], ptr [[PTR:%.*]], !prof [[PROF1:![0-9]+]]
345348
; CHECK-NEXT: [[PHI_SROA_GEP:%.*]] = getelementptr inbounds [[PAIR]], ptr [[PHI]], i64 1
346349
; CHECK-NEXT: [[PTR_SROA_GEP:%.*]] = getelementptr inbounds [[PAIR]], ptr [[PTR]], i64 1
347-
; CHECK-NEXT: [[SELECT_SROA_SEL:%.*]] = select i1 [[C1]], ptr [[PHI_SROA_GEP]], ptr [[PTR_SROA_GEP]]
348-
; CHECK-NEXT: br i1 [[C2:%.*]], label [[EXIT:%.*]], label [[WHILE_BODY]]
350+
; CHECK-NEXT: [[SELECT_SROA_SEL:%.*]] = select i1 [[C1]], ptr [[PHI_SROA_GEP]], ptr [[PTR_SROA_GEP]], !prof [[PROF1]]
351+
; CHECK-NEXT: br i1 [[C2:%.*]], label [[EXIT:%.*]], label [[WHILE_BODY]], !prof [[PROF2:![0-9]+]]
349352
; CHECK: exit:
350353
; CHECK-NEXT: ret void
351354
;
@@ -355,9 +358,9 @@ entry:
355358

356359
while.body:
357360
%phi = phi ptr [ %alloca, %entry ], [ %select, %while.body ]
358-
%select = select i1 %c1, ptr %phi, ptr %ptr
361+
%select = select i1 %c1, ptr %phi, ptr %ptr, !prof !1
359362
%gep = getelementptr inbounds %pair, ptr %select, i64 1
360-
br i1 %c2, label %exit, label %while.body
363+
br i1 %c2, label %exit, label %while.body, !prof !2
361364

362365
exit:
363366
ret void
@@ -747,6 +750,18 @@ declare ptr @foo()
747750
declare i32 @__gxx_personality_v0(...)
748751

749752
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg)
753+
754+
!0 = !{!"function_entry_count", i32 10}
755+
!1 = !{!"branch_weights", i32 3, i32 5}
756+
!2 = !{!"branch_weights", i32 7, i32 11}
757+
758+
;.
759+
; CHECK: attributes #[[ATTR0:[0-9]+]] = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
760+
;.
761+
; CHECK: [[META0:![0-9]+]] = !{!"function_entry_count", i32 10}
762+
; CHECK: [[PROF1]] = !{!"branch_weights", i32 3, i32 5}
763+
; CHECK: [[PROF2]] = !{!"branch_weights", i32 7, i32 11}
764+
;.
750765
;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
751766
; CHECK-MODIFY-CFG: {{.*}}
752767
; CHECK-PRESERVE-CFG: {{.*}}

llvm/test/Transforms/SROA/select-gep.ll

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals
22
; RUN: opt -S -passes='sroa<preserve-cfg>' < %s | FileCheck %s --check-prefixes=CHECK,CHECK-PRESERVE-CFG
33
; RUN: opt -S -passes='sroa<modify-cfg>' < %s | FileCheck %s --check-prefixes=CHECK,CHECK-MODIFY-CFG
44

@@ -203,10 +203,10 @@ define i32 @test_select_idx_mem2reg(i1 %c) {
203203

204204
; Test gep with a select-like zext index unfolding on an alloca that is
205205
; splittable and promotable.
206-
define i64 @test_select_like_zext_idx_mem2reg(i1 %c) {
206+
define i64 @test_select_like_zext_idx_mem2reg(i1 %c) !prof !0 {
207207
; CHECK-LABEL: @test_select_like_zext_idx_mem2reg(
208208
; CHECK-NEXT: [[IDX:%.*]] = zext i1 [[C:%.*]] to i64
209-
; CHECK-NEXT: [[RES:%.*]] = select i1 [[C]], i64 2, i64 1
209+
; CHECK-NEXT: [[RES:%.*]] = select i1 [[C]], i64 2, i64 1, !prof [[PROF1:![0-9]+]]
210210
; CHECK-NEXT: ret i64 [[RES]]
211211
;
212212
%alloca = alloca [2 x i64], align 8
@@ -352,3 +352,16 @@ define i32 @test_select_idx_not_constant3(i1 %c, ptr %p, i64 %arg) {
352352
%res = load i32, ptr %gep, align 4
353353
ret i32 %res
354354
}
355+
356+
!0 = !{!"function_entry_count", i32 10}
357+
;.
358+
; CHECK-PRESERVE-CFG: attributes #[[ATTR0:[0-9]+]] = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
359+
;.
360+
; CHECK-MODIFY-CFG: attributes #[[ATTR0:[0-9]+]] = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
361+
;.
362+
; CHECK-PRESERVE-CFG: [[META0:![0-9]+]] = !{!"function_entry_count", i32 10}
363+
; CHECK-PRESERVE-CFG: [[PROF1]] = !{!"unknown", !"sroa"}
364+
;.
365+
; CHECK-MODIFY-CFG: [[META0:![0-9]+]] = !{!"function_entry_count", i32 10}
366+
; CHECK-MODIFY-CFG: [[PROF1]] = !{!"unknown", !"sroa"}
367+
;.

0 commit comments

Comments
 (0)