Skip to content

Commit fb07d9c

Browse files
authored
[clang][DepScan] Make OptimizeArgs a bit mask enum and enable by default (#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.
1 parent b1af3c0 commit fb07d9c

File tree

13 files changed

+72
-28
lines changed

13 files changed

+72
-28
lines changed

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNINGSERVICE_H
1111

1212
#include "clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h"
13+
#include "llvm/ADT/BitmaskEnum.h"
1314

1415
namespace clang {
1516
namespace tooling {
@@ -44,19 +45,31 @@ enum class ScanningOutputFormat {
4445
P1689,
4546
};
4647

48+
enum class ScanningOptimizations {
49+
None = 0,
50+
51+
/// Remove unused header search paths including header maps.
52+
HeaderSearch = 1,
53+
54+
LLVM_MARK_AS_BITMASK_ENUM(HeaderSearch),
55+
All = HeaderSearch,
56+
Default = All
57+
};
58+
4759
/// The dependency scanning service contains shared configuration and state that
4860
/// is used by the individual dependency scanning workers.
4961
class DependencyScanningService {
5062
public:
51-
DependencyScanningService(ScanningMode Mode, ScanningOutputFormat Format,
52-
bool OptimizeArgs = false,
53-
bool EagerLoadModules = false);
63+
DependencyScanningService(
64+
ScanningMode Mode, ScanningOutputFormat Format,
65+
ScanningOptimizations OptimizeArgs = ScanningOptimizations::Default,
66+
bool EagerLoadModules = false);
5467

5568
ScanningMode getMode() const { return Mode; }
5669

5770
ScanningOutputFormat getFormat() const { return Format; }
5871

59-
bool canOptimizeArgs() const { return OptimizeArgs; }
72+
ScanningOptimizations getOptimizeArgs() const { return OptimizeArgs; }
6073

6174
bool shouldEagerLoadModules() const { return EagerLoadModules; }
6275

@@ -68,7 +81,7 @@ class DependencyScanningService {
6881
const ScanningMode Mode;
6982
const ScanningOutputFormat Format;
7083
/// Whether to optimize the modules' command-line arguments.
71-
const bool OptimizeArgs;
84+
const ScanningOptimizations OptimizeArgs;
7285
/// Whether to set up command-lines to load PCM files eagerly.
7386
const bool EagerLoadModules;
7487
/// The global file system cache.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class DependencyScanningWorker {
116116
llvm::IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS;
117117
ScanningOutputFormat Format;
118118
/// Whether to optimize the modules' command-line arguments.
119-
bool OptimizeArgs;
119+
ScanningOptimizations OptimizeArgs;
120120
/// Whether to set up command-lines to load PCM files eagerly.
121121
bool EagerLoadModules;
122122
};

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"
@@ -211,8 +212,9 @@ class ModuleDepCollector final : public DependencyCollector {
211212
ModuleDepCollector(std::unique_ptr<DependencyOutputOptions> Opts,
212213
CompilerInstance &ScanInstance, DependencyConsumer &C,
213214
DependencyActionController &Controller,
214-
CompilerInvocation OriginalCI, bool OptimizeArgs,
215-
bool EagerLoadModules, bool IsStdModuleP1689Format);
215+
CompilerInvocation OriginalCI,
216+
ScanningOptimizations OptimizeArgs, bool EagerLoadModules,
217+
bool IsStdModuleP1689Format);
216218

217219
void attachToPreprocessor(Preprocessor &PP) override;
218220
void attachToASTReader(ASTReader &R) override;
@@ -254,7 +256,7 @@ class ModuleDepCollector final : public DependencyCollector {
254256
/// for each individual module.
255257
CowCompilerInvocation CommonInvocation;
256258
/// Whether to optimize the modules' command-line arguments.
257-
bool OptimizeArgs;
259+
ScanningOptimizations OptimizeArgs;
258260
/// Whether to set up command-lines to load PCM files eagerly.
259261
bool EagerLoadModules;
260262
/// If we're generating dependency output in P1689 format

clang/lib/Tooling/DependencyScanning/DependencyScanningService.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ using namespace tooling;
1414
using namespace dependencies;
1515

1616
DependencyScanningService::DependencyScanningService(
17-
ScanningMode Mode, ScanningOutputFormat Format, bool OptimizeArgs,
18-
bool EagerLoadModules)
17+
ScanningMode Mode, ScanningOutputFormat Format,
18+
ScanningOptimizations OptimizeArgs, bool EagerLoadModules)
1919
: Mode(Mode), Format(Format), OptimizeArgs(OptimizeArgs),
2020
EagerLoadModules(EagerLoadModules) {
2121
// Initialize targets for object file support.

clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ class DependencyScanningAction : public tooling::ToolAction {
137137
StringRef WorkingDirectory, DependencyConsumer &Consumer,
138138
DependencyActionController &Controller,
139139
llvm::IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS,
140-
ScanningOutputFormat Format, bool OptimizeArgs, bool EagerLoadModules,
141-
bool DisableFree, std::optional<StringRef> ModuleName = std::nullopt)
140+
ScanningOutputFormat Format, ScanningOptimizations OptimizeArgs,
141+
bool EagerLoadModules, bool DisableFree,
142+
std::optional<StringRef> ModuleName = std::nullopt)
142143
: WorkingDirectory(WorkingDirectory), Consumer(Consumer),
143144
Controller(Controller), DepFS(std::move(DepFS)), Format(Format),
144145
OptimizeArgs(OptimizeArgs), EagerLoadModules(EagerLoadModules),
@@ -297,7 +298,7 @@ class DependencyScanningAction : public tooling::ToolAction {
297298
DependencyActionController &Controller;
298299
llvm::IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS;
299300
ScanningOutputFormat Format;
300-
bool OptimizeArgs;
301+
ScanningOptimizations OptimizeArgs;
301302
bool EagerLoadModules;
302303
bool DisableFree;
303304
std::optional<StringRef> ModuleName;
@@ -312,7 +313,7 @@ class DependencyScanningAction : public tooling::ToolAction {
312313
DependencyScanningWorker::DependencyScanningWorker(
313314
DependencyScanningService &Service,
314315
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)
315-
: Format(Service.getFormat()), OptimizeArgs(Service.canOptimizeArgs()),
316+
: Format(Service.getFormat()), OptimizeArgs(Service.getOptimizeArgs()),
316317
EagerLoadModules(Service.shouldEagerLoadModules()) {
317318
PCHContainerOps = std::make_shared<PCHContainerOperations>();
318319
// We need to read object files from PCH built outside the scanner.

clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
529529
CowCompilerInvocation CI =
530530
MDC.getInvocationAdjustedForModuleBuildWithoutOutputs(
531531
MD, [&](CowCompilerInvocation &BuildInvocation) {
532-
if (MDC.OptimizeArgs)
532+
if (any(MDC.OptimizeArgs & ScanningOptimizations::HeaderSearch))
533533
optimizeHeaderSearchOpts(BuildInvocation.getMutHeaderSearchOpts(),
534534
*MDC.ScanInstance.getASTReader(), *MF);
535535
});
@@ -628,7 +628,8 @@ ModuleDepCollector::ModuleDepCollector(
628628
std::unique_ptr<DependencyOutputOptions> Opts,
629629
CompilerInstance &ScanInstance, DependencyConsumer &C,
630630
DependencyActionController &Controller, CompilerInvocation OriginalCI,
631-
bool OptimizeArgs, bool EagerLoadModules, bool IsStdModuleP1689Format)
631+
ScanningOptimizations OptimizeArgs, bool EagerLoadModules,
632+
bool IsStdModuleP1689Format)
632633
: ScanInstance(ScanInstance), Consumer(C), Controller(Controller),
633634
Opts(std::move(Opts)),
634635
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-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
@@ -75,8 +75,8 @@ enum ResourceDirRecipeKind {
7575

7676
static ScanningMode ScanMode = ScanningMode::DependencyDirectivesScan;
7777
static ScanningOutputFormat Format = ScanningOutputFormat::Make;
78+
static ScanningOptimizations OptimizeArgs;
7879
static std::string ModuleFilesDir;
79-
static bool OptimizeArgs;
8080
static bool EagerLoadModules;
8181
static unsigned NumThreads = 0;
8282
static std::string CompilationDB;
@@ -148,10 +148,31 @@ static void ParseArgs(int argc, char **argv) {
148148
Format = *FormatType;
149149
}
150150

151+
std::vector<std::string> OptimizationFlags =
152+
Args.getAllArgValues(OPT_optimize_args_EQ);
153+
OptimizeArgs = ScanningOptimizations::None;
154+
for (const auto &Arg : OptimizationFlags) {
155+
auto Optimization =
156+
llvm::StringSwitch<std::optional<ScanningOptimizations>>(Arg)
157+
.Case("none", ScanningOptimizations::None)
158+
.Case("header-search", ScanningOptimizations::HeaderSearch)
159+
.Case("all", ScanningOptimizations::All)
160+
.Default(std::nullopt);
161+
if (!Optimization) {
162+
llvm::errs()
163+
<< ToolName
164+
<< ": for the --optimize-args option: Cannot find option named '"
165+
<< Arg << "'\n";
166+
std::exit(1);
167+
}
168+
OptimizeArgs |= *Optimization;
169+
}
170+
if (OptimizationFlags.empty())
171+
OptimizeArgs = ScanningOptimizations::Default;
172+
151173
if (const llvm::opt::Arg *A = Args.getLastArg(OPT_module_files_dir_EQ))
152174
ModuleFilesDir = A->getValue();
153175

154-
OptimizeArgs = Args.hasArg(OPT_optimize_args);
155176
EagerLoadModules = Args.hasArg(OPT_eager_load_pcm);
156177

157178
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)">;

llvm/include/llvm/ADT/BitmaskEnum.h

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

9394
namespace llvm {
9495

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

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

0 commit comments

Comments
 (0)