Changeset View
Changeset View
Standalone View
Standalone View
mlir/test/lib/Transforms/TestOpaqueLoc.cpp
//===- TestOpaqueLoc.cpp - Pass to test opaque locations ------------------===// | //===- TestOpaqueLoc.cpp - Pass to test opaque locations ------------------===// | ||||
Lint: Lint: clang-format-diff not found in user's PATH; not linting file. | |||||
// | // | ||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||
// See https://llvm.org/LICENSE.txt for license information. | // See https://llvm.org/LICENSE.txt for license information. | ||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||
// | // | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
#include "mlir/Dialect/StandardOps/IR/Ops.h" | #include "mlir/Dialect/StandardOps/IR/Ops.h" | ||||
#include "mlir/IR/Builders.h" | #include "mlir/IR/Builders.h" | ||||
#include "mlir/Pass/Pass.h" | #include "mlir/Pass/Pass.h" | ||||
using namespace mlir; | using namespace mlir; | ||||
namespace { | namespace { | ||||
/// Pass that changes locations to opaque locations for each operation. | /// Pass that changes locations to opaque locations for each operation. | ||||
/// It also takes all operations that are not function operations or | /// It also takes all operations that are not function operations or | ||||
/// terminators and clones them with opaque locations which store the initial | /// terminators and clones them with opaque locations which store the initial | ||||
/// locations. | /// locations. | ||||
struct TestOpaqueLoc : public ModulePass<TestOpaqueLoc> { | struct TestOpaqueLoc : public OperationPass<TestOpaqueLoc, ModuleOp> { | ||||
/// A simple structure which is used for testing as an underlying location in | /// A simple structure which is used for testing as an underlying location in | ||||
/// OpaqueLoc. | /// OpaqueLoc. | ||||
struct MyLocation { | struct MyLocation { | ||||
MyLocation() : id(42) {} | MyLocation() : id(42) {} | ||||
MyLocation(int id) : id(id) {} | MyLocation(int id) : id(id) {} | ||||
int getId() { return id; } | int getId() { return id; } | ||||
int id; | int id; | ||||
}; | }; | ||||
void runOnModule() override { | void runOnOperation() override { | ||||
std::vector<std::unique_ptr<MyLocation>> myLocs; | std::vector<std::unique_ptr<MyLocation>> myLocs; | ||||
int last_it = 0; | int last_it = 0; | ||||
getModule().walk([&](Operation *op) { | getOperation().walk([&](Operation *op) { | ||||
myLocs.push_back(std::make_unique<MyLocation>(last_it++)); | myLocs.push_back(std::make_unique<MyLocation>(last_it++)); | ||||
Location loc = op->getLoc(); | Location loc = op->getLoc(); | ||||
/// Set opaque location without fallback location to test the | /// Set opaque location without fallback location to test the | ||||
/// corresponding get method. | /// corresponding get method. | ||||
op->setLoc( | op->setLoc( | ||||
OpaqueLoc::get<MyLocation *>(myLocs.back().get(), &getContext())); | OpaqueLoc::get<MyLocation *>(myLocs.back().get(), &getContext())); | ||||
Show All 24 Lines | ScopedDiagnosticHandler diagHandler(&getContext(), [](Diagnostic &diag) { | ||||
os << "MyLocation: " << loc->id; | os << "MyLocation: " << loc->id; | ||||
else | else | ||||
os << "nullptr"; | os << "nullptr"; | ||||
} | } | ||||
os << ": " << diag << '\n'; | os << ": " << diag << '\n'; | ||||
os.flush(); | os.flush(); | ||||
}); | }); | ||||
getModule().walk([&](Operation *op) { op->emitOpError(); }); | getOperation().walk([&](Operation *op) { op->emitOpError(); }); | ||||
} | } | ||||
}; | }; | ||||
} // end anonymous namespace | } // end anonymous namespace | ||||
namespace mlir { | namespace mlir { | ||||
void registerTestOpaqueLoc() { | void registerTestOpaqueLoc() { | ||||
PassRegistration<TestOpaqueLoc> pass( | PassRegistration<TestOpaqueLoc> pass( | ||||
"test-opaque-loc", "Changes all leaf locations to opaque locations"); | "test-opaque-loc", "Changes all leaf locations to opaque locations"); | ||||
} | } | ||||
} // namespace mlir | } // namespace mlir |
clang-format-diff not found in user's PATH; not linting file.