This is an archive of the discontinued LLVM Phabricator instance.

CodeGen: Insert addr space cast for automatic/temp var at right position
ClosedPublic

Authored by yaxunl on Jul 14 2017, 2:16 PM.

Details

Summary

The uses of alloca may be in different blocks other than the block containing the alloca.
Therefore if the alloca addr space is non-zero and it needs to be casted to default
address space, the cast needs to be inserted in the same BB as the alloca insted of
the current builder insert point since the current insert point may be in a different BB.

Diff Detail

Repository
rL LLVM

Event Timeline

yaxunl created this revision.Jul 14 2017, 2:16 PM
rjmccall edited edge metadata.Jul 14 2017, 3:54 PM

Hmm. It doesn't seem unreasonable for this to require an insertion point as a precondition. In what situation do we emit addrspace casts after a return?

Hmm. It doesn't seem unreasonable for this to require an insertion point as a precondition. In what situation do we emit addrspace casts after a return?

When the alloca address space is not zero and we declare an automatic variable after return. Since we need to cast the automatic variable to default address space, we need to have a new basic block.

Oh, of course. Sadly, in C/C++ the declaration point of a variable does not necessarily dominate all uses of the variable, so you need to emit the cast immediately after the alloca. Just temporarily move CGF.Builder to that point; hopefully we can assume that this function will never try to add control flow.

Test case:

extern void use(int*);
void foo() {
  goto later;
  int x;
  later:
  use(&x);
}

Oh, of course. Sadly, in C/C++ the declaration point of a variable does not necessarily dominate all uses of the variable, so you need to emit the cast immediately after the alloca. Just temporarily move CGF.Builder to that point; hopefully we can assume that this function will never try to add control flow.

Test case:

extern void use(int*);
void foo() {
  goto later;
  int x;
  later:
  use(&x);
}

Right. performAddressSpaceCast could be used in general cases. We only need to change insert position when emitting automatic or temporary variables. I will move this logic to CodeGenFunction::CreateTempAlloca.

yaxunl updated this revision to Diff 106879.Jul 17 2017, 8:36 AM
yaxunl edited the summary of this revision. (Show Details)

Insert addr space cast in the same basic block as alloca.

yaxunl retitled this revision from CodeGen: Ensure there is basic block when performing address space cast to CodeGen: Insert addr space cast for automatic/temp var at right position.Jul 17 2017, 8:37 AM
rjmccall added inline comments.Jul 17 2017, 10:53 AM
lib/CodeGen/CGExpr.cpp
76 ↗(On Diff #106879)

IRBuilder already has saveIP() and restoreIP() methods for this purpose that deal correctly with the current insertion point being null.

yaxunl marked an inline comment as done.Jul 17 2017, 11:05 AM
yaxunl added inline comments.
lib/CodeGen/CGExpr.cpp
76 ↗(On Diff #106879)

Thanks. I will use those.

yaxunl updated this revision to Diff 106910.Jul 17 2017, 11:06 AM
yaxunl marked an inline comment as done.

Use saveIP/restoreIP.

rjmccall accepted this revision.Jul 17 2017, 2:58 PM

LGTM.

This revision is now accepted and ready to land.Jul 17 2017, 2:58 PM
This revision was automatically updated to reflect the committed changes.