CIRCT  20.0.0git
ValueMapper.h
Go to the documentation of this file.
1 //===- ValueMapper.h - Support for mapping SSA values -----------*- 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 // This file provides support for mapping SSA values between two domains.
10 // Provided a BackedgeBuilder, the ValueMapper supports mappings between
11 // GraphRegions, creating Backedges in cases of 'get'ing mapped values which are
12 // yet to be 'set'.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef CIRCT_SUPPORT_VALUEMAPPER_H
17 #define CIRCT_SUPPORT_VALUEMAPPER_H
18 
20 #include "mlir/IR/Location.h"
21 #include "mlir/IR/OperationSupport.h"
22 #include "mlir/IR/Value.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/SmallVector.h"
25 
26 #include <functional>
27 #include <variant>
28 
29 namespace circt {
30 
31 /// The ValueMapper class facilitates the definition and connection of SSA
32 /// def-use chains between two location - a 'from' location (defining
33 /// use-def chains) and a 'to' location (where new operations are created based
34 /// on the 'from' location).ยด
35 class ValueMapper {
36 public:
37  using TypeTransformer = llvm::function_ref<mlir::Type(mlir::Type)>;
38  static mlir::Type identity(mlir::Type t) { return t; };
39  explicit ValueMapper(BackedgeBuilder *bb = nullptr) : bb(bb) {}
40 
41  // Get the mapped value of value 'from'. If no mapping has been registered, a
42  // new backedge is created. The type of the mapped value may optionally be
43  // modified through the 'typeTransformer'.
44  mlir::Value get(mlir::Value from,
45  TypeTransformer typeTransformer = ValueMapper::identity);
46  llvm::SmallVector<mlir::Value>
47  get(mlir::ValueRange from,
48  TypeTransformer typeTransformer = ValueMapper::identity);
49 
50  // Set the mapped value of 'from' to 'to'. If 'from' is already mapped to a
51  // backedge, replaces that backedge with 'to'. If 'replace' is not set, and a
52  // (non-backedge) mapping already exists, an assert is thrown.
53  void set(mlir::Value from, mlir::Value to, bool replace = false);
54  void set(mlir::ValueRange from, mlir::ValueRange to, bool replace = false);
55 
56 private:
57  BackedgeBuilder *bb = nullptr;
58  llvm::DenseMap<mlir::Value, std::variant<mlir::Value, Backedge>> mapping;
59 };
60 
61 } // namespace circt
62 
63 #endif // CIRCT_SUPPORT_VALUEMAPPER_H
Instantiate one of these and use it to build typed backedges.
The ValueMapper class facilitates the definition and connection of SSA def-use chains between two loc...
Definition: ValueMapper.h:35
llvm::DenseMap< mlir::Value, std::variant< mlir::Value, Backedge > > mapping
Definition: ValueMapper.h:58
void set(mlir::Value from, mlir::Value to, bool replace=false)
llvm::function_ref< mlir::Type(mlir::Type)> TypeTransformer
Definition: ValueMapper.h:37
static mlir::Type identity(mlir::Type t)
Definition: ValueMapper.h:38
void set(mlir::ValueRange from, mlir::ValueRange to, bool replace=false)
ValueMapper(BackedgeBuilder *bb=nullptr)
Definition: ValueMapper.h:39
mlir::Value get(mlir::Value from, TypeTransformer typeTransformer=ValueMapper::identity)
BackedgeBuilder * bb
Definition: ValueMapper.h:57
llvm::SmallVector< mlir::Value > get(mlir::ValueRange from, TypeTransformer typeTransformer=ValueMapper::identity)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21