[llvm] [GlobalOpt] Fix alignments of globals introduced for allocations (PR #216480)
Eli Friedman via llvm-commits
llvm-commits at lists.llvm.org
Sun Aug 16 14:56:31 PDT 2026
=?utf-8?q?Ömer_Sinan_Ağacan?= <omer at osa1.net>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/216480 at github.com>
================
@@ -944,15 +944,37 @@ OptimizeGlobalAddressOfAllocation(GlobalVariable *GV, CallInst *CI,
UndefValue::get(GlobalType), GV->getName() + ".body", nullptr,
GV->getThreadLocalMode());
+ // Alignment of the return value of the allocator call.
+ Align GVAlign = CI->getPointerAlignment(DL);
+
+ // If the allocation function has a valid constant `allocalign` argument
+ // that's larger, increase the alignment.
+ const Value *AllocAlign = getAllocAlignment(CI, TLI);
+ if (AllocAlign) {
+ const ConstantInt *AllocAlignC = dyn_cast<ConstantInt>(AllocAlign);
+ if (AllocAlignC &&
+ AllocAlignC->getValue().ult(llvm::Value::MaximumAlignment)) {
+ uint64_t AllocAlignVal = AllocAlignC->getZExtValue();
+ if (llvm::isPowerOf2_64(AllocAlignVal)) {
+ GVAlign = std::max(GVAlign, Align(AllocAlignC->getAlignValue()));
----------------
efriedma-quic wrote:
If we're going to fix this code to handle alignment correctly, we should do it right the first time, and not leave a known miscompile.
Say someone writes "void* p = aligned_alloc(n, 128);`. Inherently, we know n >= 1 (otherwise the allocation would fail). That doesn't mean we can create a global with align 1; that's a miscompile if there's a load or store that assumes larger alignment.
https://github.com/llvm/llvm-project/pull/216480
More information about the llvm-commits
mailing list