llvm-project/mlir/unittests/IR/LocationTest.cpp
Aman LaChapelle 759a7b5933
[mlir] Add the ability to define dialect-specific location attrs. (#105584)
This patch adds the capability to define dialect-specific location
attrs. This is useful in particular for defining location structure that
doesn't necessarily fit within the core MLIR location hierarchy, but
doesn't make sense to push upstream (i.e. a custom use case).

This patch adds an AttributeTrait, `IsLocation`, which is tagged onto
all the builtin location attrs, as well as the test location attribute.
This is necessary because previously LocationAttr::classof only returned
true if the attribute was one of the builtin location attributes, and
well, the point of this patch is to allow dialects to define their own
location attributes.

There was an alternate implementation I considered wherein LocationAttr
becomes an AttrInterface, but that was discarded because there are
likely to be *many* locations in a single program, and I was concerned
that forcing every MLIR user to pay the cost of the additional
lookup/dispatch was unacceptable. It also would have been a *much* more
invasive change. It would have allowed for more flexibility in terms of
pretty printing, but it's unclear how useful/necessary that flexibility
would be given how much customizability there already is for attribute
definitions.
2024-10-03 10:25:44 -07:00

54 lines
1.9 KiB
C++

//===- LocationTest.cpp - unit tests for affine map API -------------------===//
//
// 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/Location.h"
#include "mlir/IR/Builders.h"
#include "gtest/gtest.h"
using namespace mlir;
// Check that we only walk *locations* and not non-location attributes.
TEST(LocationTest, Walk) {
MLIRContext ctx;
Builder builder(&ctx);
BoolAttr trueAttr = builder.getBoolAttr(true);
Location loc1 = FileLineColLoc::get(builder.getStringAttr("foo"), 1, 2);
Location loc2 = FileLineColLoc::get(builder.getStringAttr("foo"), 3, 4);
Location fused = builder.getFusedLoc({loc1, loc2}, trueAttr);
SmallVector<Attribute> visited;
fused->walk([&](Location l) {
visited.push_back(LocationAttr(l));
return WalkResult::advance();
});
EXPECT_EQ(llvm::ArrayRef(visited), ArrayRef<Attribute>({fused, loc1, loc2}));
}
// Check that we skip location attrs nested under a non-location attr.
TEST(LocationTest, SkipNested) {
MLIRContext ctx;
Builder builder(&ctx);
Location loc1 = FileLineColLoc::get(builder.getStringAttr("foo"), 1, 2);
Location loc2 = FileLineColLoc::get(builder.getStringAttr("foo"), 3, 4);
Location loc3 = FileLineColLoc::get(builder.getStringAttr("bar"), 1, 2);
Location loc4 = FileLineColLoc::get(builder.getStringAttr("bar"), 3, 4);
ArrayAttr arr = builder.getArrayAttr({loc3, loc4});
Location fused = builder.getFusedLoc({loc1, loc2}, arr);
SmallVector<Attribute> visited;
fused->walk([&](Location l) {
visited.push_back(LocationAttr(l));
return WalkResult::advance();
});
EXPECT_EQ(llvm::ArrayRef(visited), ArrayRef<Attribute>({fused, loc1, loc2}));
}