@@ -206,6 +206,12 @@ static cl::list<std::string> inputAnnotationFilenames(
206
206
" annotation-file" , cl::desc(" Optional input annotation file" ),
207
207
cl::CommaSeparated, cl::value_desc(" filename" ), cl::cat(mainCategory));
208
208
209
+ static cl::opt<std::string>
210
+ hwOutFile (" output-hw-mlir" ,
211
+ cl::desc (" Optional file name to output the HW IR into, in "
212
+ " addition to the output requested by -o" ),
213
+ cl::init(" " ), cl::value_desc(" filename" ), cl::cat(mainCategory));
214
+
209
215
static cl::opt<std::string>
210
216
mlirOutFile (" output-final-mlir" ,
211
217
cl::desc (" Optional file name to output the final MLIR into, in "
@@ -336,6 +342,38 @@ struct EmitSplitHGLDDPass
336
342
}
337
343
};
338
344
345
+ // / Wrapper pass to dump IR.
346
+ struct DumpIRPass
347
+ : public PassWrapper<DumpIRPass, OperationPass<mlir::ModuleOp>> {
348
+ DumpIRPass (const std::string &outputFile)
349
+ : PassWrapper<DumpIRPass, OperationPass<mlir::ModuleOp>>() {
350
+ this ->outputFile .setValue (outputFile);
351
+ }
352
+
353
+ DumpIRPass (const DumpIRPass &other) : PassWrapper(other) {
354
+ outputFile.setValue (other.outputFile .getValue ());
355
+ }
356
+
357
+ void runOnOperation () override {
358
+ assert (!outputFile.empty ());
359
+
360
+ std::string error;
361
+ auto mlirFile = openOutputFile (outputFile.getValue (), &error);
362
+ if (!mlirFile) {
363
+ errs () << error;
364
+ return signalPassFailure ();
365
+ }
366
+
367
+ if (failed (printOp (getOperation (), mlirFile->os ())))
368
+ return signalPassFailure ();
369
+ mlirFile->keep ();
370
+ markAllAnalysesPreserved ();
371
+ }
372
+
373
+ Pass::Option<std::string> outputFile{*this , " output-file" ,
374
+ cl::desc (" filename" ), cl::init (" -" )};
375
+ };
376
+
339
377
// / Process a single buffer of the input.
340
378
static LogicalResult processBuffer (
341
379
MLIRContext &context, firtool::FirtoolOptions &firtoolOptions,
@@ -456,6 +494,11 @@ static LogicalResult processBuffer(
456
494
if (failed (firtool::populateHWToBTOR2 (pm, firtoolOptions,
457
495
(*outputFile)->os ())))
458
496
return failure ();
497
+
498
+ // If requested, emit the HW IR to hwOutFile.
499
+ if (!hwOutFile.empty ())
500
+ pm.addPass (std::make_unique<DumpIRPass>(hwOutFile.getValue ()));
501
+
459
502
if (outputFormat != OutputIRHW)
460
503
if (failed (firtool::populateHWToSV (pm, firtoolOptions)))
461
504
return failure ();
@@ -504,11 +547,15 @@ static LogicalResult processBuffer(
504
547
break ;
505
548
}
506
549
507
- // Run final IR mutations to clean it up after ExportVerilog and before
508
- // emitting the final MLIR.
509
- if (!mlirOutFile.empty ())
550
+ // If requested, print the final MLIR into mlirOutFile.
551
+ if (!mlirOutFile.empty ()) {
552
+ // Run final IR mutations to clean it up after ExportVerilog and before
553
+ // emitting the final MLIR.
510
554
if (failed (firtool::populateFinalizeIR (pm, firtoolOptions)))
511
555
return failure ();
556
+
557
+ pm.addPass (std::make_unique<DumpIRPass>(mlirOutFile.getValue ()));
558
+ }
512
559
}
513
560
514
561
if (failed (pm.run (module .get ())))
@@ -521,20 +568,6 @@ static LogicalResult processBuffer(
521
568
return failure ();
522
569
}
523
570
524
- // If requested, print the final MLIR into mlirOutFile.
525
- if (!mlirOutFile.empty ()) {
526
- std::string mlirOutError;
527
- auto mlirFile = openOutputFile (mlirOutFile, &mlirOutError);
528
- if (!mlirFile) {
529
- llvm::errs () << mlirOutError;
530
- return failure ();
531
- }
532
-
533
- if (failed (printOp (*module , mlirFile->os ())))
534
- return failure ();
535
- mlirFile->keep ();
536
- }
537
-
538
571
// We intentionally "leak" the Module into the MLIRContext instead of
539
572
// deallocating it. There is no need to deallocate it right before process
540
573
// exit.
0 commit comments