CIRCT 24.0.0git
Loading...
Searching...
No Matches
LowerSizeOf.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//
9// This file defines the LowerSizeOf pass for lowering FIRRTL sizeof intrinsic
10// operations to constants.
11//
12//===----------------------------------------------------------------------===//
13
17#include "mlir/Pass/Pass.h"
18
19namespace circt {
20namespace firrtl {
21#define GEN_PASS_DEF_LOWERSIZEOF
22#include "circt/Dialect/FIRRTL/Passes.h.inc"
23} // namespace firrtl
24} // namespace circt
25
26using namespace circt;
27using namespace firrtl;
28
29namespace {
30class LowerSizeOfPass
31 : public circt::firrtl::impl::LowerSizeOfBase<LowerSizeOfPass> {
32 using Base::Base;
33
34 void runOnOperation() override {
35 SmallVector<SizeOfIntrinsicOp> sizeofOps;
36 getOperation().walk([&](SizeOfIntrinsicOp op) { sizeofOps.push_back(op); });
37
38 if (sizeofOps.empty()) {
39 markAllAnalysesPreserved();
40 return;
41 }
42
43 for (SizeOfIntrinsicOp op : sizeofOps) {
44 auto width = getBitWidth(op.getInput().getType());
45 if (!width) {
46 op.emitError(
47 "failed to elaborate sizeof intrinsic: unable to determine "
48 "operand width")
49 .attachNote(op.getInput().getLoc())
50 << "operand of type " << op.getInput().getType()
51 << " defined here has unknown width";
52 return signalPassFailure();
53 }
54
55 OpBuilder builder(op);
56 auto constant = ConstantOp::create(builder, op.getLoc(), op.getType(),
57 APInt(32, *width));
58 op.getResult().replaceAllUsesWith(constant);
59 op.erase();
60 }
61 }
62};
63} // namespace
std::optional< int64_t > getBitWidth(FIRRTLBaseType type, bool ignoreFlip=false)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.