[llvm] Global string alignment (PR #142346)
Dominik Steenken via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 24 01:00:18 PDT 2025
https://github.com/dominik-steenken updated https://github.com/llvm/llvm-project/pull/142346
>From 318f0536ce71780f808ef70a1817af515f9861bd Mon Sep 17 00:00:00 2001
From: Dominik Steenken <dost at 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..9e061257a7baa 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().getPrefTypeAlign(getInt8Ty()));
return GV;
}
>From a0ac66a93364a5ef7712b4f225ef5e80264404a0 Mon Sep 17 00:00:00 2001
From: Dominik Steenken <dost at 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 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")`.
---
.../InstCombine/SystemZ/printf-opt-alignment.ll | 15 +++++++++++++++
1 file changed, 15 insertions(+)
create mode 100644 llvm/test/Transforms/InstCombine/SystemZ/printf-opt-alignment.ll
diff --git a/llvm/test/Transforms/InstCombine/SystemZ/printf-opt-alignment.ll b/llvm/test/Transforms/InstCombine/SystemZ/printf-opt-alignment.ll
new file mode 100644
index 0000000000000..b5fb5f398caa1
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/SystemZ/printf-opt-alignment.ll
@@ -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")`
+
+ at msg1 = constant [13 x i8] c"Got Alignment?\0A\00", align 2
+; CHECK: c"Got Alignment?\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
More information about the llvm-commits
mailing list