14
14
#include " mlir/Dialect/Tosa/Transforms/Passes.h"
15
15
#include " mlir/Dialect/Tosa/Transforms/PassesEnums.cpp.inc"
16
16
17
- #include < string>
18
- #include < unordered_map>
19
-
20
17
#include " mlir/Dialect/Func/IR/FuncOps.h"
21
18
#include " mlir/Dialect/Tosa/IR/TosaOps.h"
22
19
#include " mlir/IR/Builders.h"
@@ -99,13 +96,12 @@ static constexpr tosa_level_t TOSA_LEVEL_NONE = {0, 0, 0, 0};
99
96
struct TosaValidation : public tosa ::impl::TosaValidationBase<TosaValidation> {
100
97
public:
101
98
explicit TosaValidation () { populateConstantOperandChecks (); }
102
- explicit TosaValidation (const TosaValidationOptions &options)
103
- : TosaValidation() {
99
+ explicit TosaValidation (const ValidationOptions &options) : TosaValidation() {
104
100
this ->profile = options.profile ;
105
- this ->StrictOperationSpecAlignment = options.StrictOperationSpecAlignment ;
101
+ this ->StrictOperationSpecAlignment = options.strictOperationSpecAlignment ;
106
102
this ->level = options.level ;
107
103
}
108
- void runOnOperation () final ;
104
+ void runOnOperation () override ;
109
105
110
106
LogicalResult applyConstantOperandCheck (Operation *op) {
111
107
for (auto &checker : const_checkers) {
@@ -117,9 +113,6 @@ struct TosaValidation : public tosa::impl::TosaValidationBase<TosaValidation> {
117
113
118
114
LogicalResult applyLevelCheck (Operation *op);
119
115
120
- // check variable read/write data types against variable declarations
121
- LogicalResult applyVariableCheck (Operation *op);
122
-
123
116
private:
124
117
void populateConstantOperandChecks () {
125
118
const_checkers.emplace_back (checkConstantOperandPad);
@@ -405,12 +398,8 @@ struct TosaValidation : public tosa::impl::TosaValidationBase<TosaValidation> {
405
398
}
406
399
}
407
400
408
- bool CheckVariable (Operation *op);
409
- bool CheckVariableReadOrWrite (Operation *op);
410
-
411
401
SmallVector<std::function<LogicalResult(Operation *)>> const_checkers;
412
402
tosa_level_t tosa_level;
413
- DenseMap<const mlir::StringAttr *, mlir::Type> variables_map;
414
403
};
415
404
416
405
LogicalResult TosaValidation::applyLevelCheck (Operation *op) {
@@ -438,69 +427,6 @@ LogicalResult TosaValidation::applyLevelCheck(Operation *op) {
438
427
return success ();
439
428
}
440
429
441
- inline bool CompatibleTypes (const mlir::Type &type,
442
- const mlir::Type &declared_type) {
443
- // for now, simply use type equality comparison
444
- return type == declared_type;
445
- }
446
-
447
- bool TosaValidation::CheckVariable (Operation *op) {
448
- if (isa<mlir::tosa::VariableOp>(op)) {
449
- auto name_attr = cast<mlir::StringAttr>(op->getAttr (" name" ));
450
-
451
- if (variables_map.count (&name_attr)) {
452
- op->emitOpError () << " name has already been declared" ;
453
- return false ;
454
- }
455
-
456
- auto type_attr = cast<mlir::TypeAttr>(op->getAttr (" type" ));
457
- mlir::Type type = type_attr.getValue ();
458
-
459
- variables_map[&name_attr] = type;
460
- }
461
-
462
- return true ;
463
- }
464
-
465
- bool TosaValidation::CheckVariableReadOrWrite (Operation *op) {
466
- if (isa<mlir::tosa::VariableReadOp>(op) ||
467
- isa<mlir::tosa::VariableWriteOp>(op)) {
468
- auto name_attr = cast<mlir::StringAttr>(op->getAttr (" name" ));
469
-
470
- if (!variables_map.count (&name_attr)) {
471
- op->emitOpError () << " name has not been declared" ;
472
- return false ;
473
- }
474
-
475
- auto var_type = variables_map[&name_attr];
476
-
477
- for (auto v : op->getOperands ()) {
478
- auto type = v.getType ();
479
- if (!CompatibleTypes (type, var_type)) {
480
- op->emitOpError () << " operand type does not equal variable type" ;
481
- return false ;
482
- }
483
- }
484
-
485
- for (auto v : op->getResults ()) {
486
- auto type = v.getType ();
487
- if (!CompatibleTypes (type, var_type)) {
488
- op->emitOpError () << " result type does not equal variable type" ;
489
- return false ;
490
- }
491
- }
492
- }
493
-
494
- return true ;
495
- }
496
-
497
- LogicalResult TosaValidation::applyVariableCheck (Operation *op) {
498
- if (!CheckVariable (op) || !CheckVariableReadOrWrite (op)) {
499
- return failure ();
500
- }
501
- return success ();
502
- }
503
-
504
430
void TosaValidation::runOnOperation () {
505
431
configLevelAndProfile ();
506
432
getOperation ().walk ([&](Operation *op) {
@@ -514,18 +440,18 @@ void TosaValidation::runOnOperation() {
514
440
}
515
441
}
516
442
517
- // Some uses of TOSA rely on the constant operands of particular
518
- // operations.
443
+ // Some uses of TOSA rely on the constant operands of particular operations.
519
444
if (StrictOperationSpecAlignment && failed (applyConstantOperandCheck (op)))
520
445
signalPassFailure ();
521
446
522
447
// do level checks
523
448
if (failed (applyLevelCheck (op)))
524
449
signalPassFailure ();
525
-
526
- // do variable type checks
527
- if (failed (applyVariableCheck (op)))
528
- signalPassFailure ();
529
450
});
530
451
}
531
452
} // namespace
453
+
454
+ std::unique_ptr<Pass>
455
+ mlir::tosa::createTosaValidationPass (ValidationOptions const &options) {
456
+ return std::make_unique<TosaValidation>(options);
457
+ }
0 commit comments