Skip to content

Cache ReadsWrites instances for alias analysis#5472

Open
atta-ullah01 wants to merge 1 commit intop4lang:mainfrom
atta-ullah01:fix/cache-readswrites-instances
Open

Cache ReadsWrites instances for alias analysis#5472
atta-ullah01 wants to merge 1 commit intop4lang:mainfrom
atta-ullah01:fix/cache-readswrites-instances

Conversation

@atta-ullah01
Copy link
Contributor

Addresses the FIXMEs in sideEffects.cpp and copyStructures.cpp noting that ReadsWrites was being recreated on every mayAlias() call, losing all declaration lookup caching.

Now ReadsWrites is kept as a class member and reused across calls. Also added a cache check in ReadsWrites::get() to return early when we've already analyzed an expression, and cached GetWrittenExpressions results per MethodCallExpression.

@atta-ullah01 atta-ullah01 force-pushed the fix/cache-readswrites-instances branch from 407005d to 255b8fe Compare February 2, 2026 18:42
@atta-ullah01 atta-ullah01 force-pushed the fix/cache-readswrites-instances branch from 255b8fe to 5a84e22 Compare February 5, 2026 00:44
@fruffy fruffy added the core Topics concerning the core segments of the compiler (frontend, midend, parser) label Feb 6, 2026
@fruffy fruffy requested a review from asl February 12, 2026 20:18
GetWrittenExpressions gwe(typeMap);
gwe.setCalledBy(this);
mce->apply(gwe, getContext());
auto cacheIt = writtenExpressionsCache.find(orig);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will you please use lazy_emplace here? This way only a single map lookup will be done and the map entry will be constructed in-place. Something like this:

modifies = writtenExpressionsCache.lazy_emplace(orig,
                                       [this,mce,orig](const auto &ctor) {
                                             GetWrittenExpressions gwe(typeMap);
                                             gwe.setCalledBy(this);
                                             mce->apply(gwe, getContext());
                                             ctor(orig, gwe.written);
                                        })->second;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

gwe.setCalledBy(this);
mce->apply(gwe, getContext());
writtenExpressionsCache[orig] = gwe.written;
modifies = gwe.written;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you want to copy the whole set here. Maybe you'd just use pointer here (be careful though as this will be pointer to map entry so it could be invalidated on every insertion)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed modifies to a const auto &, the reference is safe now since lazy_emplace handles both find/insert in one call

@atta-ullah01 atta-ullah01 force-pushed the fix/cache-readswrites-instances branch from 5a84e22 to bcc7df3 Compare February 18, 2026 01:22
@asl asl force-pushed the fix/cache-readswrites-instances branch from bcc7df3 to 0dd29ee Compare February 18, 2026 02:41
@asl asl added run-validation Use this tag to trigger a Validation CI run. run-sanitizer Use this tag to run a Clang+Sanitzers CI run. labels Feb 18, 2026
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.

Clear both caches in end_apply() to allow GC and prevent stale entries.

Signed-off-by: Attaullah Ansari <mdattaullahansari152@gmail.com>
@asl asl force-pushed the fix/cache-readswrites-instances branch from 0dd29ee to 1a158f0 Compare February 19, 2026 05:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Topics concerning the core segments of the compiler (frontend, midend, parser) run-sanitizer Use this tag to run a Clang+Sanitzers CI run. run-validation Use this tag to trigger a Validation CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants