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