aboutsummaryrefslogtreecommitdiff
path: root/mlir/test/lib/IR/TestOpaqueLoc.cpp
blob: c0ce8965868ab0f7d0e7651bf44f0b06f289fe6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//===- TestOpaqueLoc.cpp - Pass to test opaque locations ------------------===//
//
// 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/IR/Builders.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/Pass/Pass.h"

using namespace mlir;

namespace {
/// A simple structure which is used for testing as an underlying location in
/// OpaqueLoc.
struct MyLocation {
  MyLocation() = default;
  MyLocation(int id) : id(id) {}
  int getId() { return id; }

  int id{42};
};
} // namespace

MLIR_DECLARE_EXPLICIT_TYPE_ID(MyLocation *)
MLIR_DEFINE_EXPLICIT_TYPE_ID(MyLocation *)

namespace {
/// Pass that changes locations to opaque locations for each operation.
/// It also takes all operations that are not function operations or
/// terminators and clones them with opaque locations which store the initial
/// locations.
struct TestOpaqueLoc
    : public PassWrapper<TestOpaqueLoc, OperationPass<ModuleOp>> {
  MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestOpaqueLoc)

  StringRef getArgument() const final { return "test-opaque-loc"; }
  StringRef getDescription() const final {
    return "Changes all leaf locations to opaque locations";
  }

  void runOnOperation() override {
    std::vector<std::unique_ptr<MyLocation>> myLocs;
    int lastIt = 0;

    getOperation().getBody()->walk([&](Operation *op) {
      myLocs.push_back(std::make_unique<MyLocation>(lastIt++));

      Location loc = op->getLoc();

      /// Set opaque location without fallback location to test the
      /// corresponding get method.
      op->setLoc(
          OpaqueLoc::get<MyLocation *>(myLocs.back().get(), &getContext()));

      if (isa<ModuleOp>(op->getParentOp()) ||
          op->hasTrait<OpTrait::IsTerminator>())
        return;

      OpBuilder builder(op);

      /// Add the same operation but with fallback location to test the
      /// corresponding get method and serialization.
      Operation *opCloned1 = builder.clone(*op);
      opCloned1->setLoc(OpaqueLoc::get<MyLocation *>(myLocs.back().get(), loc));

      /// Add the same operation but with void* instead of MyLocation* to test
      /// getUnderlyingLocationOrNull method.
      Operation *opCloned2 = builder.clone(*op);
      opCloned2->setLoc(OpaqueLoc::get<void *>(nullptr, loc));
    });

    ScopedDiagnosticHandler diagHandler(&getContext(), [](Diagnostic &diag) {
      auto &os = llvm::outs();
      if (isa<OpaqueLoc>(diag.getLocation())) {
        MyLocation *loc = OpaqueLoc::getUnderlyingLocationOrNull<MyLocation *>(
            diag.getLocation());
        if (loc)
          os << "MyLocation: " << loc->id;
        else
          os << "nullptr";
      }
      os << ": " << diag << '\n';
      os.flush();
    });

    getOperation().walk([&](Operation *op) { op->emitOpError(); });
  }
};

} // namespace

namespace mlir {
namespace test {
void registerTestOpaqueLoc() { PassRegistration<TestOpaqueLoc>(); }
} // namespace test
} // namespace mlir