Skip to content

Commit

Permalink
[NFC] Extract unifyTargetFeatures
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D82579
  • Loading branch information
yxsamliu committed Jun 26, 2020
1 parent e0c02dc commit ed398c3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 18 deletions.
20 changes: 2 additions & 18 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,25 +374,9 @@ static void getTargetFeatures(const Driver &D, const llvm::Triple &Triple,
ve::getVETargetFeatures(D, Args, Features);
}

// Find the last of each feature.
llvm::StringMap<unsigned> LastOpt;
for (unsigned I = 0, N = Features.size(); I < N; ++I) {
StringRef Name = Features[I];
assert(Name[0] == '-' || Name[0] == '+');
LastOpt[Name.drop_front(1)] = I;
}

for (unsigned I = 0, N = Features.size(); I < N; ++I) {
// If this feature was overridden, ignore it.
StringRef Name = Features[I];
llvm::StringMap<unsigned>::iterator LastI = LastOpt.find(Name.drop_front(1));
assert(LastI != LastOpt.end());
unsigned Last = LastI->second;
if (Last != I)
continue;

for (auto Feature : unifyTargetFeatures(Features)) {
CmdArgs.push_back(IsAux ? "-aux-target-feature" : "-target-feature");
CmdArgs.push_back(Name.data());
CmdArgs.push_back(Feature.data());
}
}

Expand Down
25 changes: 25 additions & 0 deletions clang/lib/Driver/ToolChains/CommonArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,31 @@ void tools::handleTargetFeaturesGroup(const ArgList &Args,
}
}

std::vector<StringRef>
tools::unifyTargetFeatures(const std::vector<StringRef> &Features) {
std::vector<StringRef> UnifiedFeatures;
// Find the last of each feature.
llvm::StringMap<unsigned> LastOpt;
for (unsigned I = 0, N = Features.size(); I < N; ++I) {
StringRef Name = Features[I];
assert(Name[0] == '-' || Name[0] == '+');
LastOpt[Name.drop_front(1)] = I;
}

for (unsigned I = 0, N = Features.size(); I < N; ++I) {
// If this feature was overridden, ignore it.
StringRef Name = Features[I];
llvm::StringMap<unsigned>::iterator LastI = LastOpt.find(Name.drop_front(1));
assert(LastI != LastOpt.end());
unsigned Last = LastI->second;
if (Last != I)
continue;

UnifiedFeatures.push_back(Name);
}
return UnifiedFeatures;
}

void tools::addDirectoryList(const ArgList &Args, ArgStringList &CmdArgs,
const char *ArgName, const char *EnvVar) {
const char *DirList = ::getenv(EnvVar);
Expand Down
10 changes: 10 additions & 0 deletions clang/lib/Driver/ToolChains/CommonArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,20 @@ void AddTargetFeature(const llvm::opt::ArgList &Args,
std::string getCPUName(const llvm::opt::ArgList &Args, const llvm::Triple &T,
bool FromAs = false);

/// Iterate \p Args and convert -mxxx to +xxx and -mno-xxx to -xxx and
/// append it to \p Features.
///
/// Note: Since \p Features may contain default values before calling
/// this function, or may be appended with entries to override arguments,
/// entries in \p Features are not unique.
void handleTargetFeaturesGroup(const llvm::opt::ArgList &Args,
std::vector<StringRef> &Features,
llvm::opt::OptSpecifier Group);

/// If there are multiple +xxx or -xxx features, keep the last one.
std::vector<StringRef>
unifyTargetFeatures(const std::vector<StringRef> &Features);

/// Handles the -save-stats option and returns the filename to save statistics
/// to.
SmallString<128> getStatsFileName(const llvm::opt::ArgList &Args,
Expand Down
10 changes: 10 additions & 0 deletions clang/test/Driver/hip-toolchain-features.hip
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,13 @@

// ALL3: {{.*}}clang{{.*}}"-target-feature" "+xnack" "-target-feature" "+sram-ecc"
// NOALL3: {{.*}}clang{{.*}}"-target-feature" "-xnack" "-target-feature" "-sram-ecc"

// RUN: %clang -### -target x86_64-linux-gnu -fgpu-rdc -nogpulib \
// RUN: --cuda-gpu-arch=gfx1010 %s \
// RUN: -mcumode -mcumode -mno-cumode -mwavefrontsize64 -mcumode \
// RUN: -mwavefrontsize64 -mno-wavefrontsize64 2>&1 \
// RUN: | FileCheck %s -check-prefix=DUP
// DUP: {{.*}}clang{{.*}} "-target-feature" "-wavefrontsize16"
// DUP-SAME: "-target-feature" "+wavefrontsize32"
// DUP-SAME: "-target-feature" "-wavefrontsize64"
// DUP-SAME: "-target-feature" "+cumode"

0 comments on commit ed398c3

Please sign in to comment.