CIRCT 22.0.0git
Loading...
Searching...
No Matches
SimpleTestInlinerPass.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
12#include "mlir/IR/PatternMatch.h"
13
14namespace circt {
15namespace rtg {
16#define GEN_PASS_DEF_SIMPLETESTINLINERPASS
17#include "circt/Dialect/RTG/Transforms/RTGPasses.h.inc"
18} // namespace rtg
19} // namespace circt
20
21using namespace mlir;
22using namespace circt;
23using namespace circt::rtg;
24
25//===----------------------------------------------------------------------===//
26// Simple Test Inliner Pass
27//===----------------------------------------------------------------------===//
28
29namespace {
30struct SimpleTestInlinerPass
31 : public rtg::impl::SimpleTestInlinerPassBase<SimpleTestInlinerPass> {
32 using Base::Base;
33 void runOnOperation() override;
34};
35} // namespace
36
37void SimpleTestInlinerPass::runOnOperation() {
38 const auto &symTbl = getAnalysis<SymbolTable>();
39 IRRewriter rewriter(getOperation());
40
41 for (auto fileOp : getOperation().getOps<emit::FileOp>()) {
42 for (auto refOp :
43 llvm::make_early_inc_range(fileOp.getOps<emit::RefOp>())) {
44 auto testOp = symTbl.lookup<TestOp>(refOp.getTargetAttr().getAttr());
45 if (!testOp) {
46 refOp.emitError("invalid symbol reference: ") << refOp.getTargetAttr();
47 return signalPassFailure();
48 }
49
50 bool allArgsUnused =
51 llvm::all_of(testOp.getBody()->getArguments(),
52 [](auto arg) { return arg.use_empty(); });
53 if (!allArgsUnused) {
54 testOp->emitError("cannot inline test with used arguments");
55 return signalPassFailure();
56 }
57
58 testOp.getBody()->eraseArguments(0, testOp.getBody()->getNumArguments());
59 rewriter.setInsertionPoint(refOp);
60 CommentOp::create(rewriter, refOp->getLoc(),
61 rewriter.getStringAttr("Begin of test '" +
62 testOp.getSymName() + "'"));
63 auto newTestOp = cast<TestOp>(testOp->clone());
64 rewriter.inlineBlockBefore(newTestOp.getBody(), refOp, {});
65 CommentOp::create(
66 rewriter, refOp->getLoc(),
67 rewriter.getStringAttr("End of test '" + testOp.getSymName() + "'"));
68 newTestOp.erase();
69 refOp.erase();
70 }
71 }
72
73 for (auto &op : llvm::make_early_inc_range(getOperation().getOps()))
74 if (isa<TargetOp, TestOp>(&op))
75 op.erase();
76}
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition emit.py:1
Definition rtg.py:1