This is an archive of the discontinued LLVM Phabricator instance.

[CodeGen] Propagate LValueBaseInfo instead of AlignmentSource
ClosedPublic

Authored by kparzysz on May 17 2017, 8:45 AM.

Details

Summary

The functions creating LValues propagated information about alignment source. Extend the propagated data to also include information about possible unrestricted aliasing. A new class LValueBaseInfo will contain both AlignmentSource and MayAlias info.

This patch should not introduce any functional changes.

Diff Detail

Repository
rL LLVM

Event Timeline

rjmccall edited edge metadata.May 17 2017, 11:03 AM

Generally looks great, thanks!

lib/CodeGen/CGBlocks.cpp
923 ↗(On Diff #99311)

Please indent this line a little more to make it clear that it's part of the same argument.

lib/CodeGen/CGExpr.cpp
884 ↗(On Diff #99311)

Places that fall back to the type of a pointer/reference/whatever should probably compute the entire BaseInfo from the type. For example, if we wanted to properly support attribute may_alias on types, we'd need to compute it here. So I think you should probably change getNaturalTypeAlignment and getNaturalPointeeTypeAlignment to produce an entire BaseInfo, even if you always fill it in with may_alias=false for now.

Here you'll also need to merge the BaseInfos of the original pointer and the new one. You should probably have a method on LValueBaseInfo that does that, called maybe mergeForCast?

2256 ↗(On Diff #99311)

Hmm. I think your side of this is right, but I'm not sure that the original code is correct to override with AlignmentSource::Decl, because the captured field might be a reference. In fact, I'm not sure why this is overwriting the alignment, either; it should be able to just return the result of EmitCapturedFieldLValue, and that should be more correct for the same reason. Do any tests break if you do that?

kparzysz added inline comments.May 17 2017, 12:46 PM
lib/CodeGen/CGExpr.cpp
2256 ↗(On Diff #99311)

There is one failure in check-clang: test/OpenMP/task_codegen.cpp.

; Function Attrs: noinline nounwind
define internal i32 @.omp_task_entry..14(i32, %struct.kmp_task_t_with_privates.13* noalias) #1 {
entry:
  %.global_tid..addr.i = alloca i32, align 4
  %.part_id..addr.i = alloca i32*, align 8
  %.privates..addr.i = alloca i8*, align 8
  %.copy_fn..addr.i = alloca void (i8*, ...)*, align 8
  %.task_t..addr.i = alloca i8*, align 8
  %__context.addr.i = alloca %struct.anon.12*, align 8
  %.addr = alloca i32, align 4
  %.addr1 = alloca %struct.kmp_task_t_with_privates.13*, align 8
  store i32 %0, i32* %.addr, align 4
  store %struct.kmp_task_t_with_privates.13* %1, %struct.kmp_task_t_with_privates.13** %.addr1, align 8
  %2 = load i32, i32* %.addr, align 4
  %3 = load %struct.kmp_task_t_with_privates.13*, %struct.kmp_task_t_with_privates.13** %.addr1, align 8
  %4 = getelementptr inbounds %struct.kmp_task_t_with_privates.13, %struct.kmp_task_t_with_privates.13* %3, i32 0, i32 0
  %5 = getelementptr inbounds %struct.kmp_task_t, %struct.kmp_task_t* %4, i32 0, i32 2
  %6 = getelementptr inbounds %struct.kmp_task_t, %struct.kmp_task_t* %4, i32 0, i32 0
  %7 = load i8*, i8** %6, align 8
  %8 = bitcast i8* %7 to %struct.anon.12*
  %9 = bitcast %struct.kmp_task_t_with_privates.13* %3 to i8*
  store i32 %2, i32* %.global_tid..addr.i, align 4
  store i32* %5, i32** %.part_id..addr.i, align 8
  store i8* null, i8** %.privates..addr.i, align 8
  store void (i8*, ...)* null, void (i8*, ...)** %.copy_fn..addr.i, align 8
  store i8* %9, i8** %.task_t..addr.i, align 8
  store %struct.anon.12* %8, %struct.anon.12** %__context.addr.i, align 8
  %10 = load %struct.anon.12*, %struct.anon.12** %__context.addr.i, align 8
  store i32 4, i32* @a, align 4
  %11 = getelementptr inbounds %struct.anon.12, %struct.anon.12* %10, i32 0, i32 0
  %ref.i = load i32*, i32** %11, align 8
  store i32 5, i32* %ref.i, align 128      // XXX This alignment is 4 with that change.
  ret i32 0
}
rjmccall added inline comments.May 17 2017, 1:21 PM
lib/CodeGen/CGExpr.cpp
2256 ↗(On Diff #99311)

Ah, okay, I see what's happening here. The field is actually *always* a reference, and and because it's just a reference the type doesn't have the declared alignment of the variable.

I think your change is probably the most reasonable approach.

kparzysz updated this revision to Diff 99353.May 17 2017, 2:54 PM
kparzysz marked 2 inline comments as done.

Changed the getNatural.*TypeAlignment to produce the entire LValueBaseInfo, assuming MayAlias to be false.

Added merging of LValueBaseInfos. The merging assumes that the alignment source in the parameter overrides the existing source. That works for the existing cases (cast), but I'm not sure if that's correct in general. Is there is an implicit ordering of alignment sources?

Changed the getNatural.*TypeAlignment to produce the entire LValueBaseInfo, assuming MayAlias to be false.

Added merging of LValueBaseInfos. The merging assumes that the alignment source in the parameter overrides the existing source. That works for the existing cases (cast), but I'm not sure if that's correct in general. Is there is an implicit ordering of alignment sources?

There is an ordering of alignment sources, but we deliberately want casts to override alignment assumptions associated with the original value. But that's why the merge operation is named mergeForCasts: it doesn't suggest it should be used for other operations.

Thank you, this looks great.

If you have no further comments, could you accept this review?

rjmccall accepted this revision.May 18 2017, 8:44 AM

Yes, of course. Although "looks good to me" is acceptance whether or not I actually push a button in Phabricator. :)

This revision is now accepted and ready to land.May 18 2017, 8:44 AM
This revision was automatically updated to reflect the committed changes.
chapuni added inline comments.
cfe/trunk/lib/CodeGen/CodeGenFunction.h
3756

Could you update this? See also r303414. [-Wdocumentation]

kparzysz marked an inline comment as done.May 19 2017, 5:23 AM
kparzysz added inline comments.
cfe/trunk/lib/CodeGen/CodeGenFunction.h
3756

Done in r303419.