Skip to content

Commit 255b8fe

Browse files
committed
Cache ReadsWrites instances for alias analysis
Persist ReadsWrites instances in DoSimplifyExpressions and RemoveAliases instead of recreating them on every mayAlias() call. This reuses the internal lookup cache across multiple checks. Also cache GetWrittenExpressions results to avoid recomputing them for the same MethodCallExpression. Signed-off-by: Attaullah Ansari <mdattaullahansari152@gmail.com>
1 parent 7d36776 commit 255b8fe

File tree

5 files changed

+23
-15
lines changed

5 files changed

+23
-15
lines changed

frontends/p4/alias.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,10 @@ class ReadsWrites : public Inspector, public ResolutionContext {
242242
}
243243

244244
const SetOfLocations *get(const IR::Expression *expression, const Visitor::Context *ctxt) {
245+
if (auto it = rw.find(expression); it != rw.end()) {
246+
LOG3("SetOfLocations(" << expression << ")=" << it->second << " [cached]");
247+
return it->second;
248+
}
245249
expression->apply(*this, ctxt);
246250
auto result = ::P4::get(rw, expression);
247251
CHECK_NULL(result);

frontends/p4/sideEffects.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,7 @@ const IR::Node *DoSimplifyExpressions::preorder(IR::LOr *expression) {
320320

321321
bool DoSimplifyExpressions::mayAlias(const IR::Expression *left, const IR::Expression *right,
322322
const Visitor::Context *ctxt) const {
323-
// FIXME: This recreates ReadWrites() over and over again, loosing all
324-
// declaration lookup caching
325-
return ReadsWrites().mayAlias(left, right, ctxt);
323+
return readsWrites.mayAlias(left, right, ctxt);
326324
}
327325

328326
/// Returns true if type is a header or a struct containing a header.
@@ -406,24 +404,27 @@ const IR::Node *DoSimplifyExpressions::preorder(IR::MethodCallExpression *mce) {
406404
// large structs. We want to avoid copying these large
407405
// structs if possible.
408406
std::set<const IR::Parameter *> useTemporary;
409-
// Set of expressions modified while evaluating this method call.
410407
ordered_set<const IR::Expression *> modifies;
411-
// FIXME: We need to be able to cache results here and not to recompute over
412-
// and over again
413-
GetWrittenExpressions gwe(typeMap);
414-
gwe.setCalledBy(this);
415-
mce->apply(gwe, getContext());
408+
auto cacheIt = writtenExpressionsCache.find(orig);
409+
if (cacheIt != writtenExpressionsCache.end()) {
410+
modifies = cacheIt->second;
411+
} else {
412+
GetWrittenExpressions gwe(typeMap);
413+
gwe.setCalledBy(this);
414+
mce->apply(gwe, getContext());
415+
writtenExpressionsCache[orig] = gwe.written;
416+
modifies = gwe.written;
417+
}
416418

417419
for (auto p : *mi->substitution.getParametersInArgumentOrder()) {
418420
if (p->direction == IR::Direction::None) continue;
419421
auto arg = mi->substitution.lookup(p);
420-
if (gwe.written.find(GetWrittenExpressions::everything) != gwe.written.end()) {
422+
if (modifies.find(GetWrittenExpressions::everything) != modifies.end()) {
421423
// just copy everything.
422424
LOG3("Detected table application, using temporaries for all parameters " << arg);
423425
for (auto p : *mi->substitution.getParametersInArgumentOrder()) useTemporary.emplace(p);
424426
break;
425427
}
426-
modifies.insert(gwe.written.begin(), gwe.written.end());
427428

428429
// If an argument evaluation has side-effects then
429430
// always use a temporary to hold the argument value.

frontends/p4/sideEffects.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ limitations under the License.
2121

2222
#include "frontends/common/resolveReferences/referenceMap.h"
2323
#include "frontends/common/resolveReferences/resolveReferences.h"
24+
#include "frontends/p4/alias.h"
2425
#include "frontends/p4/methodInstance.h"
2526
#include "frontends/p4/typeChecking/typeChecker.h"
2627
#include "ir/ir.h"
@@ -149,6 +150,9 @@ class DoSimplifyExpressions : public Transform, P4WriteContext, public Resolutio
149150
TypeMap *typeMap;
150151
// Expressions holding temporaries that are already added.
151152
std::set<const IR::Expression *> *added;
153+
mutable ReadsWrites readsWrites;
154+
std::map<const IR::MethodCallExpression *, ordered_set<const IR::Expression *>>
155+
writtenExpressionsCache;
152156

153157
IR::IndexedVector<IR::Declaration> toInsert; // temporaries
154158
IR::IndexedVector<IR::StatOrDecl> statements;

midend/copyStructures.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ const IR::Node *RemoveAliases::postorder(IR::AssignmentStatement *statement) {
2727
return statement;
2828
}
2929

30-
// FIXME: This recreates ReadWrites() over and over again, loosing all
31-
// declaration lookup caching
32-
ReadsWrites rw;
33-
if (!rw.mayAlias(statement->left, statement->right, getContext())) {
30+
if (!readsWrites.mayAlias(statement->left, statement->right, getContext())) {
3431
return statement;
3532
}
3633
auto tmp = nameGen.newName("tmp");

midend/copyStructures.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
#ifndef MIDEND_COPYSTRUCTURES_H_
1818
#define MIDEND_COPYSTRUCTURES_H_
1919

20+
#include "frontends/p4/alias.h"
2021
#include "frontends/p4/typeChecking/typeChecker.h"
2122
#include "ir/ir.h"
2223

@@ -104,6 +105,7 @@ class DoCopyStructures : public Transform {
104105
class RemoveAliases : public Transform {
105106
MinimalNameGenerator nameGen;
106107
TypeMap *typeMap;
108+
mutable ReadsWrites readsWrites;
107109

108110
IR::IndexedVector<IR::Declaration> declarations;
109111

0 commit comments

Comments
 (0)