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
53void LoweringOptions::parse(StringRef text, ErrorHandlerT errorHandler) {
54 while (!text.empty()) {
55 // Remove the first option from the text.
56 auto split = text.split(",");
57 auto option = split.first.trim();
58 text = split.second;
59 if (option == "") {
60 // Empty options are fine.
61 } else if (option == "noAlwaysComb") {
62 noAlwaysComb = true;
63 } else if (option == "exprInEventControl") {
65 } else if (option == "disallowPackedArrays") {
67 } else if (option == "disallowPackedStructAssignments") {
69 } else if (option == "disallowLocalVariables") {
71 } else if (option == "verifLabels") {
72 enforceVerifLabels = true;
73 } else if (option.consume_front("emittedLineLength=")) {
74 if (option.getAsInteger(10, emittedLineLength)) {
75 errorHandler("expected integer source width");
78 errorHandler("line length '" + Twine(emittedLineLength) +
79 "' exceeds maximum of " + Twine(MAX_LINE_LENGTH));
81 }
82 } else if (option == "explicitBitcast") {
83 explicitBitcast = true;
84 } else if (option == "emitReplicatedOpsToHeader") {
86 } else if (option.consume_front("maximumNumberOfTermsPerExpression=")) {
87 if (option.getAsInteger(10, maximumNumberOfTermsPerExpression)) {
88 errorHandler("expected integer source width");
90 }
91 } else if (option.consume_front("locationInfoStyle=")) {
92 if (auto style = parseLocationInfoStyle(option)) {
93 locationInfoStyle = *style;
94 } else {
95 errorHandler("expected 'plain', 'wrapInAtSquareBracket', or 'none'");
96 }
97 } else if (option == "disallowPortDeclSharing") {
99 } else if (option == "printDebugInfo") {
100 printDebugInfo = true;
101 } else if (option == "disallowExpressionInliningInPorts") {
103 } else if (option == "disallowMuxInlining") {
104 disallowMuxInlining = true;
105 } else if (option == "mitigateVivadoArrayIndexConstPropBug") {
107 } else if (option.consume_front("wireSpillingHeuristic=")) {
108 if (auto heuristic = parseWireSpillingHeuristic(option)) {
109 wireSpillingHeuristicSet |= *heuristic;
110 } else {
111 errorHandler("expected ''spillLargeTermsWithNamehints'");
112 }
113 } else if (option.consume_front("wireSpillingNamehintTermLimit=")) {
114 if (option.getAsInteger(10, wireSpillingNamehintTermLimit)) {
115 errorHandler(
116 "expected integer for number of namehint heurstic term limit");
118 }
119 } else if (option == "emitWireInPorts") {
120 emitWireInPorts = true;
121 } else if (option == "emitBindComments") {
122 emitBindComments = true;
123 } else if (option == "omitVersionComment") {
124 omitVersionComment = true;
125 } else if (option == "caseInsensitiveKeywords") {
127 } else if (option == "emitVerilogLocations") {
129 } else if (option == "fixUpEmptyModules") {
130 fixUpEmptyModules = true;
131 } else if (option == "disallowClockedAssertions") {
133 } else if (option == "disallowDeclAssignments") {
135 } else if (option == "alwaysEmitBeginEnd") {
136 alwaysEmitBeginEnd = true;
137 } else {
138 errorHandler(llvm::Twine("unknown style option \'") + option + "\'");
139 // We continue parsing options after a failure.
140 }
141 }
142}
143
144std::string LoweringOptions::toString() const {
145 std::string options = "";
146 // All options should add a trailing comma to simplify the code.
147 if (noAlwaysComb)
148 options += "noAlwaysComb,";
150 options += "exprInEventControl,";
152 options += "disallowPackedArrays,";
154 options += "disallowPackedStructAssignments,";
156 options += "disallowLocalVariables,";
158 options += "verifLabels,";
159 if (explicitBitcast)
160 options += "explicitBitcast,";
162 options += "emitReplicatedOpsToHeader,";
164 options += "locationInfoStyle=wrapInAtSquareBracket,";
166 options += "locationInfoStyle=none,";
168 options += "disallowPortDeclSharing,";
169 if (printDebugInfo)
170 options += "printDebugInfo,";
173 options += "wireSpillingHeuristic=spillLargeTermsWithNamehints,";
175 options += "disallowExpressionInliningInPorts,";
177 options += "disallowMuxInlining,";
179 options += "mitigateVivadoArrayIndexConstPropBug,";
180
182 options += "emittedLineLength=" + std::to_string(emittedLineLength) + ',';
184 options += "maximumNumberOfTermsPerExpression=" +
185 std::to_string(maximumNumberOfTermsPerExpression) + ',';
186 if (emitWireInPorts)
187 options += "emitWireInPorts,";
189 options += "emitBindComments,";
191 options += "omitVersionComment,";
193 options += "caseInsensitiveKeywords,";
195 options += "emitVerilogLocations,";
197 options += "fixUpEmptyModules,";
199 options += "disallowClockedAssertions,";
201 options += "disallowDeclAssignments,";
203 options += "alwaysEmitBeginEnd,";
204
205 // Remove a trailing comma if present.
206 if (!options.empty()) {
207 assert(options.back() == ',' && "all options should add a trailing comma");
208 options.pop_back();
209 }
210 return options;
211}
212
213StringAttr LoweringOptions::getAttributeFrom(ModuleOp module) {
214 return module->getAttrOfType<StringAttr>("circt.loweringOptions");
215}
216
217void LoweringOptions::setAsAttribute(ModuleOp module) {
218 module->setAttr("circt.loweringOptions",
219 StringAttr::get(module.getContext(), toString()));
220}
221
223 if (auto styleAttr = getAttributeFrom(module))
224 parse(styleAttr.getValue(), [&](Twine error) { module.emitError(error); });
225}
226
227static_assert((LoweringOptions::MAX_LINE_LENGTH ==
229 "Exported limits for emitted line length not aligned with "
230 "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 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...