CIRCT  18.0.0git
BackedgeBuilder.cpp
Go to the documentation of this file.
1 //===- BackedgeBuilder.cpp - Support for building backedges ---------------===//
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 provide support for building backedges.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "circt/Support/LLVM.h"
15 
16 #include "mlir/IR/BuiltinOps.h"
17 #include "mlir/IR/PatternMatch.h"
18 
19 using namespace circt;
20 
21 Backedge::Backedge(mlir::Operation *op) : value(op->getResult(0)) {}
22 
23 void Backedge::setValue(mlir::Value newValue) {
24  assert(value.getType() == newValue.getType());
25  assert(!set && "backedge already set to a value!");
26  value.replaceAllUsesWith(newValue);
27  value = newValue; // In case the backedge is still referred to after setting.
28  set = true;
29 
30  // If the backedge is referenced again, it should now point to the updated
31  // value.
32  value = newValue;
33 }
34 
36 
38  unsigned numInUse = 0;
39  for (Operation *op : edges) {
40  if (!op->use_empty()) {
41  auto diag = op->emitError("backedge of type ")
42  << op->getResult(0).getType() << " still in use";
43  for (auto user : op->getUsers())
44  diag.attachNote(user->getLoc()) << "used by " << *user;
45  ++numInUse;
46  continue;
47  }
48  if (rewriter)
49  rewriter->eraseOp(op);
50  else
51  op->erase();
52  }
53  edges.clear();
54  if (numInUse > 0)
55  mlir::emitRemark(loc, "abandoned ") << numInUse << " backedges";
56  return success(numInUse == 0);
57 }
58 
59 void BackedgeBuilder::abandon() { edges.clear(); }
60 
61 BackedgeBuilder::BackedgeBuilder(OpBuilder &builder, Location loc)
62  : builder(builder), rewriter(nullptr), loc(loc) {}
63 BackedgeBuilder::BackedgeBuilder(PatternRewriter &rewriter, Location loc)
64  : builder(rewriter), rewriter(&rewriter), loc(loc) {}
65 Backedge BackedgeBuilder::get(Type t, mlir::LocationAttr optionalLoc) {
66  if (!optionalLoc)
67  optionalLoc = loc;
68  Operation *op = builder.create<mlir::UnrealizedConversionCastOp>(
69  optionalLoc, t, ValueRange{});
70  edges.push_back(op);
71  return Backedge(op);
72 }
lowerAnnotationsNoRefTypePorts FirtoolPreserveValuesMode value
Definition: Firtool.cpp:95
assert(baseType &&"element must be base type")
Builder builder
void abandon()
Abandon the backedges, suppressing any diagnostics if they are still active upon destruction of the b...
llvm::SmallVector< mlir::Operation *, 16 > edges
Backedge get(mlir::Type resultType, mlir::LocationAttr optionalLoc={})
Create a typed backedge.
mlir::OpBuilder & builder
mlir::PatternRewriter * rewriter
mlir::LogicalResult clearOrEmitError()
Clear the backedges, erasing any remaining cursor ops.
BackedgeBuilder(mlir::OpBuilder &builder, mlir::Location loc)
To build a backedge op and manipulate it, we need a PatternRewriter and a Location.
Backedge is a wrapper class around a Value.
mlir::Value value
void setValue(mlir::Value)
This file defines an intermediate representation for circuits acting as an abstraction for constraint...
Definition: DebugAnalysis.h:21