This is an archive of the discontinued LLVM Phabricator instance.

[ASTMatchers][NFC] Remove runtime checks where compile time is sufficient
Needs ReviewPublic

Authored by njames93 on May 12 2021, 2:18 PM.

Details

Summary

Some polymorphic matchers use dyn_cast in cases where we know the type from the TemplateParameter.
Using type_traits we can remove the need for dyn_cast and let the template instantiation handle everything.

Diff Detail

Event Timeline

njames93 requested review of this revision.May 12 2021, 2:18 PM
njames93 created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptMay 12 2021, 2:18 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript

I'm not opposed, but I don't know that this is really an improvement. We've gone from a pretty simple code pattern to needing to spell out the type twice with a type_trait, and all that we save is a call to isa<> within the casting operation. While I agree this is strictly executing less code, it's morally the same as the usual antipattern of "isa followed by cast should be a dyn_cast". Is there evidence that this is a big performance win?

njames93 added a comment.EditedMay 13 2021, 4:28 AM

I'm not opposed, but I don't know that this is really an improvement. We've gone from a pretty simple code pattern to needing to spell out the type twice with a type_trait, and all that we save is a call to isa<> within the casting operation. While I agree this is strictly executing less code, it's morally the same as the usual antipattern of "isa followed by cast should be a dyn_cast". Is there evidence that this is a big performance win?

To be honest, it would be much nicer if we could specialise isa to return false if the From type is not a base class of the To type. However I'm not sure if that would break anything else.

It's all part of the "Don't do at runtime whats known at compile time" idiom. Any performance change would be negligible in this case.

I'm not opposed, but I don't know that this is really an improvement. We've gone from a pretty simple code pattern to needing to spell out the type twice with a type_trait, and all that we save is a call to isa<> within the casting operation. While I agree this is strictly executing less code, it's morally the same as the usual antipattern of "isa followed by cast should be a dyn_cast". Is there evidence that this is a big performance win?

To be honest, it would be much nicer if we could specialise isa to return false if the From type is not a base class of the To type. However I'm not sure if that would break anything else.

That might be interesting to explore.

It's all part of the "Don't do at runtime whats known at compile time" idiom. Any performance change would be negligible in this case.

If that's the sole reason for the change, I'm not convinced it carries its weight. It's more code, it's more complicated to read, violates the DRY principle, and has negligible performance benefit. However, perhaps @klimek has different opinions as code owner.