[PATCH] D112385: [ELF] Support 128-bit bitmask in oneof(RelExpr)
Fangrui Song via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sun Oct 24 12:04:54 PDT 2021
MaskRay created this revision.
MaskRay added reviewers: arichardson, jhenderson, peter.smith, PkmX.
Herald added subscribers: pengfei, emaste.
MaskRay requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
Taken from Chih-Mao Chen's D100835 <https://reviews.llvm.org/D100835>.
RelExpr has 64 bits now and needs the extension to support new members
(`R_PLT_GOTPLT` for `R_X86_64_PLTOFF64` support).
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D112385
Files:
lld/ELF/Relocations.cpp
Index: lld/ELF/Relocations.cpp
===================================================================
--- lld/ELF/Relocations.cpp
+++ lld/ELF/Relocations.cpp
@@ -125,35 +125,28 @@
}
namespace {
-// Build a bitmask with one bit set for each RelExpr.
-//
-// Constexpr function arguments can't be used in static asserts, so we
-// use template arguments to build the mask.
-// But function template partial specializations don't exist (needed
-// for base case of the recursion), so we need a dummy struct.
-template <RelExpr... Exprs> struct RelExprMaskBuilder {
- static inline uint64_t build() { return 0; }
-};
+// Build a bitmask with one bit set for each 64 subset of RelExpr.
+constexpr uint64_t buildMask() { return 0; }
-// Specialization for recursive case.
-template <RelExpr Head, RelExpr... Tail>
-struct RelExprMaskBuilder<Head, Tail...> {
- static inline uint64_t build() {
- static_assert(0 <= Head && Head < 64,
- "RelExpr is too large for 64-bit mask!");
- return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
- }
-};
+template <typename... Tails>
+constexpr uint64_t buildMask(int head, Tails... tails) {
+ return (0 <= head && head < 64 ? (uint64_t(1) << head) : 0) |
+ buildMask(tails...);
+}
} // namespace
// Return true if `Expr` is one of `Exprs`.
-// There are fewer than 64 RelExpr's, so we can represent any set of
-// RelExpr's as a constant bit mask and test for membership with a
-// couple cheap bitwise operations.
+// There are more than 64 but less than 128 RelExprs, so we divide the set of
+// exprs into [0, 64) and [64, 128) and represent each range as a constant
+// 64-bit mask. Then we decide which mask to test depending on the value of
+// expr and use a simple shift and bitwise-and to test for membership.
template <RelExpr... Exprs> bool oneof(RelExpr expr) {
- assert(0 <= expr && (int)expr < 64 &&
- "RelExpr is too large for 64-bit mask!");
- return (uint64_t(1) << expr) & RelExprMaskBuilder<Exprs...>::build();
+ assert(0 <= expr && (int)expr < 128 &&
+ "RelExpr is too large for 128-bit mask!");
+
+ if (expr >= 64)
+ return (uint64_t(1) << (expr - 64)) & buildMask((Exprs - 64)...);
+ return (uint64_t(1) << expr) & buildMask(Exprs...);
}
static RelType getMipsPairType(RelType type, bool isLocal) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112385.381791.patch
Type: text/x-patch
Size: 2339 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211024/f3bbb223/attachment.bin>
More information about the llvm-commits
mailing list