Skip to content

Commit

Permalink
Export of internal Abseil changes
Browse files Browse the repository at this point in the history
--
d447fdcb801036cf08197eece193a5a706661120 by Gennadiy Rozental <rogeeff@google.com>:

Eliminate the need for static function holding help message. This decreases the cost of ABSL_FLAG abstraction by 120 bytes under clang.

PiperOrigin-RevId: 281107806

--
0aa6b91189f0e8b2381438c33465673a7ae02487 by Derek Mauro <dmauro@google.com>:

Disable the weak symbol CCTZ extension in the time test_util
on MinGW, which does not support it.

PiperOrigin-RevId: 280719769

--
67322c41c3e776eb541de90fa4526bdb49422eb6 by Abseil Team <absl-team@google.com>:

Tune PeriodicSampler implementation (for internal-use only)

PiperOrigin-RevId: 280708943

--
3a48c346340c7ed03816645cd327e1ff07729aa4 by Abseil Team <absl-team@google.com>:

Clean up public headers not to have warnings for "-Wcomma"

PiperOrigin-RevId: 280695373

--
981acd1ef3b13a83a84f04f11c8931f4ed4451c9 by Matthew Brown <matthewbr@google.com>:

Release absl::int128.

PiperOrigin-RevId: 280690817

--
d30fae9d2ec30b81322d2eb5afe7e13e45b4b422 by Derek Mauro <dmauro@google.com>:

Fix -Wundef warnings in random platform detection

PiperOrigin-RevId: 280669598
GitOrigin-RevId: d447fdcb801036cf08197eece193a5a706661120
Change-Id: Ie5e10e567c54b7de211833607689f233d4ddf734
  • Loading branch information
Abseil Team authored and gennadiycivil committed Nov 19, 2019
1 parent 3df7b52 commit 2103fd9
Show file tree
Hide file tree
Showing 23 changed files with 2,800 additions and 136 deletions.
35 changes: 28 additions & 7 deletions absl/base/internal/bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,22 @@ namespace base_internal {

ABSL_BASE_INTERNAL_FORCEINLINE int CountLeadingZeros64Slow(uint64_t n) {
int zeroes = 60;
if (n >> 32) zeroes -= 32, n >>= 32;
if (n >> 16) zeroes -= 16, n >>= 16;
if (n >> 8) zeroes -= 8, n >>= 8;
if (n >> 4) zeroes -= 4, n >>= 4;
if (n >> 32) {
zeroes -= 32;
n >>= 32;
}
if (n >> 16) {
zeroes -= 16;
n >>= 16;
}
if (n >> 8) {
zeroes -= 8;
n >>= 8;
}
if (n >> 4) {
zeroes -= 4;
n >>= 4;
}
return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[n] + zeroes;
}

Expand Down Expand Up @@ -95,9 +107,18 @@ ABSL_BASE_INTERNAL_FORCEINLINE int CountLeadingZeros64(uint64_t n) {

ABSL_BASE_INTERNAL_FORCEINLINE int CountLeadingZeros32Slow(uint64_t n) {
int zeroes = 28;
if (n >> 16) zeroes -= 16, n >>= 16;
if (n >> 8) zeroes -= 8, n >>= 8;
if (n >> 4) zeroes -= 4, n >>= 4;
if (n >> 16) {
zeroes -= 16;
n >>= 16;
}
if (n >> 8) {
zeroes -= 8;
n >>= 8;
}
if (n >> 4) {
zeroes -= 4;
n >>= 4;
}
return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[n] + zeroes;
}

Expand Down
6 changes: 3 additions & 3 deletions absl/base/internal/periodic_sampler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ bool PeriodicSamplerBase::SubtleConfirmSample() noexcept {

// Check if this is the first call to Sample()
if (ABSL_PREDICT_FALSE(stride_ == 1)) {
stride_ = -1 - GetExponentialBiased(current_period);
if (stride_ < -1) {
stride_ = static_cast<uint64_t>(-1 - GetExponentialBiased(current_period));
if (static_cast<int64_t>(stride_) < -1) {
++stride_;
return false;
}
}
stride_ = -1 - GetExponentialBiased(current_period);
stride_ = static_cast<uint64_t>(-1 - GetExponentialBiased(current_period));
return true;
}

Expand Down
62 changes: 39 additions & 23 deletions absl/base/internal/periodic_sampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,33 +111,49 @@ class PeriodicSamplerBase {
// Returns the current period of this sampler. Thread-safe.
virtual int period() const noexcept = 0;

int64_t stride_ = 0;
// Keep and decrement stride_ as an unsigned integer, but compare the value
// to zero casted as a signed int. clang and msvc do not create optimum code
// if we use signed for the combined decrement and sign comparison.
//
// Below 3 alternative options, all compiles generate the best code
// using the unsigned increment <---> signed int comparison option.
//
// Option 1:
// int64_t stride_;
// if (ABSL_PREDICT_TRUE(++stride_ < 0)) { ... }
//
// GCC x64 (OK) : https://gcc.godbolt.org/z/R5MzzA
// GCC ppc (OK) : https://gcc.godbolt.org/z/z7NZAt
// Clang x64 (BAD): https://gcc.godbolt.org/z/t4gPsd
// ICC x64 (OK) : https://gcc.godbolt.org/z/rE6s8W
// MSVC x64 (OK) : https://gcc.godbolt.org/z/ARMXqS
//
// Option 2:
// int64_t stride_ = 0;
// if (ABSL_PREDICT_TRUE(--stride_ >= 0)) { ... }
//
// GCC x64 (OK) : https://gcc.godbolt.org/z/jSQxYK
// GCC ppc (OK) : https://gcc.godbolt.org/z/VJdYaA
// Clang x64 (BAD): https://gcc.godbolt.org/z/Xm4NjX
// ICC x64 (OK) : https://gcc.godbolt.org/z/4snaFd
// MSVC x64 (BAD): https://gcc.godbolt.org/z/BgnEKE
//
// Option 3:
// uint64_t stride_;
// if (ABSL_PREDICT_TRUE(static_cast<int64_t>(++stride_) < 0)) { ... }
//
// GCC x64 (OK) : https://gcc.godbolt.org/z/bFbfPy
// GCC ppc (OK) : https://gcc.godbolt.org/z/S9KkUE
// Clang x64 (OK) : https://gcc.godbolt.org/z/UYzRb4
// ICC x64 (OK) : https://gcc.godbolt.org/z/ptTNfD
// MSVC x64 (OK) : https://gcc.godbolt.org/z/76j4-5
uint64_t stride_ = 0;
ExponentialBiased rng_;
};

inline bool PeriodicSamplerBase::SubtleMaybeSample() noexcept {
// We explicitly count up and not down, as the compiler does not generate
// ideal code for counting down. See also https://gcc.godbolt.org/z/FTPDSM
//
// With `if (ABSL_PREDICT_FALSE(++stride_ < 0))`
// add QWORD PTR fs:sampler@tpoff+8, 1
// jns .L15
// ret
//
// With `if (ABSL_PREDICT_FALSE(--stride_ > 0))`
// mov rax, QWORD PTR fs:sampler@tpoff+8
// sub rax, 1
// mov QWORD PTR fs:sampler@tpoff+8, rax
// test rax, rax
// jle .L15
// ret
// add QWORD PTR fs:sampler@tpoff+8, 1
// jns .L15
// ret
//
// --stride >= 0 does work, but makes our logic slightly harder as in that
// case we have less convenient zero-init and over-run values.
if (ABSL_PREDICT_FALSE(++stride_ < 0)) {
// See comments on `stride_` for the unsigned increment / signed compare.
if (ABSL_PREDICT_TRUE(static_cast<int64_t>(++stride_) < 0)) {
return false;
}
return true;
Expand Down
57 changes: 34 additions & 23 deletions absl/flags/flag.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ class Flag {
constexpr Flag(const char* name, const flags_internal::HelpGenFunc help_gen,
const char* filename,
const flags_internal::FlagMarshallingOpFn marshalling_op,
const flags_internal::InitialValGenFunc initial_value_gen,
bool, void*)
const flags_internal::InitialValGenFunc initial_value_gen)
: name_(name),
help_gen_(help_gen),
filename_(filename),
Expand All @@ -115,8 +114,11 @@ class Flag {
return impl_;
}

impl_ = new flags_internal::Flag<T>(name_, help_gen_, filename_,
marshalling_op_, initial_value_gen_);
impl_ = new flags_internal::Flag<T>(
name_,
{flags_internal::FlagHelpSrc(help_gen_),
flags_internal::FlagHelpSrcKind::kGenFunc},
filename_, marshalling_op_, initial_value_gen_);
inited_.store(true, std::memory_order_release);
}

Expand Down Expand Up @@ -307,9 +309,19 @@ void SetFlag(absl::Flag<T>* flag, const V& v) {
#define ABSL_FLAG_IMPL_FLAGHELP(txt) txt
#endif

#define ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, txt) \
static std::string AbslFlagsWrapHelp##name() { \
return ABSL_FLAG_IMPL_FLAGHELP(txt); \
// AbslFlagHelpGenFor##name is used to encapsulate both immediate (method Const)
// and lazy (method NonConst) evaluation of help message expression. We choose
// between the two via the call to HelpArg in absl::Flag instantiation below.
// If help message expression is constexpr evaluable compiler will optimize
// away this whole struct.
#define ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, txt) \
struct AbslFlagHelpGenFor##name { \
template <typename T = void> \
static constexpr const char* Const() { \
return absl::flags_internal::HelpConstexprWrap( \
ABSL_FLAG_IMPL_FLAGHELP(txt)); \
} \
static std::string NonConst() { return ABSL_FLAG_IMPL_FLAGHELP(txt); } \
}

#define ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \
Expand All @@ -326,29 +338,28 @@ void SetFlag(absl::Flag<T>* flag, const V& v) {
#define ABSL_FLAG_IMPL(Type, name, default_value, help) \
namespace absl /* block flags in namespaces */ {} \
ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \
ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, help) \
ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, help); \
ABSL_CONST_INIT absl::Flag<Type> FLAGS_##name{ \
ABSL_FLAG_IMPL_FLAGNAME(#name), &AbslFlagsWrapHelp##name, \
ABSL_FLAG_IMPL_FLAGNAME(#name), \
absl::flags_internal::HelpArg<AbslFlagHelpGenFor##name>(0), \
ABSL_FLAG_IMPL_FILENAME(), \
&absl::flags_internal::FlagMarshallingOps<Type>, \
&AbslFlagsInitFlag##name}; \
extern bool FLAGS_no##name; \
bool FLAGS_no##name = ABSL_FLAG_IMPL_REGISTRAR(Type, FLAGS_##name)
#else
// MSVC version uses aggregate initialization.
#define ABSL_FLAG_IMPL(Type, name, default_value, help) \
namespace absl /* block flags in namespaces */ {} \
ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \
ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, help) \
ABSL_CONST_INIT absl::Flag<Type> FLAGS_##name{ \
ABSL_FLAG_IMPL_FLAGNAME(#name), \
&AbslFlagsWrapHelp##name, \
ABSL_FLAG_IMPL_FILENAME(), \
&absl::flags_internal::FlagMarshallingOps<Type>, \
&AbslFlagsInitFlag##name, \
false, \
nullptr}; \
extern bool FLAGS_no##name; \
// MSVC version uses aggregate initialization. We also do not try to
// optimize away help wrapper.
#define ABSL_FLAG_IMPL(Type, name, default_value, help) \
namespace absl /* block flags in namespaces */ {} \
ABSL_FLAG_IMPL_DECLARE_DEF_VAL_WRAPPER(name, Type, default_value) \
ABSL_FLAG_IMPL_DECLARE_HELP_WRAPPER(name, help); \
ABSL_CONST_INIT absl::Flag<Type> FLAGS_##name{ \
ABSL_FLAG_IMPL_FLAGNAME(#name), &AbslFlagHelpGenFor##name::NonConst, \
ABSL_FLAG_IMPL_FILENAME(), \
&absl::flags_internal::FlagMarshallingOps<Type>, \
&AbslFlagsInitFlag##name}; \
extern bool FLAGS_no##name; \
bool FLAGS_no##name = ABSL_FLAG_IMPL_REGISTRAR(Type, FLAGS_##name)
#endif

Expand Down
17 changes: 10 additions & 7 deletions absl/flags/flag_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace {

namespace flags = absl::flags_internal;

std::string TestHelpMsg() { return "help"; }
std::string TestHelpMsg() { return "dynamic help"; }
template <typename T>
void* TestMakeDflt() {
return new T{};
Expand All @@ -41,19 +41,22 @@ void TestCallback() {}

template <typename T>
bool TestConstructionFor() {
constexpr flags::Flag<T> f1("f1", &TestHelpMsg, "file",
constexpr flags::HelpInitArg help_arg{flags::FlagHelpSrc("literal help"),
flags::FlagHelpSrcKind::kLiteral};
constexpr flags::Flag<T> f1("f1", help_arg, "file",
&flags::FlagMarshallingOps<T>, &TestMakeDflt<T>);
EXPECT_EQ(f1.Name(), "f1");
EXPECT_EQ(f1.Help(), "help");
EXPECT_EQ(f1.Help(), "literal help");
EXPECT_EQ(f1.Filename(), "file");

ABSL_CONST_INIT static flags::Flag<T> f2("f2", &TestHelpMsg, "file",
&flags::FlagMarshallingOps<T>,
&TestMakeDflt<T>);
ABSL_CONST_INIT static flags::Flag<T> f2(
"f2",
{flags::FlagHelpSrc(&TestHelpMsg), flags::FlagHelpSrcKind::kGenFunc},
"file", &flags::FlagMarshallingOps<T>, &TestMakeDflt<T>);
flags::FlagRegistrar<T, false>(&f2).OnUpdate(TestCallback);

EXPECT_EQ(f2.Name(), "f2");
EXPECT_EQ(f2.Help(), "help");
EXPECT_EQ(f2.Help(), "dynamic help");
EXPECT_EQ(f2.Filename(), "file");

return true;
Expand Down
7 changes: 0 additions & 7 deletions absl/flags/internal/commandlineflag.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,5 @@ std::string CommandLineFlag::Filename() const {
return flags_internal::GetUsageConfig().normalize_filename(filename_);
}

std::string HelpText::GetHelpText() const {
if (help_function_) return help_function_();
if (help_message_) return help_message_;

return {};
}

} // namespace flags_internal
} // namespace absl
47 changes: 5 additions & 42 deletions absl/flags/internal/commandlineflag.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,6 @@ enum ValueSource {
kProgrammaticChange,
};

// Signature for the help generation function used as an argument for the
// absl::Flag constructor.
using HelpGenFunc = std::string (*)();

// Signature for the function generating the initial flag value based (usually
// based on default value supplied in flag's definition)
using InitialValGenFunc = void* (*)();

extern const char kStrippedFlagHelp[];

// The per-type function
Expand Down Expand Up @@ -149,34 +141,6 @@ inline size_t Sizeof(FlagOpFn op) {
op(flags_internal::kSizeof, nullptr, nullptr)));
}

// Holds either a pointer to help text or a function which produces it. This is
// needed for supporting both static initialization of Flags while supporting
// the legacy registration framework. We can't use absl::variant<const char*,
// const char*(*)()> since anybody passing 0 or nullptr in to a CommandLineFlag
// would find an ambiguity.
class HelpText {
public:
static constexpr HelpText FromFunctionPointer(const HelpGenFunc fn) {
return HelpText(fn, nullptr);
}
static constexpr HelpText FromStaticCString(const char* msg) {
return HelpText(nullptr, msg);
}

std::string GetHelpText() const;

HelpText() = delete;
HelpText(const HelpText&) = default;
HelpText(HelpText&&) = default;

private:
explicit constexpr HelpText(const HelpGenFunc fn, const char* msg)
: help_function_(fn), help_message_(msg) {}

HelpGenFunc help_function_;
const char* help_message_;
};

// Handle to FlagState objects. Specific flag state objects will restore state
// of a flag produced this flag state from method CommandLineFlag::SaveState().
class FlagStateInterface {
Expand All @@ -190,9 +154,8 @@ class FlagStateInterface {
// Holds all information for a flag.
class CommandLineFlag {
public:
constexpr CommandLineFlag(const char* name, HelpText help_text,
const char* filename)
: name_(name), help_(help_text), filename_(filename) {}
constexpr CommandLineFlag(const char* name, const char* filename)
: name_(name), filename_(filename) {}

// Virtual destructor
virtual void Destroy() const = 0;
Expand All @@ -203,7 +166,6 @@ class CommandLineFlag {

// Non-polymorphic access methods.
absl::string_view Name() const { return name_; }
std::string Help() const { return help_.GetHelpText(); }
absl::string_view Typename() const;
std::string Filename() const;

Expand Down Expand Up @@ -250,6 +212,8 @@ class CommandLineFlag {

// Polymorphic access methods

// Returns help message associated with this flag
virtual std::string Help() const = 0;
// Returns true iff this object corresponds to retired flag
virtual bool IsRetired() const { return false; }
// Returns true iff this is a handle to an Abseil Flag.
Expand Down Expand Up @@ -285,12 +249,11 @@ class CommandLineFlag {
// flag's value type.
virtual void CheckDefaultValueParsingRoundtrip() const = 0;

// Constant configuration for a particular flag.
protected:
~CommandLineFlag() = default;

// Constant configuration for a particular flag.
const char* const name_; // Flags name passed to ABSL_FLAG as second arg.
const HelpText help_; // The function generating help message.
const char* const filename_; // The file name where ABSL_FLAG resides.

private:
Expand Down
5 changes: 5 additions & 0 deletions absl/flags/internal/flag.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ void FlagImpl::Destroy() const {
delete locks_;
}

std::string FlagImpl::Help() const {
return help_source_kind_ == FlagHelpSrcKind::kLiteral ? help_.literal
: help_.gen_func();
}

bool FlagImpl::IsModified() const {
absl::MutexLock l(DataGuard());
return modified_;
Expand Down
Loading

0 comments on commit 2103fd9

Please sign in to comment.