CIRCT 23.0.0git
Loading...
Searching...
No Matches
CombOps.h
Go to the documentation of this file.
1//===- CombOps.h - Declare Comb dialect operations --------------*- 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 declares the operation classes for the Comb dialect.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef CIRCT_DIALECT_COMB_COMBOPS_H
14#define CIRCT_DIALECT_COMB_COMBOPS_H
15
18#include "circt/Support/LLVM.h"
19#include "mlir/Bytecode/BytecodeOpInterface.h"
20#include "mlir/IR/BuiltinOps.h"
21#include "mlir/IR/Matchers.h"
22#include "mlir/IR/OpImplementation.h"
23#include "mlir/Interfaces/FunctionInterfaces.h"
24#include "mlir/Interfaces/InferIntRangeInterface.h"
25#include "mlir/Interfaces/InferTypeOpInterface.h"
26#include "mlir/Interfaces/SideEffectInterfaces.h"
27#include "mlir/Transforms/DialectConversion.h"
28
29namespace llvm {
30struct KnownBits;
31}
32
33namespace mlir {
34class PatternRewriter;
35}
36
37#define GET_OP_CLASSES
38#include "circt/Dialect/Comb/Comb.h.inc"
39
40namespace circt {
41namespace comb {
42
43using llvm::KnownBits;
44
45/// Compute "known bits" information about the specified value - the set of bits
46/// that are guaranteed to always be zero, and the set of bits that are
47/// guaranteed to always be one (these must be exclusive!). A bit that exists
48/// in neither set is unknown.
49KnownBits computeKnownBits(Value value);
50
51/// Create the ops to zero-extend a value to an integer of equal or larger type.
52Value createZExt(OpBuilder &builder, Location loc, Value value,
53 unsigned targetWidth);
54
55/// Create a sign extension operation from a value of integer type to an equal
56/// or larger integer type.
57Value createOrFoldSExt(Location loc, Value value, Type destTy,
58 OpBuilder &builder);
59Value createOrFoldSExt(Value value, Type destTy, ImplicitLocOpBuilder &builder);
60
61/// Create a ``Not'' gate on a value.
62Value createOrFoldNot(Location loc, Value value, OpBuilder &builder,
63 bool twoState = false);
64Value createOrFoldNot(Value value, ImplicitLocOpBuilder &builder,
65 bool twoState = false);
66
67/// Extract bits from a value.
68void extractBits(OpBuilder &builder, Value val, SmallVectorImpl<Value> &bits);
69
70/// Construct a mux tree for given leaf nodes. `selectors` is the selector for
71/// each level of the tree. Currently the selector is tested from MSB to LSB.
72Value constructMuxTree(OpBuilder &builder, Location loc,
73 ArrayRef<Value> selectors, ArrayRef<Value> leafNodes,
74 Value outOfBoundsValue);
75
76/// Extract a range of bits from an integer at a dynamic offset.
77Value createDynamicExtract(OpBuilder &builder, Location loc, Value value,
78 Value offset, unsigned width);
79
80/// Replace a range of bits in an integer at a dynamic offset, and return the
81/// updated integer value. Calls `createInject` if the offset is constant.
82Value createDynamicInject(OpBuilder &builder, Location loc, Value value,
83 Value offset, Value replacement,
84 bool twoState = false);
85
86/// Replace a range of bits in an integer and return the updated integer value.
87Value createInject(OpBuilder &builder, Location loc, Value value,
88 unsigned offset, Value replacement);
89
90/// Replace a subtraction with an addition of the two's complement.
91LogicalResult convertSubToAdd(comb::SubOp subOp,
92 mlir::PatternRewriter &rewriter);
93
94/// Convert unsigned division or modulo by a power of two.
95/// For division: divu(x, 2^n) -> concat(0...0, extract(x, n, width-n)).
96/// For modulo: modu(x, 2^n) -> concat(0...0, extract(x, 0, n))
97/// TODO: Support signed division and modulo.
98LogicalResult convertDivUByPowerOfTwo(DivUOp divOp,
99 mlir::PatternRewriter &rewriter);
100LogicalResult convertModUByPowerOfTwo(ModUOp modOp,
101 mlir::PatternRewriter &rewriter);
102
103/// Enum for mux chain folding styles.
105/// Mux chain folding that converts chains of muxes with index
106/// comparisons into array operations or balanced mux trees. `styleFn` is a
107/// callback that returns the desired folding style based on the index
108/// width and number of entries.
110 PatternRewriter &rewriter, MuxOp rootMux, bool isFalseSide,
111 llvm::function_ref<MuxChainWithComparisonFoldingStyle(size_t indexWidth,
112 size_t numEntries)>
113 styleFn);
114
115// Check if the operand is sext() and return the unextended operand:
116// signBit = comb.extract(baseValue, width-1, 1)
117// ext = comb.replicate(signBit, width-baseWidth)
118// sext = comb.concat(ext, baseValue)
119template <typename SubType>
121 SubType lhs;
122 SextMatcher(SubType lhs) : lhs(std::move(lhs)) {}
123 bool match(Operation *op) {
124 // Check if operand is a concat operation
125 auto concatOp = dyn_cast<ConcatOp>(op);
126 if (!concatOp)
127 return false;
128
129 auto operands = concatOp.getOperands();
130 // ConcatOp must have exactly 2 operands: (sign_bits, base_value)
131 if (operands.size() != 2)
132 return false;
133
134 Value signBits = operands[0];
135 Value baseValue = operands[1];
136 auto baseWidth = baseValue.getType().getIntOrFloatBitWidth();
137
138 // Check if signBits is a replicate operation
139 auto replicateOp = dyn_cast<ReplicateOp>(signBits.getDefiningOp());
140 if (!replicateOp)
141 return false;
142
143 Value signBit = replicateOp.getInput();
144
145 // Check if signBit is the msb of baseValue
146 auto extractOp = dyn_cast<ExtractOp>(signBit.getDefiningOp());
147 if (!extractOp)
148 return false;
149
150 if ((extractOp.getInput() != baseValue) ||
151 (extractOp.getLowBit() != baseWidth - 1) ||
152 (extractOp.getType().getIntOrFloatBitWidth() != 1))
153 return false;
154
155 // Match the base unextended value against the sub-matcher
156 return mlir::detail::matchOperandOrValueAtIndex(op, 1, lhs);
157 }
158};
159
160/// Helper function to create a sign extension matcher
161template <typename SubType>
162static inline SextMatcher<SubType> m_Sext(const SubType &subExpr) {
163 return SextMatcher<SubType>(subExpr);
164}
165
166} // namespace comb
167} // namespace circt
168
169#endif // CIRCT_DIALECT_COMB_COMBOPS_H
void extractBits(OpBuilder &builder, Value val, SmallVectorImpl< Value > &bits)
Extract bits from a value.
Definition CombOps.cpp:78
bool foldMuxChainWithComparison(PatternRewriter &rewriter, MuxOp rootMux, bool isFalseSide, llvm::function_ref< MuxChainWithComparisonFoldingStyle(size_t indexWidth, size_t numEntries)> styleFn)
Mux chain folding that converts chains of muxes with index comparisons into array operations or balan...
static SextMatcher< SubType > m_Sext(const SubType &subExpr)
Helper function to create a sign extension matcher.
Definition CombOps.h:162
Value createOrFoldNot(Location loc, Value value, OpBuilder &builder, bool twoState=false)
Create a `‘Not’' gate on a value.
Definition CombOps.cpp:66
MuxChainWithComparisonFoldingStyle
Enum for mux chain folding styles.
Definition CombOps.h:104
@ BalancedMuxTree
Definition CombOps.h:104
LogicalResult convertSubToAdd(comb::SubOp subOp, mlir::PatternRewriter &rewriter)
Replace a subtraction with an addition of the two's complement.
Definition CombOps.cpp:221
LogicalResult convertModUByPowerOfTwo(ModUOp modOp, mlir::PatternRewriter &rewriter)
Value createDynamicInject(OpBuilder &builder, Location loc, Value value, Value offset, Value replacement, bool twoState=false)
Replace a range of bits in an integer at a dynamic offset, and return the updated integer value.
Definition CombOps.cpp:152
Value createInject(OpBuilder &builder, Location loc, Value value, unsigned offset, Value replacement)
Replace a range of bits in an integer and return the updated integer value.
Definition CombOps.cpp:187
Value createDynamicExtract(OpBuilder &builder, Location loc, Value value, Value offset, unsigned width)
Extract a range of bits from an integer at a dynamic offset.
Definition CombOps.cpp:132
KnownBits computeKnownBits(Value value)
Compute "known bits" information about the specified value - the set of bits that are guaranteed to a...
Value createOrFoldSExt(Location loc, Value value, Type destTy, OpBuilder &builder)
Create a sign extension operation from a value of integer type to an equal or larger integer type.
Definition CombOps.cpp:43
Value constructMuxTree(OpBuilder &builder, Location loc, ArrayRef< Value > selectors, ArrayRef< Value > leafNodes, Value outOfBoundsValue)
Construct a mux tree for given leaf nodes.
Definition CombOps.cpp:105
Value createZExt(OpBuilder &builder, Location loc, Value value, unsigned targetWidth)
Create the ops to zero-extend a value to an integer of equal or larger type.
Definition CombOps.cpp:25
LogicalResult convertDivUByPowerOfTwo(DivUOp divOp, mlir::PatternRewriter &rewriter)
Convert unsigned division or modulo by a power of two.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition comb.py:1
SextMatcher(SubType lhs)
Definition CombOps.h:122
bool match(Operation *op)
Definition CombOps.h:123