Skip to content

Commit

Permalink
Remove static initializers for llvm::StringRef (#6949)
Browse files Browse the repository at this point in the history
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 #6896
  • Loading branch information
chrdavis authored Dec 24, 2024
1 parent 6c2b9f3 commit a8a4e98
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
24 changes: 13 additions & 11 deletions include/dxc/Support/DxcOptToggles.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,29 @@ namespace options {
struct Toggle {
llvm::StringRef Name;
bool Default = false;
Toggle(llvm::StringRef Name, bool Default) : Name(Name), Default(Default) {}
constexpr Toggle(llvm::StringRef Name, bool Default)
: Name(Name), Default(Default) {}
};

enum {
DEFAULT_ON = 1,
DEFAULT_OFF = 0,
};

static const Toggle TOGGLE_GVN = {"gvn", DEFAULT_ON};
static const Toggle TOGGLE_LICM = {"licm", DEFAULT_ON};
static const Toggle TOGGLE_SINK = {"sink", DEFAULT_ON};
static const Toggle TOGGLE_ENABLE_AGGRESSIVE_REASSOCIATION = {
static constexpr Toggle TOGGLE_GVN = {"gvn", DEFAULT_ON};
static constexpr Toggle TOGGLE_LICM = {"licm", DEFAULT_ON};
static constexpr Toggle TOGGLE_SINK = {"sink", DEFAULT_ON};
static constexpr Toggle TOGGLE_ENABLE_AGGRESSIVE_REASSOCIATION = {
"aggressive-reassociation", DEFAULT_ON};
static const Toggle TOGGLE_LIFETIME_MARKERS = {"lifetime-markers", DEFAULT_ON};
static const Toggle TOGGLE_PARTIAL_LIFETIME_MARKERS = {
static constexpr Toggle TOGGLE_LIFETIME_MARKERS = {"lifetime-markers",
DEFAULT_ON};
static constexpr Toggle TOGGLE_PARTIAL_LIFETIME_MARKERS = {
"partial-lifetime-markers", DEFAULT_OFF};
static const Toggle TOGGLE_STRUCTURIZE_LOOP_EXITS_FOR_UNROLL = {
static constexpr Toggle TOGGLE_STRUCTURIZE_LOOP_EXITS_FOR_UNROLL = {
"structurize-loop-exits-for-unroll", DEFAULT_ON};
static const Toggle TOGGLE_DEBUG_NOPS = {"debug-nops", DEFAULT_ON};
static const Toggle TOGGLE_STRUCTURIZE_RETURNS = {"structurize-returns",
DEFAULT_OFF};
static constexpr Toggle TOGGLE_DEBUG_NOPS = {"debug-nops", DEFAULT_ON};
static constexpr Toggle TOGGLE_STRUCTURIZE_RETURNS = {"structurize-returns",
DEFAULT_OFF};

struct OptimizationToggles {
// Optimization pass enables, disables and selects
Expand Down
21 changes: 11 additions & 10 deletions include/llvm/ADT/StringRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,20 @@ namespace llvm {
/*implicit*/ StringRef() : Data(nullptr), Length(0) {}
StringRef(std::nullptr_t) = delete; // HLSL Change - So we don't accidentally pass `false` again

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

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

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

0 comments on commit a8a4e98

Please sign in to comment.