Skip to content

Commit 1b12224

Browse files
committed
[clang][DepScan] Make OptimizeArgs a bit mask enum and enable by default (llvm#71588)
Make it easier to control which optimizations are enabled by making OptimizeArgs a bit masked enum. There's currently only one such optimization, but more will be added in followup commits. (cherry picked from commit fb07d9c)
1 parent 5386b8c commit 1b12224

16 files changed

+74
-36
lines changed

clang/include/clang/Tooling/DependencyScanning/DependencyScanningService.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "clang/CAS/CASOptions.h"
1313
#include "clang/Tooling/DependencyScanning/DependencyScanningCASFilesystem.h"
1414
#include "clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h"
15+
#include "llvm/ADT/BitmaskEnum.h"
1516
#include "llvm/CAS/ActionCache.h"
1617

1718
namespace clang {
@@ -60,6 +61,17 @@ enum class ScanningOutputFormat {
6061
P1689,
6162
};
6263

64+
enum class ScanningOptimizations {
65+
None = 0,
66+
67+
/// Remove unused header search paths including header maps.
68+
HeaderSearch = 1,
69+
70+
LLVM_MARK_AS_BITMASK_ENUM(HeaderSearch),
71+
All = HeaderSearch,
72+
Default = All
73+
};
74+
6375
/// The dependency scanning service contains shared configuration and state that
6476
/// is used by the individual dependency scanning workers.
6577
class DependencyScanningService {
@@ -69,13 +81,14 @@ class DependencyScanningService {
6981
std::shared_ptr<llvm::cas::ObjectStore> CAS,
7082
std::shared_ptr<llvm::cas::ActionCache> Cache,
7183
IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem> SharedFS,
72-
bool OptimizeArgs = false, bool EagerLoadModules = false);
84+
ScanningOptimizations OptimizeArgs = ScanningOptimizations::Default,
85+
bool EagerLoadModules = false);
7386

7487
ScanningMode getMode() const { return Mode; }
7588

7689
ScanningOutputFormat getFormat() const { return Format; }
7790

78-
bool canOptimizeArgs() const { return OptimizeArgs; }
91+
ScanningOptimizations getOptimizeArgs() const { return OptimizeArgs; }
7992

8093
bool shouldEagerLoadModules() const { return EagerLoadModules; }
8194

@@ -101,7 +114,7 @@ class DependencyScanningService {
101114
std::shared_ptr<llvm::cas::ObjectStore> CAS;
102115
std::shared_ptr<llvm::cas::ActionCache> Cache;
103116
/// Whether to optimize the modules' command-line arguments.
104-
const bool OptimizeArgs;
117+
const ScanningOptimizations OptimizeArgs;
105118
/// Whether to set up command-lines to load PCM files eagerly.
106119
const bool EagerLoadModules;
107120
/// Shared CachingOnDiskFileSystem. Set to nullptr to not use CAS dependency

clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ class DependencyScanningWorker {
174174
llvm::IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS;
175175
ScanningOutputFormat Format;
176176
/// Whether to optimize the modules' command-line arguments.
177-
bool OptimizeArgs;
177+
ScanningOptimizations OptimizeArgs;
178178
/// Whether to set up command-lines to load PCM files eagerly.
179179
bool EagerLoadModules;
180180

clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "clang/Lex/HeaderSearch.h"
1717
#include "clang/Lex/PPCallbacks.h"
1818
#include "clang/Serialization/ASTReader.h"
19+
#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
1920
#include "llvm/ADT/DenseMap.h"
2021
#include "llvm/ADT/Hashing.h"
2122
#include "llvm/ADT/StringSet.h"
@@ -223,8 +224,9 @@ class ModuleDepCollector final : public DependencyCollector {
223224
ModuleDepCollector(std::unique_ptr<DependencyOutputOptions> Opts,
224225
CompilerInstance &ScanInstance, DependencyConsumer &C,
225226
DependencyActionController &Controller,
226-
CompilerInvocation OriginalCI, bool OptimizeArgs,
227-
bool EagerLoadModules, bool IsStdModuleP1689Format);
227+
CompilerInvocation OriginalCI,
228+
ScanningOptimizations OptimizeArgs, bool EagerLoadModules,
229+
bool IsStdModuleP1689Format);
228230

229231
void attachToPreprocessor(Preprocessor &PP) override;
230232
void attachToASTReader(ASTReader &R) override;
@@ -266,7 +268,7 @@ class ModuleDepCollector final : public DependencyCollector {
266268
/// for each individual module.
267269
CowCompilerInvocation CommonInvocation;
268270
/// Whether to optimize the modules' command-line arguments.
269-
bool OptimizeArgs;
271+
ScanningOptimizations OptimizeArgs;
270272
/// Whether to set up command-lines to load PCM files eagerly.
271273
bool EagerLoadModules;
272274
/// If we're generating dependency output in P1689 format

clang/lib/Tooling/DependencyScanning/DependencyScanningService.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ DependencyScanningService::DependencyScanningService(
2121
std::shared_ptr<llvm::cas::ObjectStore> CAS,
2222
std::shared_ptr<llvm::cas::ActionCache> Cache,
2323
IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem> SharedFS,
24-
bool OptimizeArgs, bool EagerLoadModules)
24+
ScanningOptimizations OptimizeArgs, bool EagerLoadModules)
2525
: Mode(Mode), Format(Format), CASOpts(std::move(CASOpts)), CAS(std::move(CAS)), Cache(std::move(Cache)),
2626
OptimizeArgs(OptimizeArgs), EagerLoadModules(EagerLoadModules),
2727
SharedFS(std::move(SharedFS)) {

clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ class DependencyScanningAction : public tooling::ToolAction {
255255
llvm::IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS,
256256
llvm::IntrusiveRefCntPtr<DependencyScanningCASFilesystem> DepCASFS,
257257
llvm::IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem> CacheFS,
258-
ScanningOutputFormat Format, bool OptimizeArgs, bool EagerLoadModules,
258+
ScanningOutputFormat Format, ScanningOptimizations OptimizeArgs, bool EagerLoadModules,
259259
bool DisableFree, bool EmitDependencyFile,
260260
bool DiagGenerationAsCompilation, const CASOptions &CASOpts,
261261
std::optional<StringRef> ModuleName = std::nullopt,
@@ -518,7 +518,7 @@ class DependencyScanningAction : public tooling::ToolAction {
518518
llvm::IntrusiveRefCntPtr<DependencyScanningCASFilesystem> DepCASFS;
519519
llvm::IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem> CacheFS;
520520
ScanningOutputFormat Format;
521-
bool OptimizeArgs;
521+
ScanningOptimizations OptimizeArgs;
522522
bool EagerLoadModules;
523523
bool DisableFree;
524524
const CASOptions &CASOpts;
@@ -538,7 +538,7 @@ class DependencyScanningAction : public tooling::ToolAction {
538538
DependencyScanningWorker::DependencyScanningWorker(
539539
DependencyScanningService &Service,
540540
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)
541-
: Format(Service.getFormat()), OptimizeArgs(Service.canOptimizeArgs()),
541+
: Format(Service.getFormat()), OptimizeArgs(Service.getOptimizeArgs()),
542542
EagerLoadModules(Service.shouldEagerLoadModules()),
543543
CASOpts(Service.getCASOpts()), CAS(Service.getCAS()) {
544544
PCHContainerOps = std::make_shared<PCHContainerOperations>();
@@ -782,7 +782,8 @@ void DependencyScanningWorker::computeDependenciesFromCompilerInvocation(
782782
DependencyScanningAction Action(
783783
WorkingDirectory, DepsConsumer, Controller, DepFS, DepCASFS, CacheFS,
784784
Format,
785-
/*OptimizeArgs=*/false, /*DisableFree=*/false, EagerLoadModules,
785+
/*OptimizeArgs=*/ScanningOptimizations::Default, /*DisableFree=*/false,
786+
EagerLoadModules,
786787
/*EmitDependencyFile=*/!DepFile.empty(), DiagGenerationAsCompilation,
787788
getCASOpts(),
788789
/*ModuleName=*/std::nullopt, VerboseOS);

clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
614614
CowCompilerInvocation CI =
615615
MDC.getInvocationAdjustedForModuleBuildWithoutOutputs(
616616
MD, [&](CowCompilerInvocation &BuildInvocation) {
617-
if (MDC.OptimizeArgs)
617+
if (any(MDC.OptimizeArgs & ScanningOptimizations::HeaderSearch))
618618
optimizeHeaderSearchOpts(BuildInvocation.getMutHeaderSearchOpts(),
619619
*MDC.ScanInstance.getASTReader(), *MF);
620620
});
@@ -735,7 +735,8 @@ ModuleDepCollector::ModuleDepCollector(
735735
std::unique_ptr<DependencyOutputOptions> Opts,
736736
CompilerInstance &ScanInstance, DependencyConsumer &C,
737737
DependencyActionController &Controller, CompilerInvocation OriginalCI,
738-
bool OptimizeArgs, bool EagerLoadModules, bool IsStdModuleP1689Format)
738+
ScanningOptimizations OptimizeArgs, bool EagerLoadModules,
739+
bool IsStdModuleP1689Format)
739740
: ScanInstance(ScanInstance), Consumer(C), Controller(Controller),
740741
Opts(std::move(Opts)),
741742
CommonInvocation(

clang/test/ClangScanDeps/header-search-pruning-transitive.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ module X { header "X.h" }
5454
// RUN: sed -e "s|DIR|%/t|g" %t/cdb_with_a.json.template > %t/cdb_with_a.json
5555
// RUN: sed -e "s|DIR|%/t|g" %t/cdb_without_a.json.template > %t/cdb_without_a.json
5656

57-
// RUN: clang-scan-deps -compilation-database %t/cdb_with_a.json -format experimental-full -optimize-args > %t/results.json
58-
// RUN: clang-scan-deps -compilation-database %t/cdb_without_a.json -format experimental-full -optimize-args >> %t/results.json
57+
// RUN: clang-scan-deps -compilation-database %t/cdb_with_a.json -format experimental-full -optimize-args=header-search > %t/results.json
58+
// RUN: clang-scan-deps -compilation-database %t/cdb_without_a.json -format experimental-full -optimize-args=header-search >> %t/results.json
5959
// RUN: cat %t/results.json | sed 's:\\\\\?:/:g' | FileCheck %s -DPREFIX=%/t
6060

6161
// CHECK: {

clang/test/ClangScanDeps/header-search-pruning.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
// RUN: sed -e "s|DIR|%/t|g" -e "s|DEFINES|-DINCLUDE_B|g" %S/Inputs/header-search-pruning/cdb.json > %t/cdb_b.json
66
// RUN: sed -e "s|DIR|%/t|g" -e "s|DEFINES|-DINCLUDE_A -DINCLUDE_B|g" %S/Inputs/header-search-pruning/cdb.json > %t/cdb_ab.json
77
//
8-
// RUN: clang-scan-deps -compilation-database %t/cdb_a.json -format experimental-full -optimize-args >> %t/result_a.json
8+
// RUN: clang-scan-deps -compilation-database %t/cdb_a.json -format experimental-full -optimize-args=header-search >> %t/result_a.json
99
// RUN: cat %t/result_a.json | sed 's:\\\\\?:/:g' | FileCheck --check-prefixes=CHECK_A %s
1010
//
11-
// RUN: clang-scan-deps -compilation-database %t/cdb_b.json -format experimental-full -optimize-args >> %t/result_b.json
11+
// RUN: clang-scan-deps -compilation-database %t/cdb_b.json -format experimental-full -optimize-args=header-search >> %t/result_b.json
1212
// RUN: cat %t/result_b.json | sed 's:\\\\\?:/:g' | FileCheck --check-prefixes=CHECK_B %s
1313
//
14-
// RUN: clang-scan-deps -compilation-database %t/cdb_ab.json -format experimental-full -optimize-args >> %t/result_ab.json
14+
// RUN: clang-scan-deps -compilation-database %t/cdb_ab.json -format experimental-full -optimize-args=header-search >> %t/result_ab.json
1515
// RUN: cat %t/result_ab.json | sed 's:\\\\\?:/:g' | FileCheck --check-prefixes=CHECK_AB %s
1616

1717
#include "mod.h"

clang/test/ClangScanDeps/modules-cas-fs-symlink-dir-from-module.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
// RUN: clang-scan-deps -cas-path %t/cas -compilation-database %t/cdb.json -j 1 \
1212
// RUN: -format experimental-full -mode=preprocess-dependency-directives \
13-
// RUN: -optimize-args -module-files-dir %t/build > %t/deps.json
13+
// RUN: -optimize-args=all -module-files-dir %t/build > %t/deps.json
1414

1515
// RUN: %deps-to-rsp %t/deps.json --module-name=Mod > %t/mod.cc1.rsp
1616
// RUN: %deps-to-rsp %t/deps.json --module-name=Other > %t/other.cc1.rsp

clang/test/ClangScanDeps/modules-symlink-dir-from-module.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
// RUN: clang-scan-deps -compilation-database %t/cdb.json -j 1 \
1414
// RUN: -format experimental-full -mode=preprocess-dependency-directives \
15-
// RUN: -optimize-args -module-files-dir %t/build > %t/deps.json
15+
// RUN: -optimize-args=all -module-files-dir %t/build > %t/deps.json
1616

1717
// RUN: cat %t/deps.json | sed 's:\\\\\?:/:g' | FileCheck %s -DPREFIX=%/t
1818

clang/test/ClangScanDeps/modules-symlink-dir.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
// RUN: clang-scan-deps -compilation-database %t/cdb.json -j 1 \
1616
// RUN: -format experimental-full -mode=preprocess-dependency-directives \
17-
// RUN: -optimize-args -module-files-dir %t/build > %t/deps.json
17+
// RUN: -optimize-args=all -module-files-dir %t/build > %t/deps.json
1818

1919
// RUN: cat %t/deps.json | sed 's:\\\\\?:/:g' | FileCheck -DPREFIX=%/t %s
2020

clang/tools/clang-scan-deps/ClangScanDeps.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ enum ResourceDirRecipeKind {
8383

8484
static ScanningMode ScanMode = ScanningMode::DependencyDirectivesScan;
8585
static ScanningOutputFormat Format = ScanningOutputFormat::Make;
86+
static ScanningOptimizations OptimizeArgs;
8687
static std::string ModuleFilesDir;
87-
static bool OptimizeArgs;
8888
static bool EagerLoadModules;
8989
static unsigned NumThreads = 0;
9090
static std::string CompilationDB;
@@ -168,10 +168,31 @@ static void ParseArgs(int argc, char **argv) {
168168
Format = *FormatType;
169169
}
170170

171+
std::vector<std::string> OptimizationFlags =
172+
Args.getAllArgValues(OPT_optimize_args_EQ);
173+
OptimizeArgs = ScanningOptimizations::None;
174+
for (const auto &Arg : OptimizationFlags) {
175+
auto Optimization =
176+
llvm::StringSwitch<std::optional<ScanningOptimizations>>(Arg)
177+
.Case("none", ScanningOptimizations::None)
178+
.Case("header-search", ScanningOptimizations::HeaderSearch)
179+
.Case("all", ScanningOptimizations::All)
180+
.Default(std::nullopt);
181+
if (!Optimization) {
182+
llvm::errs()
183+
<< ToolName
184+
<< ": for the --optimize-args option: Cannot find option named '"
185+
<< Arg << "'\n";
186+
std::exit(1);
187+
}
188+
OptimizeArgs |= *Optimization;
189+
}
190+
if (OptimizationFlags.empty())
191+
OptimizeArgs = ScanningOptimizations::Default;
192+
171193
if (const llvm::opt::Arg *A = Args.getLastArg(OPT_module_files_dir_EQ))
172194
ModuleFilesDir = A->getValue();
173195

174-
OptimizeArgs = Args.hasArg(OPT_optimize_args);
175196
EagerLoadModules = Args.hasArg(OPT_eager_load_pcm);
176197

177198
if (const llvm::opt::Arg *A = Args.getLastArg(OPT_j)) {

clang/tools/clang-scan-deps/Opts.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ defm format : Eq<"format", "The output format for the dependencies">;
1818
defm module_files_dir : Eq<"module-files-dir",
1919
"The build directory for modules. Defaults to the value of '-fmodules-cache-path=' from command lines for implicit modules">;
2020

21-
def optimize_args : F<"optimize-args", "Whether to optimize command-line arguments of modules">;
21+
def optimize_args_EQ : CommaJoined<["-", "--"], "optimize-args=">, HelpText<"Which command-line arguments of modules to optimize">;
2222
def eager_load_pcm : F<"eager-load-pcm", "Load PCM files eagerly (instead of lazily on import)">;
2323

2424
def j : Arg<"j", "Number of worker threads to use (default: use all concurrent threads)">;

clang/tools/driver/cc1depscan_main.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -852,9 +852,7 @@ int ScanServer::listen() {
852852
ProduceIncludeTree
853853
? tooling::dependencies::ScanningOutputFormat::IncludeTree
854854
: tooling::dependencies::ScanningOutputFormat::Tree,
855-
CASOpts, CAS, Cache, FS,
856-
/*ReuseFileManager=*/false,
857-
/*SkipExcludedPPRanges=*/true);
855+
CASOpts, CAS, Cache, FS);
858856

859857
std::atomic<int> NumRunning(0);
860858

@@ -1077,9 +1075,7 @@ static Expected<llvm::cas::CASID> scanAndUpdateCC1Inline(
10771075
ProduceIncludeTree
10781076
? tooling::dependencies::ScanningOutputFormat::IncludeTree
10791077
: tooling::dependencies::ScanningOutputFormat::Tree,
1080-
CASOpts, DB, Cache, FS,
1081-
/*ReuseFileManager=*/false,
1082-
/*SkipExcludedPPRanges=*/true);
1078+
CASOpts, DB, Cache, FS);
10831079
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> UnderlyingFS =
10841080
llvm::vfs::createPhysicalFileSystem();
10851081
if (ProduceIncludeTree)

clang/tools/libclang/CDependencies.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ clang_experimental_DependencyScannerService_create_v0(CXDependencyMode Format) {
133133
IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem> FS;
134134
return wrap(new DependencyScanningService(
135135
ScanningMode::DependencyDirectivesScan, unwrap(Format), CASOpts,
136-
/*CAS=*/nullptr, /*ActionCache=*/nullptr, FS,
137-
/*ReuseFilemanager=*/false));
136+
/*CAS=*/nullptr, /*ActionCache=*/nullptr, FS));
138137
}
139138

140139
ScanningOutputFormat DependencyScannerServiceOptions::getFormat() const {
@@ -172,8 +171,7 @@ clang_experimental_DependencyScannerService_create_v1(
172171
}
173172
return wrap(new DependencyScanningService(
174173
ScanningMode::DependencyDirectivesScan, Format, unwrap(Opts)->CASOpts,
175-
std::move(CAS), std::move(Cache), std::move(FS),
176-
/*ReuseFilemanager=*/false));
174+
std::move(CAS), std::move(Cache), std::move(FS)));
177175
}
178176

179177
void clang_experimental_DependencyScannerService_dispose_v0(

llvm/include/llvm/ADT/BitmaskEnum.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@
8686
using ::llvm::BitmaskEnumDetail::operator^; \
8787
using ::llvm::BitmaskEnumDetail::operator|=; \
8888
using ::llvm::BitmaskEnumDetail::operator&=; \
89+
using ::llvm::BitmaskEnumDetail::operator^=; \
8990
/* Force a semicolon at the end of this macro. */ \
90-
using ::llvm::BitmaskEnumDetail::operator^=
91+
using ::llvm::BitmaskEnumDetail::any
9192

9293
namespace llvm {
9394

@@ -135,6 +136,11 @@ constexpr unsigned bitWidth(uint64_t Value) {
135136
return Value ? 1 + bitWidth(Value >> 1) : 0;
136137
}
137138

139+
template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
140+
constexpr bool any(E Val) {
141+
return Val != static_cast<E>(0);
142+
}
143+
138144
template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
139145
constexpr E operator~(E Val) {
140146
return static_cast<E>(~Underlying(Val) & Mask<E>());

0 commit comments

Comments
 (0)