[PATCH] D153476: [dataflow] document & test determinism of formula structure

Sam McCall via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 21 15:33:29 PDT 2023


sammccall created this revision.
Herald added subscribers: martong, mgrang, xazax.hun.
Herald added a reviewer: NoQ.
Herald added a project: All.
sammccall requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This doesn't depend on object addresses, even though they are used
in canonicalization. This is important & subtle, so add a comment+test.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D153476

Files:
  clang/lib/Analysis/FlowSensitive/Arena.cpp
  clang/unittests/Analysis/FlowSensitive/ArenaTest.cpp


Index: clang/unittests/Analysis/FlowSensitive/ArenaTest.cpp
===================================================================
--- clang/unittests/Analysis/FlowSensitive/ArenaTest.cpp
+++ clang/unittests/Analysis/FlowSensitive/ArenaTest.cpp
@@ -147,5 +147,28 @@
   EXPECT_EQ(&Formula1, &Formula2);
 }
 
+// Tests that the structure of the formula we end up with does not depend on
+// details like the pointer address of internal values.
+TEST_F(ArenaTest, Canonicalization) {
+  StringRef Expected = "((V0 | V1) = (V1 & V0))";
+
+  auto &AX = A.create<AtomicBoolValue>();
+  auto &AY = A.create<AtomicBoolValue>();
+  auto &AOr = A.makeOr(AX, AY);
+  auto &AAnd = A.makeAnd(AY, AX);
+  auto &AEqual = A.makeEquals(AOr, AAnd);
+  EXPECT_EQ(Expected, llvm::to_string(A.getFormula(AEqual)));
+
+  // Same, but create the & clause first.
+  // The relative order of (&AOr, &AAnd) and (&BOr, &BAnd) likely differs.
+  Arena B;
+  auto &BX = A.create<AtomicBoolValue>();
+  auto &BY = A.create<AtomicBoolValue>();
+  auto &BAnd = A.makeAnd(BX, BY);
+  auto &BOr = A.makeOr(BY, BX);
+  auto &BEqual = A.makeEquals(BOr, BAnd);
+  EXPECT_EQ(Expected, llvm::to_string(B.getFormula(BEqual)));
+}
+
 } // namespace
 } // namespace clang::dataflow
Index: clang/lib/Analysis/FlowSensitive/Arena.cpp
===================================================================
--- clang/lib/Analysis/FlowSensitive/Arena.cpp
+++ clang/lib/Analysis/FlowSensitive/Arena.cpp
@@ -10,6 +10,10 @@
 
 namespace clang::dataflow {
 
+// Compute a canonical key for an unordered poir of operands, so that we
+// can intern (A & B) and (B & A) to the same formula.
+// This does not affect the actual order used in the formula, which is always
+// that of the first call to makeAnd().
 static std::pair<BoolValue *, BoolValue *>
 makeCanonicalBoolValuePair(BoolValue &LHS, BoolValue &RHS) {
   auto Res = std::make_pair(&LHS, &RHS);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153476.533419.patch
Type: text/x-patch
Size: 1899 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230621/876d933a/attachment-0001.bin>


More information about the cfe-commits mailing list