llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-ir

Author: Dominik Steenken (dominik-steenken)

<details>
<summary>Changes</summary>

When creating global strings, some targets have requirements that need to be 
taken into account. Previously, the global strings created by 
`IRBuilder::createGlobalString` had a hard-coded alignment of `1`.

This commit makes it so that the alignment is taken from the data layout 
instead, giving targets the chance to align global strings according to their 
preferences.

This PR is motivated by (and should fix) #<!-- -->141491, where the 1-byte 
alignment in a global string created by a `printf` optimization led to the 
resulting assembly in a `-fno-PIC` compile to unexpectedly reference the GOT 
based on whether the code contained `printf` statements of the form 
`printf("foo\n");`.

I don't know and would appreciate feedback on whether this is the correct place 
to fix something like that.

---
Full diff: https://github.com/llvm/llvm-project/pull/142346.diff


2 Files Affected:

- (added) clang/test/CodeGen/SystemZ/align-systemz-globalstring.c (+14) 
- (modified) llvm/lib/IR/IRBuilder.cpp (+1-1) 


``````````diff
diff --git a/clang/test/CodeGen/SystemZ/align-systemz-globalstring.c 
b/clang/test/CodeGen/SystemZ/align-systemz-globalstring.c
new file mode 100644
index 0000000000000..f5178a079f898
--- /dev/null
+++ b/clang/test/CodeGen/SystemZ/align-systemz-globalstring.c
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -O1 -triple s390x-linux-gnu -emit-llvm %s -o - | FileCheck 
%s
+
+// #include <stdio.h>
+
+// CHECK: @msg1 = local_unnamed_addr constant [13 x i8] c"Hello World\0A\00", 
align 2
+// CHECK: @str = private unnamed_addr constant [12 x i8] c"Hello World\00", 
align 2
+
+const char msg1 [] = "Hello World\n";
+
+extern int printf(const char *__restrict __format, ...);
+
+void foo() {
+    printf(msg1);
+}
\ No newline at end of file
diff --git a/llvm/lib/IR/IRBuilder.cpp b/llvm/lib/IR/IRBuilder.cpp
index 580b0af709337..ab06a587a861f 100644
--- a/llvm/lib/IR/IRBuilder.cpp
+++ b/llvm/lib/IR/IRBuilder.cpp
@@ -52,7 +52,7 @@ GlobalVariable *IRBuilderBase::CreateGlobalString(StringRef 
Str,
       *M, StrConstant->getType(), true, GlobalValue::PrivateLinkage,
       StrConstant, Name, nullptr, GlobalVariable::NotThreadLocal, 
AddressSpace);
   GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
-  GV->setAlignment(Align(1));
+  GV->setAlignment(M->getDataLayout().getPreferredAlign(GV));
   return GV;
 }
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/142346
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to