14 #ifndef MLIR_REWRITE_BYTECODE_H_
15 #define MLIR_REWRITE_BYTECODE_H_
19 #if MLIR_ENABLE_PDL_IN_PATTERNMATCH
22 namespace pdl_interp {
31 using ByteCodeField = uint16_t;
32 using ByteCodeAddr = uint32_t;
33 using OwningOpRange = llvm::OwningArrayRef<Operation *>;
40 class PDLByteCodePattern :
public Pattern {
42 static PDLByteCodePattern create(pdl_interp::RecordMatchOp matchOp,
43 PDLPatternConfigSet *configSet,
44 ByteCodeAddr rewriterAddr);
47 ByteCodeAddr getRewriterAddr()
const {
return rewriterAddr; }
50 PDLPatternConfigSet *getConfigSet()
const {
return configSet; }
53 template <
typename... Args>
54 PDLByteCodePattern(ByteCodeAddr rewriterAddr, PDLPatternConfigSet *configSet,
55 Args &&...patternArgs)
56 :
Pattern(std::forward<Args>(patternArgs)...), rewriterAddr(rewriterAddr),
57 configSet(configSet) {}
60 ByteCodeAddr rewriterAddr;
63 PDLPatternConfigSet *configSet;
73 class PDLByteCodeMutableState {
87 friend class PDLByteCode;
91 std::vector<const void *> memory;
97 std::vector<OwningOpRange> opRangeMemory;
101 std::vector<TypeRange> typeRangeMemory;
104 std::vector<llvm::OwningArrayRef<Type>> allocatedTypeRangeMemory;
108 std::vector<ValueRange> valueRangeMemory;
111 std::vector<llvm::OwningArrayRef<Value>> allocatedValueRangeMemory;
116 std::vector<unsigned> loopIndex;
120 std::vector<PatternBenefit> currentPatternBenefits;
135 MatchResult(Location loc,
const PDLByteCodePattern &
pattern,
138 MatchResult(
const MatchResult &) =
delete;
139 MatchResult &operator=(
const MatchResult &) =
delete;
140 MatchResult(MatchResult &&other) =
default;
141 MatchResult &operator=(MatchResult &&) =
default;
146 SmallVector<const void *> values;
148 SmallVector<TypeRange, 0> typeRangeValues;
149 SmallVector<ValueRange, 0> valueRangeValues;
153 const PDLByteCodePattern *
pattern;
160 PDLByteCode(ModuleOp module,
161 SmallVector<std::unique_ptr<PDLPatternConfigSet>> configs,
162 const DenseMap<Operation *, PDLPatternConfigSet *> &configMap,
163 llvm::StringMap<PDLConstraintFunction> constraintFns,
164 llvm::StringMap<PDLRewriteFunction> rewriteFns);
167 ArrayRef<PDLByteCodePattern>
getPatterns()
const {
return patterns; }
175 void match(Operation *op, PatternRewriter &rewriter,
176 SmallVectorImpl<MatchResult> &matches,
177 PDLByteCodeMutableState &state)
const;
181 LogicalResult
rewrite(PatternRewriter &rewriter,
const MatchResult &
match,
182 PDLByteCodeMutableState &state)
const;
188 void executeByteCode(
const ByteCodeField *inst, PatternRewriter &rewriter,
189 PDLByteCodeMutableState &state,
190 SmallVectorImpl<MatchResult> *matches)
const;
193 SmallVector<std::unique_ptr<PDLPatternConfigSet>> configs;
199 std::vector<const void *> uniquedData;
202 SmallVector<ByteCodeField, 64> matcherByteCode;
205 SmallVector<ByteCodeField, 64> rewriterByteCode;
208 SmallVector<PDLByteCodePattern, 32> patterns;
211 std::vector<PDLConstraintFunction> constraintFunctions;
212 std::vector<PDLRewriteFunction> rewriteFunctions;
215 ByteCodeField maxValueMemoryIndex = 0;
218 ByteCodeField maxOpRangeCount = 0;
219 ByteCodeField maxTypeRangeCount = 0;
220 ByteCodeField maxValueRangeCount = 0;
223 ByteCodeField maxLoopLevel = 0;
Operation is the basic unit of execution within MLIR.
This class represents the benefit of a pattern match in a unitless scheme that ranges from 0 (very li...
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
This class contains all of the data related to a pattern, but does not contain any methods or logic f...
Pattern(StringRef rootName, PatternBenefit benefit, MLIRContext *context, ArrayRef< StringRef > generatedNames={})
Construct a pattern with a certain benefit that matches the operation with the given root name.
void updatePatternBenefit(unsigned patternIndex, PatternBenefit benefit)
Set the new benefit for a bytecode pattern.
void cleanupAfterMatchAndRewrite()
Cleanup any allocated state after a full match/rewrite has been completed.
ArrayRef< PDLByteCodePattern > getPatterns() const
void match(Operation *op, PatternRewriter &rewriter, SmallVectorImpl< MatchResult > &matches, PDLByteCodeMutableState &state) const
void initializeMutableState(PDLByteCodeMutableState &state) const
Initialize the given state such that it can be used to execute the current bytecode.
LogicalResult rewrite(PatternRewriter &rewriter, const MatchResult &match, PDLByteCodeMutableState &state) const
Include the generated interface declarations.
const PDLByteCodePattern * pattern