forked from rez5427/llvm-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
883 lines (754 loc) · 33.5 KB
/
Copy pathutils.py
File metadata and controls
883 lines (754 loc) · 33.5 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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
import json
import re
import subprocess
import shutil
from pathlib import Path
def load_config():
"""Load configuration from JSON file"""
config_file = Path(__file__).parent / "config.json"
try:
with open(config_file, 'r') as f:
config = json.load(f)
print(f"Successfully loaded configuration file: {config_file}")
except FileNotFoundError:
print(f"Error: Configuration file {config_file} does not exist")
raise
except json.JSONDecodeError as e:
print(f"Error: Invalid configuration file format: {e}")
raise
# Set paths
LLVM_BIN = Path(config["paths"]["llvm_bin"])
GCC_BIN = Path(config["paths"]["gcc_bin"])
CLANG_BIN = LLVM_BIN / "clang"
EXTRACT = LLVM_BIN / "llvm-extract"
CBE = LLVM_BIN / "llvm-cbe"
OPT = LLVM_BIN / "opt"
return {
"LLVM_BIN": LLVM_BIN,
"GCC_BIN": GCC_BIN,
"CLANG_BIN": CLANG_BIN,
"EXTRACT": EXTRACT,
"CBE": CBE,
"OPT": OPT,
"compile_flags": config["compile_flags"],
"clang_opt_flags": config.get("clang_opt_flags", []),
"timeout": config["timeout"],
"input_paths": config.get("input_paths", []),
"output_dir": config.get("output_dir", "comparison_results")
}
def strip_asm(asm_path):
"""Count assembly instruction lines (filter out irrelevant content)"""
try:
count = 0
with open(asm_path, 'r') as f:
for line in f:
# Remove comments first (both // and # style)
if '//' in line:
line = line[:line.index('//')]
if '#' in line:
line = line[:line.index('#')]
stripped = line.strip()
if not stripped: continue # Skip empty lines
if stripped.startswith('.'): continue # Skip pseudo-instructions
if stripped.startswith('#'): continue # Skip comments
if stripped.endswith(':'): continue # Skip labels
count += 1
return count
except:
return 0
def collect_ll_files(input_path):
"""Collect .ll files to process"""
ll_files = []
if input_path.is_file() and input_path.suffix == ".ll":
ll_files.append(input_path)
elif input_path.is_dir():
ll_files = list(input_path.rglob("*.ll"))
else:
print(f"Warning: Skipping invalid path {input_path}")
return []
if not ll_files:
print(f"No .ll files found in {input_path}")
return []
return ll_files
def extract_function_names(ll_path, config):
"""Extract function names from .ll file using LLVM toolchain for accuracy"""
try:
# Create temporary working directory
temp_dir = Path("temp_function_discovery")
temp_dir.mkdir(exist_ok=True)
# 1. Compile .ll file to .bc file
bc_file = temp_dir / f"{ll_path.stem}.bc"
as_cmd = [str(config["LLVM_BIN"] / "llvm-as"), str(ll_path), "-o", str(bc_file)]
subprocess.run(as_cmd, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
# 2. Use llvm-nm to get all symbols
nm_cmd = [str(config["LLVM_BIN"] / "llvm-nm"), str(bc_file)]
result = subprocess.run(nm_cmd, capture_output=True, text=True, check=True)
# 3. Parse llvm-nm output and extract function symbols
func_names = []
for line in result.stdout.strip().split('\n'):
if not line.strip():
continue
parts = line.strip().split()
if len(parts) >= 3:
# llvm-nm output format: address type symbol_name
symbol_type = parts[1]
symbol_name = parts[2]
# T indicates defined function symbols in text section, exclude U (undefined) and other types
if symbol_type in ['T', 't'] and not symbol_name.startswith('llvm.'):
# Remove platform-specific symbol prefixes (like underscore prefix on macOS)
clean_name = symbol_name.lstrip('_')
if clean_name: # Ensure there's still content after removing prefix
func_names.append(clean_name)
# Clean up temporary files
if bc_file.exists():
bc_file.unlink()
temp_dir.rmdir()
if not func_names:
print(f"No function definitions found in {ll_path}")
return []
return func_names
except subprocess.CalledProcessError as e:
print(f"LLVM tool error when processing file {ll_path}: {e}")
# Clean up potentially remaining files
if 'bc_file' in locals() and bc_file.exists():
bc_file.unlink()
if 'temp_dir' in locals() and temp_dir.exists():
temp_dir.rmdir()
return []
except Exception as e:
print(f"Failed to process file {ll_path}: {e}")
return []
def run_optimization_pipeline(output_dir, func_name, config):
"""Run clang optimization pipeline and record results of each optimization step"""
# Create subdirectories for logs
logs_dir = output_dir / "logs"
if not logs_dir.exists():
logs_dir.mkdir(exist_ok=True)
opt_log_file = logs_dir / f"{func_name}_clang_opt_steps.log"
c_file = output_dir / f"{func_name}.c"
try:
clang_cmd = [
str(config["LLVM_BIN"] / "clang"),
*config["clang_opt_flags"],
str(c_file),
"-o", "/dev/null"
]
with open(opt_log_file, 'w') as log_file:
result = subprocess.run(clang_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True)
# Write both stdout and stderr to log file
log_file.write(result.stdout)
log_file.write(result.stderr)
return opt_log_file
except subprocess.CalledProcessError as e:
print(f"Failed to record optimization steps: {e}")
if opt_log_file.exists():
opt_log_file.unlink()
return None
except Exception as e:
print(f"Unexpected error occurred during optimization step recording: {e}")
if opt_log_file.exists():
opt_log_file.unlink()
return None
def run_gcc_optimization_pipeline(output_dir, func_name, config):
"""Run GCC optimization pipeline and record results"""
# Create subdirectories for logs
logs_dir = output_dir / "logs"
logs_dir.mkdir(exist_ok=True)
# Create GCC dumps subdirectory
gcc_dumps_dir = logs_dir / "gcc_dumps"
if gcc_dumps_dir.exists():
shutil.rmtree(gcc_dumps_dir)
gcc_dumps_dir.mkdir(exist_ok=True)
opt_log_file = logs_dir / f"{func_name}_gcc_opt_steps.log"
c_file = output_dir / f"{func_name}.c"
try:
# Run GCC with dumps in the gcc_dumps directory
gcc_cmd = [
str(config["GCC_BIN"]),
*config["compile_flags"]["gcc"],
"-fdump-tree-all",
"-fdump-rtl-all",
"-dumpdir", str(gcc_dumps_dir) + "/",
str(c_file),
"-o", "/dev/null"
]
result = subprocess.run(gcc_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True)
# Collect all dump files
dump_files = list(gcc_dumps_dir.glob("*"))
with open(opt_log_file, 'w') as log_file:
log_file.write("=== GCC Optimization Pipeline Log ===\n")
log_file.write(f"Command: {' '.join(gcc_cmd)}\n")
log_file.write(f"Working Directory: {gcc_dumps_dir}\n")
log_file.write(f"Generated {len(dump_files)} dump files\n\n")
log_file.write("=== STDOUT ===\n")
log_file.write(result.stdout if result.stdout else "(empty)\n")
log_file.write("\n=== STDERR ===\n")
log_file.write(result.stderr if result.stderr else "(empty)\n")
log_file.write(f"\n=== Generated Dump Files ({len(dump_files)}) ===\n")
for dump_file in sorted(dump_files):
log_file.write(f"- {dump_file.name}\n")
return opt_log_file
except subprocess.CalledProcessError as e:
print(f"Failed to record GCC optimization steps: {e}")
if opt_log_file.exists():
opt_log_file.unlink()
return None
except Exception as e:
print(f"Unexpected error occurred during GCC optimization step recording: {e}")
if opt_log_file.exists():
opt_log_file.unlink()
return None
def run_llc_backend_pipeline(output_dir, func_name, config):
"""Run LLC backend pipeline and record debug output"""
# Create subdirectories for logs
logs_dir = output_dir / "logs"
logs_dir.mkdir(exist_ok=True)
llc_log_file = logs_dir / f"{func_name}_llc_backend.log"
bc_file = output_dir / f"{func_name}_final.bc"
# Check if bc file exists
if not bc_file.exists():
print(f"Warning: BC file not found: {bc_file}")
return None
try:
# Run llc with debug flags
llc_cmd = [
str(config["LLVM_BIN"] / "llc"),
str(bc_file),
*config["compile_flags"]["llc"],
"-debug",
"-print-after-all",
"-o", "/dev/null"
]
result = subprocess.run(llc_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
with open(llc_log_file, 'w') as log_file:
log_file.write("=== LLC Backend Pipeline Log ===\n")
log_file.write(f"Command: {' '.join(llc_cmd)}\n")
log_file.write(f"Input File: {bc_file}\n")
log_file.write(f"Return Code: {result.returncode}\n\n")
log_file.write("=== STDOUT ===\n")
log_file.write(result.stdout if result.stdout else "(empty)\n")
log_file.write("\n=== STDERR (Debug Output) ===\n")
log_file.write(result.stderr if result.stderr else "(empty)\n")
return llc_log_file
except subprocess.CalledProcessError as e:
print(f"Failed to record LLC backend steps: {e}")
if llc_log_file.exists():
llc_log_file.unlink()
return None
except Exception as e:
print(f"Unexpected error occurred during LLC backend recording: {e}")
if llc_log_file.exists():
llc_log_file.unlink()
return None
def extract_clean_function_ll(ll_path, func, output_dir, config, enable_timeout=False):
"""
Extract a single function from original LL file to clean LL file
Args:
ll_path: Original LLVM IR file path
func: Function name
output_dir: Output directory
config: Configuration dictionary
enable_timeout: Whether to enable timeout mechanism
Returns:
Path to extracted clean LL file, returns None on failure
"""
# Clean special characters in function name
clean_func = re.sub(r'[^\w]', '_', func)
# Define file paths
bc_file = output_dir / f"{clean_func}.bc"
clean_ll_file = output_dir / f"{clean_func}.ll"
timeout = config.get("timeout") if enable_timeout else None
try:
# 1. Extract function to bc file
try:
extract_cmd = [str(config["EXTRACT"]), "-func="+func, str(ll_path), "-o", str(bc_file)]
subprocess.run(extract_cmd, check=True, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL, timeout=timeout)
except subprocess.TimeoutExpired:
raise Exception("llvm-extract")
except subprocess.CalledProcessError:
raise Exception("llvm-extract")
# 2. Convert bc file to clean ll file
try:
dis_cmd = [str(config["LLVM_BIN"] / "llvm-dis"), str(bc_file), "-o", str(clean_ll_file)]
subprocess.run(dis_cmd, check=True, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL, timeout=timeout)
except subprocess.TimeoutExpired:
raise Exception("llvm-dis")
except subprocess.CalledProcessError:
raise Exception("llvm-dis")
# 3. Clean up bc file
if bc_file.exists():
bc_file.unlink()
return clean_ll_file
except Exception as e:
# Clean up potentially remaining files
for f in [bc_file, clean_ll_file]:
if f.exists():
f.unlink()
# Return error information instead of None
return {"error": str(e)}
def process_clean_function_ll(clean_ll_path, func, output_dir, config,
keep_files=False, enable_timeout=False,
enable_opt_logging=False):
"""
Process already extracted clean single-function LL file
"""
# Clean special characters in function name
clean_func = re.sub(r'[^\w]', '_', func)
# Define file paths
bc_file = output_dir / f"{clean_func}.bc"
c_file = output_dir / f"{clean_func}.c"
s_gcc = output_dir / f"{clean_func}_gcc.s"
s_clang = output_dir / f"{clean_func}_clang.s"
timeout = config.get("timeout") if enable_timeout else None
try:
# 1. Compile clean ll file to bc file
try:
as_cmd = [str(config["LLVM_BIN"] / "llvm-as"), str(clean_ll_path), "-o", str(bc_file)]
subprocess.run(as_cmd, check=True, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL, timeout=timeout)
except subprocess.TimeoutExpired:
raise Exception("llvm-as")
except subprocess.CalledProcessError:
raise Exception("llvm-as")
# 2. Run optimization pipeline and record (if needed)
opt_log_file = None
if enable_opt_logging:
opt_log_file = run_optimization_pipeline(output_dir, clean_func, config)
# 3. Generate C code
try:
cbe_cmd = [str(config["CBE"]), str(bc_file), "-o", str(c_file)]
subprocess.run(cbe_cmd, check=True, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL, timeout=timeout)
except subprocess.TimeoutExpired:
raise Exception("CBE")
except subprocess.CalledProcessError:
raise Exception("CBE")
# 4. GCC compilation
try:
gcc_cmd = [str(config["GCC_BIN"]), *config["compile_flags"]["gcc"], str(c_file), "-o", str(s_gcc)]
subprocess.run(gcc_cmd, check=True, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL, timeout=timeout)
except subprocess.TimeoutExpired:
raise Exception("GCC")
except subprocess.CalledProcessError:
raise Exception("GCC")
# 5. Clang compilation
try:
clang_cmd = [str(config["CLANG_BIN"]), *config["compile_flags"]["clang"], str(c_file), "-o", str(s_clang)]
subprocess.run(clang_cmd, check=True, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL, timeout=timeout)
except subprocess.TimeoutExpired:
raise Exception("Clang")
except subprocess.CalledProcessError:
raise Exception("Clang")
# 6. Count instruction lines
gcc_count = strip_asm(s_gcc)
clang_count = strip_asm(s_clang)
# 7. Calculate difference percentage
if gcc_count > 0:
diff_percent = ((clang_count - gcc_count) / gcc_count) * 100
else:
diff_percent = 0 if clang_count == 0 else float('inf')
# 8. File cleanup strategy
if not keep_files:
# compare mode: keep only assembly files, delete intermediate files
for f in [bc_file, c_file]:
if f.exists():
f.unlink()
# Delete clean ll file (temporary file)
if clean_ll_path.exists():
clean_ll_path.unlink()
else:
# extract mode: delete bc files but keep others
if bc_file.exists():
bc_file.unlink()
return {
"func": func,
"gcc_lines": gcc_count,
"clang_lines": clang_count,
"diff_percent": diff_percent,
"opt_log_created": opt_log_file is not None if enable_opt_logging else False,
"status": ""
}
except Exception as e:
# Get specific error information
error_msg = str(e)
if enable_timeout:
# compare mode: handle errors silently
pass
else:
# extract mode: print error information
print(f"Error processing function {func}: {error_msg}")
# Clean up files
cleanup_files = [bc_file, c_file, s_gcc, s_clang, clean_ll_path]
for f in cleanup_files:
if f.exists():
f.unlink()
if enable_opt_logging:
opt_log = output_dir / f"{clean_func}_opt_steps.log"
if opt_log.exists():
opt_log.unlink()
return {
"func": func,
"gcc_lines": 0,
"clang_lines": 0,
"diff_percent": 0,
"opt_log_created": False,
"status": error_msg
}
def process_function(ll_path, func, output_dir, config,
keep_files=False, enable_timeout=False,
enable_opt_logging=False, copy_ll=False):
"""
Generic function processing function using new two-stage workflow
Args:
ll_path: LLVM IR file path
func: Function name
output_dir: Output directory
config: Configuration dictionary
keep_files: Whether to keep intermediate files (default False, for compare mode)
enable_timeout: Whether to enable timeout mechanism (default False)
enable_opt_logging: Whether to enable optimization step recording (default False)
copy_ll: Whether to copy original LL file (default False, now handled automatically by two-stage workflow)
Returns:
Processing result dictionary or None (on failure)
"""
# Stage 1: Extract clean single-function LL file
clean_ll_result = extract_clean_function_ll(ll_path, func, output_dir, config, enable_timeout)
if isinstance(clean_ll_result, dict):
# Stage 1 failed, return error information
return {
"func": func,
"gcc_lines": 0,
"clang_lines": 0,
"diff_percent": 0,
"opt_log_created": False,
"status": clean_ll_result["error"]
}
clean_ll_path = clean_ll_result
# Stage 2: Process clean LL file
result = process_clean_function_ll(clean_ll_path, func, output_dir, config,
keep_files, enable_timeout, enable_opt_logging)
# If need to keep LL file (extract mode), don't delete
if keep_files and copy_ll:
# Rename to final filename
clean_func = re.sub(r'[^\w]', '_', func)
final_ll_path = output_dir / f"{clean_func}.ll"
if clean_ll_path != final_ll_path and clean_ll_path.exists():
clean_ll_path.rename(final_ll_path)
return result
def compile_ll_directly(ll_path, output_dir, config, enable_timeout=False):
"""
Directly compile .ll file to assembly without going through C
Used to test if the issue is in frontend/middle-end or backend
Args:
ll_path: LLVM IR file path
output_dir: Output directory
config: Configuration dictionary
enable_timeout: Whether to enable timeout
Returns:
Tuple of (clang_asm_path, instruction_count) or (None, 0) on failure
"""
timeout = config.get("timeout") if enable_timeout else None
# Output assembly file path
s_clang_ll = output_dir / f"{ll_path.stem}_from_ll_clang.s"
try:
# Directly compile .ll to assembly using clang
clang_cmd = [
str(config["CLANG_BIN"]),
*config["compile_flags"]["clang"],
str(ll_path),
"-o", str(s_clang_ll)
]
subprocess.run(clang_cmd, check=True, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL, timeout=timeout)
# Count instructions
count = strip_asm(s_clang_ll)
return s_clang_ll, count
except (subprocess.TimeoutExpired, subprocess.CalledProcessError):
# Clean up on failure
if s_clang_ll.exists():
s_clang_ll.unlink()
return None, 0
except Exception:
if s_clang_ll.exists():
s_clang_ll.unlink()
return None, 0
def process_function_with_detailed_logging(ll_path, func, output_dir, config):
"""
Function processing with detailed logging, used by extract.py
Save intermediate files and detailed error information for each step
"""
# Clean special characters in function name
clean_func = re.sub(r'[^\w]', '_', func)
# Clean up function output directory if it exists
if output_dir.exists():
shutil.rmtree(output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
# Create log file
log_file = output_dir / f"{clean_func}_processing.log"
def log_message(message, also_print=True, add_newline=False):
if add_newline:
log_entry = f"\n*** {message} ***"
else:
log_entry = f"*** {message} ***"
with open(log_file, 'a', encoding='utf-8') as f:
f.write(log_entry + '\n')
if also_print:
print(f" {message.replace('*** ', '').replace(' ***', '')}")
# Start processing log
log_message("Processing Function Start")
log_message(f"Function Name: {func}")
log_message(f"Clean Name: {clean_func}")
log_message(f"Source File: {ll_path}")
log_message(f"Output Directory: {output_dir}")
# Define all file paths
bc_temp = output_dir / f"{clean_func}_temp.bc"
clean_ll = output_dir / f"{clean_func}.ll"
bc_final = output_dir / f"{clean_func}_final.bc"
c_file = output_dir / f"{clean_func}.c"
s_gcc = output_dir / f"{clean_func}_gcc.s"
s_clang = output_dir / f"{clean_func}_clang.s"
try:
# Step 1: llvm-extract Function Extraction
log_message("Step 1: llvm-extract Function Extraction", add_newline=True)
extract_cmd = [str(config["EXTRACT"]), "-func="+func, str(ll_path), "-o", str(bc_temp)]
log_message(f"Command: {' '.join(extract_cmd)}")
result = subprocess.run(extract_cmd, capture_output=True, text=True, timeout=config.get("timeout"))
if result.returncode != 0:
log_message(f"llvm-extract Failed (exit code: {result.returncode})")
if result.stderr.strip():
log_message(f"stderr: {result.stderr}")
if result.stdout.strip():
log_message(f"stdout: {result.stdout}")
return {
"func": func,
"gcc_lines": 0,
"clang_lines": 0,
"diff_percent": 0,
"opt_log_created": False,
"status": "llvm-extract"
}
log_message("llvm-extract Success")
log_message(f"Generated File: {bc_temp} (size: {bc_temp.stat().st_size} bytes)")
# Step 2: llvm-dis Generate Readable LL
log_message("Step 2: llvm-dis Generate Readable LL", add_newline=True)
dis_cmd = [str(config["LLVM_BIN"] / "llvm-dis"), str(bc_temp), "-o", str(clean_ll)]
log_message(f"Command: {' '.join(dis_cmd)}")
result = subprocess.run(dis_cmd, capture_output=True, text=True, timeout=config.get("timeout"))
if result.returncode != 0:
log_message(f"llvm-dis Failed (exit code: {result.returncode})")
if result.stderr.strip():
log_message(f"stderr: {result.stderr}")
if result.stdout.strip():
log_message(f"stdout: {result.stdout}")
return {
"func": func,
"gcc_lines": 0,
"clang_lines": 0,
"diff_percent": 0,
"opt_log_created": False,
"status": "llvm-dis"
}
log_message("llvm-dis Success")
log_message(f"Generated File: {clean_ll} (size: {clean_ll.stat().st_size} bytes)")
# Step 3: llvm-as Recompile BC File
log_message("Step 3: llvm-as Recompile BC File", add_newline=True)
as_cmd = [str(config["LLVM_BIN"] / "llvm-as"), str(clean_ll), "-o", str(bc_final)]
log_message(f"Command: {' '.join(as_cmd)}")
result = subprocess.run(as_cmd, capture_output=True, text=True, timeout=config.get("timeout"))
if result.returncode != 0:
log_message(f"llvm-as Failed (exit code: {result.returncode})")
if result.stderr.strip():
log_message(f"stderr: {result.stderr}")
if result.stdout.strip():
log_message(f"stdout: {result.stdout}")
return {
"func": func,
"gcc_lines": 0,
"clang_lines": 0,
"diff_percent": 0,
"opt_log_created": False,
"status": "llvm-as"
}
log_message("llvm-as Success")
log_message(f"Generated File: {bc_final} (size: {bc_final.stat().st_size} bytes)")
# Step 4: CBE C Code Generation
log_message("Step 4: CBE C Code Generation", add_newline=True)
cbe_cmd = [str(config["CBE"]), str(bc_final), "-o", str(c_file)]
log_message(f"Command: {' '.join(cbe_cmd)}")
result = subprocess.run(cbe_cmd, capture_output=True, text=True, timeout=config.get("timeout"))
if result.returncode != 0:
log_message(f"CBE Failed (exit code: {result.returncode})")
log_message(f"stderr: {result.stderr}")
log_message(f"stdout: {result.stdout}")
return {
"func": func,
"gcc_lines": 0,
"clang_lines": 0,
"diff_percent": 0,
"opt_log_created": False,
"gcc_opt_log_created": False,
"status": "cbe timeout"
}
log_message("CBE Success")
log_message(f"Generated File: {c_file} (size: {c_file.stat().st_size} bytes)")
# Generate clang optimization log
log_message("Step 4.5: Clang Optimization Log Generation", add_newline=True)
opt_log_created = False
try:
opt_result = run_optimization_pipeline(output_dir, clean_func, config)
if opt_result and opt_result.exists():
opt_log_created = True
log_message("Clang Optimization Log Success")
log_message(f"Generated File: {opt_result} (size: {opt_result.stat().st_size} bytes)")
else:
log_message("Clang Optimization Log Failed - No file created")
except Exception as e:
log_message(f"Clang Optimization Log Failed: {e}")
pass
# Generate GCC optimization log
log_message("Step 4.6: GCC Optimization Log Generation", add_newline=True)
gcc_opt_log_created = False
try:
gcc_opt_result = run_gcc_optimization_pipeline(output_dir, clean_func, config)
if gcc_opt_result and gcc_opt_result.exists():
gcc_opt_log_created = True
log_message("GCC Optimization Log Success")
log_message(f"Generated File: {gcc_opt_result} (size: {gcc_opt_result.stat().st_size} bytes)")
else:
log_message("GCC Optimization Log Failed - No file created")
except Exception as e:
log_message(f"GCC Optimization Log Failed: {e}")
pass
# Generate LLC backend log
log_message("Step 4.7: LLC Backend Pipeline Log Generation", add_newline=True)
llc_log_created = False
try:
llc_result = run_llc_backend_pipeline(output_dir, clean_func, config)
if llc_result and llc_result.exists():
llc_log_created = True
log_message("LLC Backend Log Success")
log_message(f"Generated File: {llc_result} (size: {llc_result.stat().st_size} bytes)")
else:
log_message("LLC Backend Log Failed - No file created")
except Exception as e:
log_message(f"LLC Backend Log Failed: {e}")
pass
# Step 5: GCC Compile Assembly
log_message("Step 5: GCC Compile Assembly", add_newline=True)
gcc_cmd = [str(config["GCC_BIN"]), *config["compile_flags"]["gcc"], str(c_file), "-o", str(s_gcc)]
log_message(f"Command: {' '.join(gcc_cmd)}")
result = subprocess.run(gcc_cmd, capture_output=True, text=True, timeout=config.get("timeout"))
if result.returncode != 0:
log_message(f"GCC Failed (exit code: {result.returncode})")
if result.stderr.strip():
log_message(f"stderr: {result.stderr}")
if result.stdout.strip():
log_message(f"stdout: {result.stdout}")
return {
"func": func,
"gcc_lines": 0,
"clang_lines": 0,
"diff_percent": 0,
"opt_log_created": False,
"gcc_opt_log_created": False,
"status": "GCC"
}
log_message("GCC Success")
log_message(f"Generated File: {s_gcc} (size: {s_gcc.stat().st_size} bytes)")
# Step 6: Clang Compile Assembly (from C)
log_message("Step 6: Clang Compile Assembly (from C)", add_newline=True)
clang_cmd = [str(config["CLANG_BIN"]), *config["compile_flags"]["clang"], str(c_file), "-o", str(s_clang)]
log_message(f"Command: {' '.join(clang_cmd)}")
result = subprocess.run(clang_cmd, capture_output=True, text=True, timeout=config.get("timeout"))
if result.returncode != 0:
log_message(f"Clang Failed (exit code: {result.returncode})")
if result.stderr.strip():
log_message(f"stderr: {result.stderr}")
if result.stdout.strip():
log_message(f"stdout: {result.stdout}")
return {
"func": func,
"gcc_lines": 0,
"clang_lines": 0,
"diff_percent": 0,
"opt_log_created": False,
"gcc_opt_log_created": False,
"status": "Clang"
}
log_message("Clang Success")
log_message(f"Generated File: {s_clang} (size: {s_clang.stat().st_size} bytes)")
# Step 6.5: Clang Compile Assembly Directly from LL
log_message("Step 6.5: Clang Compile Assembly Directly from LL", add_newline=True)
s_clang_ll = output_dir / f"{clean_func}_clang_from_ll.s"
clang_ll_cmd = [str(config["CLANG_BIN"]), *config["compile_flags"]["clang"], str(clean_ll), "-o", str(s_clang_ll)]
log_message(f"Command: {' '.join(clang_ll_cmd)}")
result = subprocess.run(clang_ll_cmd, capture_output=True, text=True, timeout=config.get("timeout"))
if result.returncode != 0:
log_message(f"Clang (from LL) Failed (exit code: {result.returncode})")
if result.stderr.strip():
log_message(f"stderr: {result.stderr}")
if result.stdout.strip():
log_message(f"stdout: {result.stdout}")
# Don't fail the whole process, just mark it as failed
clang_ll_success = False
else:
clang_ll_success = True
log_message("Clang (from LL) Success")
log_message(f"Generated File: {s_clang_ll} (size: {s_clang_ll.stat().st_size} bytes)")
# Step 7: Assembly Instruction Counting
log_message("Step 7: Assembly Instruction Counting", add_newline=True)
gcc_count = strip_asm(s_gcc)
clang_count = strip_asm(s_clang)
clang_ll_count = strip_asm(s_clang_ll) if clang_ll_success else 0
if gcc_count > 0:
diff_percent = ((clang_count - gcc_count) / gcc_count) * 100
else:
diff_percent = 0 if clang_count == 0 else float('inf')
log_message(f"GCC Instructions: {gcc_count}")
log_message(f"Clang (from C) Instructions: {clang_count}")
if clang_ll_success:
log_message(f"Clang (from LL) Instructions: {clang_ll_count}")
log_message(f"Difference (Clang from C vs GCC): {diff_percent:.1f}%")
# Clean up temporary files
if bc_temp.exists():
bc_temp.unlink()
if bc_final.exists():
bc_final.unlink()
log_message("Processing Complete")
log_message("Final Status: Success")
return {
"func": func,
"gcc_lines": gcc_count,
"clang_lines": clang_count,
"clang_ll_lines": clang_ll_count,
"clang_ll_success": clang_ll_success,
"diff_percent": diff_percent,
"opt_log_created": opt_log_created,
"gcc_opt_log_created": gcc_opt_log_created,
"llc_log_created": llc_log_created,
"status": ""
}
except subprocess.TimeoutExpired as e:
log_message(f"Processing Timeout: {e}")
return {
"func": func,
"gcc_lines": 0,
"clang_lines": 0,
"diff_percent": 0,
"opt_log_created": False,
"status": "timeout"
}
except Exception as e:
log_message(f"Processing Exception: {e}")
return {
"func": func,
"gcc_lines": 0,
"clang_lines": 0,
"diff_percent": 0,
"opt_log_created": False,
"status": f"exception: {str(e)}"
}