[llvm] Add `AddNull` argument to `CreateGlobalString`. (PR #93036)

Thomas Symalla via llvm-commits llvm-commits at lists.llvm.org
Wed May 22 07:25:40 PDT 2024


https://github.com/tsymalla created https://github.com/llvm/llvm-project/pull/93036

There's currently no way to control whether a null terminator should be appended to the string created in `CreateGlobalString` / `CreateGlobalString`, since the methods don't expose an additional argument.
This change adds an additional argument to the methods that has the same default value, `true`, as in `ConstantDataArray::getString`, and passes it down to this internal method.

>From 2e7d0a000defaea9560fae1c6213dc88678f5876 Mon Sep 17 00:00:00 2001
From: Thomas Symalla <tsymalla at amd.com>
Date: Wed, 22 May 2024 16:22:32 +0200
Subject: [PATCH] Add `AddNull` argument to `CreateGlobalString`.

There's currently no way to control whether a null terminator should be
appended to the string created in `CreateGlobalString` /
`CreateGlobalString`, since the methods don't expose an additional
argument.
This change adds an additional argument to the methods that has the same
default value, `true`, as `ConstantDataArray::getString`, and passes it
down to this internal method.
---
 llvm/include/llvm/IR/IRBuilder.h | 7 ++++---
 llvm/lib/IR/IRBuilder.cpp        | 4 ++--
 2 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h
index 0d8746344a44b..40a9cf507248a 100644
--- a/llvm/include/llvm/IR/IRBuilder.h
+++ b/llvm/include/llvm/IR/IRBuilder.h
@@ -455,7 +455,7 @@ class IRBuilderBase {
   /// block.
   GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name = "",
                                      unsigned AddressSpace = 0,
-                                     Module *M = nullptr);
+                                     Module *M = nullptr, bool AddNull = true);
 
   /// Get a constant value representing either true or false.
   ConstantInt *getInt1(bool V) {
@@ -1992,8 +1992,9 @@ class IRBuilderBase {
   /// block.
   Constant *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "",
                                   unsigned AddressSpace = 0,
-                                  Module *M = nullptr) {
-    GlobalVariable *GV = CreateGlobalString(Str, Name, AddressSpace, M);
+                                  Module *M = nullptr, bool AddNull = true) {
+    GlobalVariable *GV =
+        CreateGlobalString(Str, Name, AddressSpace, M, AddNull);
     Constant *Zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
     Constant *Indices[] = {Zero, Zero};
     return ConstantExpr::getInBoundsGetElementPtr(GV->getValueType(), GV,
diff --git a/llvm/lib/IR/IRBuilder.cpp b/llvm/lib/IR/IRBuilder.cpp
index 0d6760ed08411..b32799355d692 100644
--- a/llvm/lib/IR/IRBuilder.cpp
+++ b/llvm/lib/IR/IRBuilder.cpp
@@ -43,8 +43,8 @@ using namespace llvm;
 GlobalVariable *IRBuilderBase::CreateGlobalString(StringRef Str,
                                                   const Twine &Name,
                                                   unsigned AddressSpace,
-                                                  Module *M) {
-  Constant *StrConstant = ConstantDataArray::getString(Context, Str);
+                                                  Module *M, bool AddNull) {
+  Constant *StrConstant = ConstantDataArray::getString(Context, Str, AddNull);
   if (!M)
     M = BB->getParent()->getParent();
   auto *GV = new GlobalVariable(



More information about the llvm-commits mailing list