Skip to content

[AbsInt] Tracking Data-Frame Shapes #1466

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

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/abstract-interpretation/data-frame/absint-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { NodeId } from '../../r-bridge/lang-4.x/ast/model/processing/node-id';
import type { DataFrameStateDomain } from './domain';
import type { DataFrameOperationArgs, DataFrameOperationName } from './semantics';

export interface DataFrameOperation<Name extends DataFrameOperationName> {
operation: Name,
operand: NodeId | undefined,
args: DataFrameOperationArgs<Name>
}

export type DataFrameOperations = {
[Name in DataFrameOperationName]: DataFrameOperation<Name>;
}[DataFrameOperationName];

interface DataFrameInfoBase {
domain?: DataFrameStateDomain
}

export interface DataFrameAssignmentInfo {
type: 'assignment',
identifier: NodeId,
expression: NodeId
}

export interface DataFrameExpressionInfo {
type: 'expression',
operations: DataFrameOperations[]
}

export type DataFrameInfo = DataFrameAssignmentInfo | DataFrameExpressionInfo;

export interface AbstractInterpretationInfo {
dataFrame?: (DataFrameInfo & DataFrameInfoBase) | DataFrameInfoBase
}
158 changes: 158 additions & 0 deletions src/abstract-interpretation/data-frame/abstract-interpretation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import type { DataflowGraph } from '../../dataflow/graph/graph';
import type { RConstant, RNode, RSingleNode } from '../../r-bridge/lang-4.x/ast/model/model';
import type { ParentInformation } from '../../r-bridge/lang-4.x/ast/model/processing/decorate';
import type { NodeId } from '../../r-bridge/lang-4.x/ast/model/processing/node-id';
import { RType } from '../../r-bridge/lang-4.x/ast/model/type';
import { CfgVertexType, ControlFlowGraph, type CfgVertex, type ControlFlowInformation } from '../../util/cfg/cfg';
import type { AbstractInterpretationInfo } from './absint-info';
import type { DataFrameDomain, DataFrameStateDomain } from './domain';
import { equalDataFrameState, joinDataFrameStates, wideningDataFrameStates } from './domain';
import { processDataFrameExpression, processDataFrameLeaf } from './processor';

const WideningThreshold = 4;

export function performDataFrameAbsint(cfinfo: ControlFlowInformation, dfg: DataflowGraph): DataFrameStateDomain {
const visited: Map<NodeId, number> = new Map();
let finalDomain: DataFrameStateDomain = new Map();

const visitor = (cfg: ControlFlowGraph, vertex: CfgVertex): CfgVertex[] => {
if(shouldSkipVertex(vertex, dfg)) {
return getSuccessorVertices(cfg, vertex.id, dfg);
}
const predecessors = getPredecessorNodes(cfg, vertex.id, dfg);
const inputDomain = joinDataFrameStates(...predecessors.map(node => node.info.dataFrame?.domain ?? new Map()));
let oldDomain = new Map<NodeId, DataFrameDomain>();
let newDomain = inputDomain;

const entryNode: RNode<ParentInformation & AbstractInterpretationInfo> | undefined = dfg.idMap?.get(vertex.id);
let node: RNode<AbstractInterpretationInfo> | undefined;

if(entryNode !== undefined && isRSingleNode(entryNode)) {
oldDomain = entryNode.info.dataFrame?.domain ?? oldDomain;
newDomain = processDataFrameLeaf(entryNode, new Map(inputDomain), dfg);
node = entryNode;
} else if(vertex.type === CfgVertexType.EndMarker) {
const exitId = getNodeIdForExitVertex(vertex.id);
const exitNode: RNode<ParentInformation & AbstractInterpretationInfo> | undefined = exitId !== undefined ? dfg.idMap?.get(exitId) : undefined;

if(exitNode !== undefined && !isRSingleNode(exitNode)) {
oldDomain = exitNode.info.dataFrame?.domain ?? oldDomain;
newDomain = processDataFrameExpression(exitNode, new Map(inputDomain), dfg);
node = exitNode;
}
}
if(cfinfo.exitPoints.includes(vertex.id)) {
finalDomain = newDomain;
}
const visitedCount = visited.get(vertex.id) ?? 0;
visited.set(vertex.id, visitedCount + 1);

if(visitedCount >= WideningThreshold) {
newDomain = wideningDataFrameStates(oldDomain, newDomain);
}
if(node !== undefined) {
node.info.dataFrame ??= {};
node.info.dataFrame.domain = new Map(newDomain);
}
if(!equalDataFrameState(oldDomain, newDomain)) {
return getSuccessorVertices(cfg, vertex.id, dfg);
}
return getSuccessorVertices(cfg, vertex.id, dfg).filter(successor => !visited.has(successor.id));
};
const cfg = flipCfg(cfinfo.graph);
const entryPoints = cfinfo.entryPoints
.map(id => cfg.vertices().get(id))
.filter(vertex => vertex !== undefined);

foldCfg(cfg, entryPoints, visitor);
return finalDomain;
}

export function flipCfg(cfg: ControlFlowGraph): ControlFlowGraph {
const flippedCfg = new ControlFlowGraph();

for(const [id, vertex] of cfg.vertices()) {
flippedCfg.addVertex(vertex, cfg.rootVertexIds().has(id));
}
for(const [to, edges] of cfg.edges()) {
for(const [from, edge] of edges) {
flippedCfg.addEdge(from, to, edge);
}
}
return flippedCfg;
}

function foldCfg(
cfg: ControlFlowGraph,
vertices: CfgVertex[],
visitor: (cfg: ControlFlowGraph, vertex: CfgVertex) => CfgVertex[]
): void {
for(const vertex of vertices) {
const successors = visitor(cfg, vertex);
foldCfg(cfg, successors, visitor);
}
}

function isRConstant(
node: RNode<ParentInformation>
): node is RConstant<ParentInformation> {
return node.type === RType.String || node.type === RType.Number || node.type === RType.Logical;
}

function isRSingleNode(
node: RNode<ParentInformation>
): node is RSingleNode<ParentInformation> {
return isRConstant(node) || node.type === RType.Symbol || node.type === RType.Break || node.type === RType.Next || node.type === RType.Comment || node.type === RType.LineDirective;
}

// We only process vertices of leaf nodes and exit vertices (no entry nodes of complex nodes)
function shouldSkipVertex(vertex: CfgVertex, dfg: DataflowGraph) {
if(vertex.type === CfgVertexType.EndMarker) {
return false;
} else if(vertex.type === CfgVertexType.MidMarker) {
return true;
}
const node = dfg.idMap?.get(vertex.id);

return node === undefined || !isRSingleNode(node);
}

function getNodeIdForExitVertex(vertexId: NodeId): number | undefined {
if(typeof vertexId === 'number') {
return vertexId;
}
const nodeId = Number(vertexId.match(/^(\d+)/)?.[1]);

return nodeId !== undefined && !isNaN(nodeId) ? nodeId : undefined;
}

function getPredecessorNodes(cfg: ControlFlowGraph, vertexId: NodeId, dfg: DataflowGraph): RNode<ParentInformation & AbstractInterpretationInfo>[] {
return cfg.ingoing(vertexId)?.keys()
.map(id => cfg.vertices().get(id))
.flatMap(vertex => {
if(vertex !== undefined && shouldSkipVertex(vertex, dfg)) {
return getPredecessorNodes(cfg, vertex.id, dfg);
} else if(vertex?.type === CfgVertexType.EndMarker) {
const nodeId = getNodeIdForExitVertex(vertex.id);
return nodeId ? [dfg.idMap?.get(nodeId)] : [];
} else {
return vertex ? [dfg.idMap?.get(vertex.id)] : [];
}
})
.filter(node => node !== undefined)
.toArray() ?? [];
}

function getSuccessorVertices(cfg: ControlFlowGraph, vertexId: NodeId, dfg: DataflowGraph): CfgVertex[] {
return cfg.outgoing(vertexId)?.keys()
.map(id => cfg.vertices().get(id))
.flatMap(vertex => {
if(vertex !== undefined && shouldSkipVertex(vertex, dfg)) {
return getSuccessorVertices(cfg, vertex.id, dfg);
} else {
return [vertex];
}
})
.filter(vertex => vertex !== undefined)
.toArray() ?? [];
}
Loading