-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcpp_validator.py
More file actions
337 lines (272 loc) · 11.8 KB
/
cpp_validator.py
File metadata and controls
337 lines (272 loc) · 11.8 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
#!/usr/bin/env python3
"""
C++ Validator Module - Validates and refines intermediate C++ code in cpp_chain pipeline
Performs functional validation and HLS compatibility checks for generated C++ code
"""
import re
import time
from pathlib import Path
from typing import Dict, Optional, Tuple, List
from config import Config
class CppValidator:
def __init__(self, llm_interface, max_iterations: int = 2):
"""
Initialize C++ validator
Args:
llm_interface: LLM interface for validation and refinement
max_iterations: Maximum refinement iterations for C++ code
"""
self.llm = llm_interface
self.max_iterations = max_iterations
self.validation_cache = {}
def validate_cpp_structure(self, cpp_code: str) -> Dict:
"""
Check C++ code for HLS compatibility and structural correctness
Args:
cpp_code: C++ code to validate
Returns:
Dictionary with validation results
"""
issues = []
# Check for dynamic memory allocation
if any(keyword in cpp_code for keyword in ['new ', 'delete ', 'malloc', 'free', 'vector<', 'list<', 'map<']):
issues.append({
'type': 'dynamic_memory',
'severity': 'error',
'message': 'Dynamic memory allocation detected - not HLS compatible'
})
# Check for recursive functions
function_pattern = r'(\w+)\s*\([^)]*\)\s*{([^}]*)}'
functions = re.findall(function_pattern, cpp_code, re.DOTALL)
for func_name, func_body in functions:
if func_name in func_body:
issues.append({
'type': 'recursion',
'severity': 'error',
'message': f'Recursive function {func_name} detected - not HLS compatible'
})
# Check for proper bit-width types
if not any(btype in cpp_code for btype in ['uint8_t', 'uint16_t', 'uint32_t', 'int8_t', 'int16_t', 'int32_t', 'bool']):
issues.append({
'type': 'bit_width',
'severity': 'warning',
'message': 'No explicit bit-width types found - may cause synthesis issues'
})
# Check for unbounded loops
while_pattern = r'while\s*\([^)]*\)'
while_loops = re.findall(while_pattern, cpp_code)
for loop in while_loops:
if 'true' in loop.lower() or '1' == loop.strip()[-2]:
issues.append({
'type': 'unbounded_loop',
'severity': 'error',
'message': 'Potentially unbounded while loop detected'
})
return {
'valid': len([i for i in issues if i['severity'] == 'error']) == 0,
'issues': issues,
'warnings': len([i for i in issues if i['severity'] == 'warning']),
'errors': len([i for i in issues if i['severity'] == 'error'])
}
def validate_cpp_functionality(self, cpp_code: str, design_spec: str) -> Dict:
"""
Validate C++ functional correctness against design specification
Args:
cpp_code: C++ code to validate
design_spec: Original design specification
Returns:
Dictionary with functional validation results
"""
prompt = f"""Analyze if this C++ code correctly implements the specified functionality.
Design Specification:
{design_spec}
C++ Code:
{cpp_code}
Check for:
1. Does the C++ code implement all required operations?
2. Are the input/output interfaces correct?
3. Is the algorithmic logic correct?
4. Are there any obvious functional errors?
Provide a structured analysis:
- Correctness: [CORRECT/INCORRECT/PARTIAL]
- Missing features: [list any]
- Logic errors: [list any]
- Interface issues: [list any]
Be concise and specific."""
system_role = "You are an expert in hardware design and HLS C++ programming. Analyze code functionality against specifications precisely."
response = self.llm.generate_response(prompt, system_role)
if response:
# Parse LLM response
correctness = "CORRECT" if "CORRECT" in response and "INCORRECT" not in response else "INCORRECT"
has_errors = "logic error" in response.lower() or "missing" in response.lower()
return {
'functionally_correct': correctness == "CORRECT",
'analysis': response,
'has_errors': has_errors
}
return {
'functionally_correct': False,
'analysis': 'Failed to analyze',
'has_errors': True
}
def should_fix_cpp(self, verilog_errors: List[Dict], cpp_code: str, design_spec: str) -> Dict:
"""
Determine if Verilog errors stem from C++ issues
Args:
verilog_errors: List of Verilog simulation errors
cpp_code: Generated C++ code
design_spec: Original design specification
Returns:
Dictionary with decision and reasoning
"""
# Quick heuristics
error_messages = ' '.join([e.get('message', '') for e in verilog_errors])
# Indicators that C++ might be wrong
cpp_indicators = [
'wrong output',
'incorrect result',
'mismatch in expected',
'logic error',
'wrong calculation',
'incorrect algorithm'
]
# Indicators that it's a Verilog translation issue
verilog_indicators = [
'synthesis',
'timing',
'clock',
'reset',
'sensitivity list',
'always block'
]
cpp_score = sum(1 for ind in cpp_indicators if ind in error_messages.lower())
verilog_score = sum(1 for ind in verilog_indicators if ind in error_messages.lower())
# If strong Verilog indicators, don't check C++
if verilog_score > cpp_score:
return {'fix_cpp': False, 'reason': 'Likely Verilog translation issue'}
# Validate C++ if indicators suggest functional problems
if cpp_score > 0 or verilog_score == 0:
structure_result = self.validate_cpp_structure(cpp_code)
if not structure_result['valid']:
return {'fix_cpp': True, 'reason': 'C++ structural issues detected'}
func_result = self.validate_cpp_functionality(cpp_code, design_spec)
if not func_result['functionally_correct']:
return {'fix_cpp': True, 'reason': 'C++ functional issues detected'}
return {'fix_cpp': False, 'reason': 'C++ appears correct, likely translation issue'}
def refine_cpp_code(self, cpp_code: str, issues: List[Dict], design_spec: str, iteration: int = 1) -> Optional[str]:
"""
Refine C++ code based on identified issues
Args:
cpp_code: Current C++ code
issues: List of issues to fix
design_spec: Original design specification
iteration: Current refinement iteration
Returns:
Refined C++ code or None if refinement fails
"""
# Format issues for prompt
issue_summary = '\n'.join([
f"- {issue['type']}: {issue['message']}"
for issue in issues[:5] # Limit to first 5 issues
])
prompt = f"""Fix the HLS C++ code based on these issues:
Issues to fix:
{issue_summary}
Current C++ Code:
{cpp_code}
Original Design Requirements:
{design_spec}
Requirements for fixed code:
- Must be HLS-compatible (no dynamic memory, no recursion)
- Use fixed-size arrays only
- Use explicit bit-width types (uint8_t, uint16_t, etc.)
- Ensure all loops are bounded
- Maintain functional correctness
Provide the complete corrected C++ code:"""
system_role = "You are an HLS C++ expert. Fix code to be synthesis-compatible while maintaining functionality."
response = self.llm.generate_response(prompt, system_role)
if response:
# Extract C++ code from response
return self.extract_cpp_code(response)
return None
def extract_cpp_code(self, response: str) -> Optional[str]:
"""
Extract C++ code from LLM response
Args:
response: LLM response containing C++ code
Returns:
Extracted C++ code or None
"""
if not response:
return None
# Look for code blocks
lines = response.split('\n')
code_lines = []
in_block = False
for line in lines:
stripped = line.strip()
if stripped.startswith('```'):
if in_block:
break
else:
in_block = True
continue
if in_block:
code_lines.append(line)
# If no code blocks, try to extract function definitions
if not code_lines:
for i, line in enumerate(lines):
stripped = line.strip()
if any(stripped.startswith(kw) for kw in ['#include', 'void ', 'int ', 'uint', 'bool ', 'class ', 'struct ']):
code_lines = lines[i:]
break
return '\n'.join(code_lines) if code_lines else response
def validate_and_refine_cpp(self, cpp_code: str, design_spec: str, verilog_errors: List[Dict] = None) -> Tuple[str, Dict]:
"""
Complete validation and refinement pipeline for C++ code
Args:
cpp_code: C++ code to validate and refine
design_spec: Original design specification
verilog_errors: Optional Verilog errors to consider
Returns:
Tuple of (refined_cpp_code, validation_info)
"""
history = []
current_code = cpp_code
for iteration in range(1, self.max_iterations + 1):
# Structural validation
structure_result = self.validate_cpp_structure(current_code)
# Functional validation
func_result = self.validate_cpp_functionality(current_code, design_spec)
history.append({
'iteration': iteration,
'structure_valid': structure_result['valid'],
'functionally_correct': func_result['functionally_correct'],
'issues': structure_result['issues']
})
# If both validations pass, return
if structure_result['valid'] and func_result['functionally_correct']:
return current_code, {
'success': True,
'iterations': iteration,
'history': history
}
# If not last iteration, try to fix
if iteration < self.max_iterations:
all_issues = structure_result['issues']
if not func_result['functionally_correct']:
all_issues.append({
'type': 'functional',
'severity': 'error',
'message': 'Functional correctness issues detected'
})
refined_code = self.refine_cpp_code(current_code, all_issues, design_spec, iteration)
if refined_code:
current_code = refined_code
else:
break
return current_code, {
'success': False,
'iterations': self.max_iterations,
'history': history
}