Skip to content

Commit 02c9e89

Browse files
authored
[InstCombine][MemProf] Preserve all metadata (#169242)
When rewriting operator new calls to their hot/cold variants for PGHO, `!alloc_token` metadata was being dropped. This metadata is required by the AllocToken pass to correctly instrument the optimized allocation. Fix it by preserving all metadata.
1 parent 1c3b10f commit 02c9e89

File tree

2 files changed

+55
-37
lines changed

2 files changed

+55
-37
lines changed

llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,119 +1806,124 @@ 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+
NewCI->copyMetadata(*CI);
1925+
1926+
return NewCall;
19221927
}
19231928

19241929
//===----------------------------------------------------------------------===//

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)