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

Razvan Lupusoru llvmlistbot at llvm.org
Tue Sep 1 16:46:13 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'");
+}
----------------
razvanlupusoru wrote:

I am not sure I follow your concern - this unittest is exercising the utility castPointerLikeTypeIfNeeded. Normally the caller of this utility needs to be providing an acc::PointerLikeType - but this unit test exercises that the utility behaves as expected when neither type is PointerLikeType.

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


More information about the Mlir-commits mailing list