forked from llvm/circt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneratorCallout.cpp
More file actions
171 lines (153 loc) · 6.3 KB
/
GeneratorCallout.cpp
File metadata and controls
171 lines (153 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//===- GeneratorCallout.cpp - Generator Callout Pass ----------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Call arbitrary programs and pass them the attributes attached to external
// modules.
//
//===----------------------------------------------------------------------===//
#include "circt/Dialect/HW/HWOps.h"
#include "circt/Dialect/SV/SVOps.h"
#include "circt/Dialect/SV/SVPasses.h"
#include "mlir/IR/Builders.h"
#include "mlir/Pass/Pass.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Process.h"
namespace circt {
namespace sv {
#define GEN_PASS_DEF_HWGENERATORCALLOUTPASS
#include "circt/Dialect/SV/SVPasses.h.inc"
} // namespace sv
} // namespace circt
using namespace circt;
using namespace sv;
using namespace hw;
//===----------------------------------------------------------------------===//
// GeneratorCalloutPass
//===----------------------------------------------------------------------===//
namespace {
struct HWGeneratorCalloutPass
: public circt::sv::impl::HWGeneratorCalloutPassBase<
HWGeneratorCalloutPass> {
using Base::Base;
void runOnOperation() override;
void processGenerator(HWModuleGeneratedOp generatedModuleOp,
StringRef generatorExe,
ArrayRef<StringRef> extraGeneratorArgs);
};
} // end anonymous namespace
void HWGeneratorCalloutPass::runOnOperation() {
ModuleOp root = getOperation();
SmallVector<StringRef> genOptions;
StringRef extraGeneratorArgs(genExecArgs);
extraGeneratorArgs.split(genOptions, ';');
SmallString<32> execName = llvm::sys::path::filename(genExecutable);
SmallString<32> execPath = llvm::sys::path::parent_path(genExecutable);
auto generatorExe = llvm::sys::findProgramByName(execName, {execPath});
// If program not found, search it in $PATH.
if (!generatorExe)
generatorExe = llvm::sys::findProgramByName(execName);
// If cannot find the executable, then nothing to do, return.
if (!generatorExe) {
root.emitError("cannot find executable '" + execName + "' in path '" +
execPath + "'");
return;
}
for (auto &op : llvm::make_early_inc_range(root.getBody()->getOperations())) {
if (auto generator = dyn_cast<HWModuleGeneratedOp>(op))
processGenerator(generator, *generatorExe, extraGeneratorArgs);
}
}
void HWGeneratorCalloutPass::processGenerator(
HWModuleGeneratedOp generatedModuleOp, StringRef generatorExe,
ArrayRef<StringRef> extraGeneratorArgs) {
// Get the corresponding schema associated with this generated op.
auto genSchema =
dyn_cast<HWGeneratorSchemaOp>(generatedModuleOp.getGeneratorKindOp());
if (!genSchema)
return;
// Ignore the generator op if the schema does not match the user specified
// schema name from command line "-schema-name"
if (genSchema.getDescriptor().str() != schemaName)
return;
SmallVector<std::string> generatorArgs;
// First argument should be the executable name.
generatorArgs.push_back(generatorExe.str());
for (auto o : extraGeneratorArgs)
generatorArgs.push_back(o.str());
auto moduleName =
generatedModuleOp.getVerilogModuleNameAttr().getValue().str();
// The moduleName option is not present in the schema, so add it
// explicitly.
generatorArgs.push_back("--moduleName");
generatorArgs.push_back(moduleName);
// Iterate over all the attributes in the schema.
// Assumption: All the options required by the generator program must be
// present in the schema.
for (auto attr : genSchema.getRequiredAttrs()) {
auto sAttr = cast<StringAttr>(attr);
// Get the port name from schema.
StringRef portName = sAttr.getValue();
generatorArgs.push_back("--" + portName.str());
// Get the value for the corresponding port name.
auto v = generatedModuleOp->getAttr(portName);
if (auto intV = dyn_cast<IntegerAttr>(v))
generatorArgs.push_back(std::to_string(intV.getValue().getZExtValue()));
else if (auto strV = dyn_cast<StringAttr>(v))
generatorArgs.push_back(strV.getValue().str());
else {
generatedModuleOp.emitError(
"portname attribute " + portName +
" value specified on the rtl.module.generated operation is not "
"handled, "
"only integer and string types supported.");
return;
}
}
SmallVector<StringRef> generatorArgStrRef;
for (const std::string &a : generatorArgs)
generatorArgStrRef.push_back(a);
std::string errMsg;
SmallString<32> genExecOutFileName;
auto errCode = llvm::sys::fs::getPotentiallyUniqueTempFileName(
"generatorCalloutTemp", StringRef(""), genExecOutFileName);
// Default error code is 0.
std::error_code ok;
if (errCode != ok) {
generatedModuleOp.emitError("cannot generate a unique temporary file name");
return;
}
std::optional<StringRef> redirects[] = {
std::nullopt, StringRef(genExecOutFileName), std::nullopt};
int result = llvm::sys::ExecuteAndWait(
generatorExe, generatorArgStrRef, /*Env=*/std::nullopt,
/*Redirects=*/redirects,
/*SecondsToWait=*/0, /*MemoryLimit=*/0, &errMsg);
if (result != 0) {
generatedModuleOp.emitError("execution of '" + generatorExe + "' failed");
return;
}
auto bufferRead = llvm::MemoryBuffer::getFile(genExecOutFileName);
if (!bufferRead || !*bufferRead) {
generatedModuleOp.emitError("execution of '" + generatorExe +
"' did not produce any output file named '" +
genExecOutFileName + "'");
return;
}
// Only extract the first line from the output.
auto fileContent = (*bufferRead)->getBuffer().split('\n').first.str();
OpBuilder builder(generatedModuleOp);
auto extMod =
hw::HWModuleExternOp::create(builder, generatedModuleOp.getLoc(),
generatedModuleOp.getVerilogModuleNameAttr(),
generatedModuleOp.getPortList());
// Attach an attribute to which file the definition of the external
// module exists in.
extMod->setAttr("filenames", builder.getStringAttr(fileContent));
generatedModuleOp.erase();
}