Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from llvm::StringRef to char* for Toggle name to prevent static initializers #6949

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

chrdavis
Copy link
Member

@chrdavis chrdavis commented Oct 7, 2024

Small change to switch from llvm::StringRef to char* to prevent incurring a large number of static initializers. See dump of static initializers before and after this change from a release build of dxcompiler.dll.

static_initializers_after.txt
static_initializers_before.txt

Fixes #6896

@chrdavis chrdavis requested a review from a team as a code owner October 7, 2024 18:29
Copy link
Contributor

github-actions bot commented Oct 7, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@damyanp damyanp requested review from bogner and llvm-beanz December 11, 2024 17:25
@llvm-beanz
Copy link
Collaborator

I think this is the wrong approach. In modern LLVM StringRef's constructor is a constant expression, so it doesn't have a global initializer.

We should probably fix the problem that way rather than giving up the safety that StringRef provides over unbound character pointers.

@llvm-beanz
Copy link
Collaborator

This change should have a potentially larger impact on static initialization without any loss of safety:

diff --git a/include/llvm/ADT/StringRef.h b/include/llvm/ADT/StringRef.h
index 957707f427c..911a7b6bc9b 100644
--- a/include/llvm/ADT/StringRef.h
+++ b/include/llvm/ADT/StringRef.h
@@ -74,7 +74,7 @@ namespace llvm {
       }
 
     /// Construct a string ref from a pointer and length.
-    /*implicit*/ StringRef(const char *data, size_t 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");

@bogner
Copy link
Collaborator

bogner commented Dec 11, 2024

Upstream LLVM has StringLiteral which would be appropriate here, but it looks like that was added in late 2016 and isn't available in DXC. Using a constexpr StringRef should probably do the trick though.

@chrdavis
Copy link
Member Author

This change should have a potentially larger impact on static initialization without any loss of safety:

diff --git a/include/llvm/ADT/StringRef.h b/include/llvm/ADT/StringRef.h
index 957707f427c..911a7b6bc9b 100644
--- a/include/llvm/ADT/StringRef.h
+++ b/include/llvm/ADT/StringRef.h
@@ -74,7 +74,7 @@ namespace llvm {
       }
 
     /// Construct a string ref from a pointer and length.
-    /*implicit*/ StringRef(const char *data, size_t 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");

I made that change but it does not result in removal of the static initializers.

@llvm-beanz
Copy link
Collaborator

I made that change but it does not result in removal of the static initializers.

What compiler are you using and how are you checking for static initializers? Technically neither of these approaches eliminates static initializers, both just make the initializer trivial so the compiler can replace a non-trivial initialization with a compile-time constant.

If the patch I provided doesn't eliminate the non-trivial initializer, that's likely a bug/limitation in the compiler you're building with. I'm not sure I like the tradeoff of replacing a bounds-checked data type with a raw pointer given the number of security-focused issues we've been addressing in DXC this year.

@llvm-beanz
Copy link
Collaborator

Nvm... my patch put the constexpr tag on the wrong constructor. You also need to mark the Toggle constructor as constexpr and frankly, we might as well mark the variables as constexpr too, because they're static constants and we don't want them to have constructors.

This works for me locally:

diff --git a/include/dxc/Support/DxcOptToggles.h b/include/dxc/Support/DxcOptToggles.h
index 04c4c684966..de40cfbcf09 100644
--- a/include/dxc/Support/DxcOptToggles.h
+++ b/include/dxc/Support/DxcOptToggles.h
@@ -27,7 +27,8 @@ 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 {
@@ -35,20 +36,20 @@ enum {
   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
   std::map<std::string, bool> Toggles; // OPT_opt_enable & OPT_opt_disable
diff --git a/include/llvm/ADT/StringRef.h b/include/llvm/ADT/StringRef.h
index 957707f427c..c103fdbf3b9 100644
--- a/include/llvm/ADT/StringRef.h
+++ b/include/llvm/ADT/StringRef.h
@@ -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)

Switch to constexpr
Add constexpr to StringRef constructors
@chrdavis
Copy link
Member Author

Thanks I've updated the change and verified the static initializers are gone after building clean.

@chrdavis
Copy link
Member Author

I need one more reviewer. Would love to close this out before the holidays.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: New
Development

Successfully merging this pull request may close these issues.

dxcompiler.dll has duplicate static initializers via DxcOptToggles.h
4 participants