16#include "llvm/ADT/PostOrderIterator.h"
17#include "llvm/Support/FileSystem.h"
18#include "llvm/Support/Path.h"
20#define DEBUG_TYPE "firrtl-assign-output-dirs"
24#define GEN_PASS_DEF_ASSIGNOUTPUTDIRS
25#include "circt/Dialect/FIRRTL/Passes.h.inc"
30using namespace firrtl;
31namespace path = llvm::sys::path;
32namespace fs = llvm::sys::fs;
34using hw::OutputFileAttr;
39 SmallString<64> &moduleOutputDir) {
40 auto sep = llvm::sys::path::get_separator();
41 if (!moduleOutputDir.empty())
42 assert(moduleOutputDir.ends_with(sep));
43 fs::make_absolute(outputDir, moduleOutputDir);
44 path::remove_dots(moduleOutputDir,
true);
45 moduleOutputDir += sep;
51 SmallString<64> &moduleOutputDir) {
52 if (moduleOutputDir.starts_with(outputDir))
53 moduleOutputDir.erase(moduleOutputDir.begin(),
54 moduleOutputDir.begin() + outputDir.size());
58 OutputFileAttr attr) {
60 SmallString<64> b(attr.getDirectory());
69 return op->getAttrOfType<hw::OutputFileAttr>(
"output_file");
73struct AssignOutputDirsPass
74 :
public circt::firrtl::impl::AssignOutputDirsBase<AssignOutputDirsPass> {
75 AssignOutputDirsPass(StringRef outputDir) {
76 if (!outputDir.empty())
77 outputDirOption = std::string(outputDir);
80 void runOnOperation()
override;
84void AssignOutputDirsPass::runOnOperation() {
86 SmallString<64> outputDir(outputDirOption);
87 if (fs::make_absolute(outputDir)) {
88 emitError(mlir::UnknownLoc::get(&getContext()),
89 "failed to convert the output directory to an absolute path");
93 path::remove_dots(outputDir,
true);
94 auto sep = path::get_separator();
95 if (!outputDir.ends_with(sep))
96 outputDir.append(sep);
100 LLVM_DEBUG(llvm::dbgs() <<
"Updating modules:\n");
101 DenseSet<InstanceGraphNode *> visited;
103 for (
auto *node :
llvm::inverse_post_order_ext(root, visited)) {
104 FModuleLike moduleLike =
105 dyn_cast<FModuleLike>(node->getModule().getOperation());
106 if (!moduleLike || !isa<FModuleOp, FExtModuleOp>(moduleLike))
108 if (moduleLike->getAttrOfType<hw::OutputFileAttr>(
"output_file") ||
109 moduleLike.isPublic())
115 SmallString<64> moduleOutputDir;
116 auto i = node->usesBegin();
117 auto e = node->usesEnd();
118 for (; i != e; ++i) {
119 auto parent = (*i)->getParent()->getModule();
122 moduleOutputDir = file.getDirectory();
125 moduleOutputDir = outputDir;
130 for (; i != e; ++i) {
131 auto parent = (*i)->getParent()->getModule();
136 if (!moduleOutputDir.empty()) {
138 hw::OutputFileAttr::getAsDirectory(&getContext(), moduleOutputDir);
139 moduleLike->setAttr(
"output_file", f);
142 llvm::dbgs() <<
" - name: " << moduleLike.getName() <<
"\n"
143 <<
" directory: " << f.getFilename() <<
"\n";
150 markAllAnalysesPreserved();
154std::unique_ptr<mlir::Pass>
156 return std::make_unique<AssignOutputDirsPass>(outputDir);
static void makeAbsolute(StringRef outputDir, SmallString< 64 > &moduleOutputDir)
static void tryMakeRelative(StringRef outputDir, SmallString< 64 > &moduleOutputDir)
static OutputFileAttr getOutputFile(igraph::ModuleOpInterface op)
assert(baseType &&"element must be base type")
This graph tracks modules and where they are instantiated.
std::unique_ptr< mlir::Pass > createAssignOutputDirsPass(mlir::StringRef outputDir="")
void makeCommonPrefix(SmallString< 64 > &a, StringRef b)
Truncate a to the common prefix of a and b.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
llvm::raw_ostream & debugPassHeader(const mlir::Pass *pass, int width=80)
Write a boilerplate header for a pass to the debug stream.
llvm::raw_ostream & debugFooter(int width=80)
Write a boilerplate footer to the debug stream to indicate that a pass has ended.