14#ifndef MLIR_REWRITE_BYTECODE_H_
15#define MLIR_REWRITE_BYTECODE_H_
19#if MLIR_ENABLE_PDL_IN_PATTERNMATCH
31using ByteCodeField = uint16_t;
32using ByteCodeAddr = uint32_t;
41 static PDLByteCodePattern create(pdl_interp::RecordMatchOp matchOp,
42 PDLPatternConfigSet *configSet,
43 ByteCodeAddr rewriterAddr);
46 ByteCodeAddr getRewriterAddr()
const {
return rewriterAddr; }
49 PDLPatternConfigSet *getConfigSet()
const {
return configSet; }
52 template <
typename... Args>
53 PDLByteCodePattern(ByteCodeAddr rewriterAddr, PDLPatternConfigSet *configSet,
54 Args &&...patternArgs)
55 :
Pattern(std::forward<Args>(patternArgs)...), rewriterAddr(rewriterAddr),
56 configSet(configSet) {}
59 ByteCodeAddr rewriterAddr;
62 PDLPatternConfigSet *configSet;
86 friend class PDLByteCode;
90 std::vector<const void *> memory;
96 std::vector<std::vector<Operation *>> opRangeMemory;
100 std::vector<TypeRange> typeRangeMemory;
103 std::vector<std::vector<Type>> allocatedTypeRangeMemory;
107 std::vector<ValueRange> valueRangeMemory;
110 std::vector<std::vector<Value>> allocatedValueRangeMemory;
115 std::vector<unsigned> loopIndex;
119 std::vector<PatternBenefit> currentPatternBenefits;
134 MatchResult(Location loc,
const PDLByteCodePattern &
pattern,
137 MatchResult(
const MatchResult &) =
delete;
138 MatchResult &operator=(
const MatchResult &) =
delete;
139 MatchResult(MatchResult &&other) =
default;
140 MatchResult &operator=(MatchResult &&) =
default;
145 SmallVector<const void *> values;
147 SmallVector<TypeRange, 0> typeRangeValues;
148 SmallVector<ValueRange, 0> valueRangeValues;
152 const PDLByteCodePattern *
pattern;
159 PDLByteCode(ModuleOp module,
160 SmallVector<std::unique_ptr<PDLPatternConfigSet>> configs,
162 llvm::StringMap<PDLConstraintFunction> constraintFns,
163 llvm::StringMap<PDLRewriteFunction> rewriteFns);
174 void match(Operation *op, PatternRewriter &rewriter,
175 SmallVectorImpl<MatchResult> &matches,
176 PDLByteCodeMutableState &state)
const;
181 PDLByteCodeMutableState &state)
const;
187 void executeByteCode(
const ByteCodeField *inst, PatternRewriter &rewriter,
188 PDLByteCodeMutableState &state,
189 SmallVectorImpl<MatchResult> *matches)
const;
192 SmallVector<std::unique_ptr<PDLPatternConfigSet>> configs;
198 std::vector<const void *> uniquedData;
201 SmallVector<ByteCodeField, 64> matcherByteCode;
204 SmallVector<ByteCodeField, 64> rewriterByteCode;
207 SmallVector<PDLByteCodePattern, 32>
patterns;
210 std::vector<PDLConstraintFunction> constraintFunctions;
211 std::vector<PDLRewriteFunction> rewriteFunctions;
214 ByteCodeField maxValueMemoryIndex = 0;
217 ByteCodeField maxOpRangeCount = 0;
218 ByteCodeField maxTypeRangeCount = 0;
219 ByteCodeField maxValueRangeCount = 0;
222 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...
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 FrozenRewritePatternSet & patterns
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
const PDLByteCodePattern * pattern