This is an archive of the discontinued LLVM Phabricator instance.

CodeGen: specify alignment for automatic variable initialization
ClosedPublic

Authored by jfb on Jul 11 2018, 5:16 PM.

Details

Diff Detail

Event Timeline

jfb created this revision.Jul 11 2018, 5:16 PM
efriedma added inline comments.
lib/CodeGen/CGDecl.cpp
902

llvm::Value::getPointerAlignment is not the right way to compute the alignment (it tries to guess the alignment based on the IR). Please convert this to use a clang::CodeGen::Address instead.

jfb updated this revision to Diff 155285.Jul 12 2018, 3:24 PM
jfb marked an inline comment as done.
  • Use Address as suggested in review.
jfb added a comment.Jul 12 2018, 3:26 PM

I updated the patch to use Address, and also use inbounds. This was a bit tricky because of the GEPs. I also updated tests which run into this codegen. Only the following tests actually hit this code, and not all check these stores:

Clang :: CodeGen/2004-03-09-LargeArrayInitializers.c
Clang :: CodeGen/indirect-goto.c
Clang :: CodeGen/init.c
Clang :: CodeGenCXX/float128-declarations.cpp
Clang :: CodeGenObjC/messages-2.m
Clang :: CodeGenObjC/messages.m
Clang :: CodeGenOpenCL/address-space-constant-initializers.cl
Clang :: CodeGenOpenCL/partial_initializer.cl
Clang :: CodeGenOpenCL/private-array-initialization.cl
jfb updated this revision to Diff 155287.Jul 12 2018, 3:34 PM
  • Fix silly naming and lookup.
efriedma added inline comments.Jul 12 2018, 4:51 PM
lib/CodeGen/CGBuilder.h
260

Not sure about the new helper. We already have CreateStructGEP and CreateConstArrayGEP which do approximately what you want.

lib/CodeGen/CGDecl.cpp
901

Builder.CreateStore(Init, Loc, isVolatile);

jfb updated this revision to Diff 155424.Jul 13 2018, 10:22 AM
  • Simplify CreateStore.
jfb marked an inline comment as done.Jul 13 2018, 10:25 AM
jfb added inline comments.
lib/CodeGen/CGBuilder.h
260

It's close to doing what we want, but not quite? It seems like the original code would have used them otherwise.

To use them we'd have to:

  • branch on struct / array
    • for struct calculate the offset there (which the new helper does)
    • for array get the element size

Seems simpler to use GEP2_32 and more fool-proof to (internal to the helper) use GEP's own idea of what the offset is, no?

efriedma accepted this revision.Jul 13 2018, 11:07 AM

LGTM

lib/CodeGen/CGBuilder.h
260

Okay.

(Actually, I think this ends up conservatively underestimating the alignment in some cases involving nested structs, but it's unlikely to matter.)

This revision is now accepted and ready to land.Jul 13 2018, 11:07 AM
This revision was automatically updated to reflect the committed changes.