Skip to content

Commit 47ecd18

Browse files
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");`.
1 parent d0c1ea9 commit 47ecd18

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

llvm/lib/IR/IRBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ GlobalVariable *IRBuilderBase::CreateGlobalString(StringRef Str,
5151
*M, StrConstant->getType(), true, GlobalValue::PrivateLinkage,
5252
StrConstant, Name, nullptr, GlobalVariable::NotThreadLocal, AddressSpace);
5353
GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
54-
GV->setAlignment(Align(1));
54+
GV->setAlignment(M->getDataLayout().getPrefTypeAlign(getInt8Ty()));
5555
return GV;
5656
}
5757

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; RUN: opt < %s --passes=instcombine -S -mtriple=systemz-unknown | FileCheck %s
2+
;
3+
; Check that string replacements inserted by the instcombiner are properly aligned.
4+
; The specific case checked replaces `printf("foo\n")` with `puts("foo")`
5+
6+
@msg1 = constant [17 x i8] c"Alignment Check\0A\00", align 2
7+
; CHECK: c"Alignment Check\00", align 2
8+
9+
; Function Attrs: noinline nounwind
10+
define dso_local void @foo() #0 {
11+
%call = call signext i32 (ptr, ...) @printf(ptr noundef @msg1)
12+
ret void
13+
}
14+
15+
declare signext i32 @printf(ptr noundef, ...) #1

0 commit comments

Comments
 (0)