Skip to content

Commit efcc7e8

Browse files
committed
[DRAFT] [C++20] [Modules] Introduce -fgen-reduced-bmi
This is draft for the user interfaces for llvm#75894 and required by @iains to get a feeling about the proposed future direction. Note that this is still in an early stage and nothing is decided.
1 parent b5bc1bc commit efcc7e8

File tree

9 files changed

+50
-1
lines changed

9 files changed

+50
-1
lines changed

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ def err_drv_invalid_argument_to_option : Error<
232232
"invalid argument '%0' to -%1">;
233233
def err_drv_missing_sanitizer_ignorelist : Error<
234234
"missing sanitizer ignorelist: '%0'">;
235+
def err_drv_incorrect_input_to_gen_reduced_bmi : Error <
236+
"-fgen-reduced-bmi can only be used with module interface unit">;
235237
def err_drv_malformed_sanitizer_ignorelist : Error<
236238
"malformed sanitizer ignorelist: '%0'">;
237239
def err_drv_malformed_sanitizer_coverage_allowlist : Error<

clang/include/clang/Basic/LangOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ BENIGN_LANGOPT(HeinousExtensions , 1, 0, "extensions that we really don't like a
175175
LANGOPT(Modules , 1, 0, "modules semantics")
176176
COMPATIBLE_LANGOPT(CPlusPlusModules, 1, 0, "C++ modules syntax")
177177
LANGOPT(BuiltinHeadersInSystemModules, 1, 0, "builtin headers belong to system modules, and _Builtin_ modules are ignored for cstdlib headers")
178+
COMPATIBLE_LANGOPT(GenReducedBMI, 1, 0, "Generate Lang Options")
178179
BENIGN_ENUM_LANGOPT(CompilingModule, CompilingModuleKind, 3, CMK_None,
179180
"compiling a module interface")
180181
BENIGN_LANGOPT(CompilingPCH, 1, 0, "building a pch")

clang/include/clang/CodeGen/CodeGenAction.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class CodeGenAction : public ASTFrontendAction {
5757
bool loadLinkModules(CompilerInstance &CI);
5858

5959
protected:
60+
bool BeginSourceFileAction(CompilerInstance &CI) override;
61+
6062
/// Create a new code generation action. If the optional \p _VMContext
6163
/// parameter is supplied, the action uses it without taking ownership,
6264
/// otherwise it creates a fresh LLVM context and takes ownership.

clang/include/clang/Driver/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2977,6 +2977,11 @@ def fmodule_output : Flag<["-"], "fmodule-output">, Flags<[NoXarchOption]>,
29772977
Visibility<[ClangOption, CC1Option]>,
29782978
HelpText<"Save intermediate module file results when compiling a standard C++ module unit.">;
29792979

2980+
defm gen_reduced_bmi : BoolFOption<"gen-reduced-bmi",
2981+
LangOpts<"GenReducedBMI">, DefaultFalse,
2982+
PosFlag<SetTrue, [], [CC1Option, ClangOption], "Generate reduced BMI.">,
2983+
NegFlag<SetFalse>, BothFlags<[], [ClangOption, CC1Option]>>;
2984+
29802985
def fmodules_prune_interval : Joined<["-"], "fmodules-prune-interval=">, Group<i_Group>,
29812986
Visibility<[ClangOption, CC1Option]>, MetaVarName<"<seconds>">,
29822987
HelpText<"Specify the interval (in seconds) between attempts to prune the module cache">,

clang/include/clang/Frontend/FrontendOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,9 @@ class FrontendOptions {
553553
/// Path which stores the output files for -ftime-trace
554554
std::string TimeTracePath;
555555

556+
/// Output Path for module output file.
557+
std::string ModuleOutputPath;
558+
556559
public:
557560
FrontendOptions()
558561
: DisableFree(false), RelocatablePCH(false), ShowHelp(false),

clang/include/clang/Serialization/ASTWriter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,10 @@ class PCHGenerator : public SemaConsumer {
825825
return SemaPtr->getDiagnostics();
826826
}
827827

828+
const Preprocessor &getPreprocessor() const {
829+
return PP;
830+
}
831+
828832
public:
829833
PCHGenerator(const Preprocessor &PP, InMemoryModuleCache &ModuleCache,
830834
StringRef OutputFile, StringRef isysroot,

clang/lib/CodeGen/CodeGenAction.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
#include "clang/Driver/DriverDiagnostic.h"
2727
#include "clang/Frontend/CompilerInstance.h"
2828
#include "clang/Frontend/FrontendDiagnostic.h"
29+
#include "clang/Frontend/FrontendActions.h"
30+
#include "clang/Frontend/MultiplexConsumer.h"
2931
#include "clang/Lex/Preprocessor.h"
32+
#include "clang/Serialization/ASTWriter.h"
3033
#include "llvm/ADT/Hashing.h"
3134
#include "llvm/Bitcode/BitcodeReader.h"
3235
#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
@@ -996,6 +999,12 @@ CodeGenerator *CodeGenAction::getCodeGenerator() const {
996999
return BEConsumer->getCodeGenerator();
9971000
}
9981001

1002+
bool CodeGenAction::BeginSourceFileAction(CompilerInstance &CI) {
1003+
if (CI.getLangOpts().GenReducedBMI)
1004+
CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleInterface);
1005+
return true;
1006+
}
1007+
9991008
static std::unique_ptr<raw_pwrite_stream>
10001009
GetOutputStream(CompilerInstance &CI, StringRef InFile, BackendAction Action) {
10011010
switch (Action) {
@@ -1053,6 +1062,16 @@ CodeGenAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
10531062
CI.getPreprocessor().addPPCallbacks(std::move(Callbacks));
10541063
}
10551064

1065+
if (CI.getLangOpts().GenReducedBMI && !CI.getFrontendOpts().ModuleOutputPath.empty()) {
1066+
std::vector<std::unique_ptr<ASTConsumer>> Consumers{2};
1067+
Consumers[0] = std::move(Result);
1068+
Consumers[1] = std::make_unique<ReducedBMIGenerator>(
1069+
CI.getPreprocessor(), CI.getModuleCache(),
1070+
CI.getFrontendOpts().OutputFile, std::make_shared<PCHBuffer>(),
1071+
/*IncludeTimestamps=*/+CI.getFrontendOpts().IncludeTimestamps);
1072+
return std::make_unique<MultiplexConsumer>(std::move(Consumers));
1073+
}
1074+
10561075
return std::move(Result);
10571076
}
10581077

clang/lib/Driver/Driver.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4688,6 +4688,10 @@ Action *Driver::ConstructPhaseAction(
46884688
if (Args.hasArg(options::OPT_extract_api))
46894689
return C.MakeAction<ExtractAPIJobAction>(Input, types::TY_API_INFO);
46904690

4691+
if (Args.hasFlag(options::OPT_fgen_reduced_bmi,
4692+
options::OPT_fno_gen_reduced_bmi, false))
4693+
return Input;
4694+
46914695
types::ID OutputTy = getPrecompiledType(Input->getType());
46924696
assert(OutputTy != types::TY_INVALID &&
46934697
"Cannot precompile this input type!");

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3889,7 +3889,16 @@ static bool RenderModulesOptions(Compilation &C, const Driver &D,
38893889

38903890
// Claim `-fmodule-output` and `-fmodule-output=` to avoid unused warnings.
38913891
Args.ClaimAllArgs(options::OPT_fmodule_output);
3892-
Args.ClaimAllArgs(options::OPT_fmodule_output_EQ);
3892+
Args.AddLastArg(CmdArgs, options::OPT_fmodule_output_EQ);
3893+
3894+
if (Args.hasFlag(options::OPT_fgen_reduced_bmi,
3895+
options::OPT_fno_gen_reduced_bmi, false)) {
3896+
if (Input.getType() != driver::types::TY_CXXModule &&
3897+
Input.getType() != driver::types::TY_PP_CXXModule)
3898+
D.Diag(diag::err_drv_incorrect_input_to_gen_reduced_bmi);
3899+
3900+
CmdArgs.push_back("-fgen-reduced-bmi");
3901+
}
38933902

38943903
return HaveModules;
38953904
}

0 commit comments

Comments
 (0)