[Mlir-commits] [mlir] [nfc][acc] Expose castPointerLikeType API (PR #220426)

Mehdi Amini llvmlistbot at llvm.org
Tue Sep 1 16:56:02 PDT 2026


================
@@ -81,3 +83,67 @@ TEST_F(OpenACCUtilsTypeTest, DynamicMemRefReturnsNullopt) {
   auto memrefTy = MemRefType::get({ShapedType::kDynamic}, b.getI32Type());
   EXPECT_FALSE(getTypeSizeAndAlignment(memrefTy, *module).has_value());
 }
+
+//===----------------------------------------------------------------------===//
+// castPointerLikeTypeIfNeeded Tests
+//===----------------------------------------------------------------------===//
+
+TEST_F(OpenACCUtilsTypeTest, CastPointerLikeTypeMatchingTypeIsNoOp) {
+  OwningOpRef<ModuleOp> module = ModuleOp::create(b, loc);
+  b.setInsertionPointToStart(module->getBody());
+
+  auto memrefTy = MemRefType::get({10}, b.getF32Type());
+  Value alloca = memref::AllocaOp::create(b, loc, memrefTy);
+
+  EXPECT_EQ(castPointerLikeTypeIfNeeded(b, loc, alloca, memrefTy), alloca);
+}
+
+TEST_F(OpenACCUtilsTypeTest, CastPointerLikeTypeGeneratesMemRefCast) {
+  OwningOpRef<ModuleOp> module = ModuleOp::create(b, loc);
+  b.setInsertionPointToStart(module->getBody());
+
+  auto memrefTy = MemRefType::get({10}, b.getF32Type());
+  auto dynamicTy = MemRefType::get({ShapedType::kDynamic}, b.getF32Type());
+  Value alloca = memref::AllocaOp::create(b, loc, memrefTy);
+
+  Value casted = castPointerLikeTypeIfNeeded(b, loc, alloca, dynamicTy);
+
+  EXPECT_EQ(casted.getType(), dynamicTy);
+  ASSERT_TRUE(casted.getDefiningOp<memref::CastOp>());
+}
+
+TEST_F(OpenACCUtilsTypeTest, CastPointerLikeTypeGeneratesAddrSpaceCast) {
+  OwningOpRef<ModuleOp> module = ModuleOp::create(b, loc);
+  b.setInsertionPointToStart(module->getBody());
+
+  auto ptrTy = LLVM::LLVMPointerType::get(&context);
+  auto devicePtrTy = LLVM::LLVMPointerType::get(&context, /*addressSpace=*/1);
+  Value one =
+      LLVM::ConstantOp::create(b, loc, b.getI64Type(), b.getI64IntegerAttr(1));
+  Value alloca = LLVM::AllocaOp::create(b, loc, ptrTy, b.getF32Type(), one);
+
+  Value casted = castPointerLikeTypeIfNeeded(b, loc, alloca, devicePtrTy);
+
+  EXPECT_EQ(casted.getType(), devicePtrTy);
+  ASSERT_TRUE(casted.getDefiningOp<LLVM::AddrSpaceCastOp>());
+}
+
+TEST_F(OpenACCUtilsTypeTest, CastPointerLikeTypeUnsupportedEmitsError) {
+  OwningOpRef<ModuleOp> module = ModuleOp::create(b, loc);
+  b.setInsertionPointToStart(module->getBody());
+
+  Value value =
+      LLVM::ConstantOp::create(b, loc, b.getI32Type(), b.getI32IntegerAttr(0));
+
+  std::string diagnostic;
+  ScopedDiagnosticHandler handler(&context, [&](Diagnostic &diag) {
+    diagnostic = diag.str();
+    return success();
+  });
+
+  Value result = castPointerLikeTypeIfNeeded(b, loc, value, b.getF32Type());
+
+  EXPECT_EQ(result, value);
+  EXPECT_EQ(diagnostic, "unsupported pointer-like type cast from 'i32' to "
+                        "'f32'");
+}
----------------
joker-eph wrote:

The concern is the use of C++ unit-test for things that are usually tested (transitively sometimes) through IR transformations. We even write test passes to test some things that too far the transitive usage. 

So I'm questioning how we end up with C++ unit-tests in the first place.

https://github.com/llvm/llvm-project/pull/220426


More information about the Mlir-commits mailing list