forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstmt.cc
More file actions
662 lines (567 loc) · 24 KB
/
stmt.cc
File metadata and controls
662 lines (567 loc) · 24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*!
* \file tvm/tir/stmt.cc
*/
#include <tvm/arith/analyzer.h>
#include <tvm/runtime/registry.h>
#include <tvm/tir/op.h>
#include <tvm/tir/op_attr_types.h>
#include <tvm/tir/stmt.h>
#include "buffer_common.h"
namespace tvm {
namespace tir {
// LetStmt
LetStmt::LetStmt(Var var, PrimExpr value, Stmt body, Span span) {
ICHECK(value.defined());
ICHECK(body.defined());
auto vdtype = value.dtype();
// It is still valid to bind a pointer type
// var to a value that is of type handle.
if (var->type_annotation.as<PointerTypeNode>()) {
ICHECK(vdtype.is_handle());
} else {
ICHECK_EQ(value.dtype(), var.dtype());
}
ObjectPtr<LetStmtNode> node = make_object<LetStmtNode>();
node->var = std::move(var);
node->value = std::move(value);
node->body = std::move(body);
node->span = std::move(span);
data_ = std::move(node);
}
TVM_REGISTER_GLOBAL("tir.LetStmt")
.set_body_typed([](Var var, PrimExpr value, Stmt body, Span span) {
return LetStmt(var, value, body, span);
});
TVM_REGISTER_NODE_TYPE(LetStmtNode);
// AttrStmt
AttrStmt::AttrStmt(ObjectRef node, String attr_key, PrimExpr value, Stmt body, Span span) {
auto n = make_object<AttrStmtNode>();
n->node = node;
n->attr_key = std::move(attr_key);
n->value = std::move(value);
n->body = std::move(body);
n->span = std::move(span);
data_ = std::move(n);
}
TVM_REGISTER_GLOBAL("tir.AttrStmt")
.set_body_typed([](ObjectRef node, String attr_key, PrimExpr value, Stmt body, Span span) {
return AttrStmt(node, attr_key, value, body, span);
});
TVM_REGISTER_NODE_TYPE(AttrStmtNode);
// AssertStmt
AssertStmt::AssertStmt(PrimExpr condition, PrimExpr message, Stmt body, Span span) {
ICHECK(condition.defined());
CHECK(condition.dtype().is_bool())
<< "AssertStmt should have boolean condition, "
<< "but received " << condition << " with dtype " << condition.dtype();
ICHECK(message.dtype() == DataType::Int(32) || message.as<StringImmNode>())
<< "TypeError: AssertStmt message must be an int or string:" << message << "\n";
ObjectPtr<AssertStmtNode> node = make_object<AssertStmtNode>();
node->condition = std::move(condition);
node->message = std::move(message);
node->body = std::move(body);
node->span = std::move(span);
data_ = std::move(node);
}
TVM_REGISTER_NODE_TYPE(AssertStmtNode);
TVM_REGISTER_GLOBAL("tir.AssertStmt")
.set_body_typed([](PrimExpr condition, ObjectRef message, Stmt body, Span span) {
if (const auto* str = message.as<StringObj>()) {
auto msg = StringImm(str->data);
return AssertStmt(condition, msg, body, span);
} else {
return AssertStmt(condition, Downcast<PrimExpr>(message), body, span);
}
});
// For
For::For(Var loop_var, PrimExpr min, PrimExpr extent, ForKind kind, Stmt body,
Optional<IterVar> thread_binding, Map<String, ObjectRef> annotations, Span span) {
ICHECK(min.defined());
ICHECK(extent.defined());
ICHECK(min.dtype().is_scalar());
ICHECK(extent.dtype().is_scalar());
ICHECK(loop_var.dtype().is_scalar());
ICHECK(body.defined());
Optional<PrimExpr> maybe_min = tir::try_reset_expr_dtype(loop_var.dtype(), min);
Optional<PrimExpr> maybe_extent = tir::try_reset_expr_dtype(loop_var.dtype(), extent);
CHECK(maybe_min && maybe_extent) << "Incompatible types when binding a loop variable " << loop_var
<< ':' << loop_var.dtype() << " to a range min=" << min << ':'
<< min.dtype() << ", extent=" << extent << ':' << extent.dtype();
min = maybe_min.value();
extent = maybe_extent.value();
ICHECK(loop_var.dtype() == min.dtype()) << loop_var.dtype() << " vs " << min.dtype();
ICHECK(loop_var.dtype() == extent.dtype()) << loop_var.dtype() << " vs " << extent.dtype();
ObjectPtr<ForNode> node = make_object<ForNode>();
node->loop_var = std::move(loop_var);
node->min = std::move(min);
node->extent = std::move(extent);
node->kind = kind;
node->body = std::move(body);
node->thread_binding = std::move(thread_binding);
node->annotations = std::move(annotations);
node->span = std::move(span);
data_ = std::move(node);
}
TVM_REGISTER_GLOBAL("tir.For").set_body_typed(
[](Var loop_var, PrimExpr min, PrimExpr extent, int kind, Stmt body,
Optional<IterVar> thread_binding, Optional<Map<String, ObjectRef>> annotations, Span span) {
return For(loop_var, min, extent, static_cast<ForKind>(kind), body, thread_binding,
annotations.value_or(Map<String, ObjectRef>()), span);
});
TVM_REGISTER_NODE_TYPE(ForNode);
std::ostream& operator<<(std::ostream& out, ForKind type) { // NOLINT(*)
switch (type) {
case ForKind::kSerial:
out << "for";
break;
case ForKind::kParallel:
out << "parallel";
break;
case ForKind::kUnrolled:
out << "unrolled";
break;
case ForKind::kVectorized:
out << "vectorized";
break;
case ForKind::kThreadBinding:
out << "launch_thread";
break;
}
return out;
}
// While
While::While(PrimExpr condition, Stmt body, Span span) {
ICHECK(condition.defined());
ICHECK(condition.dtype().is_scalar());
ICHECK(condition.as<tir::IntImmNode>() == nullptr) << "The condition should not be trivial.";
ICHECK(body.defined());
ObjectPtr<WhileNode> node = make_object<WhileNode>();
node->condition = std::move(condition);
node->body = std::move(body);
node->span = std::move(span);
data_ = std::move(node);
}
TVM_REGISTER_GLOBAL("tir.While").set_body_typed([](PrimExpr condition, Stmt body, Span span) {
return While(condition, body, span);
});
TVM_REGISTER_NODE_TYPE(WhileNode);
// ProducerStore
ProducerStore::ProducerStore(DataProducer producer, PrimExpr value, Array<PrimExpr> indices,
Span span) {
ObjectPtr<ProducerStoreNode> node = make_object<ProducerStoreNode>();
node->producer = std::move(producer);
node->value = std::move(value);
node->indices = std::move(indices);
node->span = std::move(span);
data_ = std::move(node);
}
TVM_REGISTER_GLOBAL("tir.ProducerStore")
.set_body_typed([](DataProducer producer, PrimExpr value, Array<PrimExpr> indices, Span span) {
return ProducerStore(producer, value, indices, span);
});
TVM_REGISTER_NODE_TYPE(ProducerStoreNode);
// Allocate
Allocate::Allocate(Var buffer_var, DataType dtype, Array<PrimExpr> extents, PrimExpr condition,
Stmt body, Map<String, ObjectRef> annotations, Span span) {
CHECK(IsPointerType(buffer_var->type_annotation, dtype) ||
(dtype.is_bool() && IsPointerType(buffer_var->type_annotation, DataType::Int(8))))
<< "The allocated data type (" << dtype
<< ") does not match the type annotation of the buffer " << buffer_var << " ("
<< buffer_var->type_annotation
<< "). The data type should be an element of the pointer type.";
for (size_t i = 0; i < extents.size(); ++i) {
ICHECK(extents[i].defined());
ICHECK(extents[i].dtype().is_scalar());
}
ICHECK(body.defined());
ICHECK(condition.defined());
ICHECK(condition.dtype().is_bool());
ObjectPtr<AllocateNode> node = make_object<AllocateNode>();
node->buffer_var = std::move(buffer_var);
node->dtype = dtype;
node->extents = std::move(extents);
node->condition = std::move(condition);
node->body = std::move(body);
node->annotations = std::move(annotations);
node->span = std::move(span);
data_ = std::move(node);
}
int64_t AllocateNode::ConstantAllocationSize(const Array<PrimExpr>& extents) {
int64_t result = 1;
for (size_t i = 0; i < extents.size(); ++i) {
if (const IntImmNode* int_size = extents[i].as<IntImmNode>()) {
result *= int_size->value;
if (result > std::numeric_limits<int64_t>::max()) {
return 0;
}
} else {
return 0;
}
}
return static_cast<int64_t>(result);
}
TVM_REGISTER_GLOBAL("tir.Allocate")
.set_body_typed([](Var buffer_var, DataType type, Array<PrimExpr> extents, PrimExpr condition,
Stmt body, Map<String, ObjectRef> annotations, Span span) {
return Allocate(buffer_var, type, extents, condition, body, annotations, span);
});
TVM_REGISTER_NODE_TYPE(AllocateNode);
// Const
// The constructor to create a IRNode with constant data
// depending on the type of ObjectRef, it will either
// create AllocateConstNode with irmod_storage_idx or data
AllocateConst::AllocateConst(Var buffer_var, DataType dtype, Array<PrimExpr> extents,
ObjectRef data_or_idx, Stmt body, Map<String, ObjectRef> annotations,
Span span) {
ICHECK(IsPointerType(buffer_var->type_annotation, dtype))
<< "The allocated data type (" << dtype
<< ") does not match the type annotation of the buffer " << buffer_var << " ("
<< buffer_var->type_annotation
<< "). The data type should be an element of the pointer type.";
for (size_t i = 0; i < extents.size(); ++i) {
ICHECK(extents[i].defined());
ICHECK(extents[i].dtype().is_scalar());
}
ICHECK(body.defined());
ICHECK(data_or_idx.defined());
ObjectPtr<AllocateConstNode> node = make_object<AllocateConstNode>();
node->buffer_var = std::move(buffer_var);
node->dtype = dtype;
node->extents = std::move(extents);
node->body = std::move(body);
node->annotations = annotations;
node->span = std::move(span);
if (data_or_idx->IsInstance<runtime::NDArray::ContainerType>()) {
node->data = Optional<tvm::runtime::NDArray>(Downcast<runtime::NDArray>(data_or_idx));
node->irmod_storage_idx = Optional<Integer>();
} else if (data_or_idx->IsInstance<IntImmNode>()) {
node->data = Optional<tvm::runtime::NDArray>();
node->irmod_storage_idx = Optional<Integer>(Downcast<Integer>(data_or_idx));
} else {
LOG(FATAL) << "Data type not supported: " << data_or_idx->GetTypeKey();
}
data_ = std::move(node);
}
int64_t AllocateConstNode::ConstantAllocationSize(const Array<PrimExpr>& extents) {
int64_t result = 1;
for (size_t i = 0; i < extents.size(); ++i) {
if (const IntImmNode* int_size = extents[i].as<IntImmNode>()) {
result *= int_size->value;
if (result > std::numeric_limits<int64_t>::max()) {
return 0;
}
} else {
return 0;
}
}
return static_cast<int64_t>(result);
}
TVM_REGISTER_GLOBAL("tir.AllocateConst")
.set_body_typed([](Var buffer_var, DataType dtype, Array<PrimExpr> extents,
ObjectRef data_or_idx, Stmt body, Map<String, ObjectRef> annotations,
Span span) {
return AllocateConst(buffer_var, dtype, extents, data_or_idx, body, annotations, span);
});
TVM_REGISTER_NODE_TYPE(AllocateConstNode);
// DeclBuffer
DeclBuffer::DeclBuffer(Buffer buffer, Stmt body, Span span) {
ObjectPtr<DeclBufferNode> node = make_object<DeclBufferNode>();
node->buffer = std::move(buffer);
node->body = std::move(body);
node->span = std::move(span);
data_ = std::move(node);
}
TVM_REGISTER_GLOBAL("tir.DeclBuffer").set_body_typed([](Buffer buffer, Stmt body, Span span) {
return DeclBuffer(buffer, body, span);
});
TVM_REGISTER_NODE_TYPE(DeclBufferNode);
// ProducerRealize
ProducerRealize::ProducerRealize(DataProducer producer, Region bounds, PrimExpr condition,
Stmt body, String storage_scope, Span span) {
for (size_t i = 0; i < bounds.size(); ++i) {
ICHECK(bounds[i]->min.defined());
ICHECK(bounds[i]->extent.defined());
ICHECK(bounds[i]->min.dtype().is_scalar());
ICHECK(bounds[i]->extent.dtype().is_scalar());
}
ICHECK(body.defined());
ICHECK(condition.defined());
ICHECK(condition.dtype().is_bool());
ObjectPtr<ProducerRealizeNode> node = make_object<ProducerRealizeNode>();
node->producer = std::move(producer);
node->bounds = std::move(bounds);
node->condition = std::move(condition);
node->body = std::move(body);
node->span = std::move(span);
node->storage_scope = std::move(storage_scope);
data_ = std::move(node);
}
TVM_REGISTER_GLOBAL("tir.ProducerRealize")
.set_body_typed([](DataProducer producer, Region bounds, PrimExpr condition, Stmt body,
String storage_scope, Span span) {
return ProducerRealize(producer, bounds, condition, body, storage_scope, span);
});
TVM_REGISTER_NODE_TYPE(ProducerRealizeNode);
// Prefetch
Prefetch::Prefetch(Buffer buffer, Array<Range> bounds, Span span) {
data_ = make_object<PrefetchNode>(buffer, bounds, span);
}
TVM_REGISTER_GLOBAL("tir.Prefetch")
.set_body_typed([](Buffer buffer, Array<Range> bounds, Span span) {
return Prefetch(buffer, bounds, span);
});
TVM_REGISTER_NODE_TYPE(PrefetchNode);
// SeqStmt
SeqStmt::SeqStmt(Array<Stmt> seq, Span span) {
bool requires_flattening = std::any_of(
seq.begin(), seq.end(), [](const Stmt& stmt) { return stmt->IsInstance<SeqStmtNode>(); });
if (requires_flattening) {
auto flattened = SeqStmt::Flatten(seq);
if (auto* ptr = flattened.as<SeqStmtNode>()) {
seq = ptr->seq;
} else {
seq = {flattened};
}
}
ICHECK_NE(seq.size(), 0) << "An empty SeqStmt is prohibited. "
<< "To write a no-op, use Evaluate(0), "
<< "or the result of SeqStmt::Flatten()";
ICHECK_NE(seq.size(), 1) << "A SeqStmt of length 1 is prohibited. "
<< "Use the node " << seq[0] << "directly, "
<< "or for dynamic usage, normalize using SeqStmt::Flatten()";
auto node = make_object<SeqStmtNode>();
node->seq = std::move(seq);
node->span = std::move(span);
data_ = std::move(node);
}
TVM_REGISTER_GLOBAL("tir.SeqStmt").set_body_typed([](Array<Stmt> seq, Span span) {
return SeqStmt(std::move(seq), span);
});
TVM_REGISTER_NODE_TYPE(SeqStmtNode);
// IfThenElse
IfThenElse::IfThenElse(PrimExpr condition, Stmt then_case, Optional<Stmt> else_case, Span span) {
ICHECK(condition.defined());
ICHECK(then_case.defined());
// else_case may be null.
ObjectPtr<IfThenElseNode> node = make_object<IfThenElseNode>();
node->condition = std::move(condition);
node->then_case = std::move(then_case);
node->else_case = std::move(else_case);
node->span = std::move(span);
data_ = std::move(node);
}
TVM_REGISTER_NODE_TYPE(IfThenElseNode);
TVM_REGISTER_GLOBAL("tir.IfThenElse")
.set_body_typed([](PrimExpr condition, Stmt then_case, Stmt else_case, Span span) {
return IfThenElse(condition, then_case, else_case, span);
});
// Evaluate
Evaluate::Evaluate(PrimExpr value, Span span) {
ICHECK(value.defined());
ObjectPtr<EvaluateNode> node = make_object<EvaluateNode>();
node->value = std::move(value);
node->span = std::move(span);
data_ = std::move(node);
}
TVM_REGISTER_GLOBAL("tir.Evaluate").set_body_typed([](PrimExpr value, Span span) {
return Evaluate(value, span);
});
TVM_REGISTER_NODE_TYPE(EvaluateNode);
// BufferStore
BufferStore::BufferStore(Buffer buffer, PrimExpr value, Array<PrimExpr> indices, Span span) {
ICHECK_EQ(buffer->shape.size(), indices.size())
<< "Buffer " << buffer->name << " is " << buffer->shape.size()
<< "-dimensional, cannot be indexed with the " << indices.size()
<< "-dimensional indices provided.";
for (int i = 0; i < static_cast<int>(indices.size()) - 1; i++) {
ICHECK(indices[i].dtype().is_scalar())
<< "Only the last index of a buffer access may be a vector type.";
}
int index_lanes = indices.size() ? indices.back().dtype().lanes() : 1;
int buffer_lanes = buffer->dtype.lanes();
ICHECK_EQ(index_lanes * buffer_lanes, value.dtype().lanes())
<< "Cannot store value with " << value.dtype().lanes() << ", expected value with "
<< index_lanes * buffer_lanes << " (" << index_lanes << " index lanes * " << buffer_lanes
<< " buffer element lanes)";
if (buffer->dtype.with_lanes(buffer_lanes * index_lanes) != value.dtype()) {
LOG(FATAL) << "TypeError: dtype mismatch on BufferStore: " //
<< "buffer's dtype is `" << buffer->dtype //
<< "`, the lanes of indexing are: `" << index_lanes //
<< "`, but RHS's dtype is `" << value.dtype() << "`";
}
ObjectPtr<BufferStoreNode> node = make_object<BufferStoreNode>();
node->buffer = std::move(buffer);
node->value = std::move(value);
node->indices = std::move(indices);
node->span = std::move(span);
data_ = std::move(node);
}
TVM_REGISTER_GLOBAL("tir.BufferStore")
.set_body_typed([](Buffer buffer, PrimExpr value, Array<PrimExpr> indices, Span span) {
return BufferStore(buffer, value, indices, span);
});
TVM_REGISTER_NODE_TYPE(BufferStoreNode);
// BufferRealize
BufferRealize::BufferRealize(Buffer buffer, Array<Range> bounds, PrimExpr condition, Stmt body,
Span span) {
data_ = make_object<BufferRealizeNode>(buffer, bounds, condition, body, span);
}
TVM_REGISTER_GLOBAL("tir.BufferRealize")
.set_body_typed([](Buffer buffer, Array<Range> bounds, PrimExpr condition, Stmt body,
Span span) { return BufferRealize(buffer, bounds, condition, body, span); });
TVM_REGISTER_NODE_TYPE(BufferRealizeNode);
// BufferRegion
BufferRegion::BufferRegion(Buffer buffer, Array<Range> region) {
CHECK_EQ(buffer->shape.size(), region.size())
<< "The dimension between " << buffer << " and region " << region
<< " mismatched, the buffer is " << buffer;
ObjectPtr<BufferRegionNode> node = make_object<BufferRegionNode>();
node->buffer = std::move(buffer);
node->region = std::move(region);
data_ = std::move(node);
}
BufferRegion BufferRegion::FullRegion(Buffer buffer) {
Array<Range> region;
for (PrimExpr extent : buffer->shape) {
region.push_back(Range::FromMinExtent(0, extent));
}
return BufferRegion(buffer, region);
}
BufferRegion BufferRegion::FromPoint(Buffer buffer, Array<PrimExpr> indices) {
Array<Range> region;
for (const PrimExpr& index : indices) {
if (const RampNode* ramp_index = index.as<RampNode>()) {
region.push_back(
Range::FromMinExtent(ramp_index->base, ramp_index->stride * ramp_index->lanes));
} else {
region.push_back(Range::FromMinExtent(index, make_const(index.dtype(), 1)));
}
}
return BufferRegion(buffer, region);
}
TVM_REGISTER_GLOBAL("tir.BufferRegion").set_body_typed([](Buffer buffer, Array<Range> region) {
return BufferRegion(buffer, region);
});
TVM_REGISTER_NODE_TYPE(BufferRegionNode);
// MatchBufferRegion
MatchBufferRegion::MatchBufferRegion(Buffer buffer, BufferRegion source) {
const Buffer& source_buffer = source->buffer;
arith::Analyzer analyzer;
// Check scope and dtype
CHECK_EQ(buffer.scope(), source_buffer.scope())
<< "MatchBuffer " << buffer << " scope mismatch:" << buffer.scope() << " vs. "
<< source_buffer.scope();
CHECK_EQ(buffer->dtype, source_buffer->dtype)
<< "MatchBuffer " << buffer << " data type mismatch:" << buffer->dtype << " vs. "
<< source_buffer->dtype;
// Check data_alignment
CHECK(source_buffer->data_alignment % buffer->data_alignment == 0)
<< "Trying to match buffer to another one with lower alignment requirement "
<< " required_alignment=" << buffer->data_alignment
<< ", provided_alignment=" << source_buffer->data_alignment;
// Check BufferType. AutoBroadcast is not allowed for now.
CHECK(buffer->buffer_type == BufferType::kDefault &&
source_buffer->buffer_type == BufferType::kDefault)
<< "AutoBroadcast is not allowed in MatchBuffer";
// Validate shape
CHECK(source->region.size() >= buffer->shape.size())
<< "Dimension of source Region expected to be larger or equal than target buffer shape, but "
"got "
<< source->region.size() << " vs. " << buffer->shape.size();
size_t offset = source->region.size() - buffer->shape.size();
for (size_t i = 0; i < offset; ++i) {
CHECK(analyzer.CanProve(source->region[i]->extent == 1))
<< "The higher dimension should be 1, but got " << source->region[i]->extent << ".";
}
for (size_t i = 0; i < buffer->shape.size(); ++i) {
const Range& source_range = source->region[i + offset];
const PrimExpr& buffer_shape = buffer->shape[i];
if (!buffer_shape->IsInstance<VarNode>()) {
CHECK(analyzer.CanProve(source_range->extent == buffer_shape))
<< "The dimension mismatched between source region and target buffer shape, got "
<< source_range->extent << " vs. " << buffer_shape << ".";
}
}
// Note that we do not check elem_offset and strides in this function
// Construction
ObjectPtr<MatchBufferRegionNode> node = make_object<MatchBufferRegionNode>();
node->buffer = std::move(buffer);
node->source = std::move(source);
data_ = std::move(node);
}
TVM_REGISTER_GLOBAL("tir.MatchBufferRegion").set_body_typed([](Buffer buffer, BufferRegion source) {
return MatchBufferRegion(buffer, source);
});
TVM_REGISTER_NODE_TYPE(MatchBufferRegionNode);
// Block
Block::Block(Array<IterVar> iter_vars, Array<BufferRegion> reads, Array<BufferRegion> writes,
String name_hint, Stmt body, Optional<Stmt> init, Array<Buffer> alloc_buffers,
Array<MatchBufferRegion> match_buffers, Map<String, ObjectRef> annotations,
Span span) {
ObjectPtr<BlockNode> node = make_object<BlockNode>();
node->iter_vars = std::move(iter_vars);
node->reads = std::move(reads);
node->writes = std::move(writes);
node->name_hint = std::move(name_hint);
node->body = std::move(body);
node->init = std::move(init);
node->alloc_buffers = std::move(alloc_buffers);
node->match_buffers = std::move(match_buffers);
node->annotations = std::move(annotations);
node->span = std::move(span);
data_ = std::move(node);
}
TVM_REGISTER_GLOBAL("tir.Block")
.set_body_typed([](Array<IterVar> iter_vars, Array<BufferRegion> reads,
Array<BufferRegion> writes, String name_hint, Stmt body, Optional<Stmt> init,
Array<Buffer> alloc_buffers, Array<MatchBufferRegion> match_buffers,
Map<String, ObjectRef> annotations, Span span) {
return Block(iter_vars, reads, writes, name_hint, body, init, alloc_buffers, match_buffers,
annotations, span);
});
TVM_REGISTER_NODE_TYPE(BlockNode);
// BlockRealize
BlockRealize::BlockRealize(Array<PrimExpr> values, PrimExpr predicate, Block block, Span span) {
CHECK_EQ(block->iter_vars.size(), values.size())
<< "ValueError: BlockRealize needs to have the same number of iter_vars and binding values";
CHECK(predicate.dtype().is_bool()) << "TypeError: Expect Block.predicate to be a bool expression";
ObjectPtr<BlockRealizeNode> node = make_object<BlockRealizeNode>();
node->iter_values = std::move(values);
node->predicate = std::move(predicate);
node->block = std::move(block);
node->span = std::move(span);
data_ = std::move(node);
}
TVM_REGISTER_GLOBAL("tir.BlockRealize")
.set_body_typed([](Array<PrimExpr> iter_values, PrimExpr predicate, Block block, Span span) {
return BlockRealize(iter_values, predicate, block, span);
});
TVM_REGISTER_NODE_TYPE(BlockRealizeNode);
PrimExpr TypeAnnotation(DataType dtype, Span span) {
static auto op = Op::Get("tir.type_annotation");
return tir::Call(dtype, op, {}, span);
}
TVM_TIR_REGISTER_OP("type_annotation")
.set_attr<TCallEffectKind>("TCallEffectKind", Integer(CallEffectKind::kPure))
.set_attr<TScriptDtypePrintLocation>("TScriptDtypePrintLocation",
Integer(ScriptDtypePrintLocation::kFirst));
} // namespace tir
} // namespace tvm