[clang] [CIR] Upstream __builtin_astype int_to_ptr (PR #193519)
Amr Hesham via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 22 13:55:13 PDT 2026
https://github.com/AmrDeveloper updated https://github.com/llvm/llvm-project/pull/193519
>From 338e9785f39361c9b3eb90f7c4466b3f5944f33f Mon Sep 17 00:00:00 2001
From: Amr Hesham <amr96 at programmer.net>
Date: Wed, 22 Apr 2026 17:23:44 +0200
Subject: [PATCH 1/2] [CIR] Upstream __builtin_astype int_to_ptr
---
clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 16 +++++++++-------
clang/test/CIR/CodeGenOpenCL/as_type.cl | 22 ++++++++++++++++++++++
2 files changed, 31 insertions(+), 7 deletions(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index 399d90e896eb4..2aa81f6a19153 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -1456,22 +1456,24 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
// Case 3.
if (isa<cir::PointerType>(srcTy) && !isa<cir::PointerType>(dstTy)) {
+ if (!isa<cir::IntType>(dstTy)) {
+ cgf.cgm.errorNYI(
+ "ScalarExprEmitter: createCastsForTypeOfSameSize Case 3b");
+ }
+
cgf.cgm.errorNYI(
- "ScalarExprEmitter: createCastsForTypeOfSameSize Case 3");
+ "ScalarExprEmitter: createCastsForTypeOfSameSize Case 3a and 3b");
return {};
}
// Case 4b.
- if (srcTy.isInteger()) {
+ if (!isa<cir::IntType>(srcTy)) {
cgf.cgm.errorNYI(
- "ScalarExprEmitter: createCastsForTypeOfSameSize Case 4b");
+ "ScalarExprEmitter: createCastsForTypeOfSameSize Case 4a");
return {};
}
-
// Cases 4a and 4b.
- cgf.cgm.errorNYI(
- "ScalarExprEmitter: createCastsForTypeOfSameSize Cases 4a and 4b");
- return {};
+ return builder.createIntToPtr(src, dstTy);
}
mlir::Value VisitAsTypeExpr(AsTypeExpr *e) {
diff --git a/clang/test/CIR/CodeGenOpenCL/as_type.cl b/clang/test/CIR/CodeGenOpenCL/as_type.cl
index f314b1dced5b5..05afd65665e7c 100644
--- a/clang/test/CIR/CodeGenOpenCL/as_type.cl
+++ b/clang/test/CIR/CodeGenOpenCL/as_type.cl
@@ -52,3 +52,25 @@ int f6(char4 x) {
// OGCG: define {{.*}} i32 @f6
// OGCG: %[[RET:.*]] = bitcast <4 x i8> %{{.*}} to i32
// OGCG: ret i32 %[[RET]]
+
+int* int_to_ptr(int x) {
+ return __builtin_astype(x, int*);
+}
+
+// CIR: cir.func {{.*}} @int_to_ptr
+// CIR: %[[X_ADDR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["x", init]
+// CIR: %[[RET_ADDR:.*]] = cir.alloca !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>, ["__retval"]
+// CIR: cir.store %{{.*}}, %[[X_ADDR]] : !s32i, !cir.ptr<!s32i>
+// CIR: %[[TMP_X:.*]] = cir.load {{.*}} %[[X_ADDR]] : !cir.ptr<!s32i>, !s32i
+// CIR: %[[X_PTR:.*]] = cir.cast int_to_ptr %[[TMP_X]] : !s32i -> !cir.ptr<!s32i>
+// CIR: cir.store %[[X_PTR]], %[[RET_ADDR]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
+// CIR: %[[TMP_RET]] = cir.load %[[RET_ADDR]] : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
+// CIR: cir.return %[[TMP_RET]] : !cir.ptr<!s32i>
+
+// LLVM: define {{.*}} ptr @int_to_ptr
+// LLVM: %[[INT_TO_PTR:.*]] = inttoptr i32 %{{.*}} to ptr
+// LLVM: ret ptr %[[INT_TO_PTR]]
+
+// OGCG: define {{.*}} ptr @int_to_ptr
+// OGCG: %[[INT_TO_PTR:.*]] = inttoptr i32 %{{.*}} to ptr
+// OGCG: ret ptr %[[INT_TO_PTR]]
>From ebffee1026ac8b0c6e053d91f30c848b1dabb1aa Mon Sep 17 00:00:00 2001
From: Amr Hesham <amr96 at programmer.net>
Date: Wed, 22 Apr 2026 22:21:43 +0200
Subject: [PATCH 2/2] Fix diagnostic message
---
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 2aa81f6a19153..69fe23883b0fb 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -1458,7 +1458,7 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
if (isa<cir::PointerType>(srcTy) && !isa<cir::PointerType>(dstTy)) {
if (!isa<cir::IntType>(dstTy)) {
cgf.cgm.errorNYI(
- "ScalarExprEmitter: createCastsForTypeOfSameSize Case 3b");
+ "ScalarExprEmitter: createCastsForTypeOfSameSize Case 3a");
}
cgf.cgm.errorNYI(
More information about the cfe-commits
mailing list