-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiagnose.py
More file actions
executable file
·393 lines (336 loc) · 15.7 KB
/
Copy pathdiagnose.py
File metadata and controls
executable file
·393 lines (336 loc) · 15.7 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import argparse
import subprocess
import json
import sys
import os
from collections import defaultdict
def run_yosys(file_paths, top_module):
read_cmds = []
for fp in file_paths:
if fp.endswith('.sv'):
read_cmds.append(f"read_verilog -sv {fp}")
else:
read_cmds.append(f"read_verilog {fp}")
script = "; ".join(read_cmds)
script += f"; prep -top {top_module}; flatten; opt; techmap; opt; write_json"
cmd = ["yosys", "-p", script]
try:
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True)
# Parse JSON output from stdout
json_str = ""
capture = False
for line in result.stdout.splitlines():
if line.strip() == "{":
capture = True
if capture:
json_str += line + "\n"
if not json_str:
sys.stderr.write("Error: Yosys did not output valid JSON.\n")
sys.exit(1)
return json.loads(json_str)
except subprocess.CalledProcessError as e:
sys.stderr.write(f"Yosys failed:\n{e.stderr}\n")
sys.exit(1)
except json.JSONDecodeError as e:
sys.stderr.write(f"Failed to parse Yosys JSON: {e}\n")
sys.exit(1)
def diagnose_rtl(args):
# Load exceptions if provided
exceptions = {"false_paths": [], "cdc_safe": []}
if args.exceptions and os.path.exists(args.exceptions):
try:
with open(args.exceptions, 'r') as f:
exceptions = json.load(f)
except Exception as e:
sys.stderr.write(f"Failed to load exceptions: {e}\n")
if not args.json and not args.sarif:
print(f"[*] Parsing {', '.join(args.files)} AST via headless Yosys...")
yosys_ast = run_yosys(args.files, args.top)
if not args.json and not args.sarif:
print("[*] Extracting Directed Acyclic Graph (DAG)...")
modules = yosys_ast.get("modules", {})
if not modules:
sys.stderr.write("No modules found in AST.\n")
sys.exit(1)
top_mod = modules.get(args.top) or list(modules.values())[0]
ports = top_mod.get("ports", {})
cells = top_mod.get("cells", {})
netnames = top_mod.get("netnames", {})
bit_to_netname = {}
for net, data in netnames.items():
for bit in data.get("bits", []):
if isinstance(bit, int):
bit_to_netname[bit] = net
nodes = set()
sources = set()
sinks = set()
comb_cells = set()
bit_driver = {}
node_src = {}
node_clk = {}
# Identify ports as sources/sinks
for port_name, port_data in ports.items():
direction = port_data["direction"]
node_name = f"port:{port_name}"
nodes.add(node_name)
node_src[node_name] = top_mod.get("attributes", {}).get("src", "unknown")
if direction in ["input", "inout"]:
sources.add(node_name)
for bit in port_data["bits"]:
if isinstance(bit, int):
bit_driver[bit] = node_name
if direction in ["output", "inout"]:
sinks.add(node_name)
# Identify cells and their drivers
for cell_name, cell_data in cells.items():
cell_type = cell_data["type"].lower()
is_seq = "dff" in cell_type or "latch" in cell_type or "mem" in cell_type
src_attr = cell_data.get("attributes", {}).get("src", "unknown")
if is_seq:
clk_bit = None
clk_port_name = None
connections = cell_data.get("connections", {})
for clk_port in ["CLK", "C", "RD_CLK", "WR_CLK", "EN", "ARST"]:
if clk_port in connections:
bits = connections[clk_port]
if bits and isinstance(bits[0], int):
clk_bit = bits[0]
clk_port_name = clk_port
break
clk_net = bit_to_netname.get(clk_bit, f"bit_{clk_bit}") if clk_bit is not None else None
for port, bits in connections.items():
dir = cell_data["port_directions"].get(port, "input")
if dir == "output":
node_name = f"cell:{cell_name}:{port}"
nodes.add(node_name)
sources.add(node_name)
node_src[node_name] = src_attr
node_clk[node_name] = clk_net
for bit in bits:
if isinstance(bit, int):
bit_driver[bit] = node_name
else:
node_name = f"cell:{cell_name}"
nodes.add(node_name)
comb_cells.add(node_name)
node_src[node_name] = src_attr
for port, bits in cell_data.get("connections", {}).items():
dir = cell_data["port_directions"].get(port, "input")
if dir == "output":
for bit in bits:
if isinstance(bit, int):
bit_driver[bit] = node_name
# Build adjacency list
adj = defaultdict(list)
bit_sinks = defaultdict(list)
for port_name, port_data in ports.items():
direction = port_data["direction"]
if direction in ["output", "inout"]:
node_name = f"port:{port_name}"
for bit in port_data["bits"]:
if isinstance(bit, int) and bit in bit_driver:
adj[bit_driver[bit]].append(node_name)
bit_sinks[bit].append(node_name)
for cell_name, cell_data in cells.items():
cell_type = cell_data["type"].lower()
is_seq = "dff" in cell_type or "latch" in cell_type or "mem" in cell_type
if is_seq:
clk_bit = None
clk_port_name = None
connections = cell_data.get("connections", {})
for clk_port in ["CLK", "C", "RD_CLK", "WR_CLK", "EN", "ARST"]:
if clk_port in connections:
bits = connections[clk_port]
if bits and isinstance(bits[0], int):
clk_bit = bits[0]
clk_port_name = clk_port
break
clk_net = bit_to_netname.get(clk_bit, f"bit_{clk_bit}") if clk_bit is not None else None
for port, bits in cell_data.get("connections", {}).items():
dir = cell_data["port_directions"].get(port, "input")
if dir == "input" and port != clk_port_name:
node_name = f"cell:{cell_name}:{port}"
nodes.add(node_name)
sinks.add(node_name)
node_src[node_name] = cell_data.get("attributes", {}).get("src", "unknown")
node_clk[node_name] = clk_net
for bit in bits:
if isinstance(bit, int) and bit in bit_driver:
adj[bit_driver[bit]].append(node_name)
bit_sinks[bit].append(node_name)
else:
node_name = f"cell:{cell_name}"
for port, bits in cell_data.get("connections", {}).items():
dir = cell_data["port_directions"].get(port, "input")
if dir == "input":
for bit in bits:
if isinstance(bit, int) and bit in bit_driver:
adj[bit_driver[bit]].append(node_name)
bit_sinks[bit].append(node_name)
if not args.json and not args.sarif:
print("[*] Executing Topological DFS Memoization...\n")
memo = {}
comb_loops = set()
visiting = set()
def get_weight(u):
return 1 if u in comb_cells else 0
def dfs(u):
if u in visiting:
comb_loops.add(u)
return (-1, []) # cycle
if u in memo:
return memo[u]
visiting.add(u)
max_child_depth = -1
best_child_path = []
for v in adj[u]:
depth, path = dfs(v)
if depth > max_child_depth:
max_child_depth = depth
best_child_path = path
visiting.remove(u)
if max_child_depth >= 0:
memo[u] = (get_weight(u) + max_child_depth, [u] + best_child_path)
else:
memo[u] = (get_weight(u), [u])
return memo[u]
global_max = -1
global_path = []
all_paths = []
for src in sources:
depth, path = dfs(src)
if depth >= 0:
all_paths.append((depth, path))
if depth > global_max:
global_max = depth
global_path = path
exit_code = 0
results = {
"loops": [],
"fanout": [],
"setup_violations": [],
"cdc_violations": []
}
if comb_loops:
exit_code = 1
for loop_node in list(comb_loops):
results["loops"].append({"node": loop_node, "src": node_src.get(loop_node, 'unknown')})
# Check for High Fanout
for bit, children in bit_sinks.items():
fanout = len(set(children))
if fanout > args.fanout_threshold:
u = bit_driver.get(bit, "unknown")
net_name = bit_to_netname.get(bit, f"bit_{bit}")
results["fanout"].append({
"net": net_name,
"driver": u,
"src": node_src.get(u, 'unknown'),
"count": fanout
})
exit_code = 1
# Check Paths for Setup and CDC
for depth, path in sorted(all_paths, reverse=True):
if len(path) < 2:
continue
source_node = path[0]
sink_node = path[-1]
source_clk = node_clk.get(source_node)
sink_clk = node_clk.get(sink_node)
# Check exceptions
src_file_line = node_src.get(source_node, "unknown")
sink_file_line = node_src.get(sink_node, "unknown")
is_false_path = any(fp.get("from") in src_file_line and fp.get("to") in sink_file_line for fp in exceptions.get("false_paths", []))
is_cdc_safe = any(cp.get("from") == source_clk and cp.get("to") == sink_clk for cp in exceptions.get("cdc_safe", []))
if not is_false_path and depth > args.depth_threshold:
if not results["setup_violations"]: # Just capture the worst for now
results["setup_violations"].append({
"source": source_node, "source_src": src_file_line, "source_clk": source_clk,
"sink": sink_node, "sink_src": sink_file_line, "sink_clk": sink_clk,
"depth": depth,
"trace": [ {"node": n, "src": node_src.get(n, "unknown")} for n in path ] if args.explain else []
})
exit_code = 1
if source_clk and sink_clk and source_clk != sink_clk and not is_cdc_safe:
# Only record unique CDC crossings
existing = [c for c in results["cdc_violations"] if c["source_clk"] == source_clk and c["sink_clk"] == sink_clk]
if not existing:
results["cdc_violations"].append({
"source": source_node, "source_src": src_file_line, "source_clk": source_clk,
"sink": sink_node, "sink_src": sink_file_line, "sink_clk": sink_clk
})
exit_code = 1
# Output generation
if args.json:
print(json.dumps(results, indent=2))
elif args.sarif:
# Minimal SARIF generation for GitHub Code Scanning
sarif = {
"$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
"version": "2.1.0",
"runs": [{
"tool": {
"driver": {
"name": "ConstraintForge",
"informationUri": "https://devtyagi3909.github.io/constraintforge",
"rules": [
{"id": "CF001", "name": "LogicDepth", "shortDescription": {"text": "Logic depth exceeds threshold"}},
{"id": "CF002", "name": "CDC", "shortDescription": {"text": "Unsynchronized Clock Domain Crossing"}},
{"id": "CF003", "name": "HighFanout", "shortDescription": {"text": "Net fanout exceeds threshold"}}
]
}
},
"results": []
}]
}
for sv in results["setup_violations"]:
sarif["runs"][0]["results"].append({
"ruleId": "CF001",
"message": {"text": f"Logic depth of {sv['depth']} exceeds threshold of {args.depth_threshold}."},
"locations": [{"physicalLocation": {"artifactLocation": {"uri": sv['source_src'].split(':')[0]}}}]
})
print(json.dumps(sarif, indent=2))
else:
print(f"## DIAGNOSTIC REPORT: {', '.join(args.files)} ##\n")
if results["loops"]:
print("[WARNING] Combinational loop(s) detected during DFS!")
for l in results["loops"][:5]:
print(f" Loop at: {l['node']} ({l['src']})")
print()
if results["fanout"]:
print(f"[WARNING] High-fanout nets (>{args.fanout_threshold}) detected:")
for f in results["fanout"][:5]:
print(f" Net: {f['net']} (Driver: {f['driver']} [{f['src']}]) -> Fanout: {f['count']}")
print()
if results["setup_violations"]:
v = results["setup_violations"][0]
print(f"[WARNING] Setup-time risk detected (> {args.depth_threshold} gates):")
print(f" Source: {v['source']} ({v['source_src']}) [Clk: {v['source_clk']}]")
print(f" Sink: {v['sink']} ({v['sink_src']}) [Clk: {v['sink_clk']}]")
print(f" Logic Depth: {v['depth']} gates")
if args.explain and v['trace']:
print(" Path Trace:")
for step in v['trace']:
print(f" -> {step['node']} ({step['src']})")
print()
if results["cdc_violations"]:
for c in results["cdc_violations"]:
print(f"[!] CDC ALERT: Unsynchronized crossing from {c['source_clk']} to {c['sink_clk']}!")
print(f" Path: {c['source_src']} -> {c['sink_src']}")
print()
if exit_code == 0:
print("[OK] No structural violations detected.\n")
sys.exit(exit_code)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="ConstraintForge Pre-Synthesis Structural Diagnostic Engine")
parser.add_argument("files", nargs='+', help="Path to RTL file(s)")
parser.add_argument("--top", required=True, help="Top module name")
# New CLI features
parser.add_argument("--depth-threshold", type=int, default=30, help="Maximum allowable logic depth (gates)")
parser.add_argument("--fanout-threshold", type=int, default=100, help="Maximum allowable fanout per net")
parser.add_argument("--exceptions", type=str, help="JSON file containing false_paths and cdc_safe exceptions")
parser.add_argument("--explain", action="store_true", help="Print the full node-by-node path trace for violations")
group = parser.add_mutually_exclusive_group()
group.add_argument("--json", action="store_true", help="Output results in JSON format")
group.add_argument("--sarif", action="store_true", help="Output results in SARIF format for GitHub Code Scanning")
args = parser.parse_args()
diagnose_rtl(args)