Global string alignment (#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");`.
This commit is contained in:
Dominik Steenken 2025-06-30 11:40:43 +02:00 committed by GitHub
parent d0c1ea928c
commit 47ecd18f24
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 1 deletions

View File

@ -51,7 +51,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().getPrefTypeAlign(getInt8Ty()));
return GV;
}

View File

@ -0,0 +1,15 @@
; RUN: opt < %s --passes=instcombine -S -mtriple=systemz-unknown | FileCheck %s
;
; Check that string replacements inserted by the instcombiner are properly aligned.
; The specific case checked replaces `printf("foo\n")` with `puts("foo")`
@msg1 = constant [17 x i8] c"Alignment Check\0A\00", align 2
; CHECK: c"Alignment Check\00", align 2
; Function Attrs: noinline nounwind
define dso_local void @foo() #0 {
%call = call signext i32 (ptr, ...) @printf(ptr noundef @msg1)
ret void
}
declare signext i32 @printf(ptr noundef, ...) #1