Skip to content

[clang][DepScan] Make OptimizeArgs a bit mask enum and enable by default #71588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNINGSERVICE_H

#include "clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h"
#include "llvm/ADT/BitmaskEnum.h"

namespace clang {
namespace tooling {
Expand Down Expand Up @@ -44,19 +45,31 @@ enum class ScanningOutputFormat {
P1689,
};

enum class ScanningOptimizations {
None = 0,

/// Remove unused header search paths including header maps.
HeaderSearch = 1,

LLVM_MARK_AS_BITMASK_ENUM(HeaderSearch),
All = HeaderSearch,
Default = All
};

/// The dependency scanning service contains shared configuration and state that
/// is used by the individual dependency scanning workers.
class DependencyScanningService {
public:
DependencyScanningService(ScanningMode Mode, ScanningOutputFormat Format,
bool OptimizeArgs = false,
bool EagerLoadModules = false);
DependencyScanningService(
ScanningMode Mode, ScanningOutputFormat Format,
ScanningOptimizations OptimizeArgs = ScanningOptimizations::Default,
bool EagerLoadModules = false);

ScanningMode getMode() const { return Mode; }

ScanningOutputFormat getFormat() const { return Format; }

bool canOptimizeArgs() const { return OptimizeArgs; }
ScanningOptimizations getOptimizeArgs() const { return OptimizeArgs; }

bool shouldEagerLoadModules() const { return EagerLoadModules; }

Expand All @@ -68,7 +81,7 @@ class DependencyScanningService {
const ScanningMode Mode;
const ScanningOutputFormat Format;
/// Whether to optimize the modules' command-line arguments.
const bool OptimizeArgs;
const ScanningOptimizations OptimizeArgs;
/// Whether to set up command-lines to load PCM files eagerly.
const bool EagerLoadModules;
/// The global file system cache.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class DependencyScanningWorker {
llvm::IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS;
ScanningOutputFormat Format;
/// Whether to optimize the modules' command-line arguments.
bool OptimizeArgs;
ScanningOptimizations OptimizeArgs;
/// Whether to set up command-lines to load PCM files eagerly.
bool EagerLoadModules;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "clang/Lex/HeaderSearch.h"
#include "clang/Lex/PPCallbacks.h"
#include "clang/Serialization/ASTReader.h"
#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/StringSet.h"
Expand Down Expand Up @@ -211,8 +212,9 @@ class ModuleDepCollector final : public DependencyCollector {
ModuleDepCollector(std::unique_ptr<DependencyOutputOptions> Opts,
CompilerInstance &ScanInstance, DependencyConsumer &C,
DependencyActionController &Controller,
CompilerInvocation OriginalCI, bool OptimizeArgs,
bool EagerLoadModules, bool IsStdModuleP1689Format);
CompilerInvocation OriginalCI,
ScanningOptimizations OptimizeArgs, bool EagerLoadModules,
bool IsStdModuleP1689Format);

void attachToPreprocessor(Preprocessor &PP) override;
void attachToASTReader(ASTReader &R) override;
Expand Down Expand Up @@ -254,7 +256,7 @@ class ModuleDepCollector final : public DependencyCollector {
/// for each individual module.
CowCompilerInvocation CommonInvocation;
/// Whether to optimize the modules' command-line arguments.
bool OptimizeArgs;
ScanningOptimizations OptimizeArgs;
/// Whether to set up command-lines to load PCM files eagerly.
bool EagerLoadModules;
/// If we're generating dependency output in P1689 format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ using namespace tooling;
using namespace dependencies;

DependencyScanningService::DependencyScanningService(
ScanningMode Mode, ScanningOutputFormat Format, bool OptimizeArgs,
bool EagerLoadModules)
ScanningMode Mode, ScanningOutputFormat Format,
ScanningOptimizations OptimizeArgs, bool EagerLoadModules)
: Mode(Mode), Format(Format), OptimizeArgs(OptimizeArgs),
EagerLoadModules(EagerLoadModules) {
// Initialize targets for object file support.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,9 @@ class DependencyScanningAction : public tooling::ToolAction {
StringRef WorkingDirectory, DependencyConsumer &Consumer,
DependencyActionController &Controller,
llvm::IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS,
ScanningOutputFormat Format, bool OptimizeArgs, bool EagerLoadModules,
bool DisableFree, std::optional<StringRef> ModuleName = std::nullopt)
ScanningOutputFormat Format, ScanningOptimizations OptimizeArgs,
bool EagerLoadModules, bool DisableFree,
std::optional<StringRef> ModuleName = std::nullopt)
: WorkingDirectory(WorkingDirectory), Consumer(Consumer),
Controller(Controller), DepFS(std::move(DepFS)), Format(Format),
OptimizeArgs(OptimizeArgs), EagerLoadModules(EagerLoadModules),
Expand Down Expand Up @@ -297,7 +298,7 @@ class DependencyScanningAction : public tooling::ToolAction {
DependencyActionController &Controller;
llvm::IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS;
ScanningOutputFormat Format;
bool OptimizeArgs;
ScanningOptimizations OptimizeArgs;
bool EagerLoadModules;
bool DisableFree;
std::optional<StringRef> ModuleName;
Expand All @@ -312,7 +313,7 @@ class DependencyScanningAction : public tooling::ToolAction {
DependencyScanningWorker::DependencyScanningWorker(
DependencyScanningService &Service,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)
: Format(Service.getFormat()), OptimizeArgs(Service.canOptimizeArgs()),
: Format(Service.getFormat()), OptimizeArgs(Service.getOptimizeArgs()),
EagerLoadModules(Service.shouldEagerLoadModules()) {
PCHContainerOps = std::make_shared<PCHContainerOperations>();
// We need to read object files from PCH built outside the scanner.
Expand Down
5 changes: 3 additions & 2 deletions clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
CowCompilerInvocation CI =
MDC.getInvocationAdjustedForModuleBuildWithoutOutputs(
MD, [&](CowCompilerInvocation &BuildInvocation) {
if (MDC.OptimizeArgs)
if (any(MDC.OptimizeArgs & ScanningOptimizations::HeaderSearch))
optimizeHeaderSearchOpts(BuildInvocation.getMutHeaderSearchOpts(),
*MDC.ScanInstance.getASTReader(), *MF);
});
Expand Down Expand Up @@ -628,7 +628,8 @@ ModuleDepCollector::ModuleDepCollector(
std::unique_ptr<DependencyOutputOptions> Opts,
CompilerInstance &ScanInstance, DependencyConsumer &C,
DependencyActionController &Controller, CompilerInvocation OriginalCI,
bool OptimizeArgs, bool EagerLoadModules, bool IsStdModuleP1689Format)
ScanningOptimizations OptimizeArgs, bool EagerLoadModules,
bool IsStdModuleP1689Format)
: ScanInstance(ScanInstance), Consumer(C), Controller(Controller),
Opts(std::move(Opts)),
CommonInvocation(
Expand Down
4 changes: 2 additions & 2 deletions clang/test/ClangScanDeps/header-search-pruning-transitive.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ module X { header "X.h" }
// RUN: sed -e "s|DIR|%/t|g" %t/cdb_with_a.json.template > %t/cdb_with_a.json
// RUN: sed -e "s|DIR|%/t|g" %t/cdb_without_a.json.template > %t/cdb_without_a.json

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

// CHECK: {
Expand Down
6 changes: 3 additions & 3 deletions clang/test/ClangScanDeps/header-search-pruning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
// RUN: sed -e "s|DIR|%/t|g" -e "s|DEFINES|-DINCLUDE_B|g" %S/Inputs/header-search-pruning/cdb.json > %t/cdb_b.json
// 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
//
// RUN: clang-scan-deps -compilation-database %t/cdb_a.json -format experimental-full -optimize-args >> %t/result_a.json
// RUN: clang-scan-deps -compilation-database %t/cdb_a.json -format experimental-full -optimize-args=header-search >> %t/result_a.json
// RUN: cat %t/result_a.json | sed 's:\\\\\?:/:g' | FileCheck --check-prefixes=CHECK_A %s
//
// RUN: clang-scan-deps -compilation-database %t/cdb_b.json -format experimental-full -optimize-args >> %t/result_b.json
// RUN: clang-scan-deps -compilation-database %t/cdb_b.json -format experimental-full -optimize-args=header-search >> %t/result_b.json
// RUN: cat %t/result_b.json | sed 's:\\\\\?:/:g' | FileCheck --check-prefixes=CHECK_B %s
//
// RUN: clang-scan-deps -compilation-database %t/cdb_ab.json -format experimental-full -optimize-args >> %t/result_ab.json
// RUN: clang-scan-deps -compilation-database %t/cdb_ab.json -format experimental-full -optimize-args=header-search >> %t/result_ab.json
// RUN: cat %t/result_ab.json | sed 's:\\\\\?:/:g' | FileCheck --check-prefixes=CHECK_AB %s

#include "mod.h"
Expand Down
2 changes: 1 addition & 1 deletion clang/test/ClangScanDeps/modules-symlink-dir-from-module.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

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

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

Expand Down
2 changes: 1 addition & 1 deletion clang/test/ClangScanDeps/modules-symlink-dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

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

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

Expand Down
25 changes: 23 additions & 2 deletions clang/tools/clang-scan-deps/ClangScanDeps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ enum ResourceDirRecipeKind {

static ScanningMode ScanMode = ScanningMode::DependencyDirectivesScan;
static ScanningOutputFormat Format = ScanningOutputFormat::Make;
static ScanningOptimizations OptimizeArgs;
static std::string ModuleFilesDir;
static bool OptimizeArgs;
static bool EagerLoadModules;
static unsigned NumThreads = 0;
static std::string CompilationDB;
Expand Down Expand Up @@ -148,10 +148,31 @@ static void ParseArgs(int argc, char **argv) {
Format = *FormatType;
}

std::vector<std::string> OptimizationFlags =
Args.getAllArgValues(OPT_optimize_args_EQ);
OptimizeArgs = ScanningOptimizations::None;
for (const auto &Arg : OptimizationFlags) {
auto Optimization =
llvm::StringSwitch<std::optional<ScanningOptimizations>>(Arg)
.Case("none", ScanningOptimizations::None)
.Case("header-search", ScanningOptimizations::HeaderSearch)
.Case("all", ScanningOptimizations::All)
.Default(std::nullopt);
if (!Optimization) {
llvm::errs()
<< ToolName
<< ": for the --optimize-args option: Cannot find option named '"
<< Arg << "'\n";
std::exit(1);
}
OptimizeArgs |= *Optimization;
}
if (OptimizationFlags.empty())
OptimizeArgs = ScanningOptimizations::Default;

if (const llvm::opt::Arg *A = Args.getLastArg(OPT_module_files_dir_EQ))
ModuleFilesDir = A->getValue();

OptimizeArgs = Args.hasArg(OPT_optimize_args);
EagerLoadModules = Args.hasArg(OPT_eager_load_pcm);

if (const llvm::opt::Arg *A = Args.getLastArg(OPT_j)) {
Expand Down
2 changes: 1 addition & 1 deletion clang/tools/clang-scan-deps/Opts.td
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ defm format : Eq<"format", "The output format for the dependencies">;
defm module_files_dir : Eq<"module-files-dir",
"The build directory for modules. Defaults to the value of '-fmodules-cache-path=' from command lines for implicit modules">;

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

def j : Arg<"j", "Number of worker threads to use (default: use all concurrent threads)">;
Expand Down
8 changes: 7 additions & 1 deletion llvm/include/llvm/ADT/BitmaskEnum.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@
using ::llvm::BitmaskEnumDetail::operator^; \
using ::llvm::BitmaskEnumDetail::operator|=; \
using ::llvm::BitmaskEnumDetail::operator&=; \
using ::llvm::BitmaskEnumDetail::operator^=; \
/* Force a semicolon at the end of this macro. */ \
using ::llvm::BitmaskEnumDetail::operator^=
using ::llvm::BitmaskEnumDetail::any

namespace llvm {

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

template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
constexpr bool any(E Val) {
return Val != static_cast<E>(0);
}

template <typename E, typename = std::enable_if_t<is_bitmask_enum<E>::value>>
constexpr E operator~(E Val) {
return static_cast<E>(~Underlying(Val) & Mask<E>());
Expand Down