Skip to content

Commit

Permalink
[DRAFT] [C++20] [Modules] Introduce -fgen-reduced-bmi
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ChuanqiXu9 committed Jan 4, 2024
1 parent b5bc1bc commit efcc7e8
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 1 deletion.
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticDriverKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ def err_drv_invalid_argument_to_option : Error<
"invalid argument '%0' to -%1">;
def err_drv_missing_sanitizer_ignorelist : Error<
"missing sanitizer ignorelist: '%0'">;
def err_drv_incorrect_input_to_gen_reduced_bmi : Error <
"-fgen-reduced-bmi can only be used with module interface unit">;
def err_drv_malformed_sanitizer_ignorelist : Error<
"malformed sanitizer ignorelist: '%0'">;
def err_drv_malformed_sanitizer_coverage_allowlist : Error<
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/Basic/LangOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ BENIGN_LANGOPT(HeinousExtensions , 1, 0, "extensions that we really don't like a
LANGOPT(Modules , 1, 0, "modules semantics")
COMPATIBLE_LANGOPT(CPlusPlusModules, 1, 0, "C++ modules syntax")
LANGOPT(BuiltinHeadersInSystemModules, 1, 0, "builtin headers belong to system modules, and _Builtin_ modules are ignored for cstdlib headers")
COMPATIBLE_LANGOPT(GenReducedBMI, 1, 0, "Generate Lang Options")
BENIGN_ENUM_LANGOPT(CompilingModule, CompilingModuleKind, 3, CMK_None,
"compiling a module interface")
BENIGN_LANGOPT(CompilingPCH, 1, 0, "building a pch")
Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/CodeGen/CodeGenAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class CodeGenAction : public ASTFrontendAction {
bool loadLinkModules(CompilerInstance &CI);

protected:
bool BeginSourceFileAction(CompilerInstance &CI) override;

/// Create a new code generation action. If the optional \p _VMContext
/// parameter is supplied, the action uses it without taking ownership,
/// otherwise it creates a fresh LLVM context and takes ownership.
Expand Down
5 changes: 5 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -2977,6 +2977,11 @@ def fmodule_output : Flag<["-"], "fmodule-output">, Flags<[NoXarchOption]>,
Visibility<[ClangOption, CC1Option]>,
HelpText<"Save intermediate module file results when compiling a standard C++ module unit.">;

defm gen_reduced_bmi : BoolFOption<"gen-reduced-bmi",
LangOpts<"GenReducedBMI">, DefaultFalse,
PosFlag<SetTrue, [], [CC1Option, ClangOption], "Generate reduced BMI.">,
NegFlag<SetFalse>, BothFlags<[], [ClangOption, CC1Option]>>;

def fmodules_prune_interval : Joined<["-"], "fmodules-prune-interval=">, Group<i_Group>,
Visibility<[ClangOption, CC1Option]>, MetaVarName<"<seconds>">,
HelpText<"Specify the interval (in seconds) between attempts to prune the module cache">,
Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/Frontend/FrontendOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,9 @@ class FrontendOptions {
/// Path which stores the output files for -ftime-trace
std::string TimeTracePath;

/// Output Path for module output file.
std::string ModuleOutputPath;

public:
FrontendOptions()
: DisableFree(false), RelocatablePCH(false), ShowHelp(false),
Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/Serialization/ASTWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,10 @@ class PCHGenerator : public SemaConsumer {
return SemaPtr->getDiagnostics();
}

const Preprocessor &getPreprocessor() const {
return PP;
}

public:
PCHGenerator(const Preprocessor &PP, InMemoryModuleCache &ModuleCache,
StringRef OutputFile, StringRef isysroot,
Expand Down
19 changes: 19 additions & 0 deletions clang/lib/CodeGen/CodeGenAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
#include "clang/Driver/DriverDiagnostic.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendDiagnostic.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Frontend/MultiplexConsumer.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Serialization/ASTWriter.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/Bitcode/BitcodeReader.h"
#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
Expand Down Expand Up @@ -996,6 +999,12 @@ CodeGenerator *CodeGenAction::getCodeGenerator() const {
return BEConsumer->getCodeGenerator();
}

bool CodeGenAction::BeginSourceFileAction(CompilerInstance &CI) {
if (CI.getLangOpts().GenReducedBMI)
CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleInterface);
return true;
}

static std::unique_ptr<raw_pwrite_stream>
GetOutputStream(CompilerInstance &CI, StringRef InFile, BackendAction Action) {
switch (Action) {
Expand Down Expand Up @@ -1053,6 +1062,16 @@ CodeGenAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
CI.getPreprocessor().addPPCallbacks(std::move(Callbacks));
}

if (CI.getLangOpts().GenReducedBMI && !CI.getFrontendOpts().ModuleOutputPath.empty()) {
std::vector<std::unique_ptr<ASTConsumer>> Consumers{2};
Consumers[0] = std::move(Result);
Consumers[1] = std::make_unique<ReducedBMIGenerator>(
CI.getPreprocessor(), CI.getModuleCache(),
CI.getFrontendOpts().OutputFile, std::make_shared<PCHBuffer>(),
/*IncludeTimestamps=*/+CI.getFrontendOpts().IncludeTimestamps);
return std::make_unique<MultiplexConsumer>(std::move(Consumers));
}

return std::move(Result);
}

Expand Down
4 changes: 4 additions & 0 deletions clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4688,6 +4688,10 @@ Action *Driver::ConstructPhaseAction(
if (Args.hasArg(options::OPT_extract_api))
return C.MakeAction<ExtractAPIJobAction>(Input, types::TY_API_INFO);

if (Args.hasFlag(options::OPT_fgen_reduced_bmi,
options::OPT_fno_gen_reduced_bmi, false))
return Input;

types::ID OutputTy = getPrecompiledType(Input->getType());
assert(OutputTy != types::TY_INVALID &&
"Cannot precompile this input type!");
Expand Down
11 changes: 10 additions & 1 deletion clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3889,7 +3889,16 @@ static bool RenderModulesOptions(Compilation &C, const Driver &D,

// Claim `-fmodule-output` and `-fmodule-output=` to avoid unused warnings.
Args.ClaimAllArgs(options::OPT_fmodule_output);
Args.ClaimAllArgs(options::OPT_fmodule_output_EQ);
Args.AddLastArg(CmdArgs, options::OPT_fmodule_output_EQ);

if (Args.hasFlag(options::OPT_fgen_reduced_bmi,
options::OPT_fno_gen_reduced_bmi, false)) {
if (Input.getType() != driver::types::TY_CXXModule &&
Input.getType() != driver::types::TY_PP_CXXModule)
D.Diag(diag::err_drv_incorrect_input_to_gen_reduced_bmi);

CmdArgs.push_back("-fgen-reduced-bmi");
}

return HaveModules;
}
Expand Down

1 comment on commit efcc7e8

@iains
Copy link

@iains iains commented on efcc7e8 Mar 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you say, this patch is not the important one - the one that will be interesting is the one that actually elides AST content.

Please sign in to comment.