1//===- TestTypeOffset.cpp - Test Ptr type offsets ------------------------===//
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-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "mlir/Dialect/Func/IR/FuncOps.h"
10#include "mlir/Dialect/Ptr/IR/PtrOps.h"
11#include "mlir/IR/Builders.h"
12#include "mlir/Interfaces/DataLayoutInterfaces.h"
13#include "mlir/Pass/Pass.h"
14
15using namespace mlir;
16
17namespace {
18struct TestPtrSetTypeOffset
19 : public PassWrapper<TestPtrSetTypeOffset, OperationPass<>> {
20 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestPtrSetTypeOffset)
21
22 StringRef getArgument() const final { return "test-ptr-set-type-offset"; }
23 StringRef getDescription() const final {
24 return "Attach type offsets to ptr.type_offset operations in func.func";
25 }
26
27 void runOnOperation() override {
28 Builder builder(&getContext());
29 getOperation()->walk(callback: [&](func::FuncOp func) {
30 DataLayout layout = DataLayout::closest(op: func);
31 func.walk(callback: [&](ptr::TypeOffsetOp op) {
32 llvm::TypeSize size = op.getTypeSize(layout);
33 op->setAttr(name: "type_offset",
34 value: builder.getIndexAttr(value: size.getKnownMinValue()));
35 if (size.isScalable())
36 op->setAttr(name: "type_offset_scalable", value: builder.getUnitAttr());
37 });
38 });
39 }
40};
41} // namespace
42
43namespace mlir::test {
44void registerTestPtrSetTypeOffset() {
45 PassRegistration<TestPtrSetTypeOffset>();
46}
47} // namespace mlir::test
48