-
Notifications
You must be signed in to change notification settings - Fork 235
[Fix] Fix bug 0905: tilelang doesn't vectorize B[i,j] = c[i] + A[i,j]
#798
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
96128ed
1fb176a
c08b815
9ff2008
cbf7b3c
d8ec462
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -24,17 +24,14 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include "loop_vectorize.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include <tvm/arith/iter_affine_map.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include <tvm/tir/builtin.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include <tvm/tir/stmt_functor.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include <numeric> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include "../layout/layout.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include "../layout/utils.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include "arith/int_operator.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include "arith/ir_visitor_with_analyzer.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include "common/loop_vectorization_utils.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include "tvm/tir/analysis.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include "tvm/tir/var.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include <tvm/arith/iter_affine_map.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include <tvm/tir/builtin.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include <tvm/tir/stmt_functor.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
namespace tvm { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
namespace tl { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -56,15 +53,18 @@ class VectorizePlanner : public arith::IRVisitorWithAnalyzer { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return vector_size_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bool GetDynamic() { return dynamic_; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
PrimExpr GetCondition() { return condition_; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
void VisitStmt_(const ForNode *node) final { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inner_for_ = node; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
iter_map_.Set(node->loop_var, Range(node->min, node->extent)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
auto extent_ptr = as_const_int(node->extent); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Here I disable dynamic shape completely, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// In order to do it, the Planner should accept an analyzer with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// arithmetic info outside to prove the dividiblity of vector size | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!extent_ptr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
vector_size_ = 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
vector_size_ = arith::ZeroAwareGCD(vector_size_, *extent_ptr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
arith::IRVisitorWithAnalyzer::VisitStmt_(node); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -113,76 +113,47 @@ class VectorizePlanner : public arith::IRVisitorWithAnalyzer { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
void UpdateVectorSize(const Array<PrimExpr> &indices, const Buffer &buffer) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!inner_for_) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
auto extent_ptr = inner_for_->extent.as<IntImmNode>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!extent_ptr) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// 1. Compute raw element offset | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
auto strides = buffer->strides; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (buffer->strides.empty()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
PrimExpr stride = 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for (int i = indices.size() - 1; i >= 0; --i) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
strides.push_back(stride); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
stride = stride * buffer->shape[i]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
strides = Array<PrimExpr>{strides.rbegin(), strides.rend()}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
PrimExpr elem_offset = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+117
to
+126
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reverse-stride construction is broken; Array doesn’t support rbegin/rend (build blocker).
- auto strides = buffer->strides;
- if (buffer->strides.empty()) {
- PrimExpr stride = 1;
- for (int i = indices.size() - 1; i >= 0; --i) {
- strides.push_back(stride);
- stride = stride * buffer->shape[i];
- }
- strides = Array<PrimExpr>{strides.rbegin(), strides.rend()};
- }
+ Array<PrimExpr> strides = buffer->strides;
+ if (strides.empty()) {
+ // Build row-major strides then reverse to match index order.
+ Array<PrimExpr> tmp;
+ PrimExpr stride = 1;
+ for (int i = static_cast<int>(indices.size()) - 1; i >= 0; --i) {
+ tmp.push_back(stride);
+ stride = stride * buffer->shape[i];
+ }
+ strides = Array<PrimExpr>();
+ for (int i = static_cast<int>(tmp.size()) - 1; i >= 0; --i) {
+ strides.push_back(tmp[i]);
+ }
+ } Also applies to: 127-129 🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for (int i = 0; i < indices.size(); ++i) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
elem_offset += indices[i] * strides[i]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// 2. If element offset is independent with loop_var, ignore it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (CanProveIndependent(elem_offset, inner_for_->loop_var, &analyzer_)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const DataType &access_type = buffer->dtype; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// i // 2, i % 8 can also be vectorized as factor 16 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
int max_vector_size = vector_load_bits_max_ / access_type.bits(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// so we should disable this GCD optimization | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
max_vector_size = arith::ZeroAwareGCD(max_vector_size, extent_ptr->value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
auto last_dim = buffer->shape.back(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
auto mod_set = analyzer_.modular_set(last_dim); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// when dynamic shape like [m, k]: coeff=1, base=0, GCD will block | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// conditionally tail vectorize | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (buffer->shape.back().as<IntImmNode>()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
max_vector_size = arith::ZeroAwareGCD(max_vector_size, mod_set->coeff); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
auto gcd_base = arith::ZeroAwareGCD(max_vector_size, mod_set->base); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// If gcd_base is equal to the last dimension, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// we should analyze the second-to-last dimension | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// in relation to the last dimension. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (gcd_base < Downcast<IntImm>(last_dim)->value) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
max_vector_size = gcd_base; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
vector_size_ = arith::ZeroAwareGCD(max_vector_size, vector_size_); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Generate strides if not existed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
auto strides = buffer->strides; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (buffer->strides.empty()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
PrimExpr stride = 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for (int i = indices.size() - 1; i >= 0; --i) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
strides.push_back(stride); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
stride = stride * buffer->shape[i]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
strides = Array<PrimExpr>{strides.rbegin(), strides.rend()}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// 3. Tight vectorize bound | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
vector_size_ = arith::ZeroAwareGCD(vector_size_, vector_load_bits_max_ / | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
buffer->dtype.bits()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Generate and check element offset expression | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ICHECK(indices.size() == strides.size()) << "Invalid indices and strides"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
PrimExpr elem_offset = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for (int i = 0; i < indices.size(); ++i) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
elem_offset += indices[i] * strides[i]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
while (!IndiceCanVectorize(elem_offset, inner_for_->loop_var, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inner_for_->extent, vector_size_, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
&analyzer_)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
vector_size_ /= 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else if (vector_size_ <= vector_load_bits_max_ / buffer->dtype.bits()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// dynamic shape load: get the vectorization condition | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dynamic_ = true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
PrimExpr offset = buffer.OffsetOf(indices).back(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
condition_ = (FloorMod(offset, vector_size_) == 0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// 4. Try to vectorize buffer load | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
while (!IndiceCanVectorize(elem_offset, inner_for_->loop_var, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inner_for_->extent, vector_size_, &analyzer_)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
vector_size_ /= 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const int vector_load_bits_max_ = 128; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const ForNode *inner_for_{}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Map<Var, Range> iter_map_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bool has_nonlocal_memory_access_ = false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
int vector_size_ = 128; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// conditionally vectorize | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bool dynamic_ = false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
PrimExpr condition_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class VectorizeRewriter : public StmtExprMutator { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
VectorizeRewriter(const VectorizePlanResult &plan) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
: vector_size_(plan.vector_size), condition_(plan.condition), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dynamic_(plan.dynamic) {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
VectorizeRewriter(int vector_size) : vector_size_(vector_size) {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Stmt VisitStmt_(const ForNode *node) final { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -197,23 +168,19 @@ class VectorizeRewriter : public StmtExprMutator { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ICHECK(extent % vector_size_ == 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<< "extent: " << extent << " vector_size_: " << vector_size_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ICHECK(is_zero(fnode->min)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!dynamic_) { // check dynamic shape | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (extent == vector_size_) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fnode.CopyOnWrite()->kind = ForKind::kVectorized; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return fnode; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Var inner_var = Var("vec"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Var outer_var = Var(old_var->name_hint); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Map<Var, PrimExpr> vmap; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
vmap.Set(fnode->loop_var, outer_var * vector_size_ + inner_var); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Stmt body = Substitute(fnode->body, vmap); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
body = For(inner_var, 0, vector_size_, ForKind::kVectorized, body); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
body = For(outer_var, 0, extent / vector_size_, fnode->kind, body, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fnode->thread_binding, fnode->annotations, fnode->span); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return body; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (extent == vector_size_) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fnode.CopyOnWrite()->kind = ForKind::kVectorized; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return fnode; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Var inner_var = Var("vec"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Var outer_var = Var(old_var->name_hint); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Map<Var, PrimExpr> vmap; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
vmap.Set(fnode->loop_var, outer_var * vector_size_ + inner_var); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Stmt body = Substitute(fnode->body, vmap); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
body = For(inner_var, 0, vector_size_, ForKind::kVectorized, body); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
body = For(outer_var, 0, extent / vector_size_, fnode->kind, body, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fnode->thread_binding, fnode->annotations, fnode->span); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return body; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+171
to
184
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve loop var dtype and extents when splitting; current code may mis-type on non-i32 loops.
- if (extent == vector_size_) {
+ if (extent == vector_size_) {
fnode.CopyOnWrite()->kind = ForKind::kVectorized;
return fnode;
} else {
- Var inner_var = Var("vec");
- Var outer_var = Var(old_var->name_hint);
+ DataType it = old_var->dtype;
+ Var inner_var = Var("vec", it);
+ Var outer_var = Var(old_var->name_hint, it);
Map<Var, PrimExpr> vmap;
vmap.Set(fnode->loop_var, outer_var * vector_size_ + inner_var);
Stmt body = Substitute(fnode->body, vmap);
- body = For(inner_var, 0, vector_size_, ForKind::kVectorized, body);
- body = For(outer_var, 0, extent / vector_size_, fnode->kind, body,
+ PrimExpr inner_extent = IntImm(it, vector_size_);
+ PrimExpr outer_extent = IntImm(it, extent / vector_size_);
+ body = For(inner_var, IntImm(it, 0), inner_extent, ForKind::kVectorized, body);
+ body = For(outer_var, IntImm(it, 0), outer_extent, fnode->kind, body,
fnode->thread_binding, fnode->annotations, fnode->span);
return body;
} 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return ret; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -222,18 +189,25 @@ class VectorizeRewriter : public StmtExprMutator { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const ForNode *inner_for_{}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const int vector_size_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const PrimExpr condition_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const bool dynamic_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
int GetVectorizeSize(const For &loop) { return VectorizePlanner().Plan(loop); } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
VectorizePlanResult GetVectorizePlanResult(const For &loop) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
VectorizePlanner planner; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
int vector_size = planner.Plan(loop); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bool dynamic = planner.GetDynamic(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
PrimExpr condition = planner.GetCondition(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return {vector_size, dynamic, condition}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bool CanProveIndependent(const PrimExpr &expr, Var var, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
arith::Analyzer *analyzer) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// 1. if var doesn't exist, it is independent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bool used_var = UsesVar( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expr, [&](const VarNode *v) { return GetRef<Var>(v).same_as(var); }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!used_var) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// 2. if \forall v_1, v_2, f(v_1) == f(v_2), f is independent with v | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Var var_1("_t", var.dtype()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
auto expr_1 = Substitute(expr, {{var, var_1}}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (analyzer->CanProveEqual(expr, expr_1)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bool IndiceCanVectorize(const PrimExpr &expr, Var var, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -280,14 +254,13 @@ bool IndiceCanVectorize(const PrimExpr &expr, Var var, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
For VectorizeLoop(const For &loop, int vectorize_hint) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
VectorizePlanResult res{128, false, 0}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (vectorize_hint <= 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
res = GetVectorizePlanResult(loop); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
vectorize_hint = res.vector_size; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
VectorizePlanner planner; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
vectorize_hint = planner.Plan(loop); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (vectorize_hint == 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return loop; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
auto rewriter = VectorizeRewriter(res); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
auto rewriter = VectorizeRewriter(vectorize_hint); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return Downcast<For>(rewriter(loop)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don’t blanket-disable vectorization on dynamic extents; this regresses prior behavior.
Early-returning with
vector_size_ = 1
for non-constant extents disables vectorization on dynamic loops entirely. At minimum, keep the optimistic upper bound and letUpdateVectorSize
shrink it, or gate this behind a flag.📝 Committable suggestion
🤖 Prompt for AI Agents