Skip to content

Commit a6c3b46

Browse files
author
Qiongsi Wu
committed
Frontend minor cleanup and adding a test
1 parent 6c9381e commit a6c3b46

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

clang/include/clang/Basic/CodeGenOptions.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,12 @@ class CodeGenOptions : public CodeGenOptionsBase {
494494
return getProfileInstr() == ProfileCSIRInstr;
495495
}
496496

497+
/// Check if any form of instrumentation is on.
498+
bool hasProfileInstr() const {
499+
return hasProfileClangInstr() || hasProfileIRInstr() ||
500+
hasProfileCSIRInstr();
501+
}
502+
497503
/// Check if Clang profile use is on.
498504
bool hasProfileClangUse() const {
499505
return getProfileUse() == ProfileClangInstr;

clang/lib/Frontend/InitPreprocessor.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,12 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
13641364
TI.getTargetDefines(LangOpts, Builder);
13651365
}
13661366

1367+
static void InitializePGOProfileMacros(const CodeGenOptions &CodeGenOpts,
1368+
MacroBuilder &Builder) {
1369+
if (CodeGenOpts.hasProfileInstr())
1370+
Builder.defineMacro("__LLVM_INSTR_PROFILE_GENERATE");
1371+
}
1372+
13671373
/// InitializePreprocessor - Initialize the preprocessor getting it and the
13681374
/// environment ready to process a single file.
13691375
void clang::InitializePreprocessor(Preprocessor &PP,
@@ -1417,8 +1423,10 @@ void clang::InitializePreprocessor(Preprocessor &PP,
14171423
InitializeStandardPredefinedMacros(PP.getTargetInfo(), PP.getLangOpts(),
14181424
FEOpts, Builder);
14191425

1420-
if (CodeGenOpts.hasProfileIRInstr())
1421-
Builder.defineMacro("__LLVM_INSTR_PROFILE_GENERATE");
1426+
// The PGO instrumentation profile macros are driven by options
1427+
// -fprofile[-instr]-generate/-fcs-profile-generate/-fprofile[-instr]-use,
1428+
// hence they are not guarded by InitOpts.UsePredefines.
1429+
InitializePGOProfileMacros(CodeGenOpts, Builder);
14221430

14231431
// Add on the predefines from the driver. Wrap in a #line directive to report
14241432
// that they come from the command line.

clang/test/Preprocessor/pgo-init.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// RUN: %clang -fprofile-generate -E -dM %s | FileCheck -match-full-lines -check-prefix=PROFGEN %s
2+
// RUN: %clang -fprofile-instr-generate -E -dM %s | FileCheck -match-full-lines -check-prefix=PROFGEN %s
3+
// RUN: %clang -fcs-profile-generate -E -dM %s | FileCheck -match-full-lines -check-prefix=PROFGEN %s
4+
//
5+
// PROFGEN:#define __LLVM_INSTR_PROFILE_GENERATE 1

0 commit comments

Comments
 (0)