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 3 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.

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