CIRCT  19.0.0git
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 
18 using namespace mlir;
19 using namespace circt;
20 mlir::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 
38 llvm::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 
46 void 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 
58 void 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")
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
Definition: CalyxOps.cpp:53
This file defines an intermediate representation for circuits acting as an abstraction for constraint...
Definition: DebugAnalysis.h:21