[Mlir-commits] [mlir] [mlir][tosa] Add integer type conversion pass (PR #232853)

Jack Frankland llvmlistbot at llvm.org
Wed Oct 28 05:02:46 PDT 2026


================
@@ -0,0 +1,254 @@
+//===- TosaConvertIntegerTypesBitwidth.cpp - Convert TOSA integers --------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Dialect/Tosa/Transforms/Passes.h"
+
+#include "mlir/Dialect/Func/IR/FuncOps.h"
+#include "mlir/Dialect/Tosa/IR/TosaOps.h"
+#include "mlir/IR/AttrTypeSubElements.h"
+#include "mlir/IR/DialectResourceBlobManager.h"
+
+#include "llvm/ADT/APInt.h"
+#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/MathExtras.h"
+
+#include <cstddef>
+#include <cstdint>
+#include <optional>
+#include <utility>
+
+namespace llvm::cl {
+template <>
+class parser<std::pair<unsigned, unsigned>>
+    : public basic_parser<std::pair<unsigned, unsigned>> {
+public:
+  parser(Option &option) : basic_parser(option) {}
+
+  bool parse(Option &option, StringRef argName, StringRef arg,
+             std::pair<unsigned, unsigned> &value) {
+    auto [source, destination] = arg.split(':');
+    if (source.getAsInteger(10, value.first) ||
+        destination.getAsInteger(10, value.second))
+      return option.error("expected <source>:<destination> integer bitwidths",
+                          argName);
+    return false;
+  }
+
+  StringRef getValueName() const override { return "source:destination"; }
+
+  static void print(raw_ostream &os,
+                    const std::pair<unsigned, unsigned> &value) {
+    os << value.first << ":" << value.second;
+  }
+};
+} // namespace llvm::cl
+
+namespace mlir::tosa {
+#define GEN_PASS_DEF_TOSACONVERTINTEGERTYPESBITWIDTHPASS
+#include "mlir/Dialect/Tosa/Transforms/Passes.h.inc"
+} // namespace mlir::tosa
+
+using namespace mlir;
+
+namespace {
+APInt convertValue(const APInt &value, Type type, unsigned width) {
+  return value.sextOrTrunc(width);
+}
+
+// Convert a validated byte-aligned payload, retaining storage for every
+// element. In particular, each i4 element occupies one byte, not half a byte.
+SmallVector<char> convertRawValues(ArrayRef<char> rawData, IntegerType type,
+                                   IntegerType convertedType) {
+  const unsigned inputBytes = llvm::divideCeil(type.getWidth(), 8);
+  const unsigned outputBytes = llvm::divideCeil(convertedType.getWidth(), 8);
+  const size_t numElements = rawData.size() / inputBytes;
+  SmallVector<char> data(numElements * outputBytes);
+  const auto *input = reinterpret_cast<const uint8_t *>(rawData.data());
+  auto *output = reinterpret_cast<uint8_t *>(data.data());
+  for (size_t i = 0; i < numElements; ++i) {
+    APInt value(inputBytes * 8, 0);
+    llvm::LoadIntFromMemory(value, input + i * inputBytes, inputBytes);
+    // Discard padding bits before interpreting the source sign bit.
+    value = value.trunc(type.getWidth());
+    llvm::StoreIntToMemory(convertValue(value, type, convertedType.getWidth()),
+                           output + i * outputBytes, outputBytes);
+  }
+  return data;
+}
+
+class IntegerTypeReplacer : public AttrTypeReplacer {
+public:
+  // The mappings must outlive this replacer.
----------------
FranklandJack wrote:

Okay fair enough.

https://github.com/llvm/llvm-project/pull/232853


More information about the Mlir-commits mailing list