Skip to content

Commit 77b435a

Browse files
committed
Revert "[InstrProfiling] Make COFF use the ELF comdat scheme (drop link.exe compatibility)"
This reverts commit fbb8e77. Accidentally pushed.
1 parent 95ac3d1 commit 77b435a

File tree

5 files changed

+38
-27
lines changed

5 files changed

+38
-27
lines changed

llvm/lib/IR/Verifier.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,10 +1464,10 @@ void Verifier::visitDIImportedEntity(const DIImportedEntity &N) {
14641464
void Verifier::visitComdat(const Comdat &C) {
14651465
// In COFF the Module is invalid if the GlobalValue has private linkage.
14661466
// Entities with private linkage don't have entries in the symbol table.
1467-
//if (TT.isOSBinFormatCOFF())
1468-
// if (const GlobalValue *GV = M.getNamedValue(C.getName()))
1469-
// Assert(!GV->hasPrivateLinkage(),
1470-
// "comdat global value has private linkage", GV);
1467+
if (TT.isOSBinFormatCOFF())
1468+
if (const GlobalValue *GV = M.getNamedValue(C.getName()))
1469+
Assert(!GV->hasPrivateLinkage(),
1470+
"comdat global value has private linkage", GV);
14711471
}
14721472

14731473
void Verifier::visitModuleIdents(const Module &M) {

llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -876,13 +876,17 @@ InstrProfiling::getOrCreateRegionCounters(InstrProfIncrementInst *Inc) {
876876
// nodeduplicate COMDAT which is lowered to a zero-flag section group. This
877877
// allows -z start-stop-gc to discard the entire group when the function is
878878
// discarded.
879-
bool SupportsComdat = Triple(M->getTargetTriple()).supportsCOMDAT();
879+
bool DataReferencedByCode = profDataReferencedByCode(*M);
880880
bool NeedComdat = needsComdatForCounter(*Fn, *M);
881881
std::string CntsVarName = getVarName(Inc, getInstrProfCountersVarPrefix());
882882
std::string DataVarName = getVarName(Inc, getInstrProfDataVarPrefix());
883883
auto MaybeSetComdat = [&](GlobalVariable *GV) {
884-
if (SupportsComdat) {
885-
Comdat *C = M->getOrInsertComdat(CntsVarName);
884+
bool UseComdat = (NeedComdat || TT.isOSBinFormatELF());
885+
if (UseComdat) {
886+
StringRef GroupName = TT.isOSBinFormatCOFF() && DataReferencedByCode
887+
? GV->getName()
888+
: CntsVarName;
889+
Comdat *C = M->getOrInsertComdat(GroupName);
886890
if (!NeedComdat)
887891
C->setSelectionKind(Comdat::NoDeduplicate);
888892
GV->setComdat(C);
@@ -947,8 +951,12 @@ InstrProfiling::getOrCreateRegionCounters(InstrProfIncrementInst *Inc) {
947951
// If the data variable is not referenced by code (if we don't emit
948952
// @llvm.instrprof.value.profile, NS will be 0), and the counter keeps the
949953
// data variable live under linker GC, the data variable can be private. This
950-
// optimization applies to COFF and ELF.
951-
if (NS == 0 && (TT.isOSBinFormatCOFF() || TT.isOSBinFormatELF())) {
954+
// optimization applies to ELF.
955+
//
956+
// On COFF, a comdat leader cannot be local so we require DataReferencedByCode
957+
// to be false.
958+
if (NS == 0 && (TT.isOSBinFormatELF() ||
959+
(!DataReferencedByCode && TT.isOSBinFormatCOFF()))) {
952960
Linkage = GlobalValue::PrivateLinkage;
953961
Visibility = GlobalValue::DefaultVisibility;
954962
}
@@ -1158,11 +1166,13 @@ void InstrProfiling::emitUses() {
11581166
// GlobalOpt/ConstantMerge) may not discard associated sections as a unit, so
11591167
// we conservatively retain all unconditionally in the compiler.
11601168
//
1161-
// On COFF and ELF, the linker can guarantee the associated sections will be
1162-
// retained or discarded as a unit, so llvm.compiler.used is sufficient.
1163-
// Otherwise, we have to conservatively make all of the sections retained by
1164-
// the linker.
1165-
if (TT.isOSBinFormatCOFF() || TT.isOSBinFormatELF())
1169+
// On ELF, the linker can guarantee the associated sections will be retained
1170+
// or discarded as a unit, so llvm.compiler.used is sufficient. Similarly on
1171+
// COFF, if prof data is not referenced by code we use one comdat and ensure
1172+
// this GC property as well. Otherwise, we have to conservatively make all of
1173+
// the sections retained by the linker.
1174+
if (TT.isOSBinFormatELF() ||
1175+
(TT.isOSBinFormatCOFF() && !profDataReferencedByCode(*M)))
11661176
appendToCompilerUsed(*M, CompilerUsedVars);
11671177
else
11681178
appendToUsed(*M, CompilerUsedVars);

llvm/test/Instrumentation/InstrProfiling/linkage.ll

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424
@__profn_foo_inline = linkonce_odr hidden constant [10 x i8] c"foo_inline"
2525
@__profn_foo_extern = linkonce_odr hidden constant [10 x i8] c"foo_extern"
2626

27-
; ELF: @__profc_foo = private global {{.*}} section "__llvm_prf_cnts", comdat,
27+
; ELF: @__profc_foo = private global {{.*}} section "__llvm_prf_cnts", comdat
2828
; ELF: @__profd_foo = private global {{.*}} section "__llvm_prf_data", comdat($__profc_foo)
2929
; MACHO: @__profc_foo = private global
3030
; MACHO: @__profd_foo = private global
31-
; COFF: @__profc_foo = private global {{.*}} section ".lprfc$M", comdat,
32-
; COFF: @__profd_foo = private global {{.*}} section ".lprfd$M", comdat($__profc_foo)
31+
; COFF: @__profc_foo = private global
32+
; COFF-NOT: comdat
33+
; COFF: @__profd_foo = private global
3334
define void @foo() {
3435
call void @llvm.instrprof.increment(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @__profn_foo, i32 0, i32 0), i64 0, i32 1, i32 0)
3536
ret void
@@ -61,8 +62,8 @@ define internal void @foo_internal() {
6162
; ELF: @__profd_foo_inline = private global{{.*}}section "__llvm_prf_data", comdat($__profc_foo_inline), align 8
6263
; MACHO: @__profc_foo_inline = linkonce_odr hidden global
6364
; MACHO: @__profd_foo_inline = linkonce_odr hidden global
64-
; COFF: @__profc_foo_inline = linkonce_odr hidden global{{.*}} section ".lprfc$M", comdat, align 8
65-
; COFF: @__profd_foo_inline = private global{{.*}} section ".lprfd$M", comdat($__profc_foo_inline), align 8
65+
; COFF: @__profc_foo_inline = linkonce_odr hidden global{{.*}} section ".lprfc$M", align 8
66+
; COFF: @__profd_foo_inline = private global{{.*}} section ".lprfd$M", align 8
6667
define linkonce_odr void @foo_inline() {
6768
call void @llvm.instrprof.increment(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @__profn_foo_inline, i32 0, i32 0), i64 0, i32 1, i32 0)
6869
ret void

llvm/test/Instrumentation/InstrProfiling/platform.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020

2121
; MACHO: @__profc_foo = private global [1 x i64] zeroinitializer, section "__DATA,__llvm_prf_cnts", align 8
2222
; ELF: @__profc_foo = private global [1 x i64] zeroinitializer, section "__llvm_prf_cnts", comdat, align 8
23-
; WINDOWS: @__profc_foo = private global [1 x i64] zeroinitializer, section ".lprfc$M", comdat, align 8
23+
; WINDOWS: @__profc_foo = private global [1 x i64] zeroinitializer, section ".lprfc$M", align 8
2424

2525
; MACHO: @__profd_foo = private {{.*}}, section "__DATA,__llvm_prf_data,regular,live_support", align 8
2626
; ELF: @__profd_foo = private {{.*}}, section "__llvm_prf_data", comdat($__profc_foo), align 8
27-
; WINDOWS: @__profd_foo = private global {{.*}}, section ".lprfd$M", comdat($__profc_foo), align 8
27+
; WINDOWS: @__profd_foo = private global {{.*}}, section ".lprfd$M", align 8
2828

2929
; ELF: @__llvm_prf_nm = private constant [{{.*}} x i8] c"{{.*}}", section "{{.*}}__llvm_prf_names", align 1
3030
; WINDOWS: @__llvm_prf_nm = private constant [{{.*}} x i8] c"{{.*}}", section "{{.*}}lprfn$M", align 1

llvm/test/Instrumentation/InstrProfiling/profiling.ll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
; ELF: @__profd_foo = private global { i64, i64, i64, i8*, i8*, i32, [2 x i16] } { i64 [[#]], i64 0, i64 sub (i64 ptrtoint ([1 x i64]* @__profc_foo to i64), i64 ptrtoint ({ i64, i64, i64, i8*, i8*, i32, [2 x i16] }* @__profd_foo to i64)), i8* null, i8* null, i32 1, [2 x i16] zeroinitializer }, section "__llvm_prf_data", comdat($__profc_foo), align 8
2222
; MACHO: @__profc_foo = private global [1 x i64] zeroinitializer, section "__DATA,__llvm_prf_cnts", align 8
2323
; MACHO: @__profd_foo = private {{.*}}, section "__DATA,__llvm_prf_data,regular,live_support", align 8
24-
; WIN: @__profc_foo = private global [1 x i64] zeroinitializer, section ".lprfc$M", comdat, align 8
25-
; WIN: @__profd_foo = private {{.*}}, section ".lprfd$M", comdat($__profc_foo), align 8
24+
; WIN: @__profc_foo = private global [1 x i64] zeroinitializer, section ".lprfc$M", align 8
25+
; WIN: @__profd_foo = private {{.*}}, section ".lprfd$M", align 8
2626
define void @foo() {
2727
call void @llvm.instrprof.increment(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @__profn_foo, i32 0, i32 0), i64 0, i32 1, i32 0)
2828
ret void
@@ -32,8 +32,8 @@ define void @foo() {
3232
; ELF: @__profd_bar = private {{.*}}, section "__llvm_prf_data", comdat($__profc_bar), align 8
3333
; MACHO: @__profc_bar = private global [1 x i64] zeroinitializer, section "__DATA,__llvm_prf_cnts", align 8
3434
; MACHO: @__profd_bar = private {{.*}}, section "__DATA,__llvm_prf_data,regular,live_support", align 8
35-
; WIN: @__profc_bar = private global [1 x i64] zeroinitializer, section ".lprfc$M", comdat, align 8
36-
; WIN: @__profd_bar = private {{.*}}, section ".lprfd$M", comdat($__profc_bar), align 8
35+
; WIN: @__profc_bar = private global [1 x i64] zeroinitializer, section ".lprfc$M", align 8
36+
; WIN: @__profd_bar = private {{.*}}, section ".lprfd$M", align 8
3737
define void @bar() {
3838
call void @llvm.instrprof.increment(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @__profn_bar, i32 0, i32 0), i64 0, i32 1, i32 0)
3939
ret void
@@ -43,8 +43,8 @@ define void @bar() {
4343
; ELF: @__profd_baz = private {{.*}}, section "__llvm_prf_data", comdat($__profc_baz), align 8
4444
; MACHO: @__profc_baz = private global [3 x i64] zeroinitializer, section "__DATA,__llvm_prf_cnts", align 8
4545
; MACHO: @__profd_baz = private {{.*}}, section "__DATA,__llvm_prf_data,regular,live_support", align 8
46-
; WIN: @__profc_baz = private global [3 x i64] zeroinitializer, section ".lprfc$M", comdat, align 8
47-
; WIN: @__profd_baz = private {{.*}}, section ".lprfd$M", comdat($__profc_baz), align 8
46+
; WIN: @__profc_baz = private global [3 x i64] zeroinitializer, section ".lprfc$M", align 8
47+
; WIN: @__profd_baz = private {{.*}}, section ".lprfd$M", align 8
4848
define void @baz() {
4949
call void @llvm.instrprof.increment(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @__profn_baz, i32 0, i32 0), i64 0, i32 3, i32 0)
5050
call void @llvm.instrprof.increment(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @__profn_baz, i32 0, i32 0), i64 0, i32 3, i32 1)

0 commit comments

Comments
 (0)