brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.5 KiB · f44b786 Raw
81 lines · cpp
1//===- TestBuiltinAttributeInterfaces.cpp ---------------------------------===//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#include <utility>10 11#include "TestAttributes.h"12#include "mlir/IR/BuiltinOps.h"13#include "mlir/Pass/Pass.h"14#include "llvm/ADT/TypeSwitch.h"15#include "llvm/Support/FormatVariadic.h"16 17using namespace mlir;18using namespace test;19 20// Helper to print one scalar value, force int8_t to print as integer instead of21// char.22template <typename T>23static void printOneElement(InFlightDiagnostic &os, T value) {24  os << llvm::formatv("{0}", value).str();25}26 27namespace {28struct TestElementsAttrInterface29    : public PassWrapper<TestElementsAttrInterface, OperationPass<ModuleOp>> {30  MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestElementsAttrInterface)31 32  StringRef getArgument() const final { return "test-elements-attr-interface"; }33  StringRef getDescription() const final {34    return "Test ElementsAttr interface support.";35  }36  void runOnOperation() override {37    getOperation().walk([&](Operation *op) {38      for (NamedAttribute attr : op->getAttrs()) {39        auto elementsAttr = dyn_cast<ElementsAttr>(attr.getValue());40        if (!elementsAttr)41          continue;42        testElementsAttrIteration<int64_t>(op, elementsAttr, "int64_t");43        testElementsAttrIteration<uint64_t>(op, elementsAttr, "uint64_t");44        testElementsAttrIteration<APInt>(op, elementsAttr, "APInt");45        testElementsAttrIteration<IntegerAttr>(op, elementsAttr, "IntegerAttr");46      }47    });48  }49 50  template <typename T>51  void testElementsAttrIteration(Operation *op, ElementsAttr attr,52                                 StringRef type) {53    InFlightDiagnostic diag = op->emitError()54                              << "Test iterating `" << type << "`: ";55 56    if (!isa<mlir::IntegerType>(attr.getElementType())) {57      diag << "expected element type to be an integer type";58      return;59    }60 61    auto values = attr.tryGetValues<T>();62    if (!values) {63      diag << "unable to iterate type";64      return;65    }66 67    llvm::interleaveComma(*values, diag, [&](T value) {68      printOneElement(diag, std::move(value));69    });70  }71};72} // namespace73 74namespace mlir {75namespace test {76void registerTestBuiltinAttributeInterfaces() {77  PassRegistration<TestElementsAttrInterface>();78}79} // namespace test80} // namespace mlir81