CIRCT 24.0.0git
Loading...
Searching...
No Matches
LoweringOptions.cpp
Go to the documentation of this file.
1//===- LoweringOptions.cpp - CIRCT Lowering Options -----------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Options for controlling the lowering process. Contains command line
10// option definitions and support.
11//
12//===----------------------------------------------------------------------===//
13
16#include "mlir/IR/BuiltinOps.h"
17
18using namespace circt;
19using namespace mlir;
20
21//===----------------------------------------------------------------------===//
22// LoweringOptions
23//===----------------------------------------------------------------------===//
24
25LoweringOptions::LoweringOptions(StringRef options, ErrorHandlerT errorHandler)
26 : LoweringOptions() {
27 parse(options, errorHandler);
28}
29
31 parseFromAttribute(module);
32}
33
34static std::optional<LoweringOptions::LocationInfoStyle>
35parseLocationInfoStyle(StringRef option) {
36 return llvm::StringSwitch<std::optional<LoweringOptions::LocationInfoStyle>>(
37 option)
38 .Case("plain", LoweringOptions::Plain)
39 .Case("wrapInAtSquareBracket", LoweringOptions::WrapInAtSquareBracket)
40 .Case("none", LoweringOptions::None)
41 .Default(std::nullopt);
42}
43
44static std::optional<LoweringOptions::WireSpillingHeuristic>
45parseWireSpillingHeuristic(StringRef option) {
46 return llvm::StringSwitch<
47 std::optional<LoweringOptions::WireSpillingHeuristic>>(option)
48 .Case("spillLargeTermsWithNamehints",
50 .Default(std::nullopt);
51}
52
53static std::optional<LoweringOptions::VarOpDeclStyle>
54parseVarOpDeclStyle(StringRef option) {
55 return llvm::StringSwitch<std::optional<LoweringOptions::VarOpDeclStyle>>(
56 option)
60 .Default(std::nullopt);
61}
62
63void LoweringOptions::parse(StringRef text, ErrorHandlerT errorHandler) {
64 while (!text.empty()) {
65 // Remove the first option from the text.
66 auto split = text.split(",");
67 auto option = split.first.trim();
68 text = split.second;
69 if (option == "") {
70 // Empty options are fine.
71 } else if (option == "noAlwaysComb") {
72 noAlwaysComb = true;
73 } else if (option == "exprInEventControl") {
75 } else if (option == "disallowPackedArrays") {
77 } else if (option == "disallowPackedStructAssignments") {
79 } else if (option == "disallowLocalVariables") {
81 } else if (option == "verifLabels") {
82 enforceVerifLabels = true;
83 } else if (option.consume_front("emittedLineLength=")) {
84 if (option.getAsInteger(10, emittedLineLength)) {
85 errorHandler("expected integer source width");
88 errorHandler("line length '" + Twine(emittedLineLength) +
89 "' exceeds maximum of " + Twine(MAX_LINE_LENGTH));
91 }
92 } else if (option == "explicitBitcast") {
93 explicitBitcast = true;
94 } else if (option == "emitReplicatedOpsToHeader") {
96 } else if (option.consume_front("maximumNumberOfTermsPerExpression=")) {
97 if (option.getAsInteger(10, maximumNumberOfTermsPerExpression)) {
98 errorHandler("expected integer source width");
100 }
101 } else if (option.consume_front("locationInfoStyle=")) {
102 if (auto style = parseLocationInfoStyle(option)) {
103 locationInfoStyle = *style;
104 } else {
105 errorHandler("expected 'plain', 'wrapInAtSquareBracket', or 'none'");
106 }
107 } else if (option == "disallowPortDeclSharing") {
109 } else if (option == "printDebugInfo") {
110 printDebugInfo = true;
111 } else if (option == "disallowExpressionInliningInPorts") {
113 } else if (option == "disallowMuxInlining") {
114 disallowMuxInlining = true;
115 } else if (option == "mitigateVivadoArrayIndexConstPropBug") {
117 } else if (option.consume_front("wireSpillingHeuristic=")) {
118 if (auto heuristic = parseWireSpillingHeuristic(option)) {
119 wireSpillingHeuristicSet |= *heuristic;
120 } else {
121 errorHandler("expected ''spillLargeTermsWithNamehints'");
122 }
123 } else if (option.consume_front("svVarDeclStyle=")) {
124 if (auto style = parseVarOpDeclStyle(option)) {
125 varOpDeclStyle = *style;
126 } else {
127 errorHandler("expected 'varLogic', 'logic', or 'reg'");
128 }
129 } else if (option.consume_front("wireSpillingNamehintTermLimit=")) {
130 if (option.getAsInteger(10, wireSpillingNamehintTermLimit)) {
131 errorHandler(
132 "expected integer for number of namehint heurstic term limit");
134 }
135 } else if (option == "emitWireInPorts") {
136 emitWireInPorts = true;
137 } else if (option == "emitBindComments") {
138 emitBindComments = true;
139 } else if (option == "omitVersionComment") {
140 omitVersionComment = true;
141 } else if (option == "caseInsensitiveKeywords") {
143 } else if (option == "emitVerilogLocations") {
145 } else if (option == "fixUpEmptyModules") {
146 fixUpEmptyModules = true;
147 } else if (option == "disallowClockedAssertions") {
149 } else if (option == "disallowDeclAssignments") {
151 } else if (option == "alwaysEmitBeginEnd") {
152 alwaysEmitBeginEnd = true;
153 } else {
154 errorHandler(llvm::Twine("unknown style option \'") + option + "\'");
155 // We continue parsing options after a failure.
156 }
157 }
158}
159
160std::string LoweringOptions::toString() const {
161 std::string options = "";
162 // All options should add a trailing comma to simplify the code.
163 if (noAlwaysComb)
164 options += "noAlwaysComb,";
166 options += "exprInEventControl,";
168 options += "disallowPackedArrays,";
170 options += "disallowPackedStructAssignments,";
172 options += "disallowLocalVariables,";
174 options += "verifLabels,";
175 if (explicitBitcast)
176 options += "explicitBitcast,";
178 options += "emitReplicatedOpsToHeader,";
180 options += "locationInfoStyle=wrapInAtSquareBracket,";
182 options += "locationInfoStyle=none,";
184 options += "disallowPortDeclSharing,";
185 if (printDebugInfo)
186 options += "printDebugInfo,";
189 options += "wireSpillingHeuristic=spillLargeTermsWithNamehints,";
191 options += "svVarDeclStyle=logic,";
193 options += "svVarDeclStyle=reg,";
195 options += "disallowExpressionInliningInPorts,";
197 options += "disallowMuxInlining,";
199 options += "mitigateVivadoArrayIndexConstPropBug,";
200
202 options += "emittedLineLength=" + std::to_string(emittedLineLength) + ',';
204 options += "maximumNumberOfTermsPerExpression=" +
205 std::to_string(maximumNumberOfTermsPerExpression) + ',';
206 if (emitWireInPorts)
207 options += "emitWireInPorts,";
209 options += "emitBindComments,";
211 options += "omitVersionComment,";
213 options += "caseInsensitiveKeywords,";
215 options += "emitVerilogLocations,";
217 options += "fixUpEmptyModules,";
219 options += "disallowClockedAssertions,";
221 options += "disallowDeclAssignments,";
223 options += "alwaysEmitBeginEnd,";
224
225 // Remove a trailing comma if present.
226 if (!options.empty()) {
227 assert(options.back() == ',' && "all options should add a trailing comma");
228 options.pop_back();
229 }
230 return options;
231}
232
233StringAttr LoweringOptions::getAttributeFrom(ModuleOp module) {
234 return module->getAttrOfType<StringAttr>("circt.loweringOptions");
235}
236
237void LoweringOptions::setAsAttribute(ModuleOp module) {
238 module->setAttr("circt.loweringOptions",
239 StringAttr::get(module.getContext(), toString()));
240}
241
243 if (auto styleAttr = getAttributeFrom(module))
244 parse(styleAttr.getValue(), [&](Twine error) { module.emitError(error); });
245}
246
247static_assert((LoweringOptions::MAX_LINE_LENGTH ==
249 "Exported limits for emitted line length not aligned with "
250 "underlying maximum supported");
assert(baseType &&"element must be base type")
static std::optional< LoweringOptions::LocationInfoStyle > parseLocationInfoStyle(StringRef option)
static std::optional< LoweringOptions::WireSpillingHeuristic > parseWireSpillingHeuristic(StringRef option)
static std::optional< LoweringOptions::VarOpDeclStyle > parseVarOpDeclStyle(StringRef option)
static constexpr uint32_t kMaxMargin
Largest supported target line length.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Options which control the emission from CIRCT to Verilog.
bool isWireSpillingHeuristicEnabled(WireSpillingHeuristic heurisic) const
bool noAlwaysComb
If true, emits sv.alwayscomb as Verilog always @(*) statements.
bool mitigateVivadoArrayIndexConstPropBug
If true, every expression used as an array index is driven by a wire, and the wire is marked as (* ke...
bool disallowLocalVariables
If true, do not emit SystemVerilog locally scoped "automatic" or logic declarations - emit top level ...
bool omitVersionComment
If true, do not emit a version comment at the top of each verilog file.
bool enforceVerifLabels
If true, verification statements like assert, assume, and cover will always be emitted with a label.
bool disallowExpressionInliningInPorts
If true, every expression passed to an instance port is driven by a wire.
bool printDebugInfo
Print debug info.
llvm::function_ref< void(llvm::Twine)> ErrorHandlerT
Error callback type used to indicate errors parsing the options string.
unsigned maximumNumberOfTermsPerExpression
void setAsAttribute(mlir::ModuleOp module)
Write the verilog emitter options to a module's attributes.
enum circt::LoweringOptions::LocationInfoStyle locationInfoStyle
bool disallowPortDeclSharing
If true, every port is declared separately (each includes direction and type (e.g....
bool disallowPackedArrays
If true, eliminate packed arrays for tools that don't support them (e.g.
bool explicitBitcast
Add an explicit bitcast for avoiding bitwidth mismatch LINT errors.
bool disallowMuxInlining
If true, every mux expression is spilled to a wire.
bool fixUpEmptyModules
If true, add a dummy wire to empty modules to prevent tools from regarding the module as blackbox.
bool disallowPackedStructAssignments
If true, eliminate packed struct assignments in favor of a wire + assignments to the individual field...
void parse(llvm::StringRef options, ErrorHandlerT callback)
Read in options from a string, overriding only the set options in the string.
static mlir::StringAttr getAttributeFrom(mlir::ModuleOp module)
Return the value of the circt.loweringOptions in the specified module if present, or a null attribute...
bool caseInsensitiveKeywords
If true, then unique names that collide with keywords case insensitively.
void parseFromAttribute(mlir::ModuleOp module)
Load any emitter options from the module.
bool alwaysEmitBeginEnd
If true, always emit begin/end blocks around if/else/always bodies, even when there is only a single ...
bool emitWireInPorts
If true, emit wire in port lists rather than nothing.
bool emitReplicatedOpsToHeader
If true, replicated ops are emitted to a header file.
std::string toString() const
Returns a string representation of the options.
LoweringOptions()=default
Create a LoweringOptions with the default values.
bool allowExprInEventControl
If true, expressions are allowed in the sensitivity list of always statements, otherwise they are for...
bool emitVerilogLocations
If true, then update the the mlir to include output verilog locations.
bool emitBindComments
If true, emit a comment wherever an instance wasn't printed, because it's emitted elsewhere as a bind...
enum circt::LoweringOptions::VarOpDeclStyle varOpDeclStyle