Skip to content

Commit c66b69e

Browse files
committed
[FIX][IR] Recover rewrite pattern hot paths
1 parent d15355b commit c66b69e

2 files changed

Lines changed: 17 additions & 20 deletions

File tree

src/arith/pattern_match.h

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -199,18 +199,15 @@ class PVar : public Pattern<PVar<T>> {
199199
// Store PVars by reference in the expression.
200200
using Nested = const PVar<T>&;
201201

202-
void InitMatch_() const {
203-
value_ = nullptr;
204-
filled_ = false;
205-
}
202+
void InitMatch_() const { filled_ = false; }
206203

207204
bool Match_(const T& value) const {
208205
if (!filled_) {
209206
value_ = value;
210207
filled_ = true;
211208
return true;
212209
} else {
213-
return PEqualChecker<T>()(value_.value(), value);
210+
return PEqualChecker<T>()(value_, value);
214211
}
215212
}
216213

@@ -226,14 +223,14 @@ class PVar : public Pattern<PVar<T>> {
226223

227224
T Eval() const {
228225
TVM_FFI_ICHECK(filled_);
229-
return value_.value();
226+
return value_;
230227
}
231228

232-
T EvalOr(const T& default_value) const { return filled_ ? value_.value() : default_value; }
229+
T EvalOr(const T& default_value) const { return filled_ ? value_ : default_value; }
233230

234231
protected:
235232
/*! \brief The matched value */
236-
mutable ffi::Optional<T> value_;
233+
mutable T value_;
237234
/*! \brief whether the variable has been filled */
238235
mutable bool filled_{false};
239236
};
@@ -285,7 +282,7 @@ class PVarWithDataType : public PVarWithCheck<PVarWithDataType<T, DType>, T> {
285282
public:
286283
explicit PVarWithDataType(const DType& dtype) : dtype_(dtype) {}
287284

288-
bool Match_(const T& value) const { return dtype_.Match_(value.ty()); }
285+
bool Match_(const T& value) const { return dtype_.Match_(value.ty()->dtype); }
289286

290287
protected:
291288
typename DType::Nested dtype_;
@@ -294,15 +291,15 @@ class PVarWithDataType : public PVarWithCheck<PVarWithDataType<T, DType>, T> {
294291
/*!
295292
* \brief Pattern variable container for data type with lanes.
296293
*/
297-
class PVecDataType : public PVarWithCheck<PVecDataType, PrimType> {
294+
class PVecDataType : public PVarWithCheck<PVecDataType, DLDataType> {
298295
public:
299296
/*! \brief construct vector dtype placeholder with element type check */
300-
explicit PVecDataType(PrimType elem_dtype) : elem_dtype_(elem_dtype) {}
297+
explicit PVecDataType(DLDataType elem_dtype) : elem_dtype_(elem_dtype) {}
301298

302-
bool Match_(PrimType dtype) const { return dtype.code() == elem_dtype_.code(); }
299+
bool Match_(DLDataType dtype) const { return dtype.code == elem_dtype_.code; }
303300

304301
protected:
305-
PrimType elem_dtype_;
302+
DLDataType elem_dtype_;
306303
};
307304

308305
/*!
@@ -543,15 +540,15 @@ class PCastExpr : public Pattern<PCastExpr<DType, TA>> {
543540

544541
bool Match_(const ffi::ObjectRef& node) const {
545542
if (const tirx::CastNode* ptr = node.as<tirx::CastNode>()) {
546-
if (!dtype_.Match_(ptr->ty())) return false;
543+
if (!dtype_.Match_(ptr->ty()->dtype)) return false;
547544
if (!value_.Match_(ptr->value)) return false;
548545
return true;
549546
} else {
550547
return false;
551548
}
552549
}
553550

554-
PrimExpr Eval() const { return tirx::Cast(dtype_.Eval(), value_.Eval()); }
551+
PrimExpr Eval() const { return tirx::Cast(PrimType(dtype_.Eval()), value_.Eval()); }
555552

556553
private:
557554
typename DType::Nested dtype_;
@@ -561,7 +558,7 @@ class PCastExpr : public Pattern<PCastExpr<DType, TA>> {
561558
/*!
562559
* \brief Construct a cast pattern.
563560
*
564-
* \param dtype The target data type, can be PVar<PrimType> or PConst<PrimType>.
561+
* \param dtype The target data type, can be PVar<DLDataType> or PConst<DLDataType>.
565562
* \param value The input type.
566563
*
567564
* \return The result pattern.

src/ir/type.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ PrimType::PrimType(DLDataTypeCode code, int bits, int lanes)
9090
PrimType PrimType::Int(int bits, int lanes) {
9191
if (lanes == 1) {
9292
if (bits == 32) {
93-
static const PrimType i32_ty(DLDataType{kDLInt, 32, 1});
93+
thread_local PrimType i32_ty(DLDataType{kDLInt, 32, 1});
9494
return i32_ty;
9595
}
9696
if (bits == 64) {
97-
static const PrimType i64_ty(DLDataType{kDLInt, 64, 1});
97+
thread_local PrimType i64_ty(DLDataType{kDLInt, 64, 1});
9898
return i64_ty;
9999
}
100100
}
@@ -107,7 +107,7 @@ PrimType PrimType::UInt(int bits, int lanes) {
107107

108108
PrimType PrimType::Float(int bits, int lanes) {
109109
if (bits == 32 && lanes == 1) {
110-
static const PrimType f32_ty(DLDataType{kDLFloat, 32, 1});
110+
thread_local PrimType f32_ty(DLDataType{kDLFloat, 32, 1});
111111
return f32_ty;
112112
}
113113
return PrimType(DLDataType{kDLFloat, static_cast<uint8_t>(bits), static_cast<uint16_t>(lanes)});
@@ -119,7 +119,7 @@ PrimType PrimType::BFloat(int bits, int lanes) {
119119

120120
PrimType PrimType::Bool(int lanes) {
121121
if (lanes == 1) {
122-
static const PrimType bool_ty(DLDataType{kDLBool, 8, 1});
122+
thread_local PrimType bool_ty(DLDataType{kDLBool, 8, 1});
123123
return bool_ty;
124124
}
125125
return PrimType(DLDataType{kDLBool, 8, static_cast<uint16_t>(lanes)});

0 commit comments

Comments
 (0)