Skip to content

Commit 981db7c

Browse files
authored
Merge branch 'main' into users/rampitec/08-18-_amdgpu_fold_copies_of_constant_physical_registers_into_their_uses
2 parents 7a37441 + 9403c2d commit 981db7c

File tree

16 files changed

+544
-71
lines changed

16 files changed

+544
-71
lines changed

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,13 @@ def err_drv_reduced_module_output_overrided : Warning<
581581
"please consider use '-fmodule-output=' to specify the output file for reduced BMI explicitly">,
582582
InGroup<DiagGroup<"reduced-bmi-output-overrided">>;
583583

584+
def remark_found_cxx20_module_usage : Remark<
585+
"found C++20 module usage in file '%0'">,
586+
InGroup<ModulesDriver>;
587+
def remark_performing_driver_managed_module_build : Remark<
588+
"performing driver managed module build">,
589+
InGroup<ModulesDriver>;
590+
584591
def warn_drv_delayed_template_parsing_after_cxx20 : Warning<
585592
"-fdelayed-template-parsing is deprecated after C++20">,
586593
InGroup<DiagGroup<"delayed-template-parsing-in-cxx20">>;

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@ def ModuleConflict : DiagGroup<"module-conflict">;
635635
def ModuleFileExtension : DiagGroup<"module-file-extension">;
636636
def ModuleIncludeDirectiveTranslation : DiagGroup<"module-include-translation">;
637637
def ModuleMap : DiagGroup<"module-map">;
638+
def ModulesDriver : DiagGroup<"modules-driver">;
638639
def RoundTripCC1Args : DiagGroup<"round-trip-cc1-args">;
639640
def NewlineEOF : DiagGroup<"newline-eof">;
640641
def Nullability : DiagGroup<"nullability">;

clang/include/clang/Driver/Driver.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,9 @@ class Driver {
512512

513513
/// BuildActions - Construct the list of actions to perform for the
514514
/// given arguments, which are only done for a single architecture.
515+
/// If the compilation is an explicit module build, delegates to
516+
/// BuildDriverManagedModuleBuildActions. Otherwise, BuildDefaultActions is
517+
/// used.
515518
///
516519
/// \param C - The compilation that is being built.
517520
/// \param Args - The input arguments.
@@ -796,6 +799,35 @@ class Driver {
796799
/// compilation based on which -f(no-)?lto(=.*)? option occurs last.
797800
void setLTOMode(const llvm::opt::ArgList &Args);
798801

802+
/// BuildDefaultActions - Constructs the list of actions to perform
803+
/// for the provided arguments, which are only done for a single architecture.
804+
///
805+
/// \param C - The compilation that is being built.
806+
/// \param Args - The input arguments.
807+
/// \param Actions - The list to store the resulting actions onto.
808+
void BuildDefaultActions(Compilation &C, llvm::opt::DerivedArgList &Args,
809+
const InputList &Inputs, ActionList &Actions) const;
810+
811+
/// BuildDriverManagedModuleBuildActions - Performs a dependency
812+
/// scan and constructs the list of actions to perform for dependency order
813+
/// and the provided arguments. This is only done for a single a architecture.
814+
///
815+
/// \param C - The compilation that is being built.
816+
/// \param Args - The input arguments.
817+
/// \param Actions - The list to store the resulting actions onto.
818+
void BuildDriverManagedModuleBuildActions(Compilation &C,
819+
llvm::opt::DerivedArgList &Args,
820+
const InputList &Inputs,
821+
ActionList &Actions) const;
822+
823+
/// Scans the leading lines of the C++ source inputs to detect C++20 module
824+
/// usage.
825+
///
826+
/// \returns True if module usage is detected, false otherwise, or an error on
827+
/// read failure.
828+
llvm::ErrorOr<bool>
829+
ScanInputsForCXX20ModulesUsage(const InputList &Inputs) const;
830+
799831
/// Retrieves a ToolChain for a particular \p Target triple.
800832
///
801833
/// Will cache ToolChains for the life of the driver object, and create them

clang/include/clang/Driver/Options.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3296,6 +3296,13 @@ defm modules_reduced_bmi : BoolOption<"f", "modules-reduced-bmi",
32963296
PosFlag<SetTrue, [], [ClangOption, CC1Option],
32973297
"Generate the reduced BMI">>;
32983298

3299+
def fmodules_driver : Flag<["-"], "fmodules-driver">,
3300+
Group<f_Group>, Visibility<[ClangOption]>,
3301+
HelpText<"Enable support for driver managed module builds (experimental)">;
3302+
def fno_modules_driver : Flag<["-"], "fno-modules-driver">,
3303+
Group<f_Group>, Visibility<[ClangOption]>,
3304+
HelpText<"Disable support for driver managed module builds (experimental)">;
3305+
32993306
def experimental_modules_reduced_bmi : Flag<["-"], "fexperimental-modules-reduced-bmi">,
33003307
Group<f_Group>, Visibility<[ClangOption, CC1Option]>, Alias<fmodules_reduced_bmi>;
33013308

clang/include/clang/Lex/DependencyDirectivesScanner.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ void printDependencyDirectivesAsSource(
135135
ArrayRef<dependency_directives_scan::Directive> Directives,
136136
llvm::raw_ostream &OS);
137137

138+
/// Scan an input source buffer for C++20 named module usage.
139+
///
140+
/// \param Source The input source buffer.
141+
///
142+
/// \returns true if any C++20 named modules related directive was found.
143+
bool scanInputForCXX20ModulesUsage(StringRef Source);
144+
138145
/// Functor that returns the dependency directives for a given file.
139146
class DependencyDirectivesGetter {
140147
public:

clang/lib/Driver/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,6 @@ add_clang_library(clangDriver
9898

9999
LINK_LIBS
100100
clangBasic
101+
clangLex
101102
${system_libs}
102103
)

clang/lib/Driver/Driver.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
#include "clang/Driver/Tool.h"
6767
#include "clang/Driver/ToolChain.h"
6868
#include "clang/Driver/Types.h"
69+
#include "clang/Lex/DependencyDirectivesScanner.h"
6970
#include "llvm/ADT/ArrayRef.h"
7071
#include "llvm/ADT/STLExtras.h"
7172
#include "llvm/ADT/SmallSet.h"
@@ -4188,6 +4189,11 @@ void Driver::handleArguments(Compilation &C, DerivedArgList &Args,
41884189
YcArg = nullptr;
41894190
}
41904191

4192+
if (Args.hasArgNoClaim(options::OPT_fmodules_driver))
4193+
// TODO: Check against all incompatible -fmodules-driver arguments
4194+
if (!ModulesModeCXX20 && !Args.hasArgNoClaim(options::OPT_fmodules))
4195+
Args.eraseArg(options::OPT_fmodules_driver);
4196+
41914197
Arg *FinalPhaseArg;
41924198
phases::ID FinalPhase = getFinalPhase(Args, &FinalPhaseArg);
41934199

@@ -4314,6 +4320,33 @@ void Driver::handleArguments(Compilation &C, DerivedArgList &Args,
43144320
}
43154321
}
43164322

4323+
static bool hasCXXModuleInputType(const Driver::InputList &Inputs) {
4324+
const auto IsTypeCXXModule = [](const auto &Input) -> bool {
4325+
const auto TypeID = Input.first;
4326+
return (TypeID == types::TY_CXXModule);
4327+
};
4328+
return llvm::any_of(Inputs, IsTypeCXXModule);
4329+
}
4330+
4331+
llvm::ErrorOr<bool>
4332+
Driver::ScanInputsForCXX20ModulesUsage(const InputList &Inputs) const {
4333+
const auto CXXInputs = llvm::make_filter_range(
4334+
Inputs, [](const auto &Input) { return types::isCXX(Input.first); });
4335+
for (const auto &Input : CXXInputs) {
4336+
StringRef Filename = Input.second->getSpelling();
4337+
auto ErrOrBuffer = VFS->getBufferForFile(Filename);
4338+
if (!ErrOrBuffer)
4339+
return ErrOrBuffer.getError();
4340+
const auto Buffer = std::move(*ErrOrBuffer);
4341+
4342+
if (scanInputForCXX20ModulesUsage(Buffer->getBuffer())) {
4343+
Diags.Report(diag::remark_found_cxx20_module_usage) << Filename;
4344+
return true;
4345+
}
4346+
}
4347+
return false;
4348+
}
4349+
43174350
void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
43184351
const InputList &Inputs, ActionList &Actions) const {
43194352
llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
@@ -4325,6 +4358,33 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
43254358

43264359
handleArguments(C, Args, Inputs, Actions);
43274360

4361+
if (Args.hasFlag(options::OPT_fmodules_driver,
4362+
options::OPT_fno_modules_driver, false)) {
4363+
// TODO: Move the logic for implicitly enabling explicit-module-builds out
4364+
// of -fmodules-driver once it is no longer experimental.
4365+
// Currently, this serves diagnostic purposes only.
4366+
bool UsesCXXModules = hasCXXModuleInputType(Inputs);
4367+
if (!UsesCXXModules) {
4368+
const auto ErrOrScanResult = ScanInputsForCXX20ModulesUsage(Inputs);
4369+
if (!ErrOrScanResult) {
4370+
Diags.Report(diag::err_cannot_open_file)
4371+
<< ErrOrScanResult.getError().message();
4372+
return;
4373+
}
4374+
UsesCXXModules = *ErrOrScanResult;
4375+
}
4376+
if (UsesCXXModules || Args.hasArg(options::OPT_fmodules))
4377+
BuildDriverManagedModuleBuildActions(C, Args, Inputs, Actions);
4378+
return;
4379+
}
4380+
4381+
BuildDefaultActions(C, Args, Inputs, Actions);
4382+
}
4383+
4384+
void Driver::BuildDefaultActions(Compilation &C, DerivedArgList &Args,
4385+
const InputList &Inputs,
4386+
ActionList &Actions) const {
4387+
43284388
bool UseNewOffloadingDriver =
43294389
C.isOffloadingHostKind(Action::OFK_OpenMP) ||
43304390
C.isOffloadingHostKind(Action::OFK_SYCL) ||
@@ -4608,6 +4668,13 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
46084668
Args.ClaimAllArgs(options::OPT_cl_ignored_Group);
46094669
}
46104670

4671+
void Driver::BuildDriverManagedModuleBuildActions(
4672+
Compilation &C, llvm::opt::DerivedArgList &Args, const InputList &Inputs,
4673+
ActionList &Actions) const {
4674+
Diags.Report(diag::remark_performing_driver_managed_module_build);
4675+
return;
4676+
}
4677+
46114678
/// Returns the canonical name for the offloading architecture when using a HIP
46124679
/// or CUDA architecture.
46134680
static StringRef getCanonicalArchString(Compilation &C,

clang/lib/Lex/DependencyDirectivesScanner.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ struct Scanner {
8383
/// \returns True on error.
8484
bool scan(SmallVectorImpl<Directive> &Directives);
8585

86+
friend bool clang::scanInputForCXX20ModulesUsage(StringRef Source);
87+
8688
private:
8789
/// Lexes next token and advances \p First and the \p Lexer.
8890
[[nodiscard]] dependency_directives_scan::Token &
@@ -1075,3 +1077,51 @@ void clang::printDependencyDirectivesAsSource(
10751077
}
10761078
}
10771079
}
1080+
1081+
static void skipUntilMaybeCXX20ModuleDirective(const char *&First,
1082+
const char *const End) {
1083+
assert(First <= End);
1084+
while (First != End) {
1085+
if (*First == '#') {
1086+
++First;
1087+
skipToNewlineRaw(First, End);
1088+
}
1089+
skipWhitespace(First, End);
1090+
if (const auto Len = isEOL(First, End)) {
1091+
First += Len;
1092+
continue;
1093+
}
1094+
break;
1095+
}
1096+
}
1097+
1098+
bool clang::scanInputForCXX20ModulesUsage(StringRef Source) {
1099+
const char *First = Source.begin();
1100+
const char *const End = Source.end();
1101+
skipUntilMaybeCXX20ModuleDirective(First, End);
1102+
if (First == End)
1103+
return false;
1104+
1105+
// Check if the next token can even be a module directive before creating a
1106+
// full lexer.
1107+
if (!(*First == 'i' || *First == 'e' || *First == 'm'))
1108+
return false;
1109+
1110+
llvm::SmallVector<dependency_directives_scan::Token> Tokens;
1111+
Scanner S(StringRef(First, End - First), Tokens, nullptr, SourceLocation());
1112+
S.TheLexer.setParsingPreprocessorDirective(true);
1113+
if (S.lexModule(First, End))
1114+
return false;
1115+
auto IsCXXNamedModuleDirective = [](const DirectiveWithTokens &D) {
1116+
switch (D.Kind) {
1117+
case dependency_directives_scan::cxx_module_decl:
1118+
case dependency_directives_scan::cxx_import_decl:
1119+
case dependency_directives_scan::cxx_export_module_decl:
1120+
case dependency_directives_scan::cxx_export_import_decl:
1121+
return true;
1122+
default:
1123+
return false;
1124+
}
1125+
};
1126+
return llvm::any_of(S.DirsWithToks, IsCXXNamedModuleDirective);
1127+
}

0 commit comments

Comments
 (0)