brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.1 KiB · 8711c3a Raw
115 lines · cpp
1//===- TestTransformsOps.cpp - Test Transforms ----------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// This file defines transform dialect operations for testing MLIR10// transformations11//12//===----------------------------------------------------------------------===//13 14#include "mlir/Dialect/Affine/IR/AffineOps.h"15#include "mlir/Dialect/Transform/IR/TransformDialect.h"16#include "mlir/Dialect/Transform/Interfaces/TransformInterfaces.h"17#include "mlir/Dialect/Utils/StaticValueUtils.h"18#include "mlir/IR/OpDefinition.h"19#include "mlir/Transforms/RegionUtils.h"20 21#define GET_OP_CLASSES22#include "TestTransformsOps.h.inc"23 24using namespace mlir;25using namespace mlir::transform;26 27#define GET_OP_CLASSES28#include "TestTransformsOps.cpp.inc"29 30DiagnosedSilenceableFailure31transform::TestMoveOperandDeps::apply(TransformRewriter &rewriter,32                                      TransformResults &TransformResults,33                                      TransformState &state) {34  Operation *op = *state.getPayloadOps(getOp()).begin();35  Operation *moveBefore = *state.getPayloadOps(getInsertionPoint()).begin();36  if (failed(moveOperationDependencies(rewriter, op, moveBefore))) {37    auto *listener =38        cast<ErrorCheckingTrackingListener>(rewriter.getListener());39    std::string errorMsg = listener->getLatestMatchFailureMessage();40    (void)emitRemark(errorMsg);41  }42  return DiagnosedSilenceableFailure::success();43}44 45DiagnosedSilenceableFailure46transform::TestMoveValueDefns::apply(TransformRewriter &rewriter,47                                     TransformResults &TransformResults,48                                     TransformState &state) {49  SmallVector<Value> values;50  for (auto tdValue : getValues()) {51    values.push_back(*state.getPayloadValues(tdValue).begin());52  }53  Operation *moveBefore = *state.getPayloadOps(getInsertionPoint()).begin();54  if (failed(moveValueDefinitions(rewriter, values, moveBefore))) {55    auto *listener =56        cast<ErrorCheckingTrackingListener>(rewriter.getListener());57    std::string errorMsg = listener->getLatestMatchFailureMessage();58    (void)emitRemark(errorMsg);59  }60  return DiagnosedSilenceableFailure::success();61}62 63//===----------------------------------------------------------------------===//64// Test affine functionality.65//===----------------------------------------------------------------------===//66DiagnosedSilenceableFailure67transform::TestMakeComposedFoldedAffineApply::applyToOne(68    TransformRewriter &rewriter, affine::AffineApplyOp affineApplyOp,69    ApplyToEachResultList &results, TransformState &state) {70  Location loc = affineApplyOp.getLoc();71  OpFoldResult ofr = affine::makeComposedFoldedAffineApply(72      rewriter, loc, affineApplyOp.getAffineMap(),73      getAsOpFoldResult(affineApplyOp.getOperands()),74      /*composeAffineMin=*/true);75  Value result;76  if (auto v = dyn_cast<Value>(ofr)) {77    result = v;78  } else {79    result = arith::ConstantIndexOp::create(rewriter, loc,80                                            getConstantIntValue(ofr).value());81  }82  results.push_back(result.getDefiningOp());83  rewriter.replaceOp(affineApplyOp, result);84  return DiagnosedSilenceableFailure::success();85}86 87//===----------------------------------------------------------------------===//88// Extension89//===----------------------------------------------------------------------===//90namespace {91 92class TestTransformsDialectExtension93    : public transform::TransformDialectExtension<94          TestTransformsDialectExtension> {95public:96  MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestTransformsDialectExtension)97 98  using Base::Base;99 100  void init() {101    registerTransformOps<102#define GET_OP_LIST103#include "TestTransformsOps.cpp.inc"104        >();105  }106};107} // namespace108 109namespace test {110void registerTestTransformsTransformDialectExtension(111    DialectRegistry &registry) {112  registry.addExtensions<TestTransformsDialectExtension>();113}114} // namespace test115