Skip to content

Commit 0b24571

Browse files
committed
[𝘀𝗽𝗿] changes to main this commit is based on
Created using spr 1.3.8-beta.1 [skip ci]
1 parent 362de22 commit 0b24571

File tree

3 files changed

+117
-37
lines changed

3 files changed

+117
-37
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Test end-to-end optimization pipeline with PGHO, that it does not interfere
2+
// with other allocation instrumentation features.
3+
//
4+
// RUN: split-file %s %t
5+
// RUN: llvm-profdata merge %t/memprof.yaml -o %t/use.profdata
6+
// RUN: %clang_cc1 -O2 -debug-info-kind=limited -fmemory-profile-use=%t/use.profdata -mllvm -optimize-hot-cold-new \
7+
// RUN: %t/src.cpp -triple x86_64-linux-gnu -emit-llvm -o - | FileCheck %s --check-prefixes=CHECK,DEFAULT
8+
// RUN: %clang_cc1 -O2 -fsanitize=alloc-token -debug-info-kind=limited -fmemory-profile-use=%t/use.profdata -mllvm -optimize-hot-cold-new \
9+
// RUN: %t/src.cpp -triple x86_64-linux-gnu -emit-llvm -o - | FileCheck %s --check-prefixes=CHECK,ALLOCTOKEN
10+
11+
//--- memprof.yaml
12+
---
13+
HeapProfileRecords:
14+
- GUID: 0x7f8d88fcc70a347b
15+
AllocSites:
16+
- Callstack:
17+
- { Function: 0x7f8d88fcc70a347b, LineOffset: 1, Column: 10, IsInlineFrame: false }
18+
- { Function: 0xdb956436e78dd5fa, LineOffset: 1, Column: 13, IsInlineFrame: false }
19+
MemInfoBlock:
20+
AllocCount: 1
21+
TotalAccessCount: 0
22+
MinAccessCount: 0
23+
MaxAccessCount: 0
24+
TotalSize: 10
25+
MinSize: 10
26+
MaxSize: 10
27+
AllocTimestamp: 100
28+
DeallocTimestamp: 100
29+
TotalLifetime: 100000
30+
MinLifetime: 100000
31+
MaxLifetime: 100000
32+
AllocCpuId: 0
33+
DeallocCpuId: 0
34+
NumMigratedCpu: 0
35+
NumLifetimeOverlaps: 0
36+
NumSameAllocCpu: 0
37+
NumSameDeallocCpu: 0
38+
DataTypeId: 0
39+
TotalAccessDensity: 0
40+
MinAccessDensity: 0
41+
MaxAccessDensity: 0
42+
TotalLifetimeAccessDensity: 0
43+
MinLifetimeAccessDensity: 0
44+
MaxLifetimeAccessDensity: 0
45+
AccessHistogramSize: 0
46+
AccessHistogram: 0
47+
...
48+
49+
//--- src.cpp
50+
// CHECK-LABEL: define{{.*}} ptr @_Z3foov()
51+
// DEFAULT: call {{.*}} ptr @_Znam12__hot_cold_t(i64 10, i8 -128)
52+
// ALLOCTOKEN: call {{.*}} ptr @__alloc_token__Znam12__hot_cold_t(i64 10, i8 -128, i64 1538840549748785101){{.*}} !alloc_token
53+
char *foo() {
54+
return new char[10];
55+
}
56+
57+
int main() {
58+
char *a = foo();
59+
delete[] a;
60+
return 0;
61+
}

llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,119 +1806,125 @@ Value *LibCallSimplifier::optimizeNew(CallInst *CI, IRBuilderBase &B,
18061806
// better to replace the hinted call with a non hinted call, to avoid the
18071807
// extra parameter and the if condition check of the hint value in the
18081808
// allocator. This can be considered in the future.
1809+
Value *NewCall = nullptr;
18091810
switch (Func) {
18101811
case LibFunc_Znwm12__hot_cold_t:
18111812
if (OptimizeExistingHotColdNew)
1812-
return emitHotColdNew(CI->getArgOperand(0), B, TLI,
1813-
LibFunc_Znwm12__hot_cold_t, HotCold);
1813+
NewCall = emitHotColdNew(CI->getArgOperand(0), B, TLI,
1814+
LibFunc_Znwm12__hot_cold_t, HotCold);
18141815
break;
18151816
case LibFunc_Znwm:
1816-
return emitHotColdNew(CI->getArgOperand(0), B, TLI,
1817-
LibFunc_Znwm12__hot_cold_t, HotCold);
1817+
NewCall = emitHotColdNew(CI->getArgOperand(0), B, TLI,
1818+
LibFunc_Znwm12__hot_cold_t, HotCold);
18181819
break;
18191820
case LibFunc_Znam12__hot_cold_t:
18201821
if (OptimizeExistingHotColdNew)
1821-
return emitHotColdNew(CI->getArgOperand(0), B, TLI,
1822-
LibFunc_Znam12__hot_cold_t, HotCold);
1822+
NewCall = emitHotColdNew(CI->getArgOperand(0), B, TLI,
1823+
LibFunc_Znam12__hot_cold_t, HotCold);
18231824
break;
18241825
case LibFunc_Znam:
1825-
return emitHotColdNew(CI->getArgOperand(0), B, TLI,
1826-
LibFunc_Znam12__hot_cold_t, HotCold);
1826+
NewCall = emitHotColdNew(CI->getArgOperand(0), B, TLI,
1827+
LibFunc_Znam12__hot_cold_t, HotCold);
18271828
break;
18281829
case LibFunc_ZnwmRKSt9nothrow_t12__hot_cold_t:
18291830
if (OptimizeExistingHotColdNew)
1830-
return emitHotColdNewNoThrow(
1831+
NewCall = emitHotColdNewNoThrow(
18311832
CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
18321833
LibFunc_ZnwmRKSt9nothrow_t12__hot_cold_t, HotCold);
18331834
break;
18341835
case LibFunc_ZnwmRKSt9nothrow_t:
1835-
return emitHotColdNewNoThrow(CI->getArgOperand(0), CI->getArgOperand(1), B,
1836-
TLI, LibFunc_ZnwmRKSt9nothrow_t12__hot_cold_t,
1837-
HotCold);
1836+
NewCall = emitHotColdNewNoThrow(
1837+
CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1838+
LibFunc_ZnwmRKSt9nothrow_t12__hot_cold_t, HotCold);
18381839
break;
18391840
case LibFunc_ZnamRKSt9nothrow_t12__hot_cold_t:
18401841
if (OptimizeExistingHotColdNew)
1841-
return emitHotColdNewNoThrow(
1842+
NewCall = emitHotColdNewNoThrow(
18421843
CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
18431844
LibFunc_ZnamRKSt9nothrow_t12__hot_cold_t, HotCold);
18441845
break;
18451846
case LibFunc_ZnamRKSt9nothrow_t:
1846-
return emitHotColdNewNoThrow(CI->getArgOperand(0), CI->getArgOperand(1), B,
1847-
TLI, LibFunc_ZnamRKSt9nothrow_t12__hot_cold_t,
1848-
HotCold);
1847+
NewCall = emitHotColdNewNoThrow(
1848+
CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1849+
LibFunc_ZnamRKSt9nothrow_t12__hot_cold_t, HotCold);
18491850
break;
18501851
case LibFunc_ZnwmSt11align_val_t12__hot_cold_t:
18511852
if (OptimizeExistingHotColdNew)
1852-
return emitHotColdNewAligned(
1853+
NewCall = emitHotColdNewAligned(
18531854
CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
18541855
LibFunc_ZnwmSt11align_val_t12__hot_cold_t, HotCold);
18551856
break;
18561857
case LibFunc_ZnwmSt11align_val_t:
1857-
return emitHotColdNewAligned(CI->getArgOperand(0), CI->getArgOperand(1), B,
1858-
TLI, LibFunc_ZnwmSt11align_val_t12__hot_cold_t,
1859-
HotCold);
1858+
NewCall = emitHotColdNewAligned(
1859+
CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1860+
LibFunc_ZnwmSt11align_val_t12__hot_cold_t, HotCold);
18601861
break;
18611862
case LibFunc_ZnamSt11align_val_t12__hot_cold_t:
18621863
if (OptimizeExistingHotColdNew)
1863-
return emitHotColdNewAligned(
1864+
NewCall = emitHotColdNewAligned(
18641865
CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
18651866
LibFunc_ZnamSt11align_val_t12__hot_cold_t, HotCold);
18661867
break;
18671868
case LibFunc_ZnamSt11align_val_t:
1868-
return emitHotColdNewAligned(CI->getArgOperand(0), CI->getArgOperand(1), B,
1869-
TLI, LibFunc_ZnamSt11align_val_t12__hot_cold_t,
1870-
HotCold);
1869+
NewCall = emitHotColdNewAligned(
1870+
CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1871+
LibFunc_ZnamSt11align_val_t12__hot_cold_t, HotCold);
18711872
break;
18721873
case LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t12__hot_cold_t:
18731874
if (OptimizeExistingHotColdNew)
1874-
return emitHotColdNewAlignedNoThrow(
1875+
NewCall = emitHotColdNewAlignedNoThrow(
18751876
CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2), B,
18761877
TLI, LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t12__hot_cold_t,
18771878
HotCold);
18781879
break;
18791880
case LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t:
1880-
return emitHotColdNewAlignedNoThrow(
1881+
NewCall = emitHotColdNewAlignedNoThrow(
18811882
CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2), B,
18821883
TLI, LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t12__hot_cold_t, HotCold);
18831884
break;
18841885
case LibFunc_ZnamSt11align_val_tRKSt9nothrow_t12__hot_cold_t:
18851886
if (OptimizeExistingHotColdNew)
1886-
return emitHotColdNewAlignedNoThrow(
1887+
NewCall = emitHotColdNewAlignedNoThrow(
18871888
CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2), B,
18881889
TLI, LibFunc_ZnamSt11align_val_tRKSt9nothrow_t12__hot_cold_t,
18891890
HotCold);
18901891
break;
18911892
case LibFunc_ZnamSt11align_val_tRKSt9nothrow_t:
1892-
return emitHotColdNewAlignedNoThrow(
1893+
NewCall = emitHotColdNewAlignedNoThrow(
18931894
CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2), B,
18941895
TLI, LibFunc_ZnamSt11align_val_tRKSt9nothrow_t12__hot_cold_t, HotCold);
18951896
break;
18961897
case LibFunc_size_returning_new:
1897-
return emitHotColdSizeReturningNew(CI->getArgOperand(0), B, TLI,
1898-
LibFunc_size_returning_new_hot_cold,
1899-
HotCold);
1898+
NewCall = emitHotColdSizeReturningNew(CI->getArgOperand(0), B, TLI,
1899+
LibFunc_size_returning_new_hot_cold,
1900+
HotCold);
19001901
break;
19011902
case LibFunc_size_returning_new_hot_cold:
19021903
if (OptimizeExistingHotColdNew)
1903-
return emitHotColdSizeReturningNew(CI->getArgOperand(0), B, TLI,
1904-
LibFunc_size_returning_new_hot_cold,
1905-
HotCold);
1904+
NewCall = emitHotColdSizeReturningNew(CI->getArgOperand(0), B, TLI,
1905+
LibFunc_size_returning_new_hot_cold,
1906+
HotCold);
19061907
break;
19071908
case LibFunc_size_returning_new_aligned:
1908-
return emitHotColdSizeReturningNewAligned(
1909+
NewCall = emitHotColdSizeReturningNewAligned(
19091910
CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
19101911
LibFunc_size_returning_new_aligned_hot_cold, HotCold);
19111912
break;
19121913
case LibFunc_size_returning_new_aligned_hot_cold:
19131914
if (OptimizeExistingHotColdNew)
1914-
return emitHotColdSizeReturningNewAligned(
1915+
NewCall = emitHotColdSizeReturningNewAligned(
19151916
CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
19161917
LibFunc_size_returning_new_aligned_hot_cold, HotCold);
19171918
break;
19181919
default:
19191920
return nullptr;
19201921
}
1921-
return nullptr;
1922+
1923+
if (auto *NewCI = dyn_cast_or_null<Instruction>(NewCall))
1924+
if (MDNode *MD = CI->getMetadata(LLVMContext::MD_alloc_token))
1925+
NewCI->setMetadata(LLVMContext::MD_alloc_token, MD);
1926+
1927+
return NewCall;
19221928
}
19231929

19241930
//===----------------------------------------------------------------------===//

llvm/test/Transforms/InstCombine/simplify-libcalls-new.ll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,16 @@ define void @size_returning_aligned_update_test() {
610610
ret void
611611
}
612612

613+
;; Check that !alloc_token is preserved.
614+
; HOTCOLD-LABEL: @new_alloc_token()
615+
define void @new_alloc_token() {
616+
;; Attribute cold converted to __hot_cold_t cold value.
617+
; HOTCOLD: @_Znwm12__hot_cold_t(i64 10, i8 [[COLD]]), !alloc_token ![[ALLOC_TOKEN:[0-9]+]]
618+
%call = call ptr @_Znwm(i64 10) #0, !alloc_token !0
619+
call void @dummy(ptr %call)
620+
ret void
621+
}
622+
613623
;; So that instcombine doesn't optimize out the call.
614624
declare void @dummy(ptr)
615625

@@ -649,3 +659,6 @@ attributes #5 = { "memprof" = "hot" }
649659
attributes #8 = { "memprof" = "ambiguous" }
650660

651661
attributes #6 = { nobuiltin allocsize(0) "memprof"="cold" }
662+
663+
; CHECK: [[ALLOC_TOKEN]] = !{!"MyType", i1 false}
664+
!0 = !{!"MyType", i1 false}

0 commit comments

Comments
 (0)