-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathmap_assignment.ml
More file actions
162 lines (145 loc) · 5.49 KB
/
Copy pathmap_assignment.ml
File metadata and controls
162 lines (145 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
(*
* Copyright 2025 Multikernel Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*)
(** Map Assignment Analysis Module for KernelScript
This module provides analysis for map assignment operations including
optimization detection, assignment extraction, and performance analysis.
*)
open Ast
(** Map assignment record for analysis *)
type map_assignment = {
map_name: string;
key_expr: expr;
value_expr: expr;
assignment_pos: position;
assignment_type: assignment_type;
}
and assignment_type =
| DirectAssignment (* map[key] = value *)
| ConditionalAssignment (* if condition then map[key] = value *)
| ComputedAssignment (* map[key] = map[key] + value *)
(** Optimization record *)
type optimization_record = {
optimization_type: string;
description: string;
estimated_benefit: int; (* 0-100 score *)
}
(** Optimization analysis result *)
type optimization_info = {
optimizations: optimization_record list;
constant_folding: bool;
optimization_type: string;
total_optimizations: int;
}
(** Extract map assignments from AST statements *)
let extract_map_assignments (statements: statement list) : map_assignment list =
let extract_from_stmt stmt =
match stmt.stmt_desc with
| IndexAssignment (map_expr, key_expr, value_expr) ->
let map_name = match map_expr.expr_desc with
| Identifier name -> name
| _ -> "unknown_map"
in
[{
map_name = map_name;
key_expr = key_expr;
value_expr = value_expr;
assignment_pos = stmt.stmt_pos;
assignment_type = DirectAssignment;
}]
| _ -> []
in
List.flatten (List.map extract_from_stmt statements)
(** Extract map assignments from AST declarations *)
let extract_map_assignments_from_ast (ast: declaration list) : map_assignment list =
let rec extract_from_decl decl =
match decl with
| AttributedFunction attr_func ->
extract_from_function attr_func.attr_function
| GlobalFunction func ->
extract_from_function func
| _ -> []
and extract_from_function func =
extract_map_assignments func.func_body
in
List.flatten (List.map extract_from_decl ast)
(** Analyze constant expressions for folding opportunities *)
let is_constant_expression expr =
let rec check_expr e =
match e.expr_desc with
| Literal _ -> true
| BinaryOp (left, _, right) -> check_expr left && check_expr right
| UnaryOp (_, operand) -> check_expr operand
| _ -> false
in
check_expr expr
(** Detect multiple assignments to same map key *)
let detect_multiple_assignments (assignments: map_assignment list) : (string * int) list =
let key_counts = Hashtbl.create 16 in
List.iter (fun assignment ->
let key = Printf.sprintf "%s[%s]" assignment.map_name
(match assignment.key_expr.expr_desc with
| Literal (IntLit (i, _)) -> Ast.IntegerValue.to_string i
| Identifier name -> name
| _ -> "expr")
in
let current = try Hashtbl.find key_counts key with Not_found -> 0 in
Hashtbl.replace key_counts key (current + 1)
) assignments;
Hashtbl.fold (fun key count acc ->
if count > 1 then (key, count) :: acc else acc
) key_counts []
(** Analyze assignment optimizations *)
let analyze_assignment_optimizations (assignments: map_assignment list) : optimization_info =
let optimizations = ref [] in
let has_constant_folding = ref false in
(* Check for multiple assignment elimination *)
let multiple_assigns = detect_multiple_assignments assignments in
if List.length multiple_assigns > 0 then (
optimizations := {
optimization_type = "multiple_assignment_elimination";
description = Printf.sprintf "Found %d keys with multiple assignments" (List.length multiple_assigns);
estimated_benefit = 75;
} :: !optimizations
);
(* Check for constant folding opportunities *)
let constant_exprs = List.filter (fun a -> is_constant_expression a.value_expr) assignments in
if List.length constant_exprs > 0 then (
has_constant_folding := true;
optimizations := {
optimization_type = "constant_folding";
description = Printf.sprintf "Found %d constant expressions that can be folded" (List.length constant_exprs);
estimated_benefit = 60;
} :: !optimizations
);
(* Check for sequential key patterns *)
let sequential_keys = List.filter (fun a ->
match a.key_expr.expr_desc with
| BinaryOp (_, Add, {expr_desc = Literal (IntLit _); _}) -> true
| _ -> false
) assignments in
if List.length sequential_keys > 2 then (
optimizations := {
optimization_type = "sequential_access_optimization";
description = Printf.sprintf "Found %d sequential key accesses" (List.length sequential_keys);
estimated_benefit = 40;
} :: !optimizations
);
{
optimizations = !optimizations;
constant_folding = !has_constant_folding;
optimization_type = if List.length !optimizations > 0 then "multi_optimization" else "none";
total_optimizations = List.length !optimizations;
}