File size: 4,494 Bytes
34292e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
proc check_kepler_formal { } {
  if {
    ![info exists ::env(KEPLER_FORMAL_EXE)]
    || ![file executable $::env(KEPLER_FORMAL_EXE)]
  } {
    error "Kepler Formal not found. Install Kepler Formal or set KEPLER_FORMAL_EXE."
  }
}

proc lec_check_enabled { } {
  if { ![env_var_equals LEC_CHECK 1] } {
    return 0
  }
  check_kepler_formal
  return 1
}

proc write_lec_verilog { filename } {
  set remove_cells [find_physical_only_masters]
  if { [env_var_exists_and_non_empty REMOVE_CELLS_FOR_LEC] } {
    lappend remove_cells {*}$::env(REMOVE_CELLS_FOR_LEC)
  }
  set out_file $::env(RESULTS_DIR)/$filename
  write_verilog -remove_cells $remove_cells $out_file

  # Add auxiliary Verilog files (e.g., blackbox stubs) for LEC
  if { [env_var_exists_and_non_empty LEC_AUX_VERILOG_FILES] } {
    set out [open $out_file a]
    foreach aux_file $::env(LEC_AUX_VERILOG_FILES) {
      if { ![file exists $aux_file] } {
        close $out
        error "LEC auxiliary Verilog file not found: $aux_file"
      }
      puts $out "\n// ORFS auxiliary Verilog for Kepler LEC: $aux_file"
      set in [open $aux_file r]
      fcopy $in $out
      close $in
      puts $out ""
    }
    close $out
  }
}

proc write_lec_script { step file1 file2 } {
  # Exclude select Liberty files from being passed to kepler-formal
  if { [env_var_exists_and_non_empty REMOVE_LIBS_FOR_LEC] } {
    foreach lib_to_remove $::env(REMOVE_LIBS_FOR_LEC) {
      set remove_libs_set($lib_to_remove) 1
    }
    set lib_list [lmap item $::env(LIB_FILES) {
      if { [info exists remove_libs_set($item)] } { continue }
      set item
    }]
  } else {
    set lib_list $::env(LIB_FILES)
  }
  set outfile [open "$::env(OBJECTS_DIR)/${step}_lec_test.yml" w]
  puts $outfile "format: verilog"
  puts $outfile "input_paths:"
  puts $outfile "  - $::env(RESULTS_DIR)/${file1}"
  puts $outfile "  - $::env(RESULTS_DIR)/${file2}"
  puts $outfile "liberty_files:"
  foreach libFile $lib_list {
    puts $outfile " - $libFile"
  }
  puts $outfile "log_file: $::env(LOG_DIR)/${step}_lec_check.log"
  close $outfile
}

proc write_sec_script { step file1 file2 } {
  set outfile [open "$::env(OBJECTS_DIR)/${step}_sec_test.yml" w]
  puts $outfile "format: verilog"
  puts $outfile "verification: sec"
  puts $outfile "sec_engine: pdr"
  puts $outfile "input_paths:"
  puts $outfile "  - $::env(RESULTS_DIR)/${file1}"
  puts $outfile "  - $::env(RESULTS_DIR)/${file2}"
  puts $outfile "liberty_files:"
  foreach libFile $::env(LIB_FILES) {
    puts $outfile " - $libFile"
  }
  puts $outfile "log_file: $::env(LOG_DIR)/${step}_sec_check.log"
  close $outfile
}

proc formal_check_label { step } {
  if { [string equal $step 4_rsz] } {
    return "Repair timing output"
  }
  return "Global output"
}

# Run kepler-formal through run_command.py, as the flow runs yosys and
# openroad, so its stdout and elapsed time/peak memory land in
# $LOG_DIR/${step}_${kind}.log. kepler-formal still writes its detailed
# log to the ${step}_${kind}_check.log named in its config.
proc run_kepler_formal { step kind } {
  if { [env_var_exists_and_non_empty RUN_CMD] } {
    set run_cmd $::env(RUN_CMD)
  } else {
    set run_cmd [list $::env(PYTHON_EXE) $::env(SCRIPTS_DIR)/run_command.py]
  }
  # run_command.py reports the elapsed time on stderr, which exec would
  # otherwise treat as an error.
  exec {*}$run_cmd --log $::env(LOG_DIR)/${step}_${kind}.log -- \
    {*}$::env(KEPLER_FORMAL_EXE) --config $::env(OBJECTS_DIR)/${step}_${kind}_test.yml \
    2>@ stderr
}

proc run_lec_test { step file1 file2 } {
  check_kepler_formal
  write_lec_script $step $file1 $file2
  run_kepler_formal $step lec
  try {
    set count [exec grep -c "Found difference" $::env(LOG_DIR)/${step}_lec_check.log]
  } trap CHILDSTATUS {results options} {
    # This block executes if grep returns a non-zero exit code
    set count 0
  }
  set label [formal_check_label $step]
  if { $count > 0 } {
    error "$label failed lec test"
  } else {
    puts "$label passed lec test"
  }
}

proc run_sec_test { step file1 file2 } {
  check_kepler_formal
  write_sec_script $step $file1 $file2
  run_kepler_formal $step sec
  try {
    set count [exec grep -c "SEC found a counterexample" $::env(LOG_DIR)/${step}_sec_check.log]
  } trap CHILDSTATUS {results options} {
    # This block executes if grep returns a non-zero exit code
    set count 0
  }
  if { $count > 0 } {
    error "Global output failed sec test"
  } else {
    puts "Global output passed sec test"
  }
}