Changeset View
Changeset View
Standalone View
Standalone View
mlir/lib/Rewrite/ByteCode.h
Show All 19 Lines | |||||
namespace pdl_interp { | namespace pdl_interp { | ||||
class RecordMatchOp; | class RecordMatchOp; | ||||
} // end namespace pdl_interp | } // end namespace pdl_interp | ||||
namespace detail { | namespace detail { | ||||
class PDLByteCode; | class PDLByteCode; | ||||
/// Use generic bytecode types. ByteCodeField refers to the actual bytecode | /// Use generic bytecode types. ByteCodeField refers to the actual bytecode | ||||
/// entries (set to uint8_t for "byte" bytecode). ByteCodeAddr refers to size of | /// entries. ByteCodeAddr refers to size of indices into the bytecode. | ||||
/// indices into the bytecode. Correctness is checked with static asserts. | |||||
using ByteCodeField = uint16_t; | using ByteCodeField = uint16_t; | ||||
using ByteCodeAddr = uint32_t; | using ByteCodeAddr = uint32_t; | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
// PDLByteCodePattern | // PDLByteCodePattern | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
/// All of the data pertaining to a specific pattern within the bytecode. | /// All of the data pertaining to a specific pattern within the bytecode. | ||||
Show All 19 Lines | |||||
// PDLByteCodeMutableState | // PDLByteCodeMutableState | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
/// This class contains the mutable state of a bytecode instance. This allows | /// This class contains the mutable state of a bytecode instance. This allows | ||||
/// for a bytecode instance to be cached and reused across various different | /// for a bytecode instance to be cached and reused across various different | ||||
/// threads/drivers. | /// threads/drivers. | ||||
class PDLByteCodeMutableState { | class PDLByteCodeMutableState { | ||||
public: | public: | ||||
/// Initialize the state from a bytecode instance. | |||||
void initialize(PDLByteCode &bytecode); | |||||
/// Set the new benefit for a bytecode pattern. The `patternIndex` corresponds | /// Set the new benefit for a bytecode pattern. The `patternIndex` corresponds | ||||
/// to the position of the pattern within the range returned by | /// to the position of the pattern within the range returned by | ||||
/// `PDLByteCode::getPatterns`. | /// `PDLByteCode::getPatterns`. | ||||
void updatePatternBenefit(unsigned patternIndex, PatternBenefit benefit); | void updatePatternBenefit(unsigned patternIndex, PatternBenefit benefit); | ||||
/// Cleanup any allocated state after a match/rewrite has been completed. This | |||||
/// method should be called irregardless of whether the match+rewrite was a | |||||
/// success or not. | |||||
void cleanupAfterMatchAndRewrite(); | |||||
private: | private: | ||||
/// Allow access to data fields. | /// Allow access to data fields. | ||||
friend class PDLByteCode; | friend class PDLByteCode; | ||||
/// The mutable block of memory used during the matching and rewriting phases | /// The mutable block of memory used during the matching and rewriting phases | ||||
/// of the bytecode. | /// of the bytecode. | ||||
std::vector<const void *> memory; | std::vector<const void *> memory; | ||||
/// A mutable block of memory used during the matching and rewriting phase of | |||||
/// the bytecode to store ranges of types. | |||||
std::vector<TypeRange> typeRangeMemory; | |||||
/// A set of type ranges that have been allocated by the byte code interpreter | |||||
/// to provide a guaranteed lifetime. | |||||
std::vector<llvm::OwningArrayRef<Type>> allocatedTypeRangeMemory; | |||||
/// A mutable block of memory used during the matching and rewriting phase of | |||||
/// the bytecode to store ranges of values. | |||||
std::vector<ValueRange> valueRangeMemory; | |||||
/// A set of value ranges that have been allocated by the byte code | |||||
/// interpreter to provide a guaranteed lifetime. | |||||
std::vector<llvm::OwningArrayRef<Value>> allocatedValueRangeMemory; | |||||
/// The up-to-date benefits of the patterns held by the bytecode. The order | /// The up-to-date benefits of the patterns held by the bytecode. The order | ||||
/// of this array corresponds 1-1 with the array of patterns in `PDLByteCode`. | /// of this array corresponds 1-1 with the array of patterns in `PDLByteCode`. | ||||
std::vector<PatternBenefit> currentPatternBenefits; | std::vector<PatternBenefit> currentPatternBenefits; | ||||
}; | }; | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
// PDLByteCode | // PDLByteCode | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
/// The bytecode class is also the interpreter. Contains the bytecode itself, | /// The bytecode class is also the interpreter. Contains the bytecode itself, | ||||
/// the static info, addresses of the rewriter functions, the interpreter | /// the static info, addresses of the rewriter functions, the interpreter | ||||
/// memory buffer, and the execution context. | /// memory buffer, and the execution context. | ||||
class PDLByteCode { | class PDLByteCode { | ||||
public: | public: | ||||
/// Each successful match returns a MatchResult, which contains information | /// Each successful match returns a MatchResult, which contains information | ||||
/// necessary to execute the rewriter and indicates the originating pattern. | /// necessary to execute the rewriter and indicates the originating pattern. | ||||
struct MatchResult { | struct MatchResult { | ||||
MatchResult(Location loc, const PDLByteCodePattern &pattern, | MatchResult(Location loc, const PDLByteCodePattern &pattern, | ||||
PatternBenefit benefit) | PatternBenefit benefit) | ||||
: location(loc), pattern(&pattern), benefit(benefit) {} | : location(loc), pattern(&pattern), benefit(benefit) {} | ||||
MatchResult(const MatchResult &) = delete; | |||||
MatchResult &operator=(const MatchResult &) = delete; | |||||
MatchResult(MatchResult &&other) = default; | |||||
MatchResult &operator=(MatchResult &&) = default; | |||||
/// The location of operations to be replaced. | /// The location of operations to be replaced. | ||||
Location location; | Location location; | ||||
/// Memory values defined in the matcher that are passed to the rewriter. | /// Memory values defined in the matcher that are passed to the rewriter. | ||||
SmallVector<const void *, 4> values; | SmallVector<const void *> values; | ||||
/// Memory used for the range input values. | |||||
SmallVector<TypeRange, 0> typeRangeValues; | |||||
SmallVector<ValueRange, 0> valueRangeValues; | |||||
/// The originating pattern that was matched. This is always non-null, but | /// The originating pattern that was matched. This is always non-null, but | ||||
/// represented with a pointer to allow for assignment. | /// represented with a pointer to allow for assignment. | ||||
const PDLByteCodePattern *pattern; | const PDLByteCodePattern *pattern; | ||||
/// The current benefit of the pattern that was matched. | /// The current benefit of the pattern that was matched. | ||||
PatternBenefit benefit; | PatternBenefit benefit; | ||||
}; | }; | ||||
/// Create a ByteCode instance from the given module containing operations in | /// Create a ByteCode instance from the given module containing operations in | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | private: | ||||
SmallVector<PDLByteCodePattern, 32> patterns; | SmallVector<PDLByteCodePattern, 32> patterns; | ||||
/// A set of user defined functions invoked via PDL. | /// A set of user defined functions invoked via PDL. | ||||
std::vector<PDLConstraintFunction> constraintFunctions; | std::vector<PDLConstraintFunction> constraintFunctions; | ||||
std::vector<PDLRewriteFunction> rewriteFunctions; | std::vector<PDLRewriteFunction> rewriteFunctions; | ||||
/// The maximum memory index used by a value. | /// The maximum memory index used by a value. | ||||
ByteCodeField maxValueMemoryIndex = 0; | ByteCodeField maxValueMemoryIndex = 0; | ||||
/// The maximum number of different types of ranges. | |||||
ByteCodeField maxTypeRangeCount = 0; | |||||
ByteCodeField maxValueRangeCount = 0; | |||||
}; | }; | ||||
} // end namespace detail | } // end namespace detail | ||||
} // end namespace mlir | } // end namespace mlir | ||||
#endif // MLIR_REWRITE_BYTECODE_H_ | #endif // MLIR_REWRITE_BYTECODE_H_ |