This is an archive of the discontinued LLVM Phabricator instance.

[clang][Interp] Implement for loops
ClosedPublic

Authored by tbaeder on Oct 12 2022, 4:42 AM.

Diff Detail

Event Timeline

tbaeder created this revision.Oct 12 2022, 4:42 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 12 2022, 4:42 AM
tbaeder requested review of this revision.Oct 12 2022, 4:42 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 12 2022, 4:42 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
aaron.ballman accepted this revision.Oct 20 2022, 6:53 AM

LGTM aside from some minor points.

clang/lib/AST/Interp/ByteCodeStmtGen.cpp
361

What does the call to discard() do? Evaluate the expression and discard the results? (If so, the API might want for a better name at some point, as it can also read like "do nothing with this expression".)

clang/test/AST/Interp/loops.cpp
217–227

I'd love to see a stress test for the compile time performance of:

int func() {
  int i = 0;
  for (;;) {
    if (i == __INT_MAX__)
      break;
    ++i;
  }
  return i;
}
static_assert(func() == __INT_MAX__);

I don't think there's value to testing specifically how long it takes for this to execute, but we should probably start thinking about how to validate performance characteristics pretty soon. Perhaps relative performance compared to the existing constant expression evaluator would be interesting though?

272

Another test to add here: for (;;); is just as infinite.

This revision is now accepted and ready to land.Oct 20 2022, 6:53 AM
tbaeder marked an inline comment as done.Oct 20 2022, 7:17 AM
tbaeder added inline comments.
clang/lib/AST/Interp/ByteCodeStmtGen.cpp
361

Yes, it's the same problem that https://reviews.llvm.org/D136013 tries to solve. If Inc evaluates to a value, it will leave that on the stack and something needs to remove it from there, which is what discard() does.

clang/test/AST/Interp/loops.cpp
217–227

Perhaps relative performance compared to the existing constant expression evaluator would be interesting though?

Definitely. My plan was basically to get some base functionality going and then always keep an eye on performance. Your test doesn't work because the current interpreter has a step limit, but for a simple loop up until 100'000, the current interpreter takes 42s here and the new one takes 2s.

tbaeder added inline comments.Oct 20 2022, 7:19 AM
clang/test/AST/Interp/loops.cpp
272

Heh, that's what this was supposed to be :) I already have the while(true) one above.

This revision was automatically updated to reflect the committed changes.