[clang] [CIR] Upstream handling of integral-to-pointer casts (PR #161653)
Shawn K via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 7 11:03:29 PDT 2025
https://github.com/kimsh02 updated https://github.com/llvm/llvm-project/pull/161653
>From eec6869a048bb92fd58efbe811412196a8d9d783 Mon Sep 17 00:00:00 2001
From: kimsh02 <kimshawn02 at icloud.com>
Date: Thu, 2 Oct 2025 04:37:05 -0700
Subject: [PATCH 01/14] [CIR] Upstream handling of integral-to-pointer casts
---
.../clang/CIR/Dialect/IR/CIRDataLayout.h | 18 +++++++++++++++
clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 19 ++++++++++++++++
clang/test/CIR/CodeGen/cast.cpp | 22 +++++++++++++++++++
3 files changed, 59 insertions(+)
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h b/clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h
index 417a226e44cbf..c7450d8770714 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h
+++ b/clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h
@@ -14,6 +14,11 @@
#include "mlir/Dialect/DLTI/DLTI.h"
#include "mlir/IR/BuiltinOps.h"
+#include "clang/CIR/Dialect/IR/CIRAttrs.h"
+#include "clang/CIR/Dialect/IR/CIRTypes.h"
+#include "llvm/IR/DataLayout.h"
+#include "llvm/Support/Alignment.h"
+#include "llvm/Support/TypeSize.h"
namespace cir {
@@ -81,6 +86,19 @@ class CIRDataLayout {
}
llvm::TypeSize getTypeSizeInBits(mlir::Type ty) const;
+
+ llvm::TypeSize getPointerTypeSizeInBits(mlir::Type Ty) const {
+ assert(mlir::isa<cir::PointerType>(Ty) &&
+ "This should only be called with a pointer type");
+ return layout.getTypeSizeInBits(Ty);
+ }
+
+ mlir::Type getIntPtrType(mlir::Type Ty) const {
+ assert(mlir::isa<cir::PointerType>(Ty) && "Expected pointer type");
+ auto IntTy =
+ cir::IntType::get(Ty.getContext(), getPointerTypeSizeInBits(Ty), false);
+ return IntTy;
+ }
};
} // namespace cir
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index 500007f6f241b..c64c5f7d7a9c8 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -1889,6 +1889,25 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *ce) {
}
return v;
}
+ case CK_IntegralToPointer: {
+ auto DestCIRTy = cgf.convertType(destTy);
+ mlir::Value Src = Visit(const_cast<Expr *>(subExpr));
+
+ // Properly resize by casting to an int of the same size as the pointer.
+ // Clang's IntegralToPointer includes 'bool' as the source, but in CIR
+ // 'bool' is not an integral type. So check the source type to get the
+ // correct CIR conversion.
+ auto MiddleTy = cgf.cgm.getDataLayout().getIntPtrType(DestCIRTy);
+ auto MiddleVal = builder.createCast(subExpr->getType()->isBooleanType()
+ ? cir::CastKind::bool_to_int
+ : cir::CastKind::integral,
+ Src, MiddleTy);
+
+ if (cgf.cgm.getCodeGenOpts().StrictVTablePointers)
+ llvm_unreachable("NYI");
+
+ return builder.createIntToPtr(MiddleVal, DestCIRTy);
+ }
case CK_ArrayToPointerDecay:
return cgf.emitArrayToPointerDecay(subExpr).getPointer();
diff --git a/clang/test/CIR/CodeGen/cast.cpp b/clang/test/CIR/CodeGen/cast.cpp
index 7afa955cf3bcf..a1fd92612fe37 100644
--- a/clang/test/CIR/CodeGen/cast.cpp
+++ b/clang/test/CIR/CodeGen/cast.cpp
@@ -131,3 +131,25 @@ void bitcast() {
// LLVM: %[[D_VEC:.*]] = load <2 x double>, ptr {{.*}}, align 16
// LLVM: %[[I_VEC:.*]] = bitcast <2 x double> %[[D_VEC]] to <4 x i32>
+
+void f(long int start) {
+ void *p = (void*)start;
+}
+// CIR: %[[L:.*]] = cir.load {{.*}} : !cir.ptr<!s64i>, !s64i
+// CIR: %[[MID:.*]] = cir.cast integral %[[L]] : !s64i -> !u64i
+// CIR: cir.cast int_to_ptr %[[MID]] : !u64i -> !cir.ptr<!void>
+
+struct A { int x; };
+
+void int_cast(long ptr) {
+ ((A *)ptr)->x = 0;
+}
+// CIR: cir.cast int_to_ptr {{.*}} : !u64i -> !cir.ptr<!rec_A>
+// LLVM: inttoptr {{.*}} to ptr
+
+void null_cast(long) {
+ *(int *)0 = 0;
+ ((A *)0)->x = 0;
+}
+// CIR: #cir.ptr<null> : !cir.ptr<!s32i>
+// CIR: #cir.ptr<null> : !cir.ptr<!rec_A>
>From 54e154c347a9085ac18314908d79ff1e1e5101b2 Mon Sep 17 00:00:00 2001
From: kimsh02 <kimshawn02 at icloud.com>
Date: Thu, 2 Oct 2025 04:45:22 -0700
Subject: [PATCH 02/14] Clang-format
---
clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h | 2 +-
clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h b/clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h
index c7450d8770714..5bcdbd9a0c8ca 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h
+++ b/clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h
@@ -87,7 +87,7 @@ class CIRDataLayout {
llvm::TypeSize getTypeSizeInBits(mlir::Type ty) const;
- llvm::TypeSize getPointerTypeSizeInBits(mlir::Type Ty) const {
+ llvm::TypeSize getPointerTypeSizeInBits(mlir::Type Ty) const {
assert(mlir::isa<cir::PointerType>(Ty) &&
"This should only be called with a pointer type");
return layout.getTypeSizeInBits(Ty);
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index c64c5f7d7a9c8..6938be2fa6729 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -1907,7 +1907,7 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *ce) {
llvm_unreachable("NYI");
return builder.createIntToPtr(MiddleVal, DestCIRTy);
- }
+ }
case CK_ArrayToPointerDecay:
return cgf.emitArrayToPointerDecay(subExpr).getPointer();
>From 280738848c9232fdc31a9b88a96049bbd26ebb98 Mon Sep 17 00:00:00 2001
From: Shawn K <kimshawn02 at icloud.com>
Date: Thu, 2 Oct 2025 10:10:12 -0700
Subject: [PATCH 03/14] Update clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
Co-authored-by: Andy Kaylor <akaylor at nvidia.com>
---
clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index 6938be2fa6729..6c67c0f82a427 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -1898,7 +1898,7 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *ce) {
// 'bool' is not an integral type. So check the source type to get the
// correct CIR conversion.
auto MiddleTy = cgf.cgm.getDataLayout().getIntPtrType(DestCIRTy);
- auto MiddleVal = builder.createCast(subExpr->getType()->isBooleanType()
+ cir::CastOp middleVal = builder.createCast(subExpr->getType()->isBooleanType()
? cir::CastKind::bool_to_int
: cir::CastKind::integral,
Src, MiddleTy);
>From d328d2775ea584ffba46dd408d7f3032edb981cd Mon Sep 17 00:00:00 2001
From: Shawn K <kimshawn02 at icloud.com>
Date: Thu, 2 Oct 2025 10:11:27 -0700
Subject: [PATCH 04/14] Update clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
Co-authored-by: Andy Kaylor <akaylor at nvidia.com>
---
clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index 6c67c0f82a427..82d8563af7608 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -1890,7 +1890,7 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *ce) {
return v;
}
case CK_IntegralToPointer: {
- auto DestCIRTy = cgf.convertType(destTy);
+ mlir:Type destCIRTy = cgf.convertType(destTy);
mlir::Value Src = Visit(const_cast<Expr *>(subExpr));
// Properly resize by casting to an int of the same size as the pointer.
>From ec1ca90540e34760be30882437b4a959c0c18f8e Mon Sep 17 00:00:00 2001
From: Shawn K <kimshawn02 at icloud.com>
Date: Thu, 2 Oct 2025 10:11:49 -0700
Subject: [PATCH 05/14] Update clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
Co-authored-by: Andy Kaylor <akaylor at nvidia.com>
---
clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index 82d8563af7608..6bb1e5dd8ed2a 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -1891,7 +1891,7 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *ce) {
}
case CK_IntegralToPointer: {
mlir:Type destCIRTy = cgf.convertType(destTy);
- mlir::Value Src = Visit(const_cast<Expr *>(subExpr));
+ mlir::Value src = Visit(const_cast<Expr *>(subExpr));
// Properly resize by casting to an int of the same size as the pointer.
// Clang's IntegralToPointer includes 'bool' as the source, but in CIR
>From 2b3d7cb783a50817eccb40c6c3ce21c5b9429921 Mon Sep 17 00:00:00 2001
From: Shawn K <kimshawn02 at icloud.com>
Date: Thu, 2 Oct 2025 10:12:00 -0700
Subject: [PATCH 06/14] Update clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
Co-authored-by: Andy Kaylor <akaylor at nvidia.com>
---
clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index 6bb1e5dd8ed2a..244973a12e3f5 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -1897,7 +1897,7 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *ce) {
// Clang's IntegralToPointer includes 'bool' as the source, but in CIR
// 'bool' is not an integral type. So check the source type to get the
// correct CIR conversion.
- auto MiddleTy = cgf.cgm.getDataLayout().getIntPtrType(DestCIRTy);
+ mlirType middleTy = cgf.cgm.getDataLayout().getIntPtrType(destCIRTy);
cir::CastOp middleVal = builder.createCast(subExpr->getType()->isBooleanType()
? cir::CastKind::bool_to_int
: cir::CastKind::integral,
>From 9a9e05d4f9d6a00e4a37eaa84d26b0405199ca30 Mon Sep 17 00:00:00 2001
From: Shawn K <kimshawn02 at icloud.com>
Date: Thu, 2 Oct 2025 10:12:25 -0700
Subject: [PATCH 07/14] Update clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
Co-authored-by: Andy Kaylor <akaylor at nvidia.com>
---
clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index 244973a12e3f5..c8a4f42566b17 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -1904,7 +1904,7 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *ce) {
Src, MiddleTy);
if (cgf.cgm.getCodeGenOpts().StrictVTablePointers)
- llvm_unreachable("NYI");
+ cgf.cgm.errorNYI(subExpr->getSourceRange(), "IntegralToPointer: strict vtable pointers");
return builder.createIntToPtr(MiddleVal, DestCIRTy);
}
>From 119873ceffe8ac599456a1c1a9bec925b7864951 Mon Sep 17 00:00:00 2001
From: Shawn K <kimshawn02 at icloud.com>
Date: Fri, 3 Oct 2025 04:16:59 -0700
Subject: [PATCH 08/14] Update
clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h
Co-authored-by: Amr Hesham <amr96 at programmer.net>
---
clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h b/clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h
index 5bcdbd9a0c8ca..661d57890798d 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h
+++ b/clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h
@@ -87,7 +87,7 @@ class CIRDataLayout {
llvm::TypeSize getTypeSizeInBits(mlir::Type ty) const;
- llvm::TypeSize getPointerTypeSizeInBits(mlir::Type Ty) const {
+ llvm::TypeSize getPointerTypeSizeInBits(mlir::Type ty) const {
assert(mlir::isa<cir::PointerType>(Ty) &&
"This should only be called with a pointer type");
return layout.getTypeSizeInBits(Ty);
>From c321248b791a249b879a8fa35ad0ffd6805cda32 Mon Sep 17 00:00:00 2001
From: Shawn K <kimshawn02 at icloud.com>
Date: Fri, 3 Oct 2025 04:17:31 -0700
Subject: [PATCH 09/14] Update
clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h
Co-authored-by: Amr Hesham <amr96 at programmer.net>
---
clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h b/clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h
index 661d57890798d..4eeb27d15c3f7 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h
+++ b/clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h
@@ -95,9 +95,7 @@ class CIRDataLayout {
mlir::Type getIntPtrType(mlir::Type Ty) const {
assert(mlir::isa<cir::PointerType>(Ty) && "Expected pointer type");
- auto IntTy =
- cir::IntType::get(Ty.getContext(), getPointerTypeSizeInBits(Ty), false);
- return IntTy;
+ return cir::IntType::get(ty.getContext(), getPointerTypeSizeInBits(ty), false);
}
};
>From 5866289728b80ef3f97bc0fc549bd232878ab0b8 Mon Sep 17 00:00:00 2001
From: Shawn K <kimshawn02 at icloud.com>
Date: Fri, 3 Oct 2025 04:18:21 -0700
Subject: [PATCH 10/14] Update clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
Co-authored-by: Amr Hesham <amr96 at programmer.net>
---
clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index c8a4f42566b17..8c01080993bc8 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -1901,7 +1901,7 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *ce) {
cir::CastOp middleVal = builder.createCast(subExpr->getType()->isBooleanType()
? cir::CastKind::bool_to_int
: cir::CastKind::integral,
- Src, MiddleTy);
+ src, middleTy);
if (cgf.cgm.getCodeGenOpts().StrictVTablePointers)
cgf.cgm.errorNYI(subExpr->getSourceRange(), "IntegralToPointer: strict vtable pointers");
>From 728ec84ca99b33192493804ade68fafd9ff3f7dd Mon Sep 17 00:00:00 2001
From: Shawn K <kimshawn02 at icloud.com>
Date: Fri, 3 Oct 2025 04:19:04 -0700
Subject: [PATCH 11/14] Update clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
Co-authored-by: Amr Hesham <amr96 at programmer.net>
---
clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index 8c01080993bc8..dbf5c5859957b 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -1903,8 +1903,10 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *ce) {
: cir::CastKind::integral,
src, middleTy);
- if (cgf.cgm.getCodeGenOpts().StrictVTablePointers)
+ if (cgf.cgm.getCodeGenOpts().StrictVTablePointers) {
cgf.cgm.errorNYI(subExpr->getSourceRange(), "IntegralToPointer: strict vtable pointers");
+ return {};
+ }
return builder.createIntToPtr(MiddleVal, DestCIRTy);
}
>From 142e70f7411b889054902f3957fae8678e2f035a Mon Sep 17 00:00:00 2001
From: Shawn K <kimshawn02 at icloud.com>
Date: Fri, 3 Oct 2025 04:19:16 -0700
Subject: [PATCH 12/14] Update clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
Co-authored-by: Amr Hesham <amr96 at programmer.net>
---
clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index dbf5c5859957b..7ee8c3b08979c 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -1908,7 +1908,7 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *ce) {
return {};
}
- return builder.createIntToPtr(MiddleVal, DestCIRTy);
+ return builder.createIntToPtr(middleVal, destCIRTy);
}
case CK_ArrayToPointerDecay:
>From 53244283807cf15f72ccd67757d819a84819fde3 Mon Sep 17 00:00:00 2001
From: kimsh02 <kimshawn02 at icloud.com>
Date: Tue, 7 Oct 2025 10:47:54 -0700
Subject: [PATCH 13/14] Apply feedback
---
.../include/clang/CIR/Dialect/IR/CIRDataLayout.h | 12 ++++--------
clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 10 +++++-----
clang/test/CIR/CodeGen/cast.cpp | 15 +++++++++++++--
3 files changed, 22 insertions(+), 15 deletions(-)
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h b/clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h
index 4eeb27d15c3f7..b5396e4395075 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h
+++ b/clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h
@@ -14,11 +14,7 @@
#include "mlir/Dialect/DLTI/DLTI.h"
#include "mlir/IR/BuiltinOps.h"
-#include "clang/CIR/Dialect/IR/CIRAttrs.h"
#include "clang/CIR/Dialect/IR/CIRTypes.h"
-#include "llvm/IR/DataLayout.h"
-#include "llvm/Support/Alignment.h"
-#include "llvm/Support/TypeSize.h"
namespace cir {
@@ -88,13 +84,13 @@ class CIRDataLayout {
llvm::TypeSize getTypeSizeInBits(mlir::Type ty) const;
llvm::TypeSize getPointerTypeSizeInBits(mlir::Type ty) const {
- assert(mlir::isa<cir::PointerType>(Ty) &&
+ assert(mlir::isa<cir::PointerType>(ty) &&
"This should only be called with a pointer type");
- return layout.getTypeSizeInBits(Ty);
+ return layout.getTypeSizeInBits(ty);
}
- mlir::Type getIntPtrType(mlir::Type Ty) const {
- assert(mlir::isa<cir::PointerType>(Ty) && "Expected pointer type");
+ mlir::Type getIntPtrType(mlir::Type ty) const {
+ assert(mlir::isa<cir::PointerType>(ty) && "Expected pointer type");
return cir::IntType::get(ty.getContext(), getPointerTypeSizeInBits(ty), false);
}
};
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index 7ee8c3b08979c..e7d6762b28f20 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -1890,18 +1890,18 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *ce) {
return v;
}
case CK_IntegralToPointer: {
- mlir:Type destCIRTy = cgf.convertType(destTy);
+ mlir::Type destCIRTy = cgf.convertType(destTy);
mlir::Value src = Visit(const_cast<Expr *>(subExpr));
// Properly resize by casting to an int of the same size as the pointer.
// Clang's IntegralToPointer includes 'bool' as the source, but in CIR
// 'bool' is not an integral type. So check the source type to get the
// correct CIR conversion.
- mlirType middleTy = cgf.cgm.getDataLayout().getIntPtrType(destCIRTy);
- cir::CastOp middleVal = builder.createCast(subExpr->getType()->isBooleanType()
- ? cir::CastKind::bool_to_int
+ mlir::Type middleTy = cgf.cgm.getDataLayout().getIntPtrType(destCIRTy);
+ mlir::Value middleVal = builder.createCast(
+ subExpr->getType()->isBooleanType() ? cir::CastKind::bool_to_int
: cir::CastKind::integral,
- src, middleTy);
+ src, middleTy);
if (cgf.cgm.getCodeGenOpts().StrictVTablePointers) {
cgf.cgm.errorNYI(subExpr->getSourceRange(), "IntegralToPointer: strict vtable pointers");
diff --git a/clang/test/CIR/CodeGen/cast.cpp b/clang/test/CIR/CodeGen/cast.cpp
index a1fd92612fe37..844d4dfb743da 100644
--- a/clang/test/CIR/CodeGen/cast.cpp
+++ b/clang/test/CIR/CodeGen/cast.cpp
@@ -139,6 +139,15 @@ void f(long int start) {
// CIR: %[[MID:.*]] = cir.cast integral %[[L]] : !s64i -> !u64i
// CIR: cir.cast int_to_ptr %[[MID]] : !u64i -> !cir.ptr<!void>
+// LLVM-LABEL: define{{.*}} void @_Z1fl(i64 %0)
+// LLVM: %[[ADDR:.*]] = alloca i64, i64 1, align 8
+// LLVM: %[[PADDR:.*]] = alloca ptr, i64 1, align 8
+// LLVM: store i64 %0, ptr %[[ADDR]], align 8
+// LLVM: %[[L:.*]] = load i64, ptr %[[ADDR]], align 8
+// LLVM: %[[PTR:.*]] = inttoptr i64 %[[L]] to ptr
+// LLVM: store ptr %[[PTR]], ptr %[[PADDR]], align 8
+// LLVM: ret void
+
struct A { int x; };
void int_cast(long ptr) {
@@ -151,5 +160,7 @@ void null_cast(long) {
*(int *)0 = 0;
((A *)0)->x = 0;
}
-// CIR: #cir.ptr<null> : !cir.ptr<!s32i>
-// CIR: #cir.ptr<null> : !cir.ptr<!rec_A>
+// CIR: %[[NULLPTR:.*]] = cir.const #cir.ptr<null> : !cir.ptr<!s32i>
+// CIR: cir.store{{.*}} %{{.*}}, %[[NULLPTR]] : !s32i, !cir.ptr<!s32i>
+// CIR: %[[NULLPTR_A:.*]] = cir.const #cir.ptr<null> : !cir.ptr<!rec_A>
+// CIR: %[[A_X:.*]] = cir.get_member %[[NULLPTR_A]][0] {name = "x"} : !cir.ptr<!rec_A> -> !cir.ptr<!s32i>
>From d5cd99777015a5cb05e032c9ac7f0a7853a97f4c Mon Sep 17 00:00:00 2001
From: kimsh02 <kimshawn02 at icloud.com>
Date: Tue, 7 Oct 2025 11:03:11 -0700
Subject: [PATCH 14/14] Clang-format
---
clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h | 3 ++-
clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 3 ++-
2 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h b/clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h
index b5396e4395075..5c6ce7abeae61 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h
+++ b/clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h
@@ -91,7 +91,8 @@ class CIRDataLayout {
mlir::Type getIntPtrType(mlir::Type ty) const {
assert(mlir::isa<cir::PointerType>(ty) && "Expected pointer type");
- return cir::IntType::get(ty.getContext(), getPointerTypeSizeInBits(ty), false);
+ return cir::IntType::get(ty.getContext(), getPointerTypeSizeInBits(ty),
+ false);
}
};
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index e7d6762b28f20..33c65862225da 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -1904,7 +1904,8 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *ce) {
src, middleTy);
if (cgf.cgm.getCodeGenOpts().StrictVTablePointers) {
- cgf.cgm.errorNYI(subExpr->getSourceRange(), "IntegralToPointer: strict vtable pointers");
+ cgf.cgm.errorNYI(subExpr->getSourceRange(),
+ "IntegralToPointer: strict vtable pointers");
return {};
}
More information about the cfe-commits
mailing list