This is an archive of the discontinued LLVM Phabricator instance.

[CodeGen] Fix assertion on referencing constexpr Obj-C object with ARC.
ClosedPublic

Authored by vsapsai on Oct 24 2018, 2:43 PM.

Details

Summary

Failed assertion is

Assertion failed: ((ND->isUsed(false) || !isa<VarDecl>(ND) || !E->getLocation().isValid()) && "Should not use decl without marking it used!"), function EmitDeclRefLValue, file llvm-project/clang/lib/CodeGen/CGExpr.cpp, line 2437.

EmitDeclRefLValue mentions

A DeclRefExpr for a reference initialized by a constant expression can
appear without being odr-used. Directly emit the constant initializer.

The fix is in using the similar approach for non-references of
non-odr-used variables. EmitScalarExpr will try to emit constant if
possible and we can use resulting llvm::Value * without performing
EmitLValue.

rdar://problem/40650504

Diff Detail

Repository
rL LLVM

Event Timeline

vsapsai created this revision.Oct 24 2018, 2:43 PM
rjmccall added inline comments.Oct 24 2018, 5:41 PM
clang/lib/CodeGen/CGObjC.cpp
2480 ↗(On Diff #170990)

This switch is just checking what you already computed as SuppressResultRetain. Please just assert in the second case that the qualifier is OCL_Weak.

Also, please stay consistent with the surrounding capitalization of local variables.

2527 ↗(On Diff #170990)

Can we test constant-evaluability directly instead of only applying this when the declaration isn't otherwise used?

vsapsai added inline comments.Oct 25 2018, 11:02 AM
clang/lib/CodeGen/CGObjC.cpp
2480 ↗(On Diff #170990)

Not entirely sure I understand the comment about switch correctly. Will change the code according to my understanding.

2527 ↗(On Diff #170990)

We can use IsVariableAConstantExpression but it requires removing const from Decl.

Another way to test constant-evaluability is tryEmitAsConstant. It still requires removing const qualifier but overall it is a good approach. It captures the intention well and implementation-wise it is what CGF.EmitScalarExpr is doing anyway. I'll try it and will show how it looks like.

vsapsai updated this revision to Diff 171168.Oct 25 2018, 12:28 PM
  • Address review comments.
vsapsai marked 2 inline comments as done.Oct 25 2018, 12:33 PM
vsapsai added inline comments.
clang/lib/CodeGen/CGObjC.cpp
2527 ↗(On Diff #170990)

If we go with tryEmitAsConstant, there is a separate review D53725 to use EmitConstant in more places.

rjmccall added inline comments.Oct 26 2018, 9:05 PM
clang/lib/CodeGen/CGObjC.cpp
2480 ↗(On Diff #170990)

That's exactly what I was asking for, thank you.

vsapsai updated this revision to Diff 172049.Oct 31 2018, 4:36 PM
  • Rename EmitConstant to emitScalarConstant.
  • Tweak comment to be explicitly about intended IR code, not about Obj-C++ code.
vsapsai updated this revision to Diff 172051.Oct 31 2018, 4:39 PM

Exclude commits tracked in a different review.

rjmccall accepted this revision.Oct 31 2018, 9:24 PM

LGTM.

This revision is now accepted and ready to land.Oct 31 2018, 9:24 PM

Thanks for the review, John. I'll update the comment with TODO and commit.

This revision was automatically updated to reflect the committed changes.