This is an archive of the discontinued LLVM Phabricator instance.

[clang][ConstExprEmitter] handle IntegerLiterals
ClosedPublic

Authored by nickdesaulniers on Jul 24 2023, 10:44 AM.

Details

Summary

Improves the ability of ConstExprEmitter to evaluate constants.

Found by adding asserts to ConstantEmitter::tryEmitPrivate to find cases
where ConstExprEmitter::Visit() fails to resolve (obvious) constants.

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptJul 24 2023, 10:44 AM
nickdesaulniers requested review of this revision.Jul 24 2023, 10:44 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 24 2023, 10:44 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

As a practical matter, I'm not sure this helps much; ExprConstant should be reasonably fast for simple integers. The performance issues only really show for structs/arrays. But maybe it helps a little to handle a few of the most common integer expressions.

clang/lib/CodeGen/CGExprConstant.cpp
1128

This doesn't look right; I suspect this will return an integer with the wrong width.

nickdesaulniers marked an inline comment as done.
  • put back the IntegralCast, for now

As a practical matter, I'm not sure this helps much; ExprConstant should be reasonably fast for simple integers. The performance issues only really show for structs/arrays. But maybe it helps a little to handle a few of the most common integer expressions.

Perhaps, but after D151587, ConstExprEmitter will be tried first before ExprConstant::Evaluate (or rather Evaluate in ExprConstant.cpp; god, why is that a static function and not a method???). So it seems bad if we can't just evaluate the constant then and there.

nickdesaulniers retitled this revision from [clang][ConstExprEmitter] handle IntegerLiterals and IntegralCasts to [clang][ConstExprEmitter] handle IntegerLiterals.
  • remove mention of IntegralCasts from commit description

We don't completely fall back if a subexpression fails to evaluate. EmitArrayInitialization etc. don't recursively visit; they use tryEmitPrivateForMemory, so we can fallback for subexpressions. (tryEmitPrivateForMemory can still fail for certain constructs, like the MaterializeTemporary cases we spent so much time on, but the most common cases should work.)

efriedma accepted this revision.Jul 24 2023, 11:53 AM

That said, skipping all those abstraction layers for the simplest cases is probably worth it; LGTM.

This revision is now accepted and ready to land.Jul 24 2023, 11:53 AM
shafik added a subscriber: shafik.Jul 24 2023, 3:26 PM

What practical effects will this have? What kind of code do this effect?

What practical effects will this have?

As of D151587...

From the commit message Improves the ability of ConstExprEmitter to evaluate constants. Stated another way:
Should speed up "is the below expression a constant expression?"

What kind of code do this effect?

int y = 2;
        ^ is 2 a constant expression?

Should speed up "is the below expression a constant expression?"

What kind of code do this effect?

int y = 2;
        ^ is 2 a constant expression?

Nice!

This revision was automatically updated to reflect the committed changes.