Skip to content

Commit a8a4e98

Browse files
authored
Remove static initializers for llvm::StringRef (microsoft#6949)
Update llvm::StringRef constructor to be constexpr and replace static constant values in DxcOptToggles with constexpr variables to ensure non-trivial initializers are not generated in the final binary. Fixes microsoft#6896
1 parent 6c2b9f3 commit a8a4e98

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

include/dxc/Support/DxcOptToggles.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,29 @@ namespace options {
2727
struct Toggle {
2828
llvm::StringRef Name;
2929
bool Default = false;
30-
Toggle(llvm::StringRef Name, bool Default) : Name(Name), Default(Default) {}
30+
constexpr Toggle(llvm::StringRef Name, bool Default)
31+
: Name(Name), Default(Default) {}
3132
};
3233

3334
enum {
3435
DEFAULT_ON = 1,
3536
DEFAULT_OFF = 0,
3637
};
3738

38-
static const Toggle TOGGLE_GVN = {"gvn", DEFAULT_ON};
39-
static const Toggle TOGGLE_LICM = {"licm", DEFAULT_ON};
40-
static const Toggle TOGGLE_SINK = {"sink", DEFAULT_ON};
41-
static const Toggle TOGGLE_ENABLE_AGGRESSIVE_REASSOCIATION = {
39+
static constexpr Toggle TOGGLE_GVN = {"gvn", DEFAULT_ON};
40+
static constexpr Toggle TOGGLE_LICM = {"licm", DEFAULT_ON};
41+
static constexpr Toggle TOGGLE_SINK = {"sink", DEFAULT_ON};
42+
static constexpr Toggle TOGGLE_ENABLE_AGGRESSIVE_REASSOCIATION = {
4243
"aggressive-reassociation", DEFAULT_ON};
43-
static const Toggle TOGGLE_LIFETIME_MARKERS = {"lifetime-markers", DEFAULT_ON};
44-
static const Toggle TOGGLE_PARTIAL_LIFETIME_MARKERS = {
44+
static constexpr Toggle TOGGLE_LIFETIME_MARKERS = {"lifetime-markers",
45+
DEFAULT_ON};
46+
static constexpr Toggle TOGGLE_PARTIAL_LIFETIME_MARKERS = {
4547
"partial-lifetime-markers", DEFAULT_OFF};
46-
static const Toggle TOGGLE_STRUCTURIZE_LOOP_EXITS_FOR_UNROLL = {
48+
static constexpr Toggle TOGGLE_STRUCTURIZE_LOOP_EXITS_FOR_UNROLL = {
4749
"structurize-loop-exits-for-unroll", DEFAULT_ON};
48-
static const Toggle TOGGLE_DEBUG_NOPS = {"debug-nops", DEFAULT_ON};
49-
static const Toggle TOGGLE_STRUCTURIZE_RETURNS = {"structurize-returns",
50-
DEFAULT_OFF};
50+
static constexpr Toggle TOGGLE_DEBUG_NOPS = {"debug-nops", DEFAULT_ON};
51+
static constexpr Toggle TOGGLE_STRUCTURIZE_RETURNS = {"structurize-returns",
52+
DEFAULT_OFF};
5153

5254
struct OptimizationToggles {
5355
// Optimization pass enables, disables and selects

include/llvm/ADT/StringRef.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,20 @@ namespace llvm {
6666
/*implicit*/ StringRef() : Data(nullptr), Length(0) {}
6767
StringRef(std::nullptr_t) = delete; // HLSL Change - So we don't accidentally pass `false` again
6868

69+
// HLSL Change - Begin - Make StringRef constructors constexpr.
6970
/// Construct a string ref from a cstring.
70-
/*implicit*/ StringRef(const char *Str)
71-
: Data(Str) {
72-
assert(Str && "StringRef cannot be built from a NULL argument");
73-
Length = ::strlen(Str); // invoking strlen(NULL) is undefined behavior
74-
}
71+
/*implicit*/ constexpr StringRef(const char *Str)
72+
: Data(Str), Length(Str ? std::char_traits<char>::length(Str) : 0) {
73+
assert(Str && "StringRef cannot be built from a NULL argument");
74+
}
7575

7676
/// Construct a string ref from a pointer and length.
77-
/*implicit*/ StringRef(const char *data, size_t length)
78-
: Data(data), Length(length) {
79-
assert((data || length == 0) &&
80-
"StringRef cannot be built from a NULL argument with non-null length");
81-
}
77+
/*implicit*/ constexpr StringRef(const char *data, size_t length)
78+
: Data(data), Length(length) {
79+
assert((data || length == 0) && "StringRef cannot be built from a NULL "
80+
"argument with non-null length");
81+
}
82+
// HLSL Change - End - Make StringRef constructors constexpr.
8283

8384
/// Construct a string ref from an std::string.
8485
/*implicit*/ StringRef(const std::string &Str)

0 commit comments

Comments
 (0)