llvm.org GIT mirror llvm / 2fc706b
[Profile] PE binary coverage bug fix PR/32584 Differential Revision: https://reviews.llvm.org/D32023 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@300277 91177308-0d34-0410-b5e6-96231b3b80d8 Xinliang David Li 2 years ago
10 changed file(s) with 195 addition(s) and 59 deletion(s). Raw diff Collapse all Expand all
5353 class Module;
5454
5555 /// Return the name of data section containing profile counter variables.
56 inline StringRef getInstrProfCountersSectionName(bool AddSegment) {
57 return AddSegment ? "__DATA," INSTR_PROF_CNTS_SECT_NAME_STR
58 : INSTR_PROF_CNTS_SECT_NAME_STR;
59 }
56 /// If M is null, the target platform is assumed to be the same as
57 /// the host machine, and the segment prefix will not be added.
58 std::string getInstrProfCountersSectionName(const Module *M = nullptr);
6059
6160 /// Return the name of data section containing names of instrumented
62 /// functions.
63 inline StringRef getInstrProfNameSectionName(bool AddSegment) {
64 return AddSegment ? "__DATA," INSTR_PROF_NAME_SECT_NAME_STR
65 : INSTR_PROF_NAME_SECT_NAME_STR;
66 }
61 /// functions. If M is null, the target platform is assumed to be the same as
62 /// the host machine, nor will segment prefix be added.
63 std::string getInstrProfNameSectionName(const Module *M = nullptr);
6764
6865 /// Return the name of the data section containing per-function control
69 /// data.
70 inline StringRef getInstrProfDataSectionName(bool AddSegment) {
71 return AddSegment ? "__DATA," INSTR_PROF_DATA_SECT_NAME_STR
72 ",regular,live_support"
73 : INSTR_PROF_DATA_SECT_NAME_STR;
74 }
66 /// data. If M is null, the target platform is assumed to be the same as
67 /// the host machine, and the segment prefix will not be added.
68 std::string getInstrProfDataSectionName(const Module *M = nullptr);
7569
7670 /// Return the name of data section containing pointers to value profile
77 /// counters/nodes.
78 inline StringRef getInstrProfValuesSectionName(bool AddSegment) {
79 return AddSegment ? "__DATA," INSTR_PROF_VALS_SECT_NAME_STR
80 : INSTR_PROF_VALS_SECT_NAME_STR;
81 }
71 /// counters/nodes. If M is null, the target platform is assumed to be
72 /// the same as the host machine, and the segment prefix will not be added.
73 std::string getInstrProfValuesSectionName(const Module *M = nullptr);
8274
8375 /// Return the name of data section containing nodes holdling value
84 /// profiling data.
85 inline StringRef getInstrProfVNodesSectionName(bool AddSegment) {
86 return AddSegment ? "__DATA," INSTR_PROF_VNODES_SECT_NAME_STR
87 : INSTR_PROF_VNODES_SECT_NAME_STR;
88 }
76 /// profiling data. If M is null, the target platform is assumed to be
77 /// the same as the host machine, and the segment prefix will not be added.
78 std::string getInstrProfVNodesSectionName(const Module *M = nullptr);
8979
9080 /// Return the name profile runtime entry point to do value profiling
9181 /// for a given site.
10090
10191 /// Return the name of the section containing function coverage mapping
10292 /// data.
103 inline StringRef getInstrProfCoverageSectionName(bool AddSegment) {
104 return AddSegment ? "__LLVM_COV," INSTR_PROF_COVMAP_SECT_NAME_STR
105 : INSTR_PROF_COVMAP_SECT_NAME_STR;
106 }
93 std::string getInstrProfCoverageSectionName(const Module *M = nullptr);
10794
10895 /// Return the name prefix of variables containing instrumented function names.
10996 inline StringRef getInstrProfNameVarPrefix() { return "__profn_"; }
245245 /* COVMAP_HEADER end. */
246246
247247
248 #ifdef INSTR_PROF_SECT_ENTRY
249 #define INSTR_PROF_DATA_DEFINED
250 INSTR_PROF_SECT_ENTRY(IPSK_data, INSTR_PROF_DATA_SECT_NAME_STR, \
251 INSTR_PROF_QUOTE(INSTR_PROF_DATA_COMMON), \
252 INSTR_PROF_QUOTE(INSTR_PROF_DATA_COFF), "__DATA,")
253 INSTR_PROF_SECT_ENTRY(IPSK_cnts, INSTR_PROF_CNTS_SECT_NAME_STR, \
254 INSTR_PROF_QUOTE(INSTR_PROF_CNTS_COMMON), \
255 INSTR_PROF_QUOTE(INSTR_PROF_CNTS_COFF), "__DATA,")
256 INSTR_PROF_SECT_ENTRY(IPSK_name, INSTR_PROF_NAME_SECT_NAME_STR, \
257 INSTR_PROF_QUOTE(INSTR_PROF_NAME_COMMON), \
258 INSTR_PROF_QUOTE(INSTR_PROF_NAME_COFF), "__DATA,")
259 INSTR_PROF_SECT_ENTRY(IPSK_vals, INSTR_PROF_VALS_SECT_NAME_STR, \
260 INSTR_PROF_QUOTE(INSTR_PROF_VALS_COMMON), \
261 INSTR_PROF_QUOTE(INSTR_PROF_VALS_COFF), "__DATA,")
262 INSTR_PROF_SECT_ENTRY(IPSK_vnodes, INSTR_PROF_VNODES_SECT_NAME_STR, \
263 INSTR_PROF_QUOTE(INSTR_PROF_VNODES_COMMON), \
264 INSTR_PROF_QUOTE(INSTR_PROF_VNODES_COFF), "__DATA,")
265 INSTR_PROF_SECT_ENTRY(IPSK_covmap, INSTR_PROF_COVMAP_SECT_NAME_STR, \
266 INSTR_PROF_QUOTE(INSTR_PROF_COVMAP_COMMON), \
267 INSTR_PROF_QUOTE(INSTR_PROF_COVMAP_COFF), "__LLVM_COV,")
268
269 #undef INSTR_PROF_SECT_ENTRY
270 #endif
271
272
248273 #ifdef INSTR_PROF_VALUE_PROF_DATA
249274 #define INSTR_PROF_DATA_DEFINED
250275
621646 * specified via command line. */
622647 #define INSTR_PROF_PROFILE_NAME_VAR __llvm_profile_filename
623648
649 /* section name strings common to all targets other
650 than WIN32 */
651 #define INSTR_PROF_DATA_COMMON __llvm_prf_data
652 #define INSTR_PROF_NAME_COMMON __llvm_prf_names
653 #define INSTR_PROF_CNTS_COMMON __llvm_prf_cnts
654 #define INSTR_PROF_VALS_COMMON __llvm_prf_vals
655 #define INSTR_PROF_VNODES_COMMON __llvm_prf_vnds
656 #define INSTR_PROF_COVMAP_COMMON __llvm_covmap
657 /* Win32 */
658 #define INSTR_PROF_DATA_COFF .lprfd
659 #define INSTR_PROF_NAME_COFF .lprfn
660 #define INSTR_PROF_CNTS_COFF .lprfc
661 #define INSTR_PROF_VALS_COFF .lprfv
662 #define INSTR_PROF_VNODES_COFF .lprfnd
663 #define INSTR_PROF_COVMAP_COFF .lcovmap
664
665 #ifdef _WIN32
624666 /* Runtime section names and name strings. */
625 #define INSTR_PROF_DATA_SECT_NAME __llvm_prf_data
626 #define INSTR_PROF_NAME_SECT_NAME __llvm_prf_names
627 #define INSTR_PROF_CNTS_SECT_NAME __llvm_prf_cnts
667 #define INSTR_PROF_DATA_SECT_NAME INSTR_PROF_DATA_COFF
668 #define INSTR_PROF_NAME_SECT_NAME INSTR_PROF_NAME_COFF
669 #define INSTR_PROF_CNTS_SECT_NAME INSTR_PROF_CNTS_COFF
628670 /* Array of pointers. Each pointer points to a list
629671 * of value nodes associated with one value site.
630672 */
631 #define INSTR_PROF_VALS_SECT_NAME __llvm_prf_vals
673 #define INSTR_PROF_VALS_SECT_NAME INSTR_PROF_VALS_COFF
632674 /* Value profile nodes section. */
633 #define INSTR_PROF_VNODES_SECT_NAME __llvm_prf_vnds
634 #define INSTR_PROF_COVMAP_SECT_NAME __llvm_covmap
675 #define INSTR_PROF_VNODES_SECT_NAME INSTR_PROF_VNODES_COFF
676 #define INSTR_PROF_COVMAP_SECT_NAME INSTR_PROF_COVMAP_COFF
677 #else
678 /* Runtime section names and name strings. */
679 #define INSTR_PROF_DATA_SECT_NAME INSTR_PROF_DATA_COMMON
680 #define INSTR_PROF_NAME_SECT_NAME INSTR_PROF_NAME_COMMON
681 #define INSTR_PROF_CNTS_SECT_NAME INSTR_PROF_CNTS_COMMON
682 /* Array of pointers. Each pointer points to a list
683 * of value nodes associated with one value site.
684 */
685 #define INSTR_PROF_VALS_SECT_NAME INSTR_PROF_VALS_COMMON
686 /* Value profile nodes section. */
687 #define INSTR_PROF_VNODES_SECT_NAME INSTR_PROF_VNODES_COMMON
688 #define INSTR_PROF_COVMAP_SECT_NAME INSTR_PROF_COVMAP_COMMON
689 #endif
635690
636691 #define INSTR_PROF_DATA_SECT_NAME_STR \
637692 INSTR_PROF_QUOTE(INSTR_PROF_DATA_SECT_NAME)
6666 bool isMachO() const;
6767
6868 /// Get the section name for the counter variables.
69 StringRef getCountersSection() const;
69 std::string getCountersSection() const;
7070
7171 /// Get the section name for the name variables.
72 StringRef getNameSection() const;
72 std::string getNameSection() const;
7373
7474 /// Get the section name for the profile data variables.
75 StringRef getDataSection() const;
75 std::string getDataSection() const;
7676
7777 /// Get the section name for the coverage mapping data.
78 StringRef getCoverageSection() const;
78 std::string getCoverageSection() const;
7979
8080 /// Count the number of instrumented value sites for the function.
8181 void computeNumValueSiteCounts(InstrProfValueProfileInst *Ins);
132132 //
133133 // .section .eh_frame,"a",@progbits
134134
135 if (Name == getInstrProfCoverageSectionName(false))
135 // TODO: to support Win->ELF cross compilation with coverage properly,
136 // need to pass the module pointer to the following call.
137 if (Name == getInstrProfCoverageSectionName())
136138 return SectionKind::getMetadata();
137139
138140 if (Name.empty() || Name[0] != '.') return K;
647647 : support::endianness::big;
648648
649649 // Look for the sections that we are interested in.
650 auto NamesSection = lookupSection(*OF, getInstrProfNameSectionName(false));
650 // TODO: with the current getInstrProfXXXSectionName interfaces, the
651 // the coverage reader host tool is limited to read coverage section on
652 // binaries with compatible profile section naming scheme as the host
653 // platform. Currently, COFF format binaries have different section
654 // naming scheme from the all the rest.
655 auto NamesSection = lookupSection(*OF, getInstrProfNameSectionName());
651656 if (auto E = NamesSection.takeError())
652657 return E;
653 auto CoverageSection =
654 lookupSection(*OF, getInstrProfCoverageSectionName(false));
658 auto CoverageSection = lookupSection(*OF, getInstrProfCoverageSectionName());
655659 if (auto E = CoverageSection.takeError())
656660 return E;
657661
135135 return *ErrorCategory;
136136 }
137137
138 namespace {
139
140 enum InstrProfSectKind {
141 #define INSTR_PROF_SECT_ENTRY(Kind, SectName, SectNameCommon, SectNameCoff, \
142 Prefix) \
143 Kind,
144 #include "llvm/ProfileData/InstrProfData.inc"
145 };
146
147 const char *InstrProfSectName[] = {
148 #define INSTR_PROF_SECT_ENTRY(Kind, SectName, SectNameCommon, SectNameCoff, \
149 Prefix) \
150 SectName,
151 #include "llvm/ProfileData/InstrProfData.inc"
152 };
153
154 const char *InstrProfSectNameCommon[] = {
155 #define INSTR_PROF_SECT_ENTRY(Kind, SectName, SectNameCommon, SectNameCoff, \
156 Prefix) \
157 SectNameCommon,
158 #include "llvm/ProfileData/InstrProfData.inc"
159 };
160
161 const char *InstrProfSectNameCoff[] = {
162 #define INSTR_PROF_SECT_ENTRY(Kind, SectName, SectNameCommon, SectNameCoff, \
163 Prefix) \
164 SectNameCoff,
165 #include "llvm/ProfileData/InstrProfData.inc"
166 };
167
168 const char *InstrProfSectNamePrefix[] = {
169 #define INSTR_PROF_SECT_ENTRY(Kind, SectName, SectNameCommon, SectNameCoff, \
170 Prefix) \
171 Prefix,
172 #include "llvm/ProfileData/InstrProfData.inc"
173 };
174
175 std::string getInstrProfSectionName(const Module *M, InstrProfSectKind Kind) {
176
177 if (!M)
178 return InstrProfSectName[Kind];
179
180 bool AddSegment = Triple(M->getTargetTriple()).isOSBinFormatMachO();
181 std::string SectName;
182 if (Triple(M->getTargetTriple()).isOSBinFormatCOFF())
183 SectName = InstrProfSectNameCoff[Kind];
184 else
185 SectName = InstrProfSectNameCommon[Kind];
186
187 if (AddSegment) {
188 SectName = InstrProfSectNamePrefix[Kind] + SectName;
189 if (Kind == IPSK_data) {
190 SectName += ",regular,live_support";
191 }
192 }
193 return SectName;
194 }
195
196 } // namespace
197
138198 namespace llvm {
199
200 std::string getInstrProfCountersSectionName(const Module *M) {
201 return getInstrProfSectionName(M, IPSK_cnts);
202 }
203
204 std::string getInstrProfNameSectionName(const Module *M) {
205 return getInstrProfSectionName(M, IPSK_name);
206 }
207
208 std::string getInstrProfDataSectionName(const Module *M) {
209 return getInstrProfSectionName(M, IPSK_data);
210 }
211
212 std::string getInstrProfValuesSectionName(const Module *M) {
213 return getInstrProfSectionName(M, IPSK_vals);
214 }
215
216 std::string getInstrProfVNodesSectionName(const Module *M) {
217 return getInstrProfSectionName(M, IPSK_vnodes);
218 }
219
220 std::string getInstrProfCoverageSectionName(const Module *M) {
221 return getInstrProfSectionName(M, IPSK_covmap);
222 }
139223
140224 void SoftInstrProfErrors::addError(instrprof_error IE) {
141225 if (IE == instrprof_error::success)
144144 }
145145
146146 /// Get the section name for the counter variables.
147 StringRef InstrProfiling::getCountersSection() const {
148 return getInstrProfCountersSectionName(isMachO());
147 std::string InstrProfiling::getCountersSection() const {
148 return getInstrProfCountersSectionName(M);
149149 }
150150
151151 /// Get the section name for the name variables.
152 StringRef InstrProfiling::getNameSection() const {
153 return getInstrProfNameSectionName(isMachO());
152 std::string InstrProfiling::getNameSection() const {
153 return getInstrProfNameSectionName(M);
154154 }
155155
156156 /// Get the section name for the profile data variables.
157 StringRef InstrProfiling::getDataSection() const {
158 return getInstrProfDataSectionName(isMachO());
157 std::string InstrProfiling::getDataSection() const {
158 return getInstrProfDataSectionName(M);
159159 }
160160
161161 /// Get the section name for the coverage mapping data.
162 StringRef InstrProfiling::getCoverageSection() const {
163 return getInstrProfCoverageSectionName(isMachO());
162 std::string InstrProfiling::getCoverageSection() const {
163 return getInstrProfCoverageSectionName(M);
164164 }
165165
166166 static InstrProfIncrementInst *castToIncrementInst(Instruction *Instr) {
461461 Constant::getNullValue(ValuesTy),
462462 getVarName(Inc, getInstrProfValuesVarPrefix()));
463463 ValuesVar->setVisibility(NamePtr->getVisibility());
464 ValuesVar->setSection(getInstrProfValuesSectionName(isMachO()));
464 ValuesVar->setSection(getInstrProfValuesSectionName(M));
465465 ValuesVar->setAlignment(8);
466466 ValuesVar->setComdat(ProfileVarsComdat);
467467 ValuesPtrExpr =
556556 auto *VNodesVar = new GlobalVariable(
557557 *M, VNodesTy, false, GlobalValue::PrivateLinkage,
558558 Constant::getNullValue(VNodesTy), getInstrProfVNodesVarName());
559 VNodesVar->setSection(getInstrProfVNodesSectionName(isMachO()));
559 VNodesVar->setSection(getInstrProfVNodesSectionName(M));
560560 UsedVars.push_back(VNodesVar);
561561 }
562562
279279 if (GV->hasSection()) {
280280 StringRef SectionName = GV->getSection();
281281 // Check if the global is in the PGO counters section.
282 if (SectionName.endswith(getInstrProfCountersSectionName(
283 /*AddSegment=*/false)))
282 if (SectionName.endswith(getInstrProfCountersSectionName()))
284283 return false;
285284 }
286285
1919
2020
2121 ; COFF-NOT: __profn__Z3barIvEvv
22 ; COFF: @__profc__Z3barIvEvv = linkonce_odr hidden global [1 x i64] zeroinitializer, section "{{.*}}__llvm_prf_cnts", 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 "{{.*}}__llvm_prf_data{{.*}}", comdat($__profc__Z3barIvEvv), align 8
22 ; COFF: @__profc__Z3barIvEvv = linkonce_odr hidden global [1 x i64] zeroinitializer, section "{{.*}}prfc", 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($__profc__Z3barIvEvv), align 8
2424
2525
2626 declare void @llvm.instrprof.increment(i8*, i64, i32, i32) #1
5151 StringRef Name;
5252 if (Section.getName(Name))
5353 return 1;
54 if (Name == llvm::getInstrProfNameSectionName(false)) {
54 // TODO: with the current getInstrProfXXXSectionName interfaces, the
55 // the host tool is limited to read coverage section on
56 // binaries with compatible profile section naming scheme as the host
57 // platform. Currently, COFF format binaries have different section
58 // naming scheme from the all the rest.
59 if (Name == llvm::getInstrProfNameSectionName()) {
5560 ProfileNames = Section;
56 } else if (Name == llvm::getInstrProfCoverageSectionName(false)) {
61 } else if (Name == llvm::getInstrProfCoverageSectionName()) {
5762 CoverageMapping = Section;
5863 } else
5964 continue;