Files
clang-p2996/mlir/lib/Conversion/TosaToMLProgram/TosaToMLProgram.cpp
Tai Ly 04b63ac1ab [tosa] Change VariableOp to align with spec (#142240)
This fixes Tosa VariableOp to align with spec 1.0
  - add var_shape attribute to store shape of variable type
  - change type attribute to store element type of variable type
  - add a builder so previous construction calls still work
- fix up level check of rank to be on variable type instead of initial
value which is optional
  - add level check of size for variable type
  - add lit tests for variable op's without initial values
  - add lit test for variable op with fixed rank but unknown dimension
  - add invalid lit test for variable op with unranked type

Signed-off-by: Tai Ly <tai.ly@arm.com>
2025-06-03 17:41:33 +01:00

78 lines
2.8 KiB
C++

//===- TosaToMLProgram.cpp - Lowering Tosa to MLProgram Dialect------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// These rewriters lower from the TOSA dialect to the MLProgram dialect.
//
//===----------------------------------------------------------------------===//
#include "mlir/Conversion/TosaToMLProgram/TosaToMLProgram.h"
#include "mlir/Dialect/MLProgram/IR/MLProgram.h"
#include "mlir/Dialect/Tosa/IR/TosaOps.h"
#include "mlir/IR/IRMapping.h"
#include "mlir/IR/PatternMatch.h"
using namespace mlir;
using namespace tosa;
namespace {
class VariableOpConverter : public OpRewritePattern<tosa::VariableOp> {
public:
using OpRewritePattern<tosa::VariableOp>::OpRewritePattern;
LogicalResult matchAndRewrite(tosa::VariableOp op,
PatternRewriter &rewriter) const final {
auto variableType = tosa::getVariableType(op);
auto newVariable = rewriter.create<mlir::ml_program::GlobalOp>(
op.getLoc(), op.getName(), variableType, /*is_mutable=*/true,
op.getInitialValueAttr(), /*sym_visibility=*/nullptr);
newVariable.setPrivate();
rewriter.replaceOp(op, newVariable);
return success();
}
};
class VariableWriteOpConverter
: public OpRewritePattern<tosa::VariableWriteOp> {
public:
using OpRewritePattern<tosa::VariableWriteOp>::OpRewritePattern;
LogicalResult matchAndRewrite(tosa::VariableWriteOp op,
PatternRewriter &rewriter) const final {
auto globalSymbolRef =
SymbolRefAttr::get(rewriter.getContext(), op.getName());
auto newVariableWrite = rewriter.create<ml_program::GlobalStoreOp>(
op.getLoc(), globalSymbolRef, op.getInput1());
rewriter.replaceOp(op, newVariableWrite);
return success();
}
};
class VariableReadOpConverter : public OpRewritePattern<tosa::VariableReadOp> {
public:
using OpRewritePattern<tosa::VariableReadOp>::OpRewritePattern;
LogicalResult matchAndRewrite(tosa::VariableReadOp op,
PatternRewriter &rewriter) const final {
auto globalSymbolRef =
SymbolRefAttr::get(rewriter.getContext(), op.getName());
auto newVariableRead = rewriter.create<ml_program::GlobalLoadOp>(
op.getLoc(), op.getType(), globalSymbolRef);
rewriter.replaceOp(op, newVariableRead);
return success();
}
};
} // namespace
void mlir::tosa::populateTosaToMLProgramConversionPatterns(
RewritePatternSet *patterns) {
patterns->add<VariableOpConverter, VariableWriteOpConverter,
VariableReadOpConverter>(patterns->getContext());
}