-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsair_base.td
599 lines (521 loc) · 22 KB
/
sair_base.td
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
// Copyright 2020 Google LLC
//
// Licensed 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.
#ifndef SAIR_SAIR_BASE_TD_
#define SAIR_SAIR_BASE_TD_
include "mlir/IR/AttrTypeBase.td"
include "mlir/IR/OpBase.td"
def SairDialect : Dialect {
let name = "sair";
let summary = "Structured additive program representation";
let description = [{
Program representation designed to discover optimization opportunities early
in the compilation process and to express implementation decisions without
loosing the high-level structure of the code (hence the "additive" part of
the name).
}];
let cppNamespace = "::sair";
let useDefaultAttributePrinterParser = 0;
}
//===----------------------------------------------------------------------===//
// Sair Attributes
//===----------------------------------------------------------------------===//
def MappingExpr : AttrInterface<"MappingExpr"> {
let description = [{
Interface class for mapping expressions. Mapping expressions
specify how to access a dimension of another domain given the index along
dimensions of the current domain.
}];
let methods = [
InterfaceMethod<
[{Applies a function to all sub-expressions and constructs a result in
post-order}],
"MappingExpr", "Map",
(ins "llvm::function_ref<MappingExpr(MappingExpr)>":$function)
>,
InterfaceMethod<
"Applies a function to all sub-expressions in post-order.",
"void", "Walk",
(ins "llvm::function_ref<void(MappingExpr)>":$function)
>,
InterfaceMethod<
[{Merges two mapping expressions into a single one. Calls $on_leaves to
merge expressions that do not have a sub-expression. Returns nullptr on
failure.}],
"MappingExpr", "Unify",
(ins "MappingExpr":$other,
"llvm::function_ref<MappingExpr(MappingExpr, MappingExpr)>":$on_leaves)
>,
InterfaceMethod<
[{Sets `inverses[i]` to the inverse of the current expression (and its
context) with regard to the i-th dimension of the domain. `inverses` size
must be equal to the size of the domain.
`context_inverse` is the inverse of the surrounding sub-expression. If
the current sub-expression is `e` in `C(e)`, then `context_inverse` is
the inverse of `C(x)`.
Returns a failure if the inverse of this expression cannot be unified
with inverses already set in `inverses`.}],
"mlir::LogicalResult", "SetInverse", (ins
"MappingExpr":$context_inverse,
"llvm::MutableArrayRef<MappingExpr>":$inverses
)
>,
InterfaceMethod<
[{Finds the subexpression corresponding to the inverse of this expression
in `inverse`. May return `none` or `unknown` if the expression is not
surjective and fully specified. `inverse` must be the inverse of the
mapping using this sub-expression.}],
"MappingExpr", "FindInInverse",
(ins "llvm::ArrayRef<MappingExpr>":$inverse)
>,
InterfaceMethod<
[{Converts the expression into an affine expression. The expression must
be fully specified}],
"mlir::AffineExpr", "AsAffineExpr"
>,
InterfaceMethod<
[{Canonicalizes the expression.}], "MappingExpr", "Canonicalize"
>,
];
let extraClassDeclaration = [{
// Substitute dimensions by the given expressions.
MappingExpr SubstituteDims(llvm::ArrayRef<MappingExpr> exprs) const;
// Return a mask of the dimensions appearing in the expression.
llvm::SmallBitVector DependencyMask(int domain_size) const;
// Sets the dimensions appearing in the expression in `mask`.
void SetDependenciesInMask(llvm::SmallBitVector& mask) const;
// Returns `true` if any subexpression is `none`.
bool HasNoneExprs() const;
// Returns `true` if any subexpression is `?`.
bool HasUnknownExprs() const;
// Returns the minimal domain size for the expression to be valid.
int MinDomainSize() const;
}];
}
// Tests if an attribute is an MappingAttr.
def IsSairMappingAttr : CPred<"$_self.isa<MappingAttr>()">;
// Specifies how a Sair value is accessed in the domain of another operation.
def SairMappingAttr : Attr<IsSairMappingAttr, "mapping"> {
let storageType = [{MappingAttr}];
let returnType = storageType;
}
def IsSairNamedMappingAttr : CPred<"$_self.isa<NamedMappingAttr>()">;
def SairNamedMappingAttr : Attr<IsSairNamedMappingAttr, "named_mapping"> {
let storageType = [{NamedMappingAttr}];
let returnType = storageType;
}
// Tests if an attribute is a DomainShapeAttr.
def IsSairDomainShapeAttr : CPred<"$_self.isa<DomainShapeAttr>()">;
// Specifies the shape of a Sair iteration domain.
def SairDomainShapeAttr : Attr<IsSairDomainShapeAttr, "domain shape"> {
let storageType = [{DomainShapeAttr}];
let returnType = storageType;
}
// Derived attribute that returns a domain shape with 0 dimensions.
def SairEmptyDomainShapeAttr :
DerivedAttr<"DomainShapeAttr",
[{return DomainShapeAttr::get(getContext());}]> {
let convertFromStorage = [{$_self}];
}
// Derived attribute that returns the shape of results.
def SairResultDomainShapeAttr :
DerivedAttr<"DomainShapeAttr", [{
mlir::Type type = getOperation()->getResult(0).getType();
return type.cast<ShapedType>().Shape();
}]> {
let convertFromStorage = [{$_self}];
}
// Tests if an attribute is an MappingExpr.
def IsSairMappingExpr : CPred<"$_self.isa<sair::MappingExpr>()">;
// Defines an iterator on a dimension of the domain of the current operation.
def SairMappingExpr : Attr<IsSairMappingExpr, "mapping_expr"> {
let storageType = [{MappingExpr}];
let returnType = storageType;
}
// Identifies a numbered copy of a value to use as operand.
def SairCopyAttr : AttrDef<SairDialect, "Copy"> {
let mnemonic = "copy";
let parameters = (ins "unsigned":$value);
let assemblyFormat = "`<` $value `>`";
}
// Identifies a numbered instance of an operation whose result to use as
// operand.
def SairInstanceAttr : AttrDef<SairDialect, "Instance"> {
let mnemonic = "instance";
let parameters = (ins "unsigned":$value);
let assemblyFormat = "`<` $value `>`";
}
// A predicate that matches either a copy or an instance attribute.
def IsSairCopyOrInstanceAttr : Or<[SairCopyAttr.predicate,
SairInstanceAttr.predicate,
UnitAttr.predicate]>;
def SairCopyOrInstanceAttr : Attr<IsSairCopyOrInstanceAttr,
"Sair copy or instance attribute">;
// Identifies a concrete value to use as operand.
def SairOperandsAttr
: TypedArrayAttrBase<SairCopyOrInstanceAttr,
"array of Sair copy or instance attributes">;
// An attribute describing a loop to generate when lowering an operation.
def SairLoopAttr
: Attr<CPred<"$_self.isa<::sair::LoopAttr>()">, "LoopAttr">;
// A loop nest to generate when lowering an operation.
def SairLoopNestAttr
: OptionalAttr<TypedArrayAttrBase<SairLoopAttr, "array of LoopAttr">>;
// An attribute that specifies how a value is stored in a buffer.
def SairBufferAttr
: Attr<CPred<"$_self.isa<::sair::BufferAttr>()">, "BufferAttr">;
// An attribute that specifies how results of a sair operation are stored in
// memory.
def SairStorageAttr : OptionalAttr<ArrayAttr>;
// An attribute that specifies how to implement an operation.
def SairDecisionsAttr
: Attr<CPred<"$_self.isa<::sair::DecisionsAttr>()">, "DecisionsAttr">;
// An attribute that specifies instances of an operation.
def SairInstancesAttr
: TypedArrayAttrBase<SairDecisionsAttr, "array of Sair decisions">;
// An attribute that specifies copies of the result of an operation.
def SairCopiesAttr
: TypedArrayAttrBase<SairInstancesAttr, "array of array of Sair decisions">;
//===----------------------------------------------------------------------===//
// Sair Types
//===----------------------------------------------------------------------===//
// Type constraint for dimensions.
def SairDimension : Type<CPred<"$_self.isa<DimensionType>()">, "dimension">;
// Type constraint for ranges.
def SairDynRange : Type<CPred<"$_self.isa<DynRangeType>()">, "range">;
// Type constraint for static ranges.
def SairStaticRange
: Type<CPred<"$_self.isa<StaticRangeType>()">, "static_range">;
// Type constraint for Sair values.
def SairValue : Type<CPred<"$_self.isa<ValueType>()">, "value">;
// Predicate that checks the element type of a Sair value.
class SairElementTypePred<Type type>
: SubstLeaves<"$_self", "$_self.cast<ValueType>().ElementType()",
type.predicate>;
// Type constraint for Sair values with a specific element type.
class SairValueOf<Type type>
: Type<And<[SairValue.predicate, SairElementTypePred<type>]>,
"sair.value with " # type.summary # " elements">;
// Type for arrays of mappings.
def SairMappingArrayAttr :
TypedArrayAttrBase<SairMappingAttr, "mappings array">;
//===----------------------------------------------------------------------===//
// Sair Traits
//===----------------------------------------------------------------------===//
// Ensures that two named values have the same element type. Values can be Sair
// values or mlir shaped values.
class SairSameElementType<string lhs, string rhs>
: PredOpTrait<lhs # " and " # rhs # " have the same element type",
CPred<"IsSameElementType(" # lhs # "(), " # rhs # "())">>;
// Ensures that a Sair op has at most one instance specified.
def SingleInstance: PredOpTrait<
"op has at most one instance",
CPred<"!cast<SairOp>($_op).getInstances().has_value() || "
"cast<SairOp>($_op).getInstances()->getValue().size() <= 1">>;
//===----------------------------------------------------------------------===//
// Sair Interfaces
//===----------------------------------------------------------------------===//
def SairOpInterface : OpInterface<"SairOp"> {
let description = [{
Common interface for Sair operations, that requires them to expose their
iteration domain.
The operation must have an "mapping_attr" attribute unless
ValueOperands returns an empty range.
}];
let methods = [
InterfaceMethod<
"Returns the shape of the iteration domain of the operation",
"DomainShapeAttr", "getShape"
>,
InterfaceMethod<
"Returns the dimensions that compose the domain of the operation",
"mlir::Operation::operand_range", "getDomain", (ins), [{}], [{
return mlir::OperandRange(nullptr, 0);
}]
>,
InterfaceMethod<
"Returns the number of dimensions in the domains of produced Sair values",
"int", "results_rank", (ins), [{}], [{
return cast<ConcreteOp>(this->getOperation()).getShape().NumDimensions();
}]
>,
InterfaceMethod<
"Returns the mappings for the !sair.value operands",
"mlir::ArrayAttr", "getMappingArray", (ins), [{}], [{
return mlir::ArrayAttr::get(this->getOperation()->getContext(), {});
}]
>,
InterfaceMethod<
"Sets the mapping for the given Sair value operand",
"void", "SetMapping",
(ins "int":$position, "MappingAttr":$mapping), [{}], [{
::sair::SetMapping($_op, position, mapping);
}]
>,
InterfaceMethod<
"Returns the Sair values accessed by the operation",
"ValueOperandRange", "ValueOperands", (ins), [{}], [{
return ::sair::ValueOperands(ConcreteOp($_op));
}]
>,
InterfaceMethod<
"Returns the operation domain with dependency mappings between dimensions",
"llvm::SmallVector<ValueAccess>", "DomainWithDependencies", (ins), [{}], [{
ConcreteOp op($_op);
llvm::SmallVector<ValueAccess> result;
result.reserve(op.getDomain().size());
for (auto [value, shape_dim] :
llvm::zip(op.getDomain(), op.getShape().Dimensions())) {
result.push_back({value, shape_dim.dependency_mapping()});
}
return result;
}]
>,
InterfaceMethod<
[{Returns a mask of the dimensions of the current operation that depend on
`sair_operand`. The operand must be executed before entering these
dimensions.}],
"llvm::SmallBitVector", "DimsDependingOnOperand",
(ins "int":$sair_operand), [{}], [{
int size = cast<ConcreteOp>(this->getOperation()).getDomain().size();
return llvm::SmallBitVector(size);
}]
>,
InterfaceMethod<
[{Returns the dimensions that must finish before using results}],
"llvm::SmallBitVector", "ResultsDimDependencies",
(ins), [{}], [{
int size = cast<ConcreteOp>(this->getOperation()).getDomain().size();
return llvm::SmallBitVector(size);
}]
>,
InterfaceMethod<
[{Indicates a Sair operand can be definied after this operation}],
"bool", "AllowUseBeforeDef", (ins "int":$sair_operand), [{}],
[{return false;}]
>,
InterfaceMethod<
[{Returns a mask of the dimensions of the current operation that carry
dependencies accross iterations for the given !sair.value operand}],
"llvm::SmallBitVector", "CarryingDimensions",
(ins "int":$sair_operand), [{}], [{
return llvm::SmallBitVector($_op.getDomain().size());
}]
>,
InterfaceMethod<
[{Creates a copy of the operation with a new domain. May steal the body of
the current operation but does not erase it.}],
"SairOp", "ReCreateWithNewDomain", (ins
"llvm::ArrayRef<llvm::SmallVector<mlir::Value>>":$new_domains,
"DomainShapeAttr":$new_shape,
"MappingAttr":$new_to_old_mapping,
"mlir::OpBuilder &":$builder
)
>,
InterfaceMethod<
[{Indicates the size of each sub-domain. The first sub-domain is always
the parallel domain}],
"llvm::SmallVector<int, 2>", "SubDomains", (ins), [{}]
>,
InterfaceMethod<
[{Indicates if the operation has exactly one instance and no copy.}],
"bool", "HasExactlyOneInstance", (ins), [{}], [{
return sair::HasExactlyOneInstance(cast<SairOp>(this->getOperation()));
}]
>,
InterfaceMethod<
[{Returns the instances attribute if present, std::nullopt otherwise.}],
"std::optional<mlir::ArrayAttr>", "getInstances", (ins), [{}],
[{
// PlacehoderOp is a special op that gets introduced during code
// generation and is not expected to have external decisions.
static_assert(std::is_same<ConcreteOp, sair::SairPlaceholderOp>::value,
"Sair ops must have the 'instances' attribute");
return std::nullopt;
}]
>,
InterfaceMethod<
"Returns the number of instances of the operation",
"int", "NumInstances", (ins), [{}], [{
std::optional<mlir::ArrayAttr> instances = $_op.getInstances();
if (!instances.has_value()) return 0;
return instances->size();
}]>,
InterfaceMethod<
"Returns lowering decisions for all instances as an array attribute",
"mlir::ArrayAttr", "GetAllDecisions", (ins), [{}], [{
std::optional<mlir::ArrayAttr> instances = $_op.getInstances();
if (!instances) return mlir::ArrayAttr();
return *instances;
}]
>,
InterfaceMethod<
"Returns lowering decisions for the given operation instance",
"DecisionsAttr", "GetDecisions", (ins "int":$instance), [{}], [{
mlir::ArrayAttr instances = *$_op.getInstances();
return instances.getValue()[instance].cast<DecisionsAttr>();
}]
>,
InterfaceMethod<
"Sets lowering decisions for the operation",
"void", "SetDecisions", (ins "int":$instance, "DecisionsAttr":$value),
[{}], [{
auto instances = llvm::to_vector<4>(
$_op.getInstances()->getValue());
instances[instance] = value;
$_op.setInstancesAttr(mlir::ArrayAttr::get($_op.getContext(), instances));
}]
>,
InterfaceMethod<
"Appends an instance to the list of instances",
"void", "AddInstance", (ins "DecisionsAttr":$value),
[{}], [{
llvm::SmallVector<mlir::Attribute> instances;
if ($_op.getInstances().has_value()) {
instances = llvm::to_vector<4>(
$_op.getInstances()->getValue());
}
instances.push_back(value);
$_op.setInstancesAttr(mlir::ArrayAttr::get($_op.getContext(), instances));
}]
>
];
let verify = [{return VerifySairOp(op);}];
let extraClassDeclaration = [{
// The string identifier used for mapping attribute in Sair ops.
static constexpr llvm::StringRef kMappingAttrName = "mapping_array";
// The string identifier used for instances attribute in Sair ops.
static constexpr llvm::StringRef kInstancesAttrName = "instances";
}];
}
// Base class for all Sair operators.
class SairOp<string mnemonic,
list<Trait> traits = [],
Dialect dialect = SairDialect>
: Op<
dialect, mnemonic,
!listconcat(traits, [
SairOpInterface,
DeclareOpInterfaceMethods<SairOpInterface>
])
> {
let hasCanonicalizer = 1;
}
def SairComputeOp : OpInterface<"ComputeOp"> {
let description = [{
A Sair operation that performs a computation and results in actual code.
This is as oppposition to operations defining iteration dimensions and
casting sair to/from Sair values.
Assumes that the operation has an optional `loop_nest` attribute of type
`SairLoopNestAttr`. Checks dimension names are only used once in the loop
nest, that dimensions referenced by the loop nest are dimensions of the
domain, and that the loop nest iterates on the full domain.
}];
}
def SairValueProducerOp : OpInterface<"ValueProducerOp"> {
let description = [{
A Sair operation that produces Sair values.
Value producer operations have a `copies` attribute that lists copy
operations to insert to make copies of the operation results. The copy
attribute is an array of arrays, with one array for each result of the
operation. The array corresponding to a result contains `DecisionAttr`
attributes that describes decisions for the copy operation to insert.
}];
let methods = [
InterfaceMethod<
"List of copies of the given result. This is an array of DecisionAttr.",
"llvm::ArrayRef<mlir::Attribute>", "GetCopies", (ins "int":$result), [{}], [{
auto all_copies = $_op.getCopiesAttr();
if (all_copies == nullptr) return {};
return all_copies.getValue()[result]
.template cast<mlir::ArrayAttr>().getValue();
}]>,
InterfaceMethod<
"Indicates if the operation has any copy set in its `copies` attribute`",
"bool", "HasCopies", (ins), [{}], [{
auto all_copies = $_op.getCopiesAttr();
if (all_copies == nullptr) return false;
return llvm::any_of(all_copies.getValue(), [](mlir::Attribute attr) {
return !attr.cast<mlir::ArrayAttr>().empty();
});
}]>,
InterfaceMethod<
"Set decisions for the given copy of the given result.",
"void", "SetCopy",
(ins "int":$result, "int":$copy, "DecisionsAttr":$decisions), [{}], [{
auto all_copies = llvm::to_vector<4>(*$_op.getCopies());
auto result_copies_attr = all_copies[result].template cast<mlir::ArrayAttr>();
auto result_copies = llvm::to_vector<4>(result_copies_attr.getValue());
result_copies[copy] = decisions;
mlir::MLIRContext *context = $_op.getContext();
all_copies[result] = mlir::ArrayAttr::get(context, result_copies);
$_op.setCopiesAttr(mlir::ArrayAttr::get(context, all_copies));
}]>
];
let extraClassDeclaration = [{
static constexpr llvm::StringRef kCopiesAttrName = "copies";
}];
let verify = [{return sair::VerifyValueProducerOp(op);}];
}
def SairInferIterationSpaceOp : OpInterface<"InferIterationSpaceOp"> {
let description = [{
A Sair operation whose loop nest is infered from the loop nest of an
operand.
}];
let methods = [
InterfaceMethod<
"Position of the Sair value operand to inherit the iteration space from.",
"int", "infer_iteration_space_operand"
>
];
}
def SairRangeOp : OpInterface<"RangeOp"> {
let description = [{A Sair operation that defines a !sair.range}];
let methods = [
InterfaceMethod<"Returns the range step", "int", "Step">,
InterfaceMethod<
"Returns the upper bound of the range", "ValueOrConstant", "LowerBound"
>,
InterfaceMethod<
"Returns the lower bound of the range", "ValueOrConstant", "UpperBound"
>,
];
let extraClassDeclaration = [{
static constexpr llvm::StringRef kStepAttrName = "step";
}];
}
def SairFromToMemRefOp : OpInterface<"FromToMemRefOp"> {
let description = [{Sair operation that accesses an external memref}];
let methods = [
InterfaceMethod<"Domain dimensions not mapping to memref dimensions",
"ValueRange", "getParallelDomain">,
InterfaceMethod<"Domain dimensions mapping to memref dimensions",
"ValueRange", "getMemrefDomain">,
InterfaceMethod<"Memref accessed", "ValueOperand", "MemRef">,
InterfaceMethod<"Buffer name", "llvm::StringRef", "getBufferName">,
InterfaceMethod<"Memref type", "mlir::MemRefType", "MemRefType", (ins),
[{}], [{
return $_op.MemRef()
.GetType()
.ElementType()
.template cast<mlir::MemRefType>();
}]>,
InterfaceMethod<"Mapping from value domain to layout", "MappingAttr",
"Layout", (ins), [{}], [{
return MappingAttr::GetIdentity(
$_op.getContext(), $_op.getMemrefDomain().size())
.ShiftRight($_op.getParallelDomain().size());
}]>
];
}
#endif // SAIR_SAIR_BASE_TD_