-
Notifications
You must be signed in to change notification settings - Fork 10
[Analyzer, CFG] Incremental caching #2023
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c9e4043
feat(analyzer): re-use base cfg
MaxAtoms aa8107d
refactor(dataflow): fix typo
MaxAtoms 5b98f60
refactor(dataflow): update doc comment
MaxAtoms 68e6557
tests(analyzer): add control-flow tests
MaxAtoms 86cb4ed
feat(analyzer): improved caching
MaxAtoms bf4a835
refactor(analyzer): extract cfg cache
MaxAtoms aa1c010
tests(analyzer): assert cfg kind differentiation
MaxAtoms b045bf5
refactor(analyzer): do not enforce a simplification order
MaxAtoms 5ed21a2
tests(analyzer): assert cfg quick
MaxAtoms 32f1861
Merge remote-tracking branch 'origin/main' into 1963-incremental-cfg-…
MaxAtoms b0eb34b
fixup! refactor(dataflow): update doc comment
MaxAtoms ee875d3
Merge remote-tracking branch 'origin/main' into 1963-incremental-cfg-…
MaxAtoms File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import { ObjectMap } from '../../util/collections/objectmap'; | ||
| import type { CfgSimplificationPassName } from '../../control-flow/cfg-simplification'; | ||
| import { simplifyControlFlowInformation } from '../../control-flow/cfg-simplification'; | ||
| import { CfgKind } from '../cfg-kind'; | ||
| import type { ControlFlowInformation } from '../../control-flow/control-flow-graph'; | ||
| import { guard } from '../../util/assert'; | ||
| import { extractCfg, extractCfgQuick } from '../../control-flow/extract-cfg'; | ||
| import type { NormalizedAst } from '../../r-bridge/lang-4.x/ast/model/processing/decorate'; | ||
| import type { DataflowInformation } from '../../dataflow/info'; | ||
| import type { FlowrAnalyzerContext } from '../context/flowr-analyzer-context'; | ||
|
|
||
| type ControlFlowCache = ObjectMap<[passes: readonly CfgSimplificationPassName[], kind: CfgKind], ControlFlowInformation>; | ||
|
|
||
| interface CfgInfo { | ||
MaxAtoms marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ctx: FlowrAnalyzerContext, | ||
| cfgQuick: ControlFlowInformation | undefined | ||
| dfg: () => Promise<DataflowInformation>, | ||
| ast: () => Promise<NormalizedAst>, | ||
| } | ||
|
|
||
| export class FlowrAnalyzerControlFlowCache { | ||
| private readonly cache: ControlFlowCache = new ObjectMap<[readonly CfgSimplificationPassName[], CfgKind], ControlFlowInformation>(); | ||
|
|
||
| public peek(kind: CfgKind, simplifications: readonly CfgSimplificationPassName[] | undefined): ControlFlowInformation | undefined { | ||
| return this.cache.get([simplifications ?? [], kind]); | ||
| } | ||
|
|
||
| public async get( | ||
| force: boolean | undefined, | ||
| kind: CfgKind, | ||
| cfgCacheInfo: CfgInfo, | ||
| simplifications?: readonly CfgSimplificationPassName[] | ||
| ): Promise<ControlFlowInformation> { | ||
| guard(kind === CfgKind.Quick ? simplifications === undefined : true, 'Cannot apply simplifications to quick CFG'); | ||
| simplifications ??= []; | ||
| const orderedSimplifications = this.normalizeSimplificationOrder(simplifications); | ||
|
|
||
| const cached = force ? | ||
| { cfg: undefined, missingSimplifications: orderedSimplifications } | ||
| : this.tryGetCachedCfg(orderedSimplifications, kind); | ||
| let cfg = cached.cfg; | ||
|
|
||
| if(!cfg) { | ||
| cfg = await this.createAndCacheBaseCfg(kind, cfgCacheInfo); | ||
| } | ||
|
|
||
| if(cached.missingSimplifications.length > 0) { | ||
| const cfgPassInfo = { dfg: (await cfgCacheInfo.dfg()).graph, ctx: cfgCacheInfo.ctx, ast: await cfgCacheInfo.ast() }; | ||
| cfg = simplifyControlFlowInformation(cfg, cfgPassInfo, cached.missingSimplifications); | ||
| } | ||
|
|
||
| this.cache.set([orderedSimplifications, kind], cfg); | ||
| return cfg; | ||
| } | ||
|
|
||
| /** | ||
| * Create and cache the base CFG without simplifications. | ||
| */ | ||
| private async createAndCacheBaseCfg(kind: CfgKind, { cfgQuick, dfg, ctx, ast }: CfgInfo): Promise<ControlFlowInformation> { | ||
| let result: ControlFlowInformation; | ||
| switch(kind) { | ||
| case CfgKind.WithDataflow: | ||
| result = extractCfg(await ast(), ctx, (await dfg()).graph); | ||
| break; | ||
| case CfgKind.NoDataflow: | ||
| result = extractCfg(await ast(), ctx); | ||
| break; | ||
| case CfgKind.Quick: | ||
| result = cfgQuick ?? extractCfgQuick(await ast()); | ||
| break; | ||
| } | ||
| this.cache.set([[], kind], result); | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Try to get a cached CFG with some of the requested simplifications already applied. | ||
| * Matches the longest prefix of simplifications available. | ||
| * @returns The cached CFG and the missing simplifications to be applied, or `undefined` if no cached CFG is available. | ||
| */ | ||
| private tryGetCachedCfg(simplifications: readonly CfgSimplificationPassName[], kind: CfgKind): { cfg: ControlFlowInformation | undefined, missingSimplifications: readonly CfgSimplificationPassName[] } { | ||
| for(let prefixLen = simplifications.length; prefixLen >= 0; prefixLen--) { | ||
| const prefix = simplifications.slice(0, prefixLen); | ||
| const cached = this.cache.get([prefix, kind]); | ||
| if(cached !== undefined) { | ||
| return { | ||
| cfg: cached, | ||
| missingSimplifications: simplifications.slice(prefixLen) | ||
| }; | ||
| } | ||
| } | ||
| return { cfg: undefined, missingSimplifications: simplifications }; | ||
| } | ||
|
|
||
| /** | ||
| * Normalize the order of simplification passes. | ||
| * Is currently an identity function, but may be extended in the future to enforce a specific order using heuristics. | ||
| * @param simplifications - the requested simplification passes. | ||
| */ | ||
| private normalizeSimplificationOrder(simplifications: readonly CfgSimplificationPassName[]): readonly CfgSimplificationPassName[] { | ||
| return simplifications; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { assert, describe, test } from 'vitest'; | ||
| import { escapeNewline } from '../../../src/documentation/doc-util/doc-escape'; | ||
|
|
||
| describe('Document Escape Tests', () => { | ||
| test(() => { | ||
| const input = 'Line 1\nLine 2\rLine 3\r\nLine 4'; | ||
| const expectedOutput = 'Line 1\\nLine 2\\rLine 3\\r\\nLine 4'; | ||
| const result = escapeNewline(input); | ||
| assert.strictEqual(result, expectedOutput); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.