[llvm] [GlobalOpt] Fix global SRA incorrect alignment on some elements (PR #115328)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 7 07:07:17 PST 2024


================
@@ -575,15 +575,16 @@ static GlobalVariable *SRAGlobal(GlobalVariable *GV, const DataLayout &DL) {
         *GV->getParent(), Ty, false, GlobalVariable::InternalLinkage,
         Initializer, GV->getName() + "." + Twine(NameSuffix++), GV,
         GV->getThreadLocalMode(), GV->getAddressSpace());
+    // Start out by copying attributes from the original, including alignment.
     NGV->copyAttributesFrom(GV);
     NewGlobals.insert({OffsetForTy, NGV});
 
     // Calculate the known alignment of the field.  If the original aggregate
-    // had 256 byte alignment for example, something might depend on that:
+    // had 256 byte alignment for example, then the element at a given offset
+    // may also have a known alignment, and something might depend on that:
     // propagate info to each field.
     Align NewAlign = commonAlignment(StartAlignment, OffsetForTy);
-    if (NewAlign > DL.getABITypeAlign(Ty))
-      NGV->setAlignment(NewAlign);
+    NGV->setAlignment(std::max(NewAlign, DL.getABITypeAlign(Ty)));
----------------
nikic wrote:

Are there any further test changes if we just drop the getABITypeAlign part of this completely?

I see that the later code does a getOrEnforceKnownAlignment, which will raise the global alignment to the preferred alignment anyway, so I suspect that the check for ABI alignment is just entirely unnecessary here.

https://github.com/llvm/llvm-project/pull/115328


More information about the llvm-commits mailing list