Skip to content

Commit c91f9b7

Browse files
authored
merge main into amd-staging (llvm#4139)
2 parents 3f4d5c4 + 7b5f9b8 commit c91f9b7

File tree

16 files changed

+309
-76
lines changed

16 files changed

+309
-76
lines changed

.github/workflows/pr-code-lint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ on:
1010
- 'users/**'
1111
paths:
1212
- 'clang-tools-extra/clang-tidy/**'
13+
- '.github/workflows/pr-code-lint.yml'
1314

1415
jobs:
1516
code_linter:

clang-tools-extra/clangd/FeatureModule.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ FeatureModule::Facilities &FeatureModule::facilities() {
2222
return *Fac;
2323
}
2424

25+
void FeatureModuleSet::add(std::unique_ptr<FeatureModule> M) {
26+
Modules.push_back(std::move(M));
27+
}
28+
2529
bool FeatureModuleSet::addImpl(void *Key, std::unique_ptr<FeatureModule> M,
2630
const char *Source) {
2731
if (!Map.try_emplace(Key, M.get()).second) {
@@ -33,5 +37,16 @@ bool FeatureModuleSet::addImpl(void *Key, std::unique_ptr<FeatureModule> M,
3337
return true;
3438
}
3539

40+
FeatureModuleSet FeatureModuleSet::fromRegistry() {
41+
FeatureModuleSet ModuleSet;
42+
for (FeatureModuleRegistry::entry E : FeatureModuleRegistry::entries()) {
43+
vlog("Adding feature module '{0}' ({1})", E.getName(), E.getDesc());
44+
ModuleSet.add(E.instantiate());
45+
}
46+
return ModuleSet;
47+
}
48+
3649
} // namespace clangd
3750
} // namespace clang
51+
52+
LLVM_INSTANTIATE_REGISTRY(clang::clangd::FeatureModuleRegistry)

clang-tools-extra/clangd/FeatureModule.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "llvm/ADT/FunctionExtras.h"
1616
#include "llvm/Support/Compiler.h"
1717
#include "llvm/Support/JSON.h"
18+
#include "llvm/Support/Registry.h"
1819
#include <memory>
1920
#include <optional>
2021
#include <type_traits>
@@ -143,9 +144,14 @@ class FeatureModule {
143144

144145
/// A FeatureModuleSet is a collection of feature modules installed in clangd.
145146
///
146-
/// Modules can be looked up by type, or used via the FeatureModule interface.
147-
/// This allows individual modules to expose a public API.
148-
/// For this reason, there can be only one feature module of each type.
147+
/// Modules added with explicit type specification can be looked up by type, or
148+
/// used via the FeatureModule interface. This allows individual modules to
149+
/// expose a public API. For this reason, there can be only one feature module
150+
/// of each type.
151+
///
152+
/// Modules added using a base class pointer can be used only via the
153+
/// FeatureModule interface and can't be looked up by type, thus custom public
154+
/// API (if provided by the module) can't be used.
149155
///
150156
/// The set owns the modules. It is itself owned by main, not ClangdServer.
151157
class FeatureModuleSet {
@@ -164,6 +170,8 @@ class FeatureModuleSet {
164170
public:
165171
FeatureModuleSet() = default;
166172

173+
static FeatureModuleSet fromRegistry();
174+
167175
using iterator = llvm::pointee_iterator<decltype(Modules)::iterator>;
168176
using const_iterator =
169177
llvm::pointee_iterator<decltype(Modules)::const_iterator>;
@@ -172,6 +180,7 @@ class FeatureModuleSet {
172180
const_iterator begin() const { return const_iterator(Modules.begin()); }
173181
const_iterator end() const { return const_iterator(Modules.end()); }
174182

183+
void add(std::unique_ptr<FeatureModule> M);
175184
template <typename Mod> bool add(std::unique_ptr<Mod> M) {
176185
return addImpl(&ID<Mod>::Key, std::move(M), LLVM_PRETTY_FUNCTION);
177186
}
@@ -185,6 +194,13 @@ class FeatureModuleSet {
185194

186195
template <typename Mod> int FeatureModuleSet::ID<Mod>::Key;
187196

197+
using FeatureModuleRegistry = llvm::Registry<FeatureModule>;
198+
188199
} // namespace clangd
189200
} // namespace clang
201+
202+
namespace llvm {
203+
extern template class Registry<clang::clangd::FeatureModule>;
204+
} // namespace llvm
205+
190206
#endif

clang-tools-extra/clangd/tool/ClangdMain.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "Config.h"
1414
#include "ConfigProvider.h"
1515
#include "Feature.h"
16+
#include "FeatureModule.h"
1617
#include "IncludeCleaner.h"
1718
#include "PathMapping.h"
1819
#include "Protocol.h"
@@ -1017,6 +1018,10 @@ clangd accepts flags on the commandline, and in the CLANGD_FLAGS environment var
10171018
: static_cast<int>(ErrorResultCode::CheckFailed);
10181019
}
10191020

1021+
FeatureModuleSet ModuleSet = FeatureModuleSet::fromRegistry();
1022+
if (ModuleSet.begin() != ModuleSet.end())
1023+
Opts.FeatureModules = &ModuleSet;
1024+
10201025
// Initialize and run ClangdLSPServer.
10211026
// Change stdin to binary to not lose \r\n on windows.
10221027
llvm::sys::ChangeStdinToBinary();

clang-tools-extra/clangd/unittests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ add_unittest(ClangdUnitTests ClangdTests
5454
DumpASTTests.cpp
5555
ExpectedTypeTest.cpp
5656
FeatureModulesTests.cpp
57+
FeatureModulesRegistryTests.cpp
5758
FileDistanceTests.cpp
5859
FileIndexTests.cpp
5960
FindSymbolsTests.cpp
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//===--- FeatureModulesRegistryTests.cpp ---------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "FeatureModule.h"
10+
#include "refactor/Tweak.h"
11+
#include "support/Logger.h"
12+
13+
#include "gmock/gmock.h"
14+
#include "gtest/gtest.h"
15+
16+
using testing::ElementsAre;
17+
18+
namespace llvm {
19+
raw_ostream &operator<<(raw_ostream &OS,
20+
const clang::clangd::FeatureModuleRegistry::entry &E) {
21+
OS << "(name = " << E.getName() << ", description = '" << E.getDesc() << "')";
22+
return OS;
23+
}
24+
25+
raw_ostream &operator<<(
26+
raw_ostream &OS,
27+
const iterator_range<Registry<clang::clangd::FeatureModule>::iterator>
28+
&Rng) {
29+
OS << "{ ";
30+
bool First = true;
31+
for (clang::clangd::FeatureModuleRegistry::entry E : Rng) {
32+
if (First)
33+
First = false;
34+
else
35+
OS << ", ";
36+
OS << E;
37+
}
38+
OS << " }";
39+
return OS;
40+
}
41+
42+
raw_ostream &operator<<(raw_ostream &OS, const clang::clangd::Tweak &T) {
43+
OS << "(id = " << T.id() << ", "
44+
<< "title = " << T.title() << ")";
45+
return OS;
46+
}
47+
} // namespace llvm
48+
49+
namespace clang::clangd {
50+
namespace {
51+
52+
class Dummy final : public FeatureModule {
53+
static constexpr const char *TweakID = "DummyTweak";
54+
struct DummyTweak final : public Tweak {
55+
const char *id() const override { return TweakID; }
56+
bool prepare(const Selection &) override { return true; }
57+
Expected<Effect> apply(const Selection &) override {
58+
return error("not implemented");
59+
}
60+
std::string title() const override { return id(); }
61+
llvm::StringLiteral kind() const override {
62+
return llvm::StringLiteral("");
63+
};
64+
};
65+
66+
void contributeTweaks(std::vector<std::unique_ptr<Tweak>> &Out) override {
67+
Out.emplace_back(new DummyTweak);
68+
}
69+
};
70+
71+
static FeatureModuleRegistry::Add<Dummy>
72+
X("dummy", "Dummy feature module with dummy tweak");
73+
74+
MATCHER_P(moduleName, Name, "") { return arg.getName() == Name; }
75+
MATCHER_P(tweakID, ID, "") { return arg->id() == llvm::StringRef(ID); }
76+
77+
// In this test, it is assumed that for unittests executable, all feature
78+
// modules are added to the registry only here (in this file). To implement
79+
// modules for clangd tool, one need to link them directly to the clangd
80+
// executable in clangd/tool/CMakeLists.txt.
81+
TEST(FeatureModulesRegistryTest, DummyModule) {
82+
EXPECT_THAT(FeatureModuleRegistry::entries(),
83+
ElementsAre(moduleName("dummy")));
84+
FeatureModuleSet Set = FeatureModuleSet::fromRegistry();
85+
ASSERT_EQ(Set.end() - Set.begin(), 1u);
86+
std::vector<std::unique_ptr<Tweak>> Tweaks;
87+
Set.begin()->contributeTweaks(Tweaks);
88+
EXPECT_THAT(Tweaks, ElementsAre(tweakID("DummyTweak")));
89+
}
90+
91+
} // namespace
92+
} // namespace clang::clangd

llvm/include/llvm/ADT/Hashing.h

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -490,11 +490,8 @@ namespace detail {
490490
/// recursive combining of arguments used in hash_combine. It is particularly
491491
/// useful at minimizing the code in the recursive calls to ease the pain
492492
/// caused by a lack of variadic functions.
493-
struct hash_combine_helper {
493+
struct hash_combine_recursive_helper {
494494
char buffer[64] = {};
495-
char *buffer_ptr;
496-
char *const buffer_end;
497-
size_t length = 0;
498495
hash_state state;
499496
const uint64_t seed;
500497

@@ -503,17 +500,17 @@ struct hash_combine_helper {
503500
///
504501
/// This sets up the state for a recursive hash combine, including getting
505502
/// the seed and buffer setup.
506-
hash_combine_helper()
507-
: buffer_ptr(buffer), buffer_end(buffer + 64),
508-
seed(get_execution_seed()) {}
503+
hash_combine_recursive_helper()
504+
: seed(get_execution_seed()) {}
509505

510506
/// Combine one chunk of data into the current in-flight hash.
511507
///
512508
/// This merges one chunk of data into the hash. First it tries to buffer
513509
/// the data. If the buffer is full, it hashes the buffer into its
514510
/// hash_state, empties it, and then merges the new chunk in. This also
515511
/// handles cases where the data straddles the end of the buffer.
516-
template <typename T> void combine_data(T data) {
512+
template <typename T>
513+
char *combine_data(size_t &length, char *buffer_ptr, char *buffer_end, T data) {
517514
if (!store_and_advance(buffer_ptr, buffer_end, data)) {
518515
// Check for skew which prevents the buffer from being packed, and do
519516
// a partial store into the buffer to fill it. This is only a concern
@@ -544,17 +541,28 @@ struct hash_combine_helper {
544541
partial_store_size))
545542
llvm_unreachable("buffer smaller than stored type");
546543
}
544+
return buffer_ptr;
547545
}
548546

549547
/// Recursive, variadic combining method.
550548
///
551549
/// This function recurses through each argument, combining that argument
552550
/// into a single hash.
553-
template <typename... Ts> hash_code combine(const Ts &...args) {
554-
(combine_data(get_hashable_data(args)), ...);
551+
template <typename T, typename ...Ts>
552+
hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
553+
const T &arg, const Ts &...args) {
554+
buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg));
555555

556-
// Finalize the hash by flushing any remaining data in the buffer.
557-
//
556+
// Recurse to the next argument.
557+
return combine(length, buffer_ptr, buffer_end, args...);
558+
}
559+
560+
/// Base case for recursive, variadic combining.
561+
///
562+
/// The base case when combining arguments recursively is reached when all
563+
/// arguments have been handled. It flushes the remaining buffer and
564+
/// constructs a hash_code.
565+
hash_code combine(size_t length, char *buffer_ptr, char *buffer_end) {
558566
// Check whether the entire set of values fit in the buffer. If so, we'll
559567
// use the optimized short hashing routine and skip state entirely.
560568
if (length == 0)
@@ -588,10 +596,10 @@ struct hash_combine_helper {
588596
/// The result is suitable for returning from a user's hash_value
589597
/// *implementation* for their user-defined type. Consumers of a type should
590598
/// *not* call this routine, they should instead call 'hash_value'.
591-
template <typename... Ts> hash_code hash_combine(const Ts &...args) {
599+
template <typename ...Ts> hash_code hash_combine(const Ts &...args) {
592600
// Recursively hash each argument using a helper class.
593-
::llvm::hashing::detail::hash_combine_helper helper;
594-
return helper.combine(args...);
601+
::llvm::hashing::detail::hash_combine_recursive_helper helper;
602+
return helper.combine(0, helper.buffer, helper.buffer + 64, args...);
595603
}
596604

597605
// Implementation details for implementations of hash_value overloads provided

llvm/lib/CodeGen/GlobalISel/GISelValueTracking.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,14 @@ void GISelValueTracking::computeKnownBitsImpl(Register R, KnownBits &Known,
675675
}
676676
break;
677677
}
678+
case TargetOpcode::G_ABS: {
679+
Register SrcReg = MI.getOperand(1).getReg();
680+
computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
681+
Known = Known.abs();
682+
Known.Zero.setHighBits(computeNumSignBits(SrcReg, DemandedElts, Depth + 1) -
683+
1);
684+
break;
685+
}
678686
}
679687

680688
LLVM_DEBUG(dumpResult(MI, Known, Depth));

llvm/lib/Transforms/IPO/FunctionSpecialization.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -796,18 +796,19 @@ bool FunctionSpecializer::run() {
796796
if (Count && !ProfcheckDisableMetadataFixes) {
797797
std::optional<llvm::Function::ProfileCount> MaybeCloneCount =
798798
Clone->getEntryCount();
799-
assert(MaybeCloneCount && "Clone entry count was not set!");
800-
uint64_t CallCount = *Count + MaybeCloneCount->getCount();
801-
Clone->setEntryCount(CallCount);
802-
if (std::optional<llvm::Function::ProfileCount> MaybeOriginalCount =
803-
S.F->getEntryCount()) {
804-
uint64_t OriginalCount = MaybeOriginalCount->getCount();
805-
if (OriginalCount >= *Count) {
806-
S.F->setEntryCount(OriginalCount - *Count);
807-
} else {
808-
// This should generally not happen as that would mean there are
809-
// more computed calls to the function than what was recorded.
810-
LLVM_DEBUG(S.F->setEntryCount(0));
799+
if (MaybeCloneCount) {
800+
uint64_t CallCount = *Count + MaybeCloneCount->getCount();
801+
Clone->setEntryCount(CallCount);
802+
if (std::optional<llvm::Function::ProfileCount> MaybeOriginalCount =
803+
S.F->getEntryCount()) {
804+
uint64_t OriginalCount = MaybeOriginalCount->getCount();
805+
if (OriginalCount >= *Count) {
806+
S.F->setEntryCount(OriginalCount - *Count);
807+
} else {
808+
// This should generally not happen as that would mean there are
809+
// more computed calls to the function than what was recorded.
810+
LLVM_DEBUG(S.F->setEntryCount(0));
811+
}
811812
}
812813
}
813814
}

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5717,8 +5717,11 @@ void LoopVectorizationCostModel::setCostBasedWideningDecision(ElementCount VF) {
57175717
// if the loaded register is involved in an address computation, it is
57185718
// instead changed here when we know this is the case.
57195719
InstWidening Decision = getWideningDecision(I, VF);
5720-
if (Decision == CM_Widen || Decision == CM_Widen_Reverse)
5721-
// Scalarize a widened load of address.
5720+
if (Decision == CM_Widen || Decision == CM_Widen_Reverse ||
5721+
(!isPredicatedInst(I) && !Legal->isUniformMemOp(*I, VF) &&
5722+
Decision == CM_Scalarize))
5723+
// Scalarize a widened load of address or update the cost of a scalar
5724+
// load of an address.
57225725
setWideningDecision(
57235726
I, VF, CM_Scalarize,
57245727
(VF.getKnownMinValue() *

0 commit comments

Comments
 (0)