https://github.com/dominik-steenken created https://github.com/llvm/llvm-project/pull/142346
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. >From ff817c7a1b79596712d24b7cfac9212edcef9280 Mon Sep 17 00:00:00 2001 From: Dominik Steenken <d...@de.ibm.com> Date: Mon, 26 May 2025 14:53:41 +0200 Subject: [PATCH 1/2] Align global strings according to data layout 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. --- llvm/lib/IR/IRBuilder.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; } >From b48d7b108150d154bb15b83a96f6b26dbe9f100c Mon Sep 17 00:00:00 2001 From: Dominik Steenken <d...@de.ibm.com> Date: Fri, 30 May 2025 15:01:45 +0200 Subject: [PATCH 2/2] [SystemZ] Add codegen test for global string alignment This commit adds a test to the `clang` test suite for the SystemZ backend that checks for the correct alignment of global strings created by the PrintFOptimizer for the case `printf("foo\n")` -> `puts("foo")`. --- .../CodeGen/SystemZ/align-systemz-globalstring.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 clang/test/CodeGen/SystemZ/align-systemz-globalstring.c 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 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits