Skip to content

Commit 84682d4

Browse files
committed
[CGData] Clang Optinos
1 parent 0b026f3 commit 84682d4

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

clang/include/clang/Driver/Options.td

+12
Original file line numberDiff line numberDiff line change
@@ -1834,6 +1834,18 @@ def fprofile_selected_function_group :
18341834
Visibility<[ClangOption, CC1Option]>, MetaVarName<"<i>">,
18351835
HelpText<"Partition functions into N groups using -fprofile-function-groups and select only functions in group i to be instrumented. The valid range is 0 to N-1 inclusive">,
18361836
MarshallingInfoInt<CodeGenOpts<"ProfileSelectedFunctionGroup">>;
1837+
def fcodegen_data_generate : Joined<["-"], "fcodegen-data-generate">,
1838+
Group<f_Group>, Visibility<[ClangOption, CC1Option]>,
1839+
HelpText<"Emit codegen data into object file. LLD for MachO (for now) merges them into default.cgdata">;
1840+
def fcodegen_data_generate_EQ : Joined<["-"], "fcodegen-data-generate=">,
1841+
Group<f_Group>, Visibility<[ClangOption, CC1Option]>, MetaVarName<"<directory>">,
1842+
HelpText<"Emit codegen data into object file. LLD for MachO (for now) merges them into <directory>/default.cgdata">;
1843+
def fcodegen_data_use : Joined<["-"], "fcodegen-data-use">,
1844+
Group<f_Group>, Visibility<[ClangOption, CC1Option]>,
1845+
HelpText<"Use codegen data read from default.cgdata to optimize the binary">;
1846+
def fcodegen_data_use_EQ : Joined<["-"], "fcodegen-data-use=">,
1847+
Group<f_Group>, Visibility<[ClangOption, CC1Option]>, MetaVarName<"<directory>">,
1848+
HelpText<"Use codegen data read from <directory>/default.cgdata to optimize the binary">;
18371849
def fswift_async_fp_EQ : Joined<["-"], "fswift-async-fp=">,
18381850
Group<f_Group>,
18391851
Visibility<[ClangOption, CC1Option, CC1AsOption, CLOption]>,

clang/lib/Driver/ToolChains/CommonArgs.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -2752,6 +2752,33 @@ void tools::addMachineOutlinerArgs(const Driver &D,
27522752
addArg(Twine("-enable-machine-outliner=never"));
27532753
}
27542754
}
2755+
2756+
auto *CodeGenDataGenArg =
2757+
Args.getLastArg(options::OPT_fcodegen_data_generate,
2758+
options::OPT_fcodegen_data_generate_EQ);
2759+
auto *CodeGenDataUseArg = Args.getLastArg(options::OPT_fcodegen_data_use,
2760+
options::OPT_fcodegen_data_use_EQ);
2761+
2762+
// We only allow one of them to be specified.
2763+
if (CodeGenDataGenArg && CodeGenDataUseArg)
2764+
D.Diag(diag::err_drv_argument_not_allowed_with)
2765+
<< CodeGenDataGenArg->getAsString(Args)
2766+
<< CodeGenDataUseArg->getAsString(Args);
2767+
2768+
// For codegen data gen, the output file is passed to the linker
2769+
// while a boolean flag is passed to the LLVM backend.
2770+
if (CodeGenDataGenArg)
2771+
addArg(Twine("-codegen-data-generate"));
2772+
2773+
// For codegen data use, the input file is passed to the LLVM backend.
2774+
if (CodeGenDataUseArg) {
2775+
SmallString<128> Path(CodeGenDataUseArg->getNumValues() == 0
2776+
? ""
2777+
: CodeGenDataUseArg->getValue());
2778+
if (Path.empty() || llvm::sys::fs::is_directory(Path))
2779+
llvm::sys::path::append(Path, "default.cgdata");
2780+
addArg(Twine("-codegen-data-use-path=" + Path.str()));
2781+
}
27552782
}
27562783

27572784
void tools::addOpenMPDeviceRTL(const Driver &D,

clang/lib/Driver/ToolChains/Darwin.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,19 @@ void darwin::Linker::AddLinkArgs(Compilation &C, const ArgList &Args,
476476
llvm::sys::path::append(Path, "default.profdata");
477477
CmdArgs.push_back(Args.MakeArgString(Twine("--cs-profile-path=") + Path));
478478
}
479+
480+
auto *CodeGenDataGenArg =
481+
Args.getLastArg(options::OPT_fcodegen_data_generate,
482+
options::OPT_fcodegen_data_generate_EQ);
483+
if (CodeGenDataGenArg) {
484+
SmallString<128> Path(CodeGenDataGenArg->getNumValues() == 0
485+
? ""
486+
: CodeGenDataGenArg->getValue());
487+
if (Path.empty() || llvm::sys::fs::is_directory(Path))
488+
llvm::sys::path::append(Path, "default.cgdata");
489+
CmdArgs.push_back(
490+
Args.MakeArgString(Twine("--codegen-data-generate-path=") + Path));
491+
}
479492
}
480493
}
481494

@@ -633,6 +646,39 @@ void darwin::Linker::ConstructJob(Compilation &C, const JobAction &JA,
633646
CmdArgs.push_back("-mllvm");
634647
CmdArgs.push_back("-enable-linkonceodr-outlining");
635648

649+
// Propagate codegen data flags to the linker for the LLVM backend.
650+
auto *CodeGenDataGenArg =
651+
Args.getLastArg(options::OPT_fcodegen_data_generate,
652+
options::OPT_fcodegen_data_generate_EQ);
653+
auto *CodeGenDataUseArg = Args.getLastArg(options::OPT_fcodegen_data_use,
654+
options::OPT_fcodegen_data_use_EQ);
655+
656+
// We only allow one of them to be specified.
657+
const Driver &D = getToolChain().getDriver();
658+
if (CodeGenDataGenArg && CodeGenDataUseArg)
659+
D.Diag(diag::err_drv_argument_not_allowed_with)
660+
<< CodeGenDataGenArg->getAsString(Args)
661+
<< CodeGenDataUseArg->getAsString(Args);
662+
663+
// For codegen data gen, the output file is passed to the linker
664+
// while a boolean flag is passed to the LLVM backend.
665+
if (CodeGenDataGenArg) {
666+
CmdArgs.push_back("-mllvm");
667+
CmdArgs.push_back("-codegen-data-generate");
668+
}
669+
670+
// For codegen data use, the input file is passed to the LLVM backend.
671+
if (CodeGenDataUseArg) {
672+
SmallString<128> Path(CodeGenDataUseArg->getNumValues() == 0
673+
? ""
674+
: CodeGenDataUseArg->getValue());
675+
if (Path.empty() || llvm::sys::fs::is_directory(Path))
676+
llvm::sys::path::append(Path, "default.cgdata");
677+
CmdArgs.push_back("-mllvm");
678+
CmdArgs.push_back(
679+
Args.MakeArgString("-codegen-data-use-path=" + Path.str()));
680+
}
681+
636682
// Setup statistics file output.
637683
SmallString<128> StatsFile =
638684
getStatsFileName(Args, Output, Inputs[0], getToolChain().getDriver());

clang/test/Driver/codegen-data.c

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Verify only one of codegen-data flag is passed.
2+
// RUN: not %clang -### -S --target=aarch64-linux-gnu -fcodegen-data-generate -fcodegen-data-use %s 2>&1 | FileCheck %s --check-prefix=CONFLICT
3+
// RUN: not %clang -### -S --target=arm64-apple-darwin -fcodegen-data-generate -fcodegen-data-use %s 2>&1 | FileCheck %s --check-prefix=CONFLICT
4+
// CONFLICT: error: invalid argument '-fcodegen-data-generate' not allowed with '-fcodegen-data-use'
5+
6+
// Verify the codegen-data-generate (boolean) flag is passed to LLVM
7+
// RUN: %clang -### -S --target=aarch64-linux-gnu -fcodegen-data-generate %s 2>&1| FileCheck %s --check-prefix=GENERATE
8+
// RUN: %clang -### -S --target=arm64-apple-darwin -fcodegen-data-generate %s 2>&1| FileCheck %s --check-prefix=GENERATE
9+
// GENERATE: "-mllvm" "-codegen-data-generate"
10+
11+
// Verify the codegen-data-use-path flag (with a default value) is passed to LLVM.
12+
// RUN: %clang -### -S --target=aarch64-linux-gnu -fcodegen-data-use %s 2>&1| FileCheck %s --check-prefix=USE
13+
// RUN: %clang -### -S --target=arm64-apple-darwin -fcodegen-data-use %s 2>&1| FileCheck %s --check-prefix=USE
14+
// RUN: mkdir -p %t.d/some/dir
15+
// RUN: %clang -### -S --target=aarch64-linux-gnu -fcodegen-data-use=%t.d/some/dir %s 2>&1 | FileCheck %s --check-prefix=USE-DIR
16+
// RUN: %clang -### -S --target=arm64-apple-darwin -fcodegen-data-use=%t.d/some/dir %s 2>&1 | FileCheck %s --check-prefix=USE-DIR
17+
// RUN: %clang -### -S --target=aarch64-linux-gnu -fcodegen-data-use=file %s 2>&1 | FileCheck %s --check-prefix=USE-FILE
18+
// RUN: %clang -### -S --target=arm64-apple-darwin -fcodegen-data-use=file %s 2>&1 | FileCheck %s --check-prefix=USE-FILE
19+
// USE: "-mllvm" "-codegen-data-use-path=default.cgdata"
20+
// USE-DIR: "-mllvm" "-codegen-data-use-path={{.*}}.d/some/dir{{/|\\\\}}default.cgdata"
21+
// USE-FILE: "-mllvm" "-codegen-data-use-path=file"
22+
23+
// Verify the codegen-data-generate (boolean) flag with a LTO.
24+
// RUN: %clang -### -flto --target=aarch64-linux-gnu -fcodegen-data-generate %s 2>&1 | FileCheck %s --check-prefix=GENERATE-LTO
25+
// GENERATE-LTO: {{ld(.exe)?"}}
26+
// GENERATE-LTO-SAME: "-plugin-opt=-codegen-data-generate"
27+
// RUN: %clang -### -flto --target=arm64-apple-darwin -fcodegen-data-generate %s 2>&1 | FileCheck %s --check-prefix=GENERATE-LTO-DARWIN
28+
// GENERATE-LTO-DARWIN: {{ld(.exe)?"}}
29+
// GENERATE-LTO-DARWIN-SAME: "-mllvm" "-codegen-data-generate"
30+
31+
// Verify the codegen-data-use-path flag with a LTO is passed to LLVM.
32+
// RUN: %clang -### -flto=thin --target=aarch64-linux-gnu -fcodegen-data-use %s 2>&1 | FileCheck %s --check-prefix=USE-LTO
33+
// USE-LTO: {{ld(.exe)?"}}
34+
// USE-LTO-SAME: "-plugin-opt=-codegen-data-use-path=default.cgdata"
35+
// RUN: %clang -### -flto=thin --target=arm64-apple-darwin -fcodegen-data-use %s 2>&1 | FileCheck %s --check-prefix=USE-LTO-DARWIN
36+
// USE-LTO-DARWIN: {{ld(.exe)?"}}
37+
// USE-LTO-DARWIN-SAME: "-mllvm" "-codegen-data-use-path=default.cgdata"
38+
39+
// For now, LLD MachO supports for generating the codegen data at link time.
40+
// RUN: %clang -### -fuse-ld=lld -B%S/Inputs/lld --target=arm64-apple-darwin -fcodegen-data-generate %s 2>&1 | FileCheck %s --check-prefix=GENERATE-LLD-DARWIN
41+
// GENERATE-LLD-DARWIN: {{ld(.exe)?"}}
42+
// GENERATE-LLD-DARWIN-SAME: "--codegen-data-generate-path=default.cgdata"

0 commit comments

Comments
 (0)