CIRCT  20.0.0git
Namespaces | Enumerations | Functions
FIRRTLFolds.cpp File Reference
#include "circt/Dialect/FIRRTL/FIRRTLAttributes.h"
#include "circt/Dialect/FIRRTL/FIRRTLOps.h"
#include "circt/Dialect/FIRRTL/FIRRTLTypes.h"
#include "circt/Dialect/FIRRTL/FIRRTLUtils.h"
#include "circt/Support/APInt.h"
#include "circt/Support/LLVM.h"
#include "circt/Support/Naming.h"
#include "mlir/IR/Matchers.h"
#include "mlir/IR/PatternMatch.h"
#include "llvm/ADT/APSInt.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/TypeSwitch.h"
#include "circt/Dialect/FIRRTL/FIRRTLCanonicalization.h.inc"
Include dependency graph for FIRRTLFolds.cpp:

Go to the source code of this file.

Namespaces

 circt
 The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
 
 circt::firrtl
 
 circt::firrtl::patterns
 

Enumerations

enum class  BinOpKind { Normal , Compare , DivideOrShift }
 This is the policy for folding, which depends on the sort of operator we're processing. More...
 

Functions

static Value dropWrite (PatternRewriter &rewriter, OpResult old, Value passthrough)
 
static Value moveNameHint (OpResult old, Value passthrough)
 
static bool hasKnownWidthIntTypes (Operation *op)
 Return true if this operation's operands and results all have a known width. More...
 
static bool isUInt1 (Type type)
 Return true if this value is 1 bit UInt. More...
 
static void updateName (PatternRewriter &rewriter, Operation *op, StringAttr name)
 Set the name of an op based on the best of two names: The current name, and the name passed in. More...
 
static void replaceOpAndCopyName (PatternRewriter &rewriter, Operation *op, Value newValue)
 A wrapper of PatternRewriter::replaceOp to propagate "name" attribute. More...
 
template<typename OpTy , typename... Args>
static OpTy replaceOpWithNewOpAndCopyName (PatternRewriter &rewriter, Operation *op, Args &&...args)
 A wrapper of PatternRewriter::replaceOpWithNewOp to propagate "name" attribute. More...
 
static std::optional< APSInt > getExtendedConstant (Value operand, Attribute constant, int32_t destWidth)
 Implicitly replace the operand to a constant folding operation with a const 0 in case the operand is non-constant but has a bit width 0, or if the operand is an invalid value. More...
 
static std::optional< APSInt > getConstant (Attribute operand)
 Determine the value of a constant operand for the sake of constant folding. More...
 
static bool isConstantZero (Attribute operand)
 Determine whether a constant operand is a zero value for the sake of constant folding. More...
 
static bool isConstantOne (Attribute operand)
 Determine whether a constant operand is a one value for the sake of constant folding. More...
 
static Attribute constFoldFIRRTLBinaryOp (Operation *op, ArrayRef< Attribute > operands, BinOpKind opKind, const function_ref< APInt(const APSInt &, const APSInt &)> &calculate)
 Applies the constant folding function calculate to the given operands. More...
 
static LogicalResult canonicalizePrimOp (Operation *op, PatternRewriter &rewriter, const function_ref< OpFoldResult(ArrayRef< Attribute >)> &canonicalize)
 Applies the canonicalization function canonicalize to the given operation. More...
 
static APInt getMaxUnsignedValue (unsigned bitWidth)
 Get the largest unsigned value of a given bit width. More...
 
static APInt getMinSignedValue (unsigned bitWidth)
 Get the smallest signed value of a given bit width. More...
 
static APInt getMaxSignedValue (unsigned bitWidth)
 Get the largest signed value of a given bit width. More...
 
static void replaceWithBits (Operation *op, Value value, unsigned hiBit, unsigned loBit, PatternRewriter &rewriter)
 Replace the specified operation with a 'bits' op from the specified hi/lo bits. More...
 
template<typename OpTy >
static OpFoldResult foldMux (OpTy op, typename OpTy::FoldAdaptor adaptor)
 
static LogicalResult canonicalizeSingleSetConnect (MatchingConnectOp op, PatternRewriter &rewriter)
 
static AttachOp getDominatingAttachUser (Value value, AttachOp dominatedAttach)
 If the specified value has an AttachOp user strictly dominating by "dominatingAttach" then return it. More...
 
static void replaceOpWithRegion (PatternRewriter &rewriter, Operation *op, Region &region)
 Replaces the given op with the contents of the given single-block region. More...
 
template<typename OpTy >
static LogicalResult demoteForceableIfUnused (OpTy op, PatternRewriter &rewriter)
 
static Attribute collectFields (MLIRContext *context, ArrayRef< Attribute > operands)
 
static bool isDefinedByOneConstantOp (Value v)
 
static LogicalResult canonicalizeRegResetWithOneReset (RegResetOp reg, PatternRewriter &rewriter)
 
static Value getPortFieldValue (Value port, StringRef name)
 
static bool isPortDisabled (Value port)
 
static bool isPortUnused (Value port, StringRef data)
 
static void replacePortField (PatternRewriter &rewriter, Value port, StringRef name, Value value)
 
static void erasePort (PatternRewriter &rewriter, Value port)
 
static LogicalResult foldHiddenReset (RegOp reg, PatternRewriter &rewriter)
 
static LogicalResult eraseIfZeroOrNotZero (Operation *op, Value predicate, Value enable, PatternRewriter &rewriter, bool eraseIfZero)
 
template<class Op , bool EraseIfZero = false>
static LogicalResult canonicalizeImmediateVerifOp (Op op, PatternRewriter &rewriter)
 
static LogicalResult canonicalizeRefResolveOfForceable (RefResolveOp op, PatternRewriter &rewriter)
 
static bool isConstantZero (Value operand)
 
template<typename Op >
static LogicalResult eraseIfPredFalse (Op op, PatternRewriter &rewriter)
 
static bool isTypeEmpty (FIRRTLType type)
 

Enumeration Type Documentation

◆ BinOpKind

enum BinOpKind
strong

This is the policy for folding, which depends on the sort of operator we're processing.

Enumerator
Normal 
Compare 
DivideOrShift 

Definition at line 198 of file FIRRTLFolds.cpp.

Function Documentation

◆ canonicalizeImmediateVerifOp()

template<class Op , bool EraseIfZero = false>
static LogicalResult canonicalizeImmediateVerifOp ( Op  op,
PatternRewriter &  rewriter 
)
static

Definition at line 3176 of file FIRRTLFolds.cpp.

References eraseIfZeroOrNotZero().

◆ canonicalizePrimOp()

static LogicalResult canonicalizePrimOp ( Operation *  op,
PatternRewriter &  rewriter,
const function_ref< OpFoldResult(ArrayRef< Attribute >)> &  canonicalize 
)
static

Applies the canonicalization function canonicalize to the given operation.

Determines which (if any) of the operation's operands are constants, and provides them as arguments to the callback function. Any invalidvalue in the input is mapped to a constant zero. The value returned from the callback is used as the replacement for op, and an additional pad operation is inserted if necessary. Does nothing if the result of op is of unknown width, in which case the necessity of a pad cannot be determined.

Definition at line 281 of file FIRRTLFolds.cpp.

References assert(), AssertLikeOp::canonicalize(), replaceOpAndCopyName(), and width.

◆ canonicalizeRefResolveOfForceable()

static LogicalResult canonicalizeRefResolveOfForceable ( RefResolveOp  op,
PatternRewriter &  rewriter 
)
static

Definition at line 3287 of file FIRRTLFolds.cpp.

◆ canonicalizeRegResetWithOneReset()

static LogicalResult canonicalizeRegResetWithOneReset ( RegResetOp  reg,
PatternRewriter &  rewriter 
)
static

Definition at line 2270 of file FIRRTLFolds.cpp.

References dropWrite(), isDefinedByOneConstantOp(), and seq::reg().

◆ canonicalizeSingleSetConnect()

static LogicalResult canonicalizeSingleSetConnect ( MatchingConnectOp  op,
PatternRewriter &  rewriter 
)
static

◆ collectFields()

static Attribute collectFields ( MLIRContext *  context,
ArrayRef< Attribute >  operands 
)
static

Definition at line 2153 of file FIRRTLFolds.cpp.

References circt::calyx::direction::get().

◆ constFoldFIRRTLBinaryOp()

static Attribute constFoldFIRRTLBinaryOp ( Operation *  op,
ArrayRef< Attribute >  operands,
BinOpKind  opKind,
const function_ref< APInt(const APSInt &, const APSInt &)> &  calculate 
)
static

Applies the constant folding function calculate to the given operands.

Sign or zero extends the operands appropriately to the bitwidth of the result type if useDstWidth is true, else to the larger of the two operand bit widths and depending on whether the operation is to be performed on signed or unsigned operands.

Definition at line 210 of file FIRRTLFolds.cpp.

References assert(), Compare, DivideOrShift, getExtendedConstant(), circt::firrtl::getIntAttr(), and Normal.

◆ demoteForceableIfUnused()

template<typename OpTy >
static LogicalResult demoteForceableIfUnused ( OpTy  op,
PatternRewriter &  rewriter 
)
static

◆ dropWrite()

static Value dropWrite ( PatternRewriter &  rewriter,
OpResult  old,
Value  passthrough 
)
static

Definition at line 32 of file FIRRTLFolds.cpp.

References Python.support::connect().

Referenced by canonicalizeRegResetWithOneReset().

◆ eraseIfPredFalse()

template<typename Op >
static LogicalResult eraseIfPredFalse ( Op  op,
PatternRewriter &  rewriter 
)
static

Definition at line 3316 of file FIRRTLFolds.cpp.

References circt::isConstantZero().

◆ eraseIfZeroOrNotZero()

static LogicalResult eraseIfZeroOrNotZero ( Operation *  op,
Value  predicate,
Value  enable,
PatternRewriter &  rewriter,
bool  eraseIfZero 
)
static

Definition at line 3152 of file FIRRTLFolds.cpp.

Referenced by canonicalizeImmediateVerifOp().

◆ erasePort()

static void erasePort ( PatternRewriter &  rewriter,
Value  port 
)
static

◆ foldHiddenReset()

static LogicalResult foldHiddenReset ( RegOp  reg,
PatternRewriter &  rewriter 
)
static

Definition at line 3069 of file FIRRTLFolds.cpp.

References circt::firrtl::getSingleConnectUserOf(), and seq::reg().

◆ foldMux()

template<typename OpTy >
static OpFoldResult foldMux ( OpTy  op,
typename OpTy::FoldAdaptor  adaptor 
)
static

Definition at line 1282 of file FIRRTLFolds.cpp.

References getConstant(), and circt::firrtl::getIntAttr().

◆ getConstant()

static std::optional<APSInt> getConstant ( Attribute  operand)
static

Determine the value of a constant operand for the sake of constant folding.

Definition at line 170 of file FIRRTLFolds.cpp.

Referenced by foldMux(), isConstantOne(), isConstantZero(), circt::firrtl::InstanceInfo::LatticeValue::mergeIn(), and circt::firrtl::InstanceInfo::LatticeValue::operator!().

◆ getDominatingAttachUser()

static AttachOp getDominatingAttachUser ( Value  value,
AttachOp  dominatedAttach 
)
static

If the specified value has an AttachOp user strictly dominating by "dominatingAttach" then return it.

Definition at line 1835 of file FIRRTLFolds.cpp.

◆ getExtendedConstant()

static std::optional<APSInt> getExtendedConstant ( Value  operand,
Attribute  constant,
int32_t  destWidth 
)
static

Implicitly replace the operand to a constant folding operation with a const 0 in case the operand is non-constant but has a bit width 0, or if the operand is an invalid value.

This makes constant folding significantly easier, as we can simply pass the operands to an operation through this function to appropriately replace any zero-width dynamic values or invalid values with a constant of value 0.

Definition at line 149 of file FIRRTLFolds.cpp.

References assert(), and circt::extOrTruncZeroWidth().

Referenced by constFoldFIRRTLBinaryOp().

◆ getMaxSignedValue()

static APInt getMaxSignedValue ( unsigned  bitWidth)
static

Get the largest signed value of a given bit width.

Returns a 1-bit zero value if bitWidth is 0.

Definition at line 351 of file FIRRTLFolds.cpp.

◆ getMaxUnsignedValue()

static APInt getMaxUnsignedValue ( unsigned  bitWidth)
static

Get the largest unsigned value of a given bit width.

Returns a 1-bit zero value if bitWidth is 0.

Definition at line 339 of file FIRRTLFolds.cpp.

◆ getMinSignedValue()

static APInt getMinSignedValue ( unsigned  bitWidth)
static

Get the smallest signed value of a given bit width.

Returns a 1-bit zero value if bitWidth is 0.

Definition at line 345 of file FIRRTLFolds.cpp.

◆ getPortFieldValue()

static Value getPortFieldValue ( Value  port,
StringRef  name 
)
static

Definition at line 2290 of file FIRRTLFolds.cpp.

References assert(), and circt::firrtl::getSingleConnectUserOf().

Referenced by isPortDisabled().

◆ hasKnownWidthIntTypes()

static bool hasKnownWidthIntTypes ( Operation *  op)
static

Return true if this operation's operands and results all have a known width.

This only works for integer types.

Definition at line 71 of file FIRRTLFolds.cpp.

◆ isConstantOne()

static bool isConstantOne ( Attribute  operand)
static

Determine whether a constant operand is a one value for the sake of constant folding.

Definition at line 190 of file FIRRTLFolds.cpp.

References getConstant().

◆ isConstantZero() [1/2]

static bool isConstantZero ( Attribute  operand)
static

Determine whether a constant operand is a zero value for the sake of constant folding.

This considers invalidvalue to be zero.

Definition at line 182 of file FIRRTLFolds.cpp.

References getConstant().

◆ isConstantZero() [2/2]

static bool isConstantZero ( Value  operand)
static

Definition at line 3310 of file FIRRTLFolds.cpp.

◆ isDefinedByOneConstantOp()

static bool isDefinedByOneConstantOp ( Value  v)
static

Definition at line 2261 of file FIRRTLFolds.cpp.

Referenced by canonicalizeRegResetWithOneReset().

◆ isPortDisabled()

static bool isPortDisabled ( Value  port)
static

Definition at line 2309 of file FIRRTLFolds.cpp.

References getPortFieldValue().

◆ isPortUnused()

static bool isPortUnused ( Value  port,
StringRef  data 
)
static

Definition at line 2320 of file FIRRTLFolds.cpp.

References assert(), and circt::firrtl::data.

◆ isTypeEmpty()

static bool isTypeEmpty ( FIRRTLType  type)
static

◆ isUInt1()

static bool isUInt1 ( Type  type)
static

Return true if this value is 1 bit UInt.

Definition at line 82 of file FIRRTLFolds.cpp.

◆ moveNameHint()

static Value moveNameHint ( OpResult  old,
Value  passthrough 
)
static

Definition at line 48 of file FIRRTLFolds.cpp.

References assert().

◆ replaceOpAndCopyName()

static void replaceOpAndCopyName ( PatternRewriter &  rewriter,
Operation *  op,
Value  newValue 
)
static

A wrapper of PatternRewriter::replaceOp to propagate "name" attribute.

If a replaced op has a "name" attribute, this function propagates the name to the new value.

Definition at line 111 of file FIRRTLFolds.cpp.

References updateName().

Referenced by canonicalizePrimOp(), and canonicalizeSingleSetConnect().

◆ replaceOpWithNewOpAndCopyName()

template<typename OpTy , typename... Args>
static OpTy replaceOpWithNewOpAndCopyName ( PatternRewriter &  rewriter,
Operation *  op,
Args &&...  args 
)
static

A wrapper of PatternRewriter::replaceOpWithNewOp to propagate "name" attribute.

If a replaced op has a "name" attribute, this function propagates the name to the new value.

Definition at line 124 of file FIRRTLFolds.cpp.

References updateName().

◆ replaceOpWithRegion()

static void replaceOpWithRegion ( PatternRewriter &  rewriter,
Operation *  op,
Region &  region 
)
static

Replaces the given op with the contents of the given single-block region.

Definition at line 1893 of file FIRRTLFolds.cpp.

References assert().

◆ replacePortField()

static void replacePortField ( PatternRewriter &  rewriter,
Value  port,
StringRef  name,
Value  value 
)
static

Definition at line 2337 of file FIRRTLFolds.cpp.

References assert().

◆ replaceWithBits()

static void replaceWithBits ( Operation *  op,
Value  value,
unsigned  hiBit,
unsigned  loBit,
PatternRewriter &  rewriter 
)
static

Replace the specified operation with a 'bits' op from the specified hi/lo bits.

Insert a cast to handle the case where the original operation returned a signed integer.

Definition at line 1266 of file FIRRTLFolds.cpp.

◆ updateName()

static void updateName ( PatternRewriter &  rewriter,
Operation *  op,
StringAttr  name 
)
static

Set the name of an op based on the best of two names: The current name, and the name passed in.

Definition at line 91 of file FIRRTLFolds.cpp.

References assert(), and circt::chooseName().

Referenced by replaceOpAndCopyName(), and replaceOpWithNewOpAndCopyName().