Files
TheRock/compiler/amd-llvm/mlir/unittests/Dialect/Index/IndexOpsFoldersTest.cpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

105 lines
3.4 KiB
C++

//===- IndexOpsFoldersTest.cpp - unit tests for index op folders ----------===//
//
// 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/Index/IR/IndexDialect.h"
#include "mlir/Dialect/Index/IR/IndexOps.h"
#include "mlir/IR/OwningOpRef.h"
#include "gtest/gtest.h"
using namespace mlir;
namespace {
/// Test fixture for testing operation folders.
class IndexFolderTest : public testing::Test {
public:
IndexFolderTest() { ctx.getOrLoadDialect<index::IndexDialect>(); }
/// Instantiate an operation, invoke its folder, and return the attribute
/// result.
template <typename OpT>
void foldOp(IntegerAttr &value, Type type, ArrayRef<Attribute> operands);
protected:
/// The MLIR context to use.
MLIRContext ctx;
/// A builder to use.
OpBuilder b{&ctx};
};
} // namespace
template <typename OpT>
void IndexFolderTest::foldOp(IntegerAttr &value, Type type,
ArrayRef<Attribute> operands) {
// This function returns null so that `ASSERT_*` works within it.
OperationState state(UnknownLoc::get(&ctx), OpT::getOperationName());
state.addTypes(type);
OwningOpRef<OpT> op = cast<OpT>(b.create(state));
SmallVector<OpFoldResult> results;
LogicalResult result = op->getOperation()->fold(operands, results);
// Propagate the failure to the test.
if (failed(result)) {
value = nullptr;
return;
}
ASSERT_EQ(results.size(), 1u);
value = dyn_cast_or_null<IntegerAttr>(dyn_cast<Attribute>(results.front()));
ASSERT_TRUE(value);
}
TEST_F(IndexFolderTest, TestCastUOpFolder) {
IntegerAttr value;
auto fold = [&](Type type, Attribute input) {
foldOp<index::CastUOp>(value, type, input);
};
// Target width less than or equal to 32 bits.
fold(b.getIntegerType(16), b.getIndexAttr(8000000000));
ASSERT_TRUE(value);
EXPECT_EQ(value.getInt(), 20480u);
// Target width greater than or equal to 64 bits.
fold(b.getIntegerType(64), b.getIndexAttr(2000));
ASSERT_TRUE(value);
EXPECT_EQ(value.getInt(), 2000u);
// Fails to fold, because truncating to 32 bits and then extending creates a
// different value.
fold(b.getIntegerType(64), b.getIndexAttr(8000000000));
EXPECT_FALSE(value);
// Target width between 32 and 64 bits.
fold(b.getIntegerType(40), b.getIndexAttr(0x10000000010000));
// Fold succeeds because the upper bits are truncated in the cast.
ASSERT_TRUE(value);
EXPECT_EQ(value.getInt(), 65536);
// Fails to fold because the upper bits are not truncated.
fold(b.getIntegerType(60), b.getIndexAttr(0x10000000010000));
EXPECT_FALSE(value);
}
TEST_F(IndexFolderTest, TestCastSOpFolder) {
IntegerAttr value;
auto fold = [&](Type type, Attribute input) {
foldOp<index::CastSOp>(value, type, input);
};
// Just test the extension cases to ensure signs are being respected.
// Target width greater than or equal to 64 bits.
fold(b.getIntegerType(64), b.getIndexAttr(-2000));
ASSERT_TRUE(value);
EXPECT_EQ(value.getInt(), -2000);
// Target width between 32 and 64 bits.
fold(b.getIntegerType(40), b.getIndexAttr(-0x10000000010000));
// Fold succeeds because the upper bits are truncated in the cast.
ASSERT_TRUE(value);
EXPECT_EQ(value.getInt(), -65536);
}