CIRCT 21.0.0git
Loading...
Searching...
No Matches
ValueMapper.cpp
Go to the documentation of this file.
1//===- ValueMapper.cpp - 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
17
18using namespace mlir;
19using namespace circt;
20mlir::Value ValueMapper::get(Value from, TypeTransformer typeTransformer) {
21 if (mapping.count(from) == 0) {
22 assert(bb && "Trying to 'get' a mapped value without any value set. No "
23 "BackedgeBuilder was provided, so cannot provide any mapped "
24 "SSA value!");
25 // Create a backedge which will be resolved at a later time once all
26 // operands are created.
27 mapping[from] = bb->get(typeTransformer(from.getType()));
28 }
29 auto operandMapping = mapping[from];
30 Value mappedOperand;
31 if (auto *v = std::get_if<Value>(&operandMapping))
32 mappedOperand = *v;
33 else
34 mappedOperand = std::get<Backedge>(operandMapping);
35 return mappedOperand;
36}
37
38llvm::SmallVector<Value> ValueMapper::get(ValueRange from,
39 TypeTransformer typeTransformer) {
40 llvm::SmallVector<Value> to;
41 for (auto f : from)
42 to.push_back(get(f, typeTransformer));
43 return to;
44}
45
46void ValueMapper::set(Value from, Value to, bool replace) {
47 auto it = mapping.find(from);
48 if (it != mapping.end()) {
49 if (auto *backedge = std::get_if<Backedge>(&it->second))
50 backedge->setValue(to);
51 else if (!replace)
52 assert(false && "'from' was already mapped to a final value!");
53 }
54 // Register the new mapping
55 mapping[from] = to;
56}
57
58void ValueMapper::set(ValueRange from, ValueRange to, bool replace) {
59 assert(from.size() == to.size() &&
60 "Expected # of 'from' values and # of 'to' values to be identical.");
61 for (auto [f, t] : llvm::zip(from, to))
62 set(f, t, replace);
63}
assert(baseType &&"element must be base type")
Backedge get(mlir::Type resultType, mlir::LocationAttr optionalLoc={})
Create a typed backedge.
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)
mlir::Value get(mlir::Value from, TypeTransformer typeTransformer=ValueMapper::identity)
BackedgeBuilder * bb
Definition ValueMapper.h:57
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.