1414#include " mlir/Dialect/Tosa/Transforms/Passes.h"
1515#include " mlir/Dialect/Tosa/Transforms/PassesEnums.cpp.inc"
1616
17- #include < string>
18- #include < unordered_map>
19-
2017#include " mlir/Dialect/Func/IR/FuncOps.h"
2118#include " mlir/Dialect/Tosa/IR/TosaOps.h"
2219#include " mlir/IR/Builders.h"
@@ -99,13 +96,12 @@ static constexpr tosa_level_t TOSA_LEVEL_NONE = {0, 0, 0, 0};
9996struct TosaValidation : public tosa ::impl::TosaValidationBase<TosaValidation> {
10097public:
10198 explicit TosaValidation () { populateConstantOperandChecks (); }
102- explicit TosaValidation (const TosaValidationOptions &options)
103- : TosaValidation() {
99+ explicit TosaValidation (const ValidationOptions &options) : TosaValidation() {
104100 this ->profile = options.profile ;
105- this ->StrictOperationSpecAlignment = options.StrictOperationSpecAlignment ;
101+ this ->StrictOperationSpecAlignment = options.strictOperationSpecAlignment ;
106102 this ->level = options.level ;
107103 }
108- void runOnOperation () final ;
104+ void runOnOperation () override ;
109105
110106 LogicalResult applyConstantOperandCheck (Operation *op) {
111107 for (auto &checker : const_checkers) {
@@ -117,9 +113,6 @@ struct TosaValidation : public tosa::impl::TosaValidationBase<TosaValidation> {
117113
118114 LogicalResult applyLevelCheck (Operation *op);
119115
120- // check variable read/write data types against variable declarations
121- LogicalResult applyVariableCheck (Operation *op);
122-
123116private:
124117 void populateConstantOperandChecks () {
125118 const_checkers.emplace_back (checkConstantOperandPad);
@@ -405,12 +398,8 @@ struct TosaValidation : public tosa::impl::TosaValidationBase<TosaValidation> {
405398 }
406399 }
407400
408- bool CheckVariable (Operation *op);
409- bool CheckVariableReadOrWrite (Operation *op);
410-
411401 SmallVector<std::function<LogicalResult(Operation *)>> const_checkers;
412402 tosa_level_t tosa_level;
413- DenseMap<const mlir::StringAttr *, mlir::Type> variables_map;
414403};
415404
416405LogicalResult TosaValidation::applyLevelCheck (Operation *op) {
@@ -438,69 +427,6 @@ LogicalResult TosaValidation::applyLevelCheck(Operation *op) {
438427 return success ();
439428}
440429
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-
504430void TosaValidation::runOnOperation () {
505431 configLevelAndProfile ();
506432 getOperation ().walk ([&](Operation *op) {
@@ -514,18 +440,18 @@ void TosaValidation::runOnOperation() {
514440 }
515441 }
516442
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.
519444 if (StrictOperationSpecAlignment && failed (applyConstantOperandCheck (op)))
520445 signalPassFailure ();
521446
522447 // do level checks
523448 if (failed (applyLevelCheck (op)))
524449 signalPassFailure ();
525-
526- // do variable type checks
527- if (failed (applyVariableCheck (op)))
528- signalPassFailure ();
529450 });
530451}
531452} // 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