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.
Details
Diff Detail
- Repository
- rL LLVM
Event Timeline
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); }
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.
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. |
lib/CodeGen/CGExpr.cpp | ||
---|---|---|
76 ↗ | (On Diff #106879) | Thanks. I will use those. |