Disable dead code when an if/else if condition is constant#3134
Disable dead code when an if/else if condition is constant#3134kinke merged 30 commits intoldc-developers:masterfrom baziotis:if_false2
Conversation
|
Just saw GDC. I have some problems to compile from source and verify the path but 99% it's the following. |
|
Just saw Clang as well. I did not expect to have so clear code. And it's pretty similar to LDC to. Anyway, here there is again a call to ContainsLabel. |
|
@kinke , @JohanEngelen what do you think? I'm pinging you as you may haven't seen it because it's marked as WIP and also fails.
I have discussed this on Slack and I don't see us getting this info, so I guess the only option is to walk the tree. |
|
Thx for the analysis. I don't find this If the compilation speed isn't the primary concern, we could make our lives easy and leverage some LLVM optimization pass to get rid of these superfluous branches and basic blocks. At Checking for labels in the dead branch during codegen shouldn't be very hard; there are |
Ok, I was not aware. Yes the implication with "implement our own" is that it can't be as simple as Clang's. There has to be some sort of visitor.
TBH, I don't find it very useful either. I mean, it's good to have it, but if the expense is to have a whole visitor for it, probably not worth it. Even more in D, where as you said, there is Here are though some maybe more subtle things:
One does not care to simplify the CFG in b) Using a simple grep, I had seen
I think that this is a bad option. Both for debugging but also, correct me if I'm wrong, LDC would like to be faster. |
|
I believe this helps in compilation speed for large if statements. The benefit is probably not so big, but the cost isn't either. (you should only traverse the tree when the condition is known at compile-time) Some extra pointers:
|
Thanks I planned to implement my own visitor. I'm comfortable with the solution, it's the DMD (?) visitors that I'm not sure about. I'll search about them.
Clang seems to have both emitCounterIncrement, incrementProfileCounter. I'm not at all familiar with PGO. It seems that it tracks branches (or basic blocks..) that are taken, so it makes sense that we want it only for |
|
I meant that you should create your own visitor subclassed from
|
I had some problems last time I tried to run dmd testsuite. I'll see what I can do, but till then, in this
It's weird yes. But I think it starts to make sense. For a simple code like this:
Is it kind of what I described? And the PGO tests should try to verify that every block has this pattern? |
|
For coverage: although the code changes may clearly show no bugginess, it's good to test that For PGO: indeed, there is only a counter for the if-branch, and not for the else-branch. (assuming we know the execution count before the if-statement, else_count = before_if - if_branch_count. I wrote the tests and also have to study them a bit ;-) I think things will make more sense if you study the IR output of the PGO tests a little. |
I get the idea but not how to do the test. cov2 or dmd_coverDestPath doc don't help either.
Smart!
They start to make sense yes. I'll write some tests tomorrow and you can review the way forward. :) Ah, unrelated but just crossed my mind: Currently we generate the |
Note the
Yeah that's fine. People and tooling may query the counters and expect there to be the counter with the correct count.
What platform are you on? When building LLVM, you also need to build LLVM's compiler-rt for the functionality. (should be part of LDC's LLVM repo)
I would leave this optimization out, it's |
Thanks, I'll study it tomorrow.
I see, alright.
I'm on ubuntu linux. When I ran CMake, I used
Indeed. Ok, as we're doing right now. |
Thanks. I actually did the change but of course, because it's me, I forgot to
If I understood correctly, now it should be ok. I rebased back to the commit that the ldc dmd-testsuite submodule is and added my commit on top. |
|
A little trick to quickly locate the dmd-testsuite failures for Azure: click on the dmd-testsuite step to open the log, then press Ctrl+F to open the VS-Code-search, and then search for |
| irs->DBuilder.EmitBlockStart(executed->loc); | ||
| } | ||
| // True condition, the branch is taken so emit counter increment. | ||
| if (!const_val->isZeroValue()) { |
There was a problem hiding this comment.
Can it ever happen to have !zeroValue && !executed ? Basically, this boils down to can ever the stmt->ifbody be null ?
There was a problem hiding this comment.
I don't think so, but it's probably better to assume the front-end might spit out a null.
There was a problem hiding this comment.
Good, just double-checking.
Oh really ? What now ? :P |
|
Yeah sry about that :] - you'll have to rebase this and your dmd-testsuite branch again. As a bonus, the unrelated Win64 Azure failures will be fixed. |
Ok ok, thanks. :) |
|
I think that something's wrong with the 2 failed tests. |
|
Yeah don't worry about those 2 CI failures for now, they generally fail at the moment, so this PR is virtually green now. |
|
Great! |
Conflicts: tests/d2/dmd-testsuite
|
@baziotis Rather than merging master into your branch, you should rebase the changes onto master. That way history stays much cleaner. |
Could you specify where I did the opposite ? I might have missed it, AFAIK, I did what you propose. |
|
I did, because I also created a corresponding dmd-testsuite branch in the official repo (rebased and both previous commits squashed into one), and that doesn't play with nice with multiple commits in here touching dmd-testsuite. I was going to squash these 30 commits anyway - are there other preferences? |
|
@JohanEngelen: Do you prefer it unsquashed then? Otherwise I'll squash-merge later this evening and release the first 1.19 beta. |
|
@kinke sry for late response, I was out traveling. Indeed, with squash merge it doesn't matter |
A more specific PR, result of a previous: #3133
Issue: #2344
Although @JohanEngelen you proposed that I focus on
if (0)on this PR, when I wrote this,if (1)had exactly the same logic and seemed like it fit quite nicely. I can remove it though.-- Doesn't take into consideration labels.
The comments in the previous PR are worth a look. The tl;dr is:
Regarding the last one, I had no hope with DMD. I did spend some time and I could not even target where an if statement (or such) is handled.
Lastly, I don't quite get
insertBBandirs->DBuilder. I know what a basic block is and that the DBuilder has to do with debug info. But I don't understand exactly how they are used, when etc.Edit: I should probably add a
delete cond_eas it is done in e.g. (WhileStatement](https://github.com/ldc-developers/ldc/blob/master/gen/statements.cpp#L410). Probably was missed by accident here.