llvm.org GIT mirror llvm / c2f5309
Merging r372020 and r372182: ------------------------------------------------------------------------ r372020 | rnk | 2019-09-16 11:49:09 -0700 (Mon, 16 Sep 2019) | 30 lines [PGO] Use linkonce_odr linkage for __profd_ variables in comdat groups This fixes relocations against __profd_ symbols in discarded sections, which is PR41380. In general, instrumentation happens very early, and optimization and inlining happens afterwards. The counters for a function are calculated early, and after inlining, counters for an inlined function may be widely referenced by other functions. For C++ inline functions of all kinds (linkonce_odr & available_externally mainly), instr profiling wants to deduplicate these __profc_ and __profd_ globals. Otherwise the binary would be quite large. I made __profd_ and __profc_ comdat in r355044, but I chose to make __profd_ internal. At the time, I was only dealing with coverage, and in that case, none of the instrumentation needs to reference __profd_. However, if you use PGO, then instrumentation passes add calls to __llvm_profile_instrument_range which reference __profd_ globals. The solution is to make these globals externally visible by using linkonce_odr linkage for data as was done for counters. This is safe because PGO adds a CFG hash to the names of the data and counter globals, so if different TUs have different globals, they will get different data and counter arrays. Reviewers: xur, hans Differential Revision: https://reviews.llvm.org/D67579 ------------------------------------------------------------------------ ------------------------------------------------------------------------ r372182 | rnk | 2019-09-17 14:10:49 -0700 (Tue, 17 Sep 2019) | 12 lines [PGO] Don't use comdat groups for counters & data on COFF For COFF, a comdat group is really a symbol marked IMAGE_COMDAT_SELECT_ANY and zero or more other symbols marked IMAGE_COMDAT_SELECT_ASSOCIATIVE. Typically the associative symbols in the group are not external and are not referenced by other TUs, they are things like debug info, C++ dynamic initializers, or other section registration schemes. The Visual C++ linker reports a duplicate symbol error for symbols marked IMAGE_COMDAT_SELECT_ASSOCIATIVE even if they would be discarded after handling the leader symbol. Fixes coverage-inline.cpp in check-profile after r372020. ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_90@374858 91177308-0d34-0410-b5e6-96231b3b80d8 Tom Stellard a month ago
4 changed file(s) with 32 addition(s) and 26 deletion(s). Raw diff Collapse all Expand all
730730 PD = It->second;
731731 }
732732
733 // Match the linkage and visibility of the name global, except on COFF, where
734 // the linkage must be local and consequentially the visibility must be
735 // default.
733 // Match the linkage and visibility of the name global. COFF supports using
734 // comdats with internal symbols, so do that if we can.
736735 Function *Fn = Inc->getParent()->getParent();
737736 GlobalValue::LinkageTypes Linkage = NamePtr->getLinkage();
738737 GlobalValue::VisibilityTypes Visibility = NamePtr->getVisibility();
748747 // new comdat group for the counters and profiling data. If we use the comdat
749748 // of the parent function, that will result in relocations against discarded
750749 // sections.
751 Comdat *Cmdt = nullptr;
752 GlobalValue::LinkageTypes CounterLinkage = Linkage;
753 if (needsComdatForCounter(*Fn, *M)) {
754 StringRef CmdtPrefix = getInstrProfComdatPrefix();
750 bool NeedComdat = needsComdatForCounter(*Fn, *M);
751 Comdat *Cmdt = nullptr; // Comdat group.
752 if (NeedComdat) {
755753 if (TT.isOSBinFormatCOFF()) {
756 // For COFF, the comdat group name must be the name of a symbol in the
757 // group. Use the counter variable name, and upgrade its linkage to
758 // something externally visible, like linkonce_odr.
759 CmdtPrefix = getInstrProfCountersVarPrefix();
760 CounterLinkage = GlobalValue::LinkOnceODRLinkage;
754 // For COFF, put the counters, data, and values each into their own
755 // comdats. We can't use a group because the Visual C++ linker will
756 // report duplicate symbol errors if there are multiple external symbols
757 // with the same name marked IMAGE_COMDAT_SELECT_ASSOCIATIVE.
758 Linkage = GlobalValue::LinkOnceODRLinkage;
759 Visibility = GlobalValue::HiddenVisibility;
760 } else {
761 // Otherwise, create one comdat group for everything.
762 Cmdt = M->getOrInsertComdat(getVarName(Inc, getInstrProfComdatPrefix()));
761763 }
762 Cmdt = M->getOrInsertComdat(getVarName(Inc, CmdtPrefix));
763 }
764 }
765 auto MaybeSetComdat = [=](GlobalVariable *GV) {
766 if (NeedComdat)
767 GV->setComdat(Cmdt ? Cmdt : M->getOrInsertComdat(GV->getName()));
768 };
764769
765770 uint64_t NumCounters = Inc->getNumCounters()->getZExtValue();
766771 LLVMContext &Ctx = M->getContext();
775780 CounterPtr->setSection(
776781 getInstrProfSectionName(IPSK_cnts, TT.getObjectFormat()));
777782 CounterPtr->setAlignment(8);
778 CounterPtr->setComdat(Cmdt);
779 CounterPtr->setLinkage(CounterLinkage);
783 MaybeSetComdat(CounterPtr);
784 CounterPtr->setLinkage(Linkage);
780785
781786 auto *Int8PtrTy = Type::getInt8PtrTy(Ctx);
782787 // Allocate statically the array of pointers to value profile nodes for
797802 ValuesVar->setSection(
798803 getInstrProfSectionName(IPSK_vals, TT.getObjectFormat()));
799804 ValuesVar->setAlignment(8);
800 ValuesVar->setComdat(Cmdt);
805 MaybeSetComdat(ValuesVar);
801806 ValuesPtrExpr =
802807 ConstantExpr::getBitCast(ValuesVar, Type::getInt8PtrTy(Ctx));
803808 }
830835 Data->setVisibility(Visibility);
831836 Data->setSection(getInstrProfSectionName(IPSK_data, TT.getObjectFormat()));
832837 Data->setAlignment(INSTR_PROF_DATA_ALIGNMENT);
833 Data->setComdat(Cmdt);
838 MaybeSetComdat(Data);
839 Data->setLinkage(Linkage);
834840
835841 PD.RegionCounters = CounterPtr;
836842 PD.DataVar = Data;
1919
2020
2121 ; COFF-NOT: __profn__Z3barIvEvv
22 ; COFF: @__profc__Z3barIvEvv = linkonce_odr dso_local global [1 x i64] zeroinitializer, section "{{.*}}prfc$M", comdat, align 8
23 ; COFF: @__profd__Z3barIvEvv = internal global { i64, i64, i64*, i8*, i8*, i32, [2 x i16] } { i64 4947693190065689389, i64 0, i64* getelementptr inbounds ([1 x i64], [1 x i64]* @__profc__Z3barIvEvv, i32 0, i32 0), i8*{{.*}}, i8* null, i32 1, [2 x i16] zeroinitializer }, section "{{.*}}prfd{{.*}}", comdat($__profc__Z3barIvEvv), align 8
22 ; COFF: @__profc__Z3barIvEvv = linkonce_odr hidden global [1 x i64] zeroinitializer, section "{{.*}}prfc$M", comdat, align 8
23 ; COFF: @__profd__Z3barIvEvv = linkonce_odr hidden global { i64, i64, i64*, i8*, i8*, i32, [2 x i16] } { i64 4947693190065689389, i64 0, i64* getelementptr inbounds ([1 x i64], [1 x i64]* @__profc__Z3barIvEvv, i32 0, i32 0), i8*{{.*}}, i8* null, i32 1, [2 x i16] zeroinitializer }, section "{{.*}}prfd{{.*}}", comdat, align 8
2424
2525
2626 declare void @llvm.instrprof.increment(i8*, i64, i32, i32) #1
1616
1717 ; ELF: @__profc_foo_inline = linkonce_odr hidden global{{.*}}, section "__llvm_prf_cnts", comdat($__profv_foo_inline), align 8
1818 ; ELF: @__profd_foo_inline = linkonce_odr hidden global{{.*}}, section "__llvm_prf_data", comdat($__profv_foo_inline), align 8
19 ; COFF: @__profc_foo_inline = linkonce_odr dso_local global{{.*}}, section ".lprfc$M", comdat, align 8
20 ; COFF: @__profd_foo_inline = internal global{{.*}}, section ".lprfd$M", comdat($__profc_foo_inline), align 8
19 ; COFF: @__profc_foo_inline = linkonce_odr hidden global{{.*}}, section ".lprfc$M", comdat, align 8
20 ; COFF: @__profd_foo_inline = linkonce_odr hidden global{{.*}}, section ".lprfd$M", comdat, align 8
2121 define weak_odr void @foo_inline() comdat {
2222 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)
2323 ret void
2929
3030 ; ELF: @__profc_foo_extern = linkonce_odr hidden global{{.*}}, section "__llvm_prf_cnts", comdat($__profv_foo_extern)
3131 ; ELF: @__profd_foo_extern = linkonce_odr hidden global{{.*}}, section "__llvm_prf_data", comdat($__profv_foo_extern)
32 ; COFF: @__profc_foo_extern = linkonce_odr dso_local global{{.*}}, section ".lprfc$M", comdat, align 8
33 ; COFF: @__profd_foo_extern = internal global{{.*}}, section ".lprfd$M", comdat($__profc_foo_extern), align 8
32 ; COFF: @__profc_foo_extern = linkonce_odr hidden global{{.*}}, section ".lprfc$M", comdat, align 8
33 ; COFF: @__profd_foo_extern = linkonce_odr hidden global{{.*}}, section ".lprfd$M", comdat, align 8
3434 define available_externally void @foo_extern() {
3535 call void @llvm.instrprof.increment(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @__profn_foo_extern, i32 0, i32 0), i64 0, i32 1, i32 0)
3636 ret void
5656 ; LINUX: @__profd_foo_extern = linkonce_odr hidden global {{.*}}section "__llvm_prf_data", comdat($__profv_foo_extern), align 8
5757 ; MACHO: @__profc_foo_extern = linkonce_odr hidden global
5858 ; MACHO: @__profd_foo_extern = linkonce_odr hidden global
59 ; COFF: @__profc_foo_extern = linkonce_odr dso_local global {{.*}}section ".lprfc$M", comdat, align 8
60 ; COFF: @__profd_foo_extern = internal global {{.*}}section ".lprfd$M", comdat($__profc_foo_extern), align 8
59 ; COFF: @__profc_foo_extern = linkonce_odr hidden global {{.*}}section ".lprfc$M", comdat, align 8
60 ; COFF: @__profd_foo_extern = linkonce_odr hidden global {{.*}}section ".lprfd$M", comdat, align 8
6161 define available_externally void @foo_extern() {
6262 call void @llvm.instrprof.increment(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @__profn_foo_extern, i32 0, i32 0), i64 0, i32 1, i32 0)
6363 ret void