Skip to content

Commit 83fb49d

Browse files
committed
Add ImportVerilogDebugStream
1 parent 09f0bbe commit 83fb49d

File tree

5 files changed

+130
-0
lines changed

5 files changed

+130
-0
lines changed

lib/Conversion/ImportVerilog/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
include(SlangCompilerOptions)
22

33
add_circt_translation_library(CIRCTImportVerilog
4+
ImportVerilogDebugStream.cpp
45
AssertionExpr.cpp
56
Expressions.cpp
67
FormatStrings.cpp

lib/Conversion/ImportVerilog/ImportVerilog.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "slang/parsing/Preprocessor.h"
2424
#include "slang/syntax/SyntaxPrinter.h"
2525
#include "slang/util/VersionInfo.h"
26+
#include "ImportVerilogDebugStream.h"
2627

2728
using namespace mlir;
2829
using namespace circt;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "ImportVerilogDebugStream.h"
2+
#include "ImportVerilogInternals.h"
3+
4+
ImportVerilogDebugStream
5+
dbgs(const std::optional<mlir::Location> sourceLocation,
6+
const std::optional<std::source_location> cl) {
7+
8+
return ImportVerilogDebugStream(sourceLocation, cl);
9+
}
10+
11+
ImportVerilogDebugStream circt::ImportVerilog::Context::dbgs(
12+
const std::optional<std::variant<mlir::Location, slang::SourceLocation>>
13+
sourceLocation,
14+
const std::optional<std::source_location> cl) {
15+
16+
std::optional<mlir::Location> mlirLoc = {};
17+
18+
if (sourceLocation) {
19+
if (auto *ml = std::get_if<mlir::Location>(&*sourceLocation)) {
20+
mlirLoc = *ml;
21+
} else if (auto *sl =
22+
std::get_if<slang::SourceLocation>(&*sourceLocation)) {
23+
mlirLoc = convertLocation(*sl);
24+
}
25+
}
26+
27+
return ImportVerilogDebugStream(mlirLoc, cl);
28+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
//===- ImportVerilogDebugStream.h - A debug stream wrapper for importverilog
2+
//---------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef IMPORT_VERILOG_DEBUG_H
11+
#define IMPORT_VERILOG_DEBUG_H
12+
13+
#include "mlir/IR/Location.h"
14+
#include "slang/ast/Compilation.h"
15+
#include "llvm/ADT/SmallString.h"
16+
#include "llvm/Support/Debug.h"
17+
#include "llvm/Support/raw_ostream.h"
18+
#include <optional>
19+
#include <source_location>
20+
#include <utility>
21+
22+
// If includer defined DEBUG_TYPE, use that; else fall back to our default.
23+
#ifndef IMPORTVERILOG_DEBUG_TYPE
24+
#ifdef DEBUG_TYPE
25+
#define IMPORTVERILOG_DEBUG_TYPE DEBUG_TYPE
26+
#else
27+
#define IMPORTVERILOG_DEBUG_TYPE "import-verilog"
28+
#endif
29+
#endif
30+
31+
struct ImportVerilogDebugStream {
32+
std::optional<mlir::Location> srcLoc;
33+
std::optional<std::source_location> compLoc;
34+
llvm::SmallString<128> buffer;
35+
llvm::raw_svector_ostream os;
36+
37+
ImportVerilogDebugStream(std::optional<mlir::Location> sl,
38+
std::optional<std::source_location> cl)
39+
: srcLoc(sl), compLoc(cl), os(buffer) {}
40+
41+
inline ~ImportVerilogDebugStream() {
42+
DEBUG_WITH_TYPE(IMPORTVERILOG_DEBUG_TYPE, {
43+
if(compLoc)
44+
llvm::dbgs() << compLoc->file_name() << ":" << compLoc->line() << " ("
45+
<< compLoc->function_name() << ") ";
46+
if (srcLoc) {
47+
srcLoc->print(llvm::dbgs());
48+
llvm::dbgs() << ": ";
49+
}
50+
llvm::dbgs() << buffer << "\n";
51+
});
52+
}
53+
54+
inline llvm::raw_ostream &stream() { return os; }
55+
inline const llvm::raw_ostream &stream() const { return os; }
56+
};
57+
58+
// lvalue wrapper
59+
template <typename T>
60+
inline ImportVerilogDebugStream &operator<<(ImportVerilogDebugStream &s,
61+
const T &v) {
62+
s.stream() << v;
63+
return s;
64+
}
65+
66+
// rvalue/temporary wrapper
67+
template <typename T>
68+
inline ImportVerilogDebugStream &&operator<<(ImportVerilogDebugStream &&s,
69+
const T &v) {
70+
s.stream() << v;
71+
return std::move(s);
72+
}
73+
74+
// support ostream manipulators that look like functions, e.g. write_hex, etc.
75+
inline ImportVerilogDebugStream &
76+
operator<<(ImportVerilogDebugStream &s,
77+
llvm::raw_ostream &(*manip)(llvm::raw_ostream &)) {
78+
manip(s.stream());
79+
return s;
80+
}
81+
inline ImportVerilogDebugStream &&
82+
operator<<(ImportVerilogDebugStream &&s,
83+
llvm::raw_ostream &(*manip)(llvm::raw_ostream &)) {
84+
manip(s.stream());
85+
return std::move(s);
86+
}
87+
88+
/// Helper function to set up a debug stream with reasonable defaults
89+
ImportVerilogDebugStream dbgs(
90+
std::optional<mlir::Location> sourceLocation = {},
91+
std::optional<std::source_location> cl = std::source_location::current());
92+
93+
#endif // IMPORT_VERILOG_DEBUG_H

lib/Conversion/ImportVerilog/ImportVerilogInternals.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#ifndef CONVERSION_IMPORTVERILOG_IMPORTVERILOGINTERNALS_H
1111
#define CONVERSION_IMPORTVERILOG_IMPORTVERILOGINTERNALS_H
1212

13+
#include "ImportVerilogDebugStream.h"
1314
#include "circt/Conversion/ImportVerilog.h"
1415
#include "circt/Dialect/Debug/DebugOps.h"
1516
#include "circt/Dialect/HW/HWOps.h"
@@ -94,6 +95,12 @@ struct Context {
9495
/// Convert a slang `SourceRange` into an MLIR `Location`.
9596
Location convertLocation(slang::SourceRange range);
9697

98+
/// A convenient debug stream wrapper which attaches information about input source location and compiler source location; also accepts slang::SourceLocation and tries to unwrap it.
99+
ImportVerilogDebugStream dbgs(
100+
std::optional<std::variant<mlir::Location, slang::SourceLocation>>
101+
sourceLocation = {},
102+
std::optional<std::source_location> cl = std::source_location::current());
103+
97104
/// Convert a slang type into an MLIR type. Returns null on failure. Uses the
98105
/// provided location for error reporting, or tries to guess one from the
99106
/// given type. Types tend to have unreliable location information, so it's

0 commit comments

Comments
 (0)