e5dda9c246c4cf5c5266529c3976aeaa291a77a3
[yosys.git] / passes / equiv / equiv_opt.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2018 whitequark <whitequark@whitequark.org>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 */
19
20 #include "kernel/register.h"
21
22 USING_YOSYS_NAMESPACE
23 PRIVATE_NAMESPACE_BEGIN
24
25 struct EquivOptPass:public ScriptPass
26 {
27 EquivOptPass() : ScriptPass("equiv_opt", "prove equivalence for optimized circuit") { }
28
29 void help() YS_OVERRIDE
30 {
31 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
32 log("\n");
33 log(" equiv_opt [options] [command]\n");
34 log("\n");
35 log("This command checks circuit equivalence before and after an optimization pass.\n");
36 log("\n");
37 log(" -run <from_label>:<to_label>\n");
38 log(" only run the commands between the labels (see below). an empty\n");
39 log(" from label is synonymous to the start of the command list, and empty to\n");
40 log(" label is synonymous to the end of the command list.\n");
41 log("\n");
42 log(" -map <filename>\n");
43 log(" expand the modules in this file before proving equivalence. this is\n");
44 log(" useful for handling architecture-specific primitives.\n");
45 log("\n");
46 log(" -assert\n");
47 log(" produce an error if the circuits are not equivalent\n");
48 log("\n");
49 log("The following commands are executed by this verification command:\n");
50 help_script();
51 log("\n");
52 }
53
54 std::string command, techmap_opts;
55 bool assert;
56
57 void clear_flags() YS_OVERRIDE
58 {
59 command = "";
60 techmap_opts = "";
61 assert = false;
62 }
63
64 void execute(std::vector < std::string > args, RTLIL::Design * design) YS_OVERRIDE
65 {
66 string run_from, run_to;
67 clear_flags();
68
69 size_t argidx;
70 for (argidx = 1; argidx < args.size(); argidx++) {
71 if (args[argidx] == "-run" && argidx + 1 < args.size()) {
72 size_t pos = args[argidx + 1].find(':');
73 if (pos == std::string::npos)
74 break;
75 run_from = args[++argidx].substr(0, pos);
76 run_to = args[argidx].substr(pos + 1);
77 continue;
78 }
79 if (args[argidx] == "-map" && argidx + 1 < args.size()) {
80 techmap_opts += " -map " + args[++argidx];
81 continue;
82 }
83 if (args[argidx] == "-assert") {
84 assert = true;
85 continue;
86 }
87 break;
88 }
89
90 for (; argidx < args.size(); argidx++) {
91 if (command.empty()) {
92 if (args[argidx].substr(0, 1) == "-")
93 cmd_error(args, argidx, "Unknown option.");
94 } else {
95 command += " ";
96 }
97 command += args[argidx];
98 }
99
100 if (command.empty())
101 log_cmd_error("No optimization pass specified!\n");
102
103 if (!design->full_selection())
104 log_cmd_error("This command only operates on fully selected designs!\n");
105
106 log_header(design, "Executing EQUIV_OPT pass.\n");
107 log_push();
108
109 run_script(design, run_from, run_to);
110
111 log_pop();
112 }
113
114 void script() YS_OVERRIDE
115 {
116 if (check_label("run_pass")) {
117 run("hierarchy -auto-top");
118 run("design -save preopt");
119 if (help_mode)
120 run("[command]");
121 else
122 run(command);
123 run("design -stash postopt");
124 }
125
126 if (check_label("prepare")) {
127 run("design -copy-from preopt -as gold A:top");
128 run("design -copy-from postopt -as gate A:top");
129 }
130
131 if ((!techmap_opts.empty() || help_mode) && check_label("techmap", "(only with -map)")) {
132 string opts;
133 if (help_mode)
134 opts = " -map <filename> ...";
135 else
136 opts = techmap_opts;
137 run("techmap -wb -D EQUIV -autoproc" + opts);
138 }
139
140 if (check_label("prove")) {
141 run("equiv_make gold gate equiv");
142 run("equiv_induct equiv");
143 if (help_mode)
144 run("equiv_status [-assert] equiv");
145 else if (assert)
146 run("equiv_status -assert equiv");
147 else
148 run("equiv_status equiv");
149 }
150
151 if (check_label("restore")) {
152 run("design -load preopt");
153 }
154 }
155 } EquivOptPass;
156
157 PRIVATE_NAMESPACE_END