CIRCT 22.0.0git
Loading...
Searching...
No Matches
ImportAIGER.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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// This file implements the AIGER file import functionality.
10//
11//===----------------------------------------------------------------------===//
12
18#include "mlir/IR/Builders.h"
19#include "mlir/IR/BuiltinOps.h"
20#include "mlir/IR/Diagnostics.h"
21#include "mlir/IR/Location.h"
22#include "mlir/IR/MLIRContext.h"
23#include "mlir/Support/FileUtilities.h"
24#include "mlir/Support/LogicalResult.h"
25#include "mlir/Support/Timing.h"
26#include "mlir/Tools/mlir-translate/Translation.h"
27#include "llvm/ADT/DenseMap.h"
28#include "llvm/ADT/StringExtras.h"
29#include "llvm/ADT/StringRef.h"
30#include "llvm/Support/Debug.h"
31#include "llvm/Support/ErrorHandling.h"
32#include "llvm/Support/SMLoc.h"
33#include "llvm/Support/SourceMgr.h"
34#include "llvm/Support/raw_ostream.h"
35#include <cctype>
36#include <string>
37
38using namespace mlir;
39using namespace circt;
40using namespace circt::hw;
41using namespace circt::synth;
42using namespace circt::seq;
43using namespace circt::aiger;
44
45#define DEBUG_TYPE "import-aiger"
46
47namespace {
48
49/// AIGER token types for lexical analysis
50enum class AIGERTokenKind {
51 // Literals
52 Number,
53 Identifier,
54
55 // Special characters
56 Newline,
57 EndOfFile,
58
59 // Error
60 Error
61};
62
63/// Represents a token in the AIGER file
64struct AIGERToken {
65 AIGERTokenKind kind;
66 StringRef spelling;
67 SMLoc location;
68
69 AIGERToken(AIGERTokenKind kind, StringRef spelling, SMLoc location)
70 : kind(kind), spelling(spelling), location(location) {}
71};
72
73/// Simple lexer for AIGER files
74///
75/// This lexer handles both ASCII (.aag) and binary (.aig) AIGER formats.
76/// It provides basic tokenization for header parsing and symbol tables,
77/// while also supporting byte-level reading for binary format.
78class AIGERLexer {
79public:
80 AIGERLexer(const llvm::SourceMgr &sourceMgr, MLIRContext *context)
81 : sourceMgr(sourceMgr),
82 bufferNameIdentifier(getMainBufferNameIdentifier(sourceMgr, context)),
83 curBuffer(
84 sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID())->getBuffer()),
85 curPtr(curBuffer.begin()) {}
86
87 /// Get the next token
88 AIGERToken nextToken();
89
90 /// Lex the current position as a symbol (used for symbol table parsing)
91 AIGERToken lexAsSymbol();
92
93 /// Peek at the current token without consuming it
94 AIGERToken peekToken();
95
96 /// Check if we're at end of file
97 bool isAtEOF() const { return curPtr >= curBuffer.end(); }
98
99 /// Read a single byte for binary parsing
100 ParseResult readByte(unsigned char &byte) {
101 if (curPtr >= curBuffer.end())
102 return failure();
103 byte = *curPtr++;
104 return success();
105 }
106
107 /// Get current location
108 SMLoc getCurrentLoc() const { return SMLoc::getFromPointer(curPtr); }
109
110 /// Encode the specified source location information into a Location object
111 /// for attachment to the IR or error reporting.
112 Location translateLocation(llvm::SMLoc loc) {
113 assert(loc.isValid());
114 unsigned mainFileID = sourceMgr.getMainFileID();
115 auto lineAndColumn = sourceMgr.getLineAndColumn(loc, mainFileID);
116 return FileLineColLoc::get(bufferNameIdentifier, lineAndColumn.first,
117 lineAndColumn.second);
118 }
119
120 /// Emit an error message and return an error token.
121 AIGERToken emitError(const char *loc, const Twine &message) {
122 mlir::emitError(translateLocation(SMLoc::getFromPointer(loc)), message);
123 return AIGERToken(AIGERTokenKind::Error, StringRef(loc, 1),
124 SMLoc::getFromPointer(loc));
125 }
126
127private:
128 const llvm::SourceMgr &sourceMgr;
129 StringAttr bufferNameIdentifier;
130 StringRef curBuffer;
131 const char *curPtr;
132
133 /// Get the main buffer name identifier
134 static StringAttr
135 getMainBufferNameIdentifier(const llvm::SourceMgr &sourceMgr,
136 MLIRContext *context) {
137 auto mainBuffer = sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID());
138 StringRef bufferName = mainBuffer->getBufferIdentifier();
139 if (bufferName.empty())
140 bufferName = "<unknown>";
141 return StringAttr::get(context, bufferName);
142 }
143
144 /// Skip whitespace (except newlines)
145 void skipWhitespace();
146
147 /// Skip to end of line (for comment handling)
148 void skipUntilNewline();
149
150 /// Lex a number
151 AIGERToken lexNumber();
152
153 /// Lex an identifier
154 AIGERToken lexIdentifier();
155
156 /// Create a token
157 AIGERToken makeToken(AIGERTokenKind kind, const char *start) {
158 return AIGERToken(kind, StringRef(start, curPtr - start),
159 SMLoc::getFromPointer(start));
160 }
161};
162
163/// Main AIGER parser class
164///
165/// This parser implements the complete AIGER format specification including:
166/// - ASCII (.aag) and binary (.aig) formats
167/// - Basic AIGER components (inputs, latches, outputs, AND gates)
168/// - Optional sections (bad states, constraints, justice, fairness)
169/// - Symbol tables and comments
170///
171/// The parser creates MLIR modules using the HW, AIG, and Seq dialects.
172class AIGERParser {
173public:
174 AIGERParser(const llvm::SourceMgr &sourceMgr, MLIRContext *context,
175 ModuleOp module, const ImportAIGEROptions &options)
176 : lexer(sourceMgr, context), context(context), module(module),
177 options(options), builder(context) {}
178
179 /// Parse the AIGER file and populate the MLIR module
180 ParseResult parse();
181
182private:
183 AIGERLexer lexer;
184 MLIRContext *context;
185 ModuleOp module;
186 const ImportAIGEROptions &options;
187 OpBuilder builder;
188
189 // AIGER file data
190 unsigned maxVarIndex = 0;
191 unsigned numInputs = 0;
192 unsigned numLatches = 0;
193 unsigned numOutputs = 0;
194 unsigned numAnds = 0;
195 bool isBinaryFormat = false;
196
197 // A mapping from {kind, index} -> symbol where kind is 0 for inputs, 1 for
198 // latches, and 2 for outputs.
199 enum SymbolKind : unsigned { Input, Latch, Output };
200 DenseMap<std::pair<SymbolKind, unsigned>, StringAttr> symbolTable;
201
202 // Parsed data storage
203 SmallVector<unsigned> inputLiterals;
204 SmallVector<std::tuple<unsigned, unsigned, SMLoc>>
205 latchDefs; // current, next, loc
206 SmallVector<std::pair<unsigned, SMLoc>> outputLiterals; // literal, loc
207 SmallVector<std::tuple<unsigned, unsigned, unsigned, SMLoc>>
208 andGateDefs; // lhs, rhs0, rhs1
209
210 /// Parse the header line (format and counts)
211 ParseResult parseHeader();
212
213 /// Parse inputs section
214 ParseResult parseInputs();
215
216 /// Parse latches section
217 ParseResult parseLatches();
218
219 /// Parse outputs section
220 ParseResult parseOutputs();
221
222 /// Parse AND gates section (dispatches to ASCII or binary)
223 ParseResult parseAndGates();
224
225 /// Parse AND gates in ASCII format
226 ParseResult parseAndGatesASCII();
227
228 /// Parse AND gates in binary format with delta compression
229 ParseResult parseAndGatesBinary();
230
231 /// Parse symbol table (optional)
232 ParseResult parseSymbolTable();
233
234 /// Parse comments (optional)
235 ParseResult parseComments();
236
237 /// Convert AIGER literal to MLIR value using backedges
238 ///
239 /// \param literal The AIGER literal (variable * 2 + inversion)
240 /// \param backedges Map from literals to backedge values
241 /// \param loc Location for created operations
242 /// \return The MLIR value corresponding to the literal, or nullptr on error
243 Value getLiteralValue(unsigned literal,
244 DenseMap<unsigned, Backedge> &backedges, Location loc);
245
246 /// Create the top-level HW module from parsed data
247 ParseResult createModule();
248
249 InFlightDiagnostic emitError(llvm::SMLoc loc, const Twine &message) {
250 return mlir::emitError(lexer.translateLocation(loc), message);
251 }
252
253 /// Emit error at current location
254 InFlightDiagnostic emitError(const Twine &message) {
255 return emitError(lexer.getCurrentLoc(), message);
256 }
257
258 /// Parse a number token into result
259 ParseResult parseNumber(unsigned &result, SMLoc *loc = nullptr);
260
261 /// Parse a binary encoded number (variable-length encoding)
262 ParseResult parseBinaryNumber(unsigned &result);
263
264 /// Expect and consume a newline token
265 ParseResult parseNewLine();
266};
267
268} // anonymous namespace
269
270//===----------------------------------------------------------------------===//
271// AIGERLexer Implementation
272//===----------------------------------------------------------------------===//
273
274void AIGERLexer::skipWhitespace() {
275 while (curPtr < curBuffer.end()) {
276 // NOTE: Don't use llvm::isSpace here because it also skips '\n'.
277 if (*curPtr == ' ' || *curPtr == '\t' || *curPtr == '\r') {
278 ++curPtr;
279 continue;
280 }
281
282 // Treat "//" as whitespace. This is not part of the AIGER format, but we
283 // support it for FileCheck tests.
284 if (*curPtr == '/' &&
285 (curPtr + 1 < curBuffer.end() && *(curPtr + 1) == '/')) {
286 skipUntilNewline();
287 continue;
288 }
289 break;
290 }
291}
292
293void AIGERLexer::skipUntilNewline() {
294 while (curPtr < curBuffer.end() && *curPtr != '\n')
295 ++curPtr;
296 if (curPtr < curBuffer.end() && *curPtr == '\n')
297 ++curPtr;
298}
299
300AIGERToken AIGERLexer::lexNumber() {
301 const char *start = curPtr;
302 while (curPtr < curBuffer.end() && llvm::isDigit(*curPtr))
303 ++curPtr;
304 return makeToken(AIGERTokenKind::Number, start);
305}
306
307AIGERToken AIGERLexer::lexIdentifier() {
308 const char *start = curPtr;
309 while (curPtr < curBuffer.end() && (llvm::isAlnum(*curPtr) || *curPtr == '_'))
310 ++curPtr;
311
312 StringRef spelling(start, curPtr - start);
313 AIGERTokenKind kind = AIGERTokenKind::Identifier;
314
315 return makeToken(kind, start);
316}
317
318AIGERToken AIGERLexer::nextToken() {
319 skipWhitespace();
320
321 auto impl = [this]() {
322 if (curPtr >= curBuffer.end())
323 return makeToken(AIGERTokenKind::EndOfFile, curPtr);
324
325 const char *start = curPtr;
326 char c = *curPtr++;
327
328 switch (c) {
329 case '\n':
330 return makeToken(AIGERTokenKind::Newline, start);
331 case '\r':
332 case ' ':
333 case '\t':
334 llvm_unreachable("Whitespace should have been skipped");
335 return makeToken(AIGERTokenKind::Error, start);
336 default:
337 if (llvm::isDigit(c)) {
338 --curPtr; // Back up to re-lex the number
339 return lexNumber();
340 }
341 if (llvm::isAlpha(c) || c == '_') {
342 --curPtr; // Back up to re-lex the identifier
343 return lexIdentifier();
344 }
345 assert((c != '/' || *curPtr != '/') && "// should have been skipped");
346 return makeToken(AIGERTokenKind::Error, start);
347 }
348 };
349
350 auto token = impl();
351 return token;
352}
353
354AIGERToken AIGERLexer::lexAsSymbol() {
355 skipWhitespace();
356 const char *start = curPtr;
357 while (curPtr < curBuffer.end() &&
358 (llvm::isPrint(*curPtr) && !llvm::isSpace(*curPtr)))
359 ++curPtr;
360 return makeToken(AIGERTokenKind::Identifier, start);
361}
362
363AIGERToken AIGERLexer::peekToken() {
364 const char *savedPtr = curPtr;
365 AIGERToken token = nextToken();
366 curPtr = savedPtr;
367 return token;
368}
369
370//===----------------------------------------------------------------------===//
371// AIGERParser Implementation
372//===----------------------------------------------------------------------===//
373
374ParseResult AIGERParser::parse() {
375 if (parseHeader() || parseInputs() || parseLatches() || parseOutputs() ||
376 parseAndGates() || parseSymbolTable() || parseComments())
377 return failure();
378 // Create the final module
379 return createModule();
380}
381
382ParseResult AIGERParser::parseNumber(unsigned &result, SMLoc *loc) {
383 auto token = lexer.nextToken();
384 if (loc)
385 *loc = token.location;
386
387 if (token.kind != AIGERTokenKind::Number)
388 return emitError(token.location, "expected number");
389
390 if (token.spelling.getAsInteger(10, result))
391 return emitError(token.location, "invalid number format");
392
393 return success();
394}
395
396ParseResult AIGERParser::parseBinaryNumber(unsigned &result) {
397 // AIGER binary format uses variable-length encoding
398 // Each byte has 7 data bits and 1 continuation bit (MSB)
399 // If continuation bit is set, more bytes follow
400
401 result = 0;
402 unsigned shift = 0;
403
404 while (true) {
405 unsigned char byte;
406 if (lexer.readByte(byte))
407 return emitError("unexpected end of file in binary number");
408
409 LLVM_DEBUG(llvm::dbgs() << "Read byte: 0x" << llvm::utohexstr(byte) << " ("
410 << (unsigned)byte << ")\n");
411
412 result |= (byte & 0x7F) << shift;
413
414 if ((byte & 0x80) == 0) { // No continuation bit
415 LLVM_DEBUG(llvm::dbgs() << "Decoded binary number: " << result << "\n");
416 break;
417 }
418
419 shift += 7;
420 if (shift >= 32) // Prevent overflow
421 return emitError("binary number too large");
422 }
423
424 return success();
425}
426
427ParseResult AIGERParser::parseHeader() {
428 LLVM_DEBUG(llvm::dbgs() << "Parsing AIGER header\n");
429
430 // Parse format identifier (aag or aig)
431 while (lexer.peekToken().kind != AIGERTokenKind::Identifier)
432 lexer.nextToken();
433
434 auto formatToken = lexer.nextToken();
435 if (formatToken.spelling == "aag") {
436 isBinaryFormat = false;
437 LLVM_DEBUG(llvm::dbgs() << "Format: aag (ASCII)\n");
438 } else if (formatToken.spelling == "aig") {
439 isBinaryFormat = true;
440 LLVM_DEBUG(llvm::dbgs() << "Format: aig (binary)\n");
441 } else {
442 return emitError(formatToken.location,
443 "expected 'aag' or 'aig' format identifier");
444 }
445
446 // Parse M I L O A (numbers separated by spaces)
447 SMLoc loc;
448 if (parseNumber(maxVarIndex, &loc))
449 return emitError(loc, "failed to parse M (max variable index)");
450
451 if (parseNumber(numInputs, &loc))
452 return emitError(loc, "failed to parse I (number of inputs)");
453
454 if (parseNumber(numLatches, &loc))
455 return emitError(loc, "failed to parse L (number of latches)");
456
457 if (parseNumber(numOutputs, &loc))
458 return emitError(loc, "failed to parse O (number of outputs)");
459
460 if (parseNumber(numAnds, &loc))
461 return emitError(loc, "failed to parse A (number of AND gates)");
462
463 LLVM_DEBUG(llvm::dbgs() << "Header: M=" << maxVarIndex << " I=" << numInputs
464 << " L=" << numLatches << " O=" << numOutputs
465 << " A=" << numAnds << "\n");
466
467 // Expect newline after header
468 return parseNewLine();
469}
470
471ParseResult AIGERParser::parseNewLine() {
472 auto token = lexer.nextToken();
473 if (token.kind != AIGERTokenKind::Newline)
474 return emitError(token.location, "expected newline");
475
476 return success();
477}
478
479ParseResult AIGERParser::parseInputs() {
480 LLVM_DEBUG(llvm::dbgs() << "Parsing " << numInputs << " inputs\n");
481 if (isBinaryFormat) {
482 // In binary format, inputs are implicit (literals 2, 4, 6, ...)
483 for (unsigned i = 0; i < numInputs; ++i)
484 inputLiterals.push_back(2 * (i + 1));
485 return success();
486 }
487
488 for (unsigned i = 0; i < numInputs; ++i) {
489 unsigned literal;
490 SMLoc loc;
491 if (parseNumber(literal, &loc) || parseNewLine())
492 return emitError(loc, "failed to parse input literal");
493 inputLiterals.push_back(literal);
494 }
495
496 return success();
497}
498
499ParseResult AIGERParser::parseLatches() {
500 LLVM_DEBUG(llvm::dbgs() << "Parsing " << numLatches << " latches\n");
501 if (isBinaryFormat) {
502 // In binary format, latches are implicit (literals 2, 4, 6, ...)
503 for (unsigned i = 0; i < numLatches; ++i) {
504 unsigned literal;
505 SMLoc loc;
506 if (parseNumber(literal, &loc))
507 return emitError(loc, "failed to parse latch next state literal");
508
509 latchDefs.push_back({2 * (i + 1 + numInputs), literal, loc});
510
511 // Expect newline after each latch next state
512 if (parseNewLine())
513 return failure();
514 }
515 return success();
516 }
517
518 // Parse latch definitions: current_state next_state
519 for (unsigned i = 0; i < numLatches; ++i) {
520 unsigned currentState, nextState;
521 SMLoc loc;
522 if (parseNumber(currentState, &loc) || parseNumber(nextState) ||
523 parseNewLine())
524 return emitError(loc, "failed to parse latch definition");
525
526 LLVM_DEBUG(llvm::dbgs() << "Latch " << i << ": " << currentState << " -> "
527 << nextState << "\n");
528
529 // Validate current state literal (should be even and positive)
530 if (currentState % 2 != 0 || currentState == 0)
531 return emitError(loc, "invalid latch current state literal");
532
533 latchDefs.push_back({currentState, nextState, loc});
534 }
535
536 return success();
537}
538
539ParseResult AIGERParser::parseOutputs() {
540 LLVM_DEBUG(llvm::dbgs() << "Parsing " << numOutputs << " outputs\n");
541 // NOTE: Parsing is same for binary and ASCII formats
542 // Parse output literals
543 for (unsigned i = 0; i < numOutputs; ++i) {
544 unsigned literal;
545 SMLoc loc;
546 if (parseNumber(literal, &loc) || parseNewLine())
547 return emitError(loc, "failed to parse output literal");
548
549 LLVM_DEBUG(llvm::dbgs() << "Output " << i << ": " << literal << "\n");
550
551 // Output literals can be any valid literal (including inverted)
552 outputLiterals.push_back({literal, loc});
553 }
554
555 return success();
556}
557
558ParseResult AIGERParser::parseAndGates() {
559 LLVM_DEBUG(llvm::dbgs() << "Parsing " << numAnds << " AND gates\n");
560
561 if (isBinaryFormat) {
562 return parseAndGatesBinary();
563 }
564 return parseAndGatesASCII();
565}
566
567ParseResult AIGERParser::parseAndGatesASCII() {
568 // Parse AND gate definitions: lhs rhs0 rhs1
569 for (unsigned i = 0; i < numAnds; ++i) {
570 unsigned lhs, rhs0, rhs1;
571 SMLoc loc;
572 if (parseNumber(lhs, &loc) || parseNumber(rhs0) || parseNumber(rhs1) ||
573 parseNewLine())
574 return emitError(loc, "failed to parse AND gate definition");
575
576 LLVM_DEBUG(llvm::dbgs() << "AND Gate " << i << ": " << lhs << " = " << rhs0
577 << " & " << rhs1 << "\n");
578
579 // Validate LHS (should be even and positive)
580 if (lhs % 2 != 0 || lhs == 0)
581 return emitError(loc, "invalid AND gate LHS literal");
582
583 // Validate literal bounds
584 if (lhs / 2 > maxVarIndex || rhs0 / 2 > maxVarIndex ||
585 rhs1 / 2 > maxVarIndex)
586 return emitError(loc, "AND gate literal exceeds maximum variable index");
587
588 andGateDefs.push_back({lhs, rhs0, rhs1, loc});
589 }
590
591 return success();
592}
593
594ParseResult AIGERParser::parseAndGatesBinary() {
595 // In binary format, AND gates are encoded with delta compression
596 // Each AND gate is encoded as: delta0 delta1
597 // where: rhs0 = lhs - delta0, rhs1 = rhs0 - delta1
598
599 LLVM_DEBUG(llvm::dbgs() << "Starting binary AND gate parsing\n");
600
601 // First AND gate LHS starts after inputs and latches
602 // Variables are numbered: 1, 2, ..., maxVarIndex
603 // Literals are: 2, 4, 6, ..., 2*maxVarIndex
604 // Inputs: 2, 4, ..., 2*numInputs
605 // Latches: 2*(numInputs+1), 2*(numInputs+2), ..., 2*(numInputs+numLatches)
606 // AND gates: 2*(numInputs+numLatches+1), 2*(numInputs+numLatches+2), ...
607 auto currentLHS = 2 * (numInputs + numLatches + 1);
608
609 LLVM_DEBUG(llvm::dbgs() << "First AND gate LHS should be: " << currentLHS
610 << "\n");
611
612 for (unsigned i = 0; i < numAnds; ++i) {
613 unsigned delta0, delta1;
614 SMLoc loc = lexer.getCurrentLoc();
615 if (parseBinaryNumber(delta0) || parseBinaryNumber(delta1))
616 return emitError(loc, "failed to parse binary AND gate deltas");
617
618 auto lhs = static_cast<int64_t>(currentLHS);
619
620 // Check for underflow before subtraction
621 if (delta0 > lhs || delta1 > (lhs - delta0)) {
622 LLVM_DEBUG(llvm::dbgs() << "Delta underflow: lhs=" << lhs << ", delta0="
623 << delta0 << ", delta1=" << delta1 << "\n");
624 return emitError("invalid binary AND gate: delta causes underflow");
625 }
626
627 auto rhs0 = lhs - delta0;
628 auto rhs1 = rhs0 - delta1;
629
630 LLVM_DEBUG(llvm::dbgs() << "Binary AND Gate " << i << ": " << lhs << " = "
631 << rhs0 << " & " << rhs1 << " (deltas: " << delta0
632 << ", " << delta1 << ")\n");
633
634 if (lhs / 2 > maxVarIndex || rhs0 / 2 > maxVarIndex ||
635 rhs1 / 2 > maxVarIndex)
636 return emitError(
637 "binary AND gate literal exceeds maximum variable index");
638
639 assert(lhs > rhs0 && rhs0 >= rhs1 &&
640 "invalid binary AND gate: ordering constraint violated");
641
642 andGateDefs.push_back({static_cast<unsigned>(lhs),
643 static_cast<unsigned>(rhs0),
644 static_cast<unsigned>(rhs1), loc});
645 currentLHS += 2; // Next AND gate LHS
646 }
647
648 return success();
649}
650
651ParseResult AIGERParser::parseSymbolTable() {
652 // Symbol table is optional and starts with 'i', 'l', or 'o' followed by
653 // position
654 while (!lexer.isAtEOF()) {
655 auto token = lexer.peekToken();
656 if (token.kind != AIGERTokenKind::Identifier)
657 break;
658 (void)lexer.nextToken();
659
660 char symbolType = token.spelling.front();
661 if (symbolType != 'i' && symbolType != 'l' && symbolType != 'o')
662 break;
663
664 unsigned literal;
665 if (token.spelling.drop_front().getAsInteger(10, literal))
666 return emitError("failed to parse symbol position");
667
668 SymbolKind kind;
669 switch (symbolType) {
670 case 'i':
671 kind = SymbolKind::Input;
672 break;
673 case 'l':
674 kind = SymbolKind::Latch;
675 break;
676 case 'o':
677 kind = SymbolKind::Output;
678 break;
679 }
680
681 auto nextToken = lexer.lexAsSymbol();
682 if (nextToken.kind != AIGERTokenKind::Identifier)
683 return emitError("expected symbol name");
684
685 LLVM_DEBUG(llvm::dbgs()
686 << "Symbol " << literal << ": " << nextToken.spelling << "\n");
687
688 symbolTable[{kind, literal}] = StringAttr::get(context, nextToken.spelling);
689 if (parseNewLine())
690 return failure();
691 }
692
693 return success();
694}
695
696ParseResult AIGERParser::parseComments() {
697 // Comments start with 'c' and continue to end of file
698 auto token = lexer.peekToken();
699 if (token.kind == AIGERTokenKind::Identifier && token.spelling == "c") {
700 // Skip comments for now
701 return success();
702 }
703
704 return success();
705}
706
707Value AIGERParser::getLiteralValue(unsigned literal,
708 DenseMap<unsigned, Backedge> &backedges,
709 Location loc) {
710 LLVM_DEBUG(llvm::dbgs() << "Getting value for literal " << literal << "\n");
711
712 // Handle constants
713 if (literal == 0) {
714 // FALSE constant
716 builder, loc, builder.getI1Type(),
717 builder.getIntegerAttr(builder.getI1Type(), 0));
718 }
719
720 if (literal == 1) {
721 // TRUE constant
723 builder, loc, builder.getI1Type(),
724 builder.getIntegerAttr(builder.getI1Type(), 1));
725 }
726
727 // Extract variable and inversion
728 unsigned variable = literal / 2;
729 bool inverted = literal % 2;
730 unsigned baseLiteral = variable * 2;
731
732 LLVM_DEBUG(llvm::dbgs() << " Variable: " << variable
733 << ", inverted: " << inverted
734 << ", baseLiteral: " << baseLiteral << "\n");
735
736 // Validate literal bounds
737 if (variable > maxVarIndex) {
738 LLVM_DEBUG(llvm::dbgs() << " ERROR: Variable " << variable
739 << " exceeds maxVarIndex " << maxVarIndex << "\n");
740 return nullptr;
741 }
742
743 // Look up the backedge for this literal
744 auto backedgeIt = backedges.find(baseLiteral);
745 if (backedgeIt == backedges.end()) {
746 LLVM_DEBUG(llvm::dbgs() << " ERROR: No backedge found for literal "
747 << baseLiteral << "\n");
748 return nullptr; // Error: undefined literal
749 }
750
751 Value baseValue = backedgeIt->second;
752 if (!baseValue) {
753 LLVM_DEBUG(llvm::dbgs() << " ERROR: Backedge value is null for literal "
754 << baseLiteral << "\n");
755 return nullptr;
756 }
757
758 // Apply inversion if needed
759 if (inverted) {
760 // Create an inverter using synth.aig.and_inv with single input
761 SmallVector<bool> inverts = {true};
762 return aig::AndInverterOp::create(builder, loc, builder.getI1Type(),
763 ValueRange{baseValue}, inverts);
764 }
765
766 return baseValue;
767}
768
769ParseResult AIGERParser::createModule() {
770
771 // Create the top-level module
772 std::string moduleName = options.topLevelModule;
773 if (moduleName.empty())
774 moduleName = "aiger_top";
775
776 // Set insertion point to the provided module
777 builder.setInsertionPointToStart(module.getBody());
778
779 // Build input/output port info
780 SmallVector<hw::PortInfo> ports;
781
782 // Add input ports
783 for (unsigned i = 0; i < numInputs; ++i) {
784 hw::PortInfo port;
785 auto name = symbolTable.lookup({SymbolKind::Input, i});
786 port.name =
787 name ? name : builder.getStringAttr("input_" + std::to_string(i));
788 port.type = builder.getI1Type();
789 port.dir = hw::ModulePort::Direction::Input;
790 port.argNum = i;
791 ports.push_back(port);
792 }
793
794 // Add output ports
795 for (unsigned i = 0; i < numOutputs; ++i) {
796 hw::PortInfo port;
797 auto name = symbolTable.lookup({SymbolKind::Output, i});
798 port.name =
799 name ? name : builder.getStringAttr("output_" + std::to_string(i));
800 port.type = builder.getI1Type();
801 port.dir = hw::ModulePort::Direction::Output;
802 port.argNum = numInputs + i;
803 ports.push_back(port);
804 }
805
806 // Add clock port if we have latches
807 if (numLatches > 0) {
808 hw::PortInfo clockPort;
809 clockPort.name = builder.getStringAttr("clock");
810 clockPort.type = seq::ClockType::get(builder.getContext());
811 clockPort.dir = hw::ModulePort::Direction::Input;
812 clockPort.argNum = numInputs + numOutputs;
813 ports.push_back(clockPort);
814 }
815
816 // Create the HW module
817 auto hwModule =
818 hw::HWModuleOp::create(builder, builder.getUnknownLoc(),
819 builder.getStringAttr(moduleName), ports);
820
821 // Set insertion point inside the module
822 builder.setInsertionPointToStart(hwModule.getBodyBlock());
823
824 // Get clock value if we have latches
825 Value clockValue;
826 if (numLatches > 0)
827 clockValue = hwModule.getBodyBlock()->getArgument(numInputs);
828
829 // Use BackedgeBuilder to handle all values uniformly
830 BackedgeBuilder bb(builder, builder.getUnknownLoc());
831 DenseMap<unsigned, Backedge> backedges;
832
833 // Create backedges for all literals (inputs, latches, AND gates)
834 for (unsigned i = 0; i < numInputs; ++i) {
835 auto literal = inputLiterals[i];
836 backedges[literal] = bb.get(builder.getI1Type());
837 }
838 for (auto [currentState, nextState, _] : latchDefs)
839 backedges[currentState] = bb.get(builder.getI1Type());
840
841 for (auto [lhs, rhs0, rhs1, loc] : andGateDefs)
842 backedges[lhs] = bb.get(builder.getI1Type());
843
844 // Set input values
845 for (unsigned i = 0; i < numInputs; ++i) {
846 auto inputValue = hwModule.getBodyBlock()->getArgument(i);
847 auto literal = inputLiterals[i];
848 backedges[literal].setValue(inputValue);
849 }
850
851 // Create latches (registers) with backedges for next state
852 for (auto [i, latchDef] : llvm::enumerate(latchDefs)) {
853 auto [currentState, nextState, loc] = latchDef;
854 // Get the backedge for the next state value
855 auto nextBackedge = bb.get(builder.getI1Type());
856
857 // Create the register with the backedge as input
858 auto regValue = seq::CompRegOp::create(
859 builder, lexer.translateLocation(loc), (Value)nextBackedge, clockValue);
860 if (auto name = symbolTable.lookup({SymbolKind::Latch, i}))
861 regValue.setNameAttr(name);
862
863 // Set the backedge for this latch's current state
864 backedges[currentState].setValue(regValue);
865 }
866
867 // Build AND gates using backedges to handle forward references
868 for (auto [lhs, rhs0, rhs1, loc] : andGateDefs) {
869 // Get or create backedges for operands
870 auto location = lexer.translateLocation(loc);
871 auto rhs0Value = getLiteralValue(rhs0 & ~1u, backedges, location);
872 auto rhs1Value = getLiteralValue(rhs1 & ~1u, backedges, location);
873
874 if (!rhs0Value || !rhs1Value)
875 return emitError(loc, "failed to get operand values for AND gate");
876
877 // Determine inversion for inputs
878 SmallVector<bool> inverts = {static_cast<bool>(rhs0 % 2),
879 static_cast<bool>(rhs1 % 2)};
880
881 // Create AND gate with potential inversions
882 auto andResult =
883 aig::AndInverterOp::create(builder, location, builder.getI1Type(),
884 ValueRange{rhs0Value, rhs1Value}, inverts);
885
886 // Set the backedge for this AND gate's result
887 backedges[lhs].setValue(andResult);
888 }
889
890 // Now resolve the latch next state connections.
891 // We need to update the CompRegOp operations with their actual next state
892 // values
893 for (auto [currentState, nextState, sourceLoc] : latchDefs) {
894 auto loc = lexer.translateLocation(sourceLoc);
895 auto nextValue = getLiteralValue(nextState, backedges, loc);
896 if (!nextValue)
897 return emitError(sourceLoc, "undefined literal in latch next state");
898
899 // Find the register operation for this latch and update its input
900 Value currentValue = backedges[currentState];
901 if (auto regOp = currentValue.getDefiningOp<seq::CompRegOp>())
902 regOp.getInputMutable().assign(nextValue);
903 else
904 return emitError(sourceLoc, "failed to find register for latch");
905 }
906
907 // Create output values
908 SmallVector<Value> outputValues;
909 for (auto [literal, sourceLoc] : outputLiterals) {
910 auto loc = lexer.translateLocation(sourceLoc);
911 auto outputValue = getLiteralValue(literal, backedges, loc);
912 if (!outputValue)
913 return emitError(sourceLoc, "undefined literal in output");
914 outputValues.push_back(outputValue);
915 }
916
917 // Create output operation
918 auto *outputOp = hwModule.getBodyBlock()->getTerminator();
919 outputOp->setOperands(outputValues);
920
921 return success();
922}
923
924//===----------------------------------------------------------------------===//
925// Public API Implementation
926//===----------------------------------------------------------------------===//
927
928LogicalResult circt::aiger::importAIGER(llvm::SourceMgr &sourceMgr,
929 MLIRContext *context,
930 mlir::TimingScope &ts, ModuleOp module,
931 const ImportAIGEROptions *options) {
932 // Load required dialects
933 context->loadDialect<hw::HWDialect>();
934 context->loadDialect<synth::SynthDialect>();
935 context->loadDialect<seq::SeqDialect>();
936
937 // Use default options if none provided
938 ImportAIGEROptions defaultOptions;
939 if (!options)
940 options = &defaultOptions;
941
942 // Create parser and parse the file
943 AIGERParser parser(sourceMgr, context, module, *options);
944 return parser.parse();
945}
946
947//===----------------------------------------------------------------------===//
948// Translation Registration
949//===----------------------------------------------------------------------===//
950
952 static mlir::TranslateToMLIRRegistration fromAIGER(
953 "import-aiger", "import AIGER file",
954 [](llvm::SourceMgr &sourceMgr, MLIRContext *context) {
955 mlir::TimingScope ts;
957 ModuleOp::create(UnknownLoc::get(context)));
958 ImportAIGEROptions options;
959 if (failed(importAIGER(sourceMgr, context, ts, module.get(), &options)))
960 module = {};
961 return module;
962 });
963}
assert(baseType &&"element must be base type")
static StringAttr getMainBufferNameIdentifier(const llvm::SourceMgr &sourceMgr, MLIRContext *context)
Definition FIRLexer.cpp:150
@ Input
Definition HW.h:35
@ Output
Definition HW.h:35
Instantiate one of these and use it to build typed backedges.
create(data_type, value)
Definition hw.py:433
create(cls, result_type, reset=None, reset_value=None, name=None, sym_name=None, **kwargs)
Definition seq.py:157
mlir::LogicalResult importAIGER(llvm::SourceMgr &sourceMgr, mlir::MLIRContext *context, mlir::TimingScope &ts, mlir::ModuleOp module, const ImportAIGEROptions *options=nullptr)
Parse an AIGER file and populate the given MLIR module with corresponding AIG dialect operations.
void registerImportAIGERTranslation()
Register the import-aiger MLIR translation.
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
Definition CalyxOps.cpp:55
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Options for AIGER import.
Definition ImportAIGER.h:29
mlir::Type type
Definition HWTypes.h:31
mlir::StringAttr name
Definition HWTypes.h:30
This holds the name, type, direction of a module's ports.
size_t argNum
This is the argument index or the result index depending on the direction.