Loading [MathJax]/extensions/tex2jax.js
CIRCT 21.0.0git
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AssignOutputDirs.cpp
Go to the documentation of this file.
1//===- AssignOutputDirs.cpp - Assign Output Directories ---------*- C++ -*-===//
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
15#include "circt/Support/Debug.h"
16#include "llvm/ADT/PostOrderIterator.h"
17#include "llvm/Support/FileSystem.h"
18#include "llvm/Support/Path.h"
19
20#define DEBUG_TYPE "firrtl-assign-output-dirs"
21
22namespace circt {
23namespace firrtl {
24#define GEN_PASS_DEF_ASSIGNOUTPUTDIRS
25#include "circt/Dialect/FIRRTL/Passes.h.inc"
26} // namespace firrtl
27} // namespace circt
28
29using namespace circt;
30using namespace firrtl;
31namespace path = llvm::sys::path;
32namespace fs = llvm::sys::fs;
33
34using hw::OutputFileAttr;
35
36// If moduleOutputDir is a relative path, convert it to an absolute path, by
37// interpreting moduleOutputDir as relative to the outputDir.
38static void makeAbsolute(StringRef outputDir,
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;
46}
47
48// If outputDir is a prefix of moduleOutputDir, then make moduleOutputDir
49// relative to outputDir. Otherwise, leave moduleOutputDir as absolute.
50static void tryMakeRelative(StringRef outputDir,
51 SmallString<64> &moduleOutputDir) {
52 if (moduleOutputDir.starts_with(outputDir))
53 moduleOutputDir.erase(moduleOutputDir.begin(),
54 moduleOutputDir.begin() + outputDir.size());
55}
56
57static void makeCommonPrefix(StringRef outputDir, SmallString<64> &a,
58 OutputFileAttr attr) {
59 if (attr) {
60 SmallString<64> b(attr.getDirectory());
61 makeAbsolute(outputDir, b);
62 makeCommonPrefix(a, b);
63 } else {
64 makeCommonPrefix(a, outputDir);
65 }
66}
67
68static OutputFileAttr getOutputFile(igraph::ModuleOpInterface op) {
69 return op->getAttrOfType<hw::OutputFileAttr>("output_file");
70}
71
72namespace {
73struct AssignOutputDirsPass
74 : public circt::firrtl::impl::AssignOutputDirsBase<AssignOutputDirsPass> {
75 AssignOutputDirsPass(StringRef outputDir) {
76 if (!outputDir.empty())
77 outputDirOption = std::string(outputDir);
78 }
79
80 void runOnOperation() override;
81};
82} // namespace
83
84void AssignOutputDirsPass::runOnOperation() {
85 LLVM_DEBUG(debugPassHeader(this) << "\n");
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");
90 signalPassFailure();
91 return;
92 }
93 path::remove_dots(outputDir, true);
94 auto sep = path::get_separator();
95 if (!outputDir.ends_with(sep))
96 outputDir.append(sep);
97
98 bool changed = false;
99
100 LLVM_DEBUG(llvm::dbgs() << "Updating modules:\n");
101 DenseSet<InstanceGraphNode *> visited;
102 for (auto *root : getAnalysis<InstanceGraph>()) {
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))
107 continue;
108 if (moduleLike->getAttrOfType<hw::OutputFileAttr>("output_file") ||
109 moduleLike.isPublic())
110 continue;
111
112 // Get the output directory of the first parent, and then fold the current
113 // output directory with the LCA of all other discovered output
114 // directories.
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();
120 auto file = getOutputFile(parent);
121 if (file) {
122 moduleOutputDir = file.getDirectory();
123 makeAbsolute(outputDir, moduleOutputDir);
124 } else {
125 moduleOutputDir = outputDir;
126 }
127 ++i;
128 break;
129 }
130 for (; i != e; ++i) {
131 auto parent = (*i)->getParent()->getModule();
132 makeCommonPrefix(outputDir, moduleOutputDir, getOutputFile(parent));
133 }
134
135 tryMakeRelative(outputDir, moduleOutputDir);
136 if (!moduleOutputDir.empty()) {
137 auto f =
138 hw::OutputFileAttr::getAsDirectory(&getContext(), moduleOutputDir);
139 moduleLike->setAttr("output_file", f);
140 changed = true;
141 LLVM_DEBUG({
142 llvm::dbgs() << " - name: " << moduleLike.getName() << "\n"
143 << " directory: " << f.getFilename() << "\n";
144 });
145 }
146 }
147 }
148
149 if (!changed)
150 markAllAnalysesPreserved();
151 LLVM_DEBUG(debugFooter() << "\n");
152}
153
154std::unique_ptr<mlir::Pass>
156 return std::make_unique<AssignOutputDirsPass>(outputDir);
157}
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.
Definition Debug.cpp:31
llvm::raw_ostream & debugFooter(int width=80)
Write a boilerplate footer to the debug stream to indicate that a pass has ended.
Definition Debug.cpp:35