This is an archive of the discontinued LLVM Phabricator instance.

[clang][dataflow] Implement transferBranch
ClosedPublic

Authored by martong on Sep 12 2022, 7:15 AM.

Details

Summary

This patch introduces transferBranch, which Applies the analysis
transfer function for a given edge from a CFG block of a conditional
statement.

RFC:
https://discourse.llvm.org/t/rfc-clang-dataflow-signanalysis-edgetransfer-branchtransfer/65220

Diff Detail

Event Timeline

martong created this revision.Sep 12 2022, 7:15 AM
Herald added a reviewer: NoQ. · View Herald Transcript
Herald added a project: Restricted Project. · View Herald Transcript
martong requested review of this revision.Sep 12 2022, 7:15 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 12 2022, 7:15 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
martong edited the summary of this revision. (Show Details)Sep 12 2022, 7:19 AM
martong edited the summary of this revision. (Show Details)Sep 12 2022, 8:14 AM
gribozavr2 added inline comments.Sep 15 2022, 4:04 PM
clang/include/clang/Analysis/FlowSensitive/DataflowAnalysis.h
115

Please use CRTP (a non-virtual function here), and you'll need SFINAE to detect the presence of the overload of branchTransfer in branchTransferTypeErased.

clang/include/clang/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.h
86

Please add a doc comment. Please add a parameter name for the condition, it is not obvious what it is.

Could you think of a more descriptive name for Branch? For example, if the second parameter is Cond, WDYT about CondValue?

86

WDYT about calling it transferBranch... instead of branchTransfer...?

clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
78–81

Please add a comment to RetTy.

149

CondValue?

254

It would be nice to still call branchTransfer even when BuiltinTransferOpts == false. Please leave a TODO if you don't want to implement that.

clang/unittests/Analysis/FlowSensitive/SignAnalysisTest.cpp
586 ↗(On Diff #459451)

This file is an amazing example of how to use the framework, but could you add a more direct unit test with a simpler lattice and analysis that exercises the new callback?

dkrupp added inline comments.Sep 16 2022, 1:00 AM
clang/unittests/Analysis/FlowSensitive/SignAnalysisTest.cpp
241 ↗(On Diff #459451)

Isn't this SignLattice::top() instead?

This is an initialization expression, which we cannot evaluate to int, but the variable is initialized.

martong planned changes to this revision.Sep 20 2022, 4:46 AM

Aligned with the RFC, I am going to dissect this patch into two patches:

  1. This patch will remain a simple infrastructural change that introduces transferBranch. This could be useful for complex lattices (e.g integer value ranges) until we have an SMT solver in the core framework. I'll try to add a very simple unit test case instead of having SignAnalysis itself as a huge integration test case.
  2. An independent patch for SignAnalysis implementation. I am going to rework it by using the boolean Value abstraction as suggested in the RFC.
martong updated this revision to Diff 469625.Oct 21 2022, 8:30 AM
martong marked 8 inline comments as done.
  • Add comments
  • Assumption -> ConditionValue
  • Use CRTP
  • branchTransfer -> transferBranch
  • Make just one simple test case for transferBranch
martong retitled this revision from [clang][dataflow] SignAnalysis, edgeTransfer, branchTransfer to [clang][dataflow] Implement transferBranch.Oct 21 2022, 8:32 AM
martong edited the summary of this revision. (Show Details)
martong edited the summary of this revision. (Show Details)
martong updated this revision to Diff 469636.Oct 21 2022, 8:47 AM
martong edited the summary of this revision. (Show Details)
  • Rebase to latest llvm/main
martong added inline comments.Oct 21 2022, 8:50 AM
clang/include/clang/Analysis/FlowSensitive/DataflowAnalysis.h
115

Ok, changed it to use CRTP.

clang/include/clang/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.h
86

Ok, added a doc comment and renamed to transferBranch.

clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
149

Ok, changed to ConditionValue.

254

Ok, I've added a TODO.

clang/unittests/Analysis/FlowSensitive/SignAnalysisTest.cpp
241 ↗(On Diff #459451)

I am moving the sign analysis out of this patch.

586 ↗(On Diff #459451)

Ok. I've removed the sign analysis from this patch and added a very simple test file with a very simple lattice to check the effect of transferBranch.

clang/unittests/Analysis/FlowSensitive/TransferBranchTest.cpp
60–66

This is unused (a leftover). I am going remove this matcher.

martong marked an inline comment as not done.Oct 21 2022, 8:51 AM
ymandel requested changes to this revision.Oct 25 2022, 8:33 AM

Overall, looks quite good.

clang/include/clang/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.h
87–88
clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
78–79

please lift this out and define it as a struct. then, refer to it by name on line 76. that will improve the readability of the code and provide a way to document explicitly the role of the two fields.

258

I generally prefer explicit comparison but it's particularly important here, given that Cond sounds like it could be a boolean.

259
clang/unittests/Analysis/FlowSensitive/TransferBranchTest.cpp
28

feels odd to mix and match namespace handling here. I think that if you want to go with namespace clang... that's fine but then do the same for test. In fact, since we're now in C++17, maybe:

namespace clang::dataflow::test {
namespace {
31

bool? i understand that you don't want to use bool directly, but still seems like the better base type here.

86–88

Can your test below use AnalysisOutputs directly, and thereby avoid this conversion lambda?

120

Please be explicit about the optional, since this is an important detail of the test. Same below.

123

EXPECT_THAT...

This revision now requires changes to proceed.Oct 25 2022, 8:33 AM
martong added inline comments.Oct 25 2022, 8:48 AM
clang/unittests/Analysis/FlowSensitive/TransferBranchTest.cpp
9–11

I should update this comment as well.

xazax.hun accepted this revision.Oct 25 2022, 9:02 AM

With the rest of the comments addressed it looks good to me.

martong marked 9 inline comments as done.Oct 26 2022, 2:12 AM

Thanks for the assiduous review @ymandel !

clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
78–79

Ok, I hoisted this to be TerminatorVisitorRetTy.

martong updated this revision to Diff 470748.Oct 26 2022, 2:12 AM
martong marked an inline comment as done.
  • Address ymandel's comments
ymandel accepted this revision.Oct 26 2022, 5:21 AM
ymandel added inline comments.
clang/unittests/Analysis/FlowSensitive/TransferBranchTest.cpp
74

nit: maybe just rename the parameter to VerifyResults (and then no need for the comment)?

This revision is now accepted and ready to land.Oct 26 2022, 5:21 AM
This revision was automatically updated to reflect the committed changes.
martong marked an inline comment as done.
martong added inline comments.Oct 26 2022, 6:25 AM
clang/unittests/Analysis/FlowSensitive/TransferBranchTest.cpp
74

Good idea, I've renamed it within the commit.

martong added inline comments.Oct 26 2022, 8:25 AM
clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
161

Actually, this member is not used, I am not sure why was this added. Anyway, I am removing this in a follow up commit.

martong added inline comments.Oct 26 2022, 8:30 AM
clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
161

FWIW, it looks like this change broke the AIX bot: https://lab.llvm.org/buildbot/#/builders/214/builds/3932

Yes, I've received many build bot failures complaining about the unused private field. I hope that I've fixed that in
https://github.com/llvm/llvm-project/commit/5bd142ca265d8243ecebb63ffed0c7afd3abf440