CIRCT  19.0.0git
ESILowerTypes.cpp
Go to the documentation of this file.
1 //===- ESILowerTypes.cpp ----------------------------------------*- 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 //
9 // Lower high-level ESI types to HW conversions and pass.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "../PassDetails.h"
14 
19 
20 #include "mlir/IR/BuiltinTypes.h"
21 #include "mlir/Interfaces/ControlFlowInterfaces.h"
22 #include "mlir/Pass/Pass.h"
23 #include "mlir/Transforms/DialectConversion.h"
24 
25 using namespace circt;
26 using namespace circt::esi;
27 
28 namespace {
29 /// Lower all "high-level" ESI types on modules to some lower construct.
30 struct ESILowerTypesPass : public LowerESITypesBase<ESILowerTypesPass> {
31  void runOnOperation() override;
32 };
33 } // anonymous namespace
34 
35 namespace {
36 /// Materializations and type conversions to lower ESI data windows.
37 class LowerTypesConverter : public TypeConverter {
38 public:
39  LowerTypesConverter() {
40  addConversion([](Type t) { return t; });
41  addConversion([](WindowType window) { return window.getLoweredType(); });
42  addSourceMaterialization(wrapMaterialization);
43  addArgumentMaterialization(wrapMaterialization);
44  addTargetMaterialization(unwrapMaterialization);
45  }
46 
47 private:
48  static std::optional<mlir::Value> wrapMaterialization(OpBuilder &b,
49  WindowType resultType,
50  ValueRange inputs,
51  Location loc) {
52  if (inputs.size() != 1)
53  return std::nullopt;
54  auto wrap = b.create<WrapWindow>(loc, resultType, inputs[0]);
55  return wrap.getWindow();
56  }
57 
58  static std::optional<mlir::Value>
59  unwrapMaterialization(OpBuilder &b, hw::UnionType resultType,
60  ValueRange inputs, Location loc) {
61  if (inputs.size() != 1 || !isa<WindowType>(inputs[0].getType()))
62  return std::nullopt;
63  auto unwrap = b.create<UnwrapWindow>(loc, resultType, inputs[0]);
64  return unwrap.getFrame();
65  }
66 };
67 } // namespace
68 
69 void ESILowerTypesPass::runOnOperation() {
70  ConversionTarget target(getContext());
71 
72  // We need to lower instances, modules, and outputs with data windows.
73  target.markUnknownOpDynamicallyLegal([](Operation *op) {
74  return TypeSwitch<Operation *, bool>(op)
75  .Case([](igraph::InstanceOpInterface inst) {
76  return !(
77  llvm::any_of(inst->getOperandTypes(), hw::type_isa<WindowType>) ||
78  llvm::any_of(inst->getResultTypes(), hw::type_isa<WindowType>));
79  })
80  .Case([](hw::HWMutableModuleLike mod) {
81  auto isWindowPort = [](hw::PortInfo p) {
82  return hw::type_isa<WindowType>(p.type);
83  };
84  return !(llvm::any_of(mod.getPortList(), isWindowPort));
85  })
86  .Default([](Operation *op) {
87  if (op->hasTrait<OpTrait::ReturnLike>())
88  return !llvm::any_of(op->getOperandTypes(),
89  hw::type_isa<WindowType>);
90  return true;
91  });
92  });
93 
94  LowerTypesConverter types;
95  RewritePatternSet patterns(&getContext());
96  patterns.add<TypeConversionPattern>(types, &getContext());
97  if (failed(
98  applyPartialConversion(getOperation(), target, std::move(patterns))))
99  signalPassFailure();
100 }
101 
102 std::unique_ptr<OperationPass<ModuleOp>>
104  return std::make_unique<ESILowerTypesPass>();
105 }
return wrap(CMemoryType::get(unwrap(ctx), baseType, numElements))
static EvaluatorValuePtr unwrap(OMEvaluatorValue c)
Definition: OM.cpp:96
llvm::SmallVector< StringAttr > inputs
std::unique_ptr< OperationPass< ModuleOp > > createESITypeLoweringPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21
Generic pattern which replaces an operation by one of the same operation name, but with converted att...