[clang] [CIR] Upstream __builtin_astype int_to_ptr (PR #193519)

via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 22 08:31:14 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Amr Hesham (AmrDeveloper)

<details>
<summary>Changes</summary>

Upstream support for __builtin_astype int_to_ptr case

Issue https://github.com/llvm/llvm-project/issues/192311

---
Full diff: https://github.com/llvm/llvm-project/pull/193519.diff


2 Files Affected:

- (modified) clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp (+9-7) 
- (modified) clang/test/CIR/CodeGenOpenCL/as_type.cl (+22) 


``````````diff
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]]

``````````

</details>


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


More information about the cfe-commits mailing list