[llvm-branch-commits] [clang] [CIR] Upstream `AddressSpace` conversions support (PR #161212)

2025-10-18 Thread David Rivera via llvm-branch-commits

https://github.com/RiverDave edited 
https://github.com/llvm/llvm-project/pull/161212
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] [CIR] Upstream `AddressSpace` conversions support (PR #161212)

2025-10-18 Thread David Rivera via llvm-branch-commits

https://github.com/RiverDave updated 
https://github.com/llvm/llvm-project/pull/161212

>From e446b4723e7df94f057d20a9ad0fdc28964bb2fd Mon Sep 17 00:00:00 2001
From: David Rivera 
Date: Mon, 29 Sep 2025 11:05:44 -0400
Subject: [PATCH] [CIR] Upstream AddressSpace casting support

---
 .../CIR/Dialect/Builder/CIRBaseBuilder.h  |  9 +++
 clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp   | 41 +++
 clang/lib/CIR/CodeGen/CIRGenExpr.cpp  | 19 +-
 clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp| 22 ++
 clang/lib/CIR/CodeGen/CIRGenFunction.h|  4 ++
 clang/lib/CIR/CodeGen/CIRGenModule.cpp| 17 +
 clang/lib/CIR/CodeGen/CIRGenModule.h  |  6 ++
 clang/lib/CIR/CodeGen/CIRGenTypes.cpp |  2 +-
 clang/lib/CIR/CodeGen/TargetInfo.cpp  | 13 
 clang/lib/CIR/CodeGen/TargetInfo.h| 12 
 clang/test/CIR/address-space-conversion.cpp   | 68 +++
 11 files changed, 195 insertions(+), 18 deletions(-)
 create mode 100644 clang/test/CIR/address-space-conversion.cpp

diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h 
b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
index cef8624e65d57..bf4a9b8438982 100644
--- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
+++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
@@ -424,6 +424,15 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
 return createBitcast(src, getPointerTo(newPointeeTy));
   }
 
+  mlir::Value createAddrSpaceCast(mlir::Location loc, mlir::Value src,
+  mlir::Type newTy) {
+return createCast(loc, cir::CastKind::address_space, src, newTy);
+  }
+
+  mlir::Value createAddrSpaceCast(mlir::Value src, mlir::Type newTy) {
+return createAddrSpaceCast(src.getLoc(), src, newTy);
+  }
+
   
//======//
   // Binary Operators
   
//======//
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp 
b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index cf17de144f4d9..95e392d860518 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -58,6 +58,24 @@ static RValue emitBuiltinBitOp(CIRGenFunction &cgf, const 
CallExpr *e,
   return RValue::get(result);
 }
 
+// Initialize the alloca with the given size and alignment according to the 
lang
+// opts. Supporting only the trivial non-initialization for now.
+static void initializeAlloca(CIRGenFunction &CGF,
+ [[maybe_unused]] mlir::Value AllocaAddr,
+ [[maybe_unused]] mlir::Value Size,
+ [[maybe_unused]] CharUnits AlignmentInBytes) {
+
+  switch (CGF.getLangOpts().getTrivialAutoVarInit()) {
+  case LangOptions::TrivialAutoVarInitKind::Uninitialized:
+// Nothing to initialize.
+return;
+  case LangOptions::TrivialAutoVarInitKind::Zero:
+  case LangOptions::TrivialAutoVarInitKind::Pattern:
+assert(false && "unexpected trivial auto var init kind NYI");
+return;
+  }
+}
+
 RValue CIRGenFunction::emitRotate(const CallExpr *e, bool isRotateLeft) {
   mlir::Value input = emitScalarExpr(e->getArg(0));
   mlir::Value amount = emitScalarExpr(e->getArg(1));
@@ -172,21 +190,8 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
 builder.getUInt8Ty(), "bi_alloca", suitableAlignmentInBytes, size);
 
 // Initialize the allocated buffer if required.
-if (builtinID != Builtin::BI__builtin_alloca_uninitialized) {
-  // Initialize the alloca with the given size and alignment according to
-  // the lang opts. Only the trivial non-initialization is supported for
-  // now.
-
-  switch (getLangOpts().getTrivialAutoVarInit()) {
-  case LangOptions::TrivialAutoVarInitKind::Uninitialized:
-// Nothing to initialize.
-break;
-  case LangOptions::TrivialAutoVarInitKind::Zero:
-  case LangOptions::TrivialAutoVarInitKind::Pattern:
-cgm.errorNYI("trivial auto var init");
-break;
-  }
-}
+if (builtinID != Builtin::BI__builtin_alloca_uninitialized)
+  initializeAlloca(*this, allocaAddr, size, suitableAlignmentInBytes);
 
 // An alloca will always return a pointer to the alloca (stack) address
 // space. This address space need not be the same as the AST / Language
@@ -194,6 +199,12 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
 // the AST level this is handled within CreateTempAlloca et al., but for 
the
 // builtin / dynamic alloca we have to handle it here.
 assert(!cir::MissingFeatures::addressSpace());
+cir::AddressSpace aas = getCIRAllocaAddressSpace();
+cir::AddressSpace eas = cir::toCIRAddressSpace(
+e->getType()->getPointeeType().getAddressSpace());
+if (eas != aas) {
+  assert(false && "Non-default address

[llvm-branch-commits] [clang] [CIR] Upstream `AddressSpace` conversions support (PR #161212)

2025-10-18 Thread David Rivera via llvm-branch-commits

https://github.com/RiverDave updated 
https://github.com/llvm/llvm-project/pull/161212

>From 4d58946e6813b357d3adc0cf826066afc0bdc115 Mon Sep 17 00:00:00 2001
From: David Rivera 
Date: Mon, 29 Sep 2025 11:05:44 -0400
Subject: [PATCH 1/3] [CIR] Upstream AddressSpace casting support

---
 .../CIR/Dialect/Builder/CIRBaseBuilder.h  |  9 +++
 clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp   | 41 +++
 clang/lib/CIR/CodeGen/CIRGenExpr.cpp  | 19 +-
 clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp| 22 ++
 clang/lib/CIR/CodeGen/CIRGenFunction.h|  4 ++
 clang/lib/CIR/CodeGen/CIRGenModule.cpp| 17 +
 clang/lib/CIR/CodeGen/CIRGenModule.h  |  6 ++
 clang/lib/CIR/CodeGen/CIRGenTypes.cpp |  2 +-
 clang/lib/CIR/CodeGen/TargetInfo.cpp  | 13 
 clang/lib/CIR/CodeGen/TargetInfo.h| 13 
 clang/test/CIR/address-space-conversion.cpp   | 68 +++
 11 files changed, 196 insertions(+), 18 deletions(-)
 create mode 100644 clang/test/CIR/address-space-conversion.cpp

diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h 
b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
index cef8624e65d57..bf4a9b8438982 100644
--- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
+++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
@@ -424,6 +424,15 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
 return createBitcast(src, getPointerTo(newPointeeTy));
   }
 
+  mlir::Value createAddrSpaceCast(mlir::Location loc, mlir::Value src,
+  mlir::Type newTy) {
+return createCast(loc, cir::CastKind::address_space, src, newTy);
+  }
+
+  mlir::Value createAddrSpaceCast(mlir::Value src, mlir::Type newTy) {
+return createAddrSpaceCast(src.getLoc(), src, newTy);
+  }
+
   
//======//
   // Binary Operators
   
//======//
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp 
b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index cf17de144f4d9..95e392d860518 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -58,6 +58,24 @@ static RValue emitBuiltinBitOp(CIRGenFunction &cgf, const 
CallExpr *e,
   return RValue::get(result);
 }
 
+// Initialize the alloca with the given size and alignment according to the 
lang
+// opts. Supporting only the trivial non-initialization for now.
+static void initializeAlloca(CIRGenFunction &CGF,
+ [[maybe_unused]] mlir::Value AllocaAddr,
+ [[maybe_unused]] mlir::Value Size,
+ [[maybe_unused]] CharUnits AlignmentInBytes) {
+
+  switch (CGF.getLangOpts().getTrivialAutoVarInit()) {
+  case LangOptions::TrivialAutoVarInitKind::Uninitialized:
+// Nothing to initialize.
+return;
+  case LangOptions::TrivialAutoVarInitKind::Zero:
+  case LangOptions::TrivialAutoVarInitKind::Pattern:
+assert(false && "unexpected trivial auto var init kind NYI");
+return;
+  }
+}
+
 RValue CIRGenFunction::emitRotate(const CallExpr *e, bool isRotateLeft) {
   mlir::Value input = emitScalarExpr(e->getArg(0));
   mlir::Value amount = emitScalarExpr(e->getArg(1));
@@ -172,21 +190,8 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
 builder.getUInt8Ty(), "bi_alloca", suitableAlignmentInBytes, size);
 
 // Initialize the allocated buffer if required.
-if (builtinID != Builtin::BI__builtin_alloca_uninitialized) {
-  // Initialize the alloca with the given size and alignment according to
-  // the lang opts. Only the trivial non-initialization is supported for
-  // now.
-
-  switch (getLangOpts().getTrivialAutoVarInit()) {
-  case LangOptions::TrivialAutoVarInitKind::Uninitialized:
-// Nothing to initialize.
-break;
-  case LangOptions::TrivialAutoVarInitKind::Zero:
-  case LangOptions::TrivialAutoVarInitKind::Pattern:
-cgm.errorNYI("trivial auto var init");
-break;
-  }
-}
+if (builtinID != Builtin::BI__builtin_alloca_uninitialized)
+  initializeAlloca(*this, allocaAddr, size, suitableAlignmentInBytes);
 
 // An alloca will always return a pointer to the alloca (stack) address
 // space. This address space need not be the same as the AST / Language
@@ -194,6 +199,12 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
 // the AST level this is handled within CreateTempAlloca et al., but for 
the
 // builtin / dynamic alloca we have to handle it here.
 assert(!cir::MissingFeatures::addressSpace());
+cir::AddressSpace aas = getCIRAllocaAddressSpace();
+cir::AddressSpace eas = cir::toCIRAddressSpace(
+e->getType()->getPointeeType().getAddressSpace());
+if (eas != aas) {
+  assert(false && "Non-default add

[llvm-branch-commits] [clang] [CIR] Upstream `AddressSpace` conversions support (PR #161212)

2025-10-18 Thread David Rivera via llvm-branch-commits

https://github.com/RiverDave updated 
https://github.com/llvm/llvm-project/pull/161212

>From 4d58946e6813b357d3adc0cf826066afc0bdc115 Mon Sep 17 00:00:00 2001
From: David Rivera 
Date: Mon, 29 Sep 2025 11:05:44 -0400
Subject: [PATCH 1/2] [CIR] Upstream AddressSpace casting support

---
 .../CIR/Dialect/Builder/CIRBaseBuilder.h  |  9 +++
 clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp   | 41 +++
 clang/lib/CIR/CodeGen/CIRGenExpr.cpp  | 19 +-
 clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp| 22 ++
 clang/lib/CIR/CodeGen/CIRGenFunction.h|  4 ++
 clang/lib/CIR/CodeGen/CIRGenModule.cpp| 17 +
 clang/lib/CIR/CodeGen/CIRGenModule.h  |  6 ++
 clang/lib/CIR/CodeGen/CIRGenTypes.cpp |  2 +-
 clang/lib/CIR/CodeGen/TargetInfo.cpp  | 13 
 clang/lib/CIR/CodeGen/TargetInfo.h| 13 
 clang/test/CIR/address-space-conversion.cpp   | 68 +++
 11 files changed, 196 insertions(+), 18 deletions(-)
 create mode 100644 clang/test/CIR/address-space-conversion.cpp

diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h 
b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
index cef8624e65d57..bf4a9b8438982 100644
--- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
+++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
@@ -424,6 +424,15 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
 return createBitcast(src, getPointerTo(newPointeeTy));
   }
 
+  mlir::Value createAddrSpaceCast(mlir::Location loc, mlir::Value src,
+  mlir::Type newTy) {
+return createCast(loc, cir::CastKind::address_space, src, newTy);
+  }
+
+  mlir::Value createAddrSpaceCast(mlir::Value src, mlir::Type newTy) {
+return createAddrSpaceCast(src.getLoc(), src, newTy);
+  }
+
   
//======//
   // Binary Operators
   
//======//
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp 
b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index cf17de144f4d9..95e392d860518 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -58,6 +58,24 @@ static RValue emitBuiltinBitOp(CIRGenFunction &cgf, const 
CallExpr *e,
   return RValue::get(result);
 }
 
+// Initialize the alloca with the given size and alignment according to the 
lang
+// opts. Supporting only the trivial non-initialization for now.
+static void initializeAlloca(CIRGenFunction &CGF,
+ [[maybe_unused]] mlir::Value AllocaAddr,
+ [[maybe_unused]] mlir::Value Size,
+ [[maybe_unused]] CharUnits AlignmentInBytes) {
+
+  switch (CGF.getLangOpts().getTrivialAutoVarInit()) {
+  case LangOptions::TrivialAutoVarInitKind::Uninitialized:
+// Nothing to initialize.
+return;
+  case LangOptions::TrivialAutoVarInitKind::Zero:
+  case LangOptions::TrivialAutoVarInitKind::Pattern:
+assert(false && "unexpected trivial auto var init kind NYI");
+return;
+  }
+}
+
 RValue CIRGenFunction::emitRotate(const CallExpr *e, bool isRotateLeft) {
   mlir::Value input = emitScalarExpr(e->getArg(0));
   mlir::Value amount = emitScalarExpr(e->getArg(1));
@@ -172,21 +190,8 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
 builder.getUInt8Ty(), "bi_alloca", suitableAlignmentInBytes, size);
 
 // Initialize the allocated buffer if required.
-if (builtinID != Builtin::BI__builtin_alloca_uninitialized) {
-  // Initialize the alloca with the given size and alignment according to
-  // the lang opts. Only the trivial non-initialization is supported for
-  // now.
-
-  switch (getLangOpts().getTrivialAutoVarInit()) {
-  case LangOptions::TrivialAutoVarInitKind::Uninitialized:
-// Nothing to initialize.
-break;
-  case LangOptions::TrivialAutoVarInitKind::Zero:
-  case LangOptions::TrivialAutoVarInitKind::Pattern:
-cgm.errorNYI("trivial auto var init");
-break;
-  }
-}
+if (builtinID != Builtin::BI__builtin_alloca_uninitialized)
+  initializeAlloca(*this, allocaAddr, size, suitableAlignmentInBytes);
 
 // An alloca will always return a pointer to the alloca (stack) address
 // space. This address space need not be the same as the AST / Language
@@ -194,6 +199,12 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
 // the AST level this is handled within CreateTempAlloca et al., but for 
the
 // builtin / dynamic alloca we have to handle it here.
 assert(!cir::MissingFeatures::addressSpace());
+cir::AddressSpace aas = getCIRAllocaAddressSpace();
+cir::AddressSpace eas = cir::toCIRAddressSpace(
+e->getType()->getPointeeType().getAddressSpace());
+if (eas != aas) {
+  assert(false && "Non-default add

[llvm-branch-commits] [clang] [CIR] Upstream `AddressSpace` conversions support (PR #161212)

2025-10-18 Thread David Rivera via llvm-branch-commits

https://github.com/RiverDave updated 
https://github.com/llvm/llvm-project/pull/161212

>From 4d58946e6813b357d3adc0cf826066afc0bdc115 Mon Sep 17 00:00:00 2001
From: David Rivera 
Date: Mon, 29 Sep 2025 11:05:44 -0400
Subject: [PATCH 1/4] [CIR] Upstream AddressSpace casting support

---
 .../CIR/Dialect/Builder/CIRBaseBuilder.h  |  9 +++
 clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp   | 41 +++
 clang/lib/CIR/CodeGen/CIRGenExpr.cpp  | 19 +-
 clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp| 22 ++
 clang/lib/CIR/CodeGen/CIRGenFunction.h|  4 ++
 clang/lib/CIR/CodeGen/CIRGenModule.cpp| 17 +
 clang/lib/CIR/CodeGen/CIRGenModule.h  |  6 ++
 clang/lib/CIR/CodeGen/CIRGenTypes.cpp |  2 +-
 clang/lib/CIR/CodeGen/TargetInfo.cpp  | 13 
 clang/lib/CIR/CodeGen/TargetInfo.h| 13 
 clang/test/CIR/address-space-conversion.cpp   | 68 +++
 11 files changed, 196 insertions(+), 18 deletions(-)
 create mode 100644 clang/test/CIR/address-space-conversion.cpp

diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h 
b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
index cef8624e65d57..bf4a9b8438982 100644
--- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
+++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
@@ -424,6 +424,15 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
 return createBitcast(src, getPointerTo(newPointeeTy));
   }
 
+  mlir::Value createAddrSpaceCast(mlir::Location loc, mlir::Value src,
+  mlir::Type newTy) {
+return createCast(loc, cir::CastKind::address_space, src, newTy);
+  }
+
+  mlir::Value createAddrSpaceCast(mlir::Value src, mlir::Type newTy) {
+return createAddrSpaceCast(src.getLoc(), src, newTy);
+  }
+
   
//======//
   // Binary Operators
   
//======//
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp 
b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index cf17de144f4d9..95e392d860518 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -58,6 +58,24 @@ static RValue emitBuiltinBitOp(CIRGenFunction &cgf, const 
CallExpr *e,
   return RValue::get(result);
 }
 
+// Initialize the alloca with the given size and alignment according to the 
lang
+// opts. Supporting only the trivial non-initialization for now.
+static void initializeAlloca(CIRGenFunction &CGF,
+ [[maybe_unused]] mlir::Value AllocaAddr,
+ [[maybe_unused]] mlir::Value Size,
+ [[maybe_unused]] CharUnits AlignmentInBytes) {
+
+  switch (CGF.getLangOpts().getTrivialAutoVarInit()) {
+  case LangOptions::TrivialAutoVarInitKind::Uninitialized:
+// Nothing to initialize.
+return;
+  case LangOptions::TrivialAutoVarInitKind::Zero:
+  case LangOptions::TrivialAutoVarInitKind::Pattern:
+assert(false && "unexpected trivial auto var init kind NYI");
+return;
+  }
+}
+
 RValue CIRGenFunction::emitRotate(const CallExpr *e, bool isRotateLeft) {
   mlir::Value input = emitScalarExpr(e->getArg(0));
   mlir::Value amount = emitScalarExpr(e->getArg(1));
@@ -172,21 +190,8 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
 builder.getUInt8Ty(), "bi_alloca", suitableAlignmentInBytes, size);
 
 // Initialize the allocated buffer if required.
-if (builtinID != Builtin::BI__builtin_alloca_uninitialized) {
-  // Initialize the alloca with the given size and alignment according to
-  // the lang opts. Only the trivial non-initialization is supported for
-  // now.
-
-  switch (getLangOpts().getTrivialAutoVarInit()) {
-  case LangOptions::TrivialAutoVarInitKind::Uninitialized:
-// Nothing to initialize.
-break;
-  case LangOptions::TrivialAutoVarInitKind::Zero:
-  case LangOptions::TrivialAutoVarInitKind::Pattern:
-cgm.errorNYI("trivial auto var init");
-break;
-  }
-}
+if (builtinID != Builtin::BI__builtin_alloca_uninitialized)
+  initializeAlloca(*this, allocaAddr, size, suitableAlignmentInBytes);
 
 // An alloca will always return a pointer to the alloca (stack) address
 // space. This address space need not be the same as the AST / Language
@@ -194,6 +199,12 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
 // the AST level this is handled within CreateTempAlloca et al., but for 
the
 // builtin / dynamic alloca we have to handle it here.
 assert(!cir::MissingFeatures::addressSpace());
+cir::AddressSpace aas = getCIRAllocaAddressSpace();
+cir::AddressSpace eas = cir::toCIRAddressSpace(
+e->getType()->getPointeeType().getAddressSpace());
+if (eas != aas) {
+  assert(false && "Non-default add

[llvm-branch-commits] [clang] [CIR] Upstream `AddressSpace` conversions support (PR #161212)

2025-10-18 Thread David Rivera via llvm-branch-commits

https://github.com/RiverDave updated 
https://github.com/llvm/llvm-project/pull/161212

>From 4d58946e6813b357d3adc0cf826066afc0bdc115 Mon Sep 17 00:00:00 2001
From: David Rivera 
Date: Mon, 29 Sep 2025 11:05:44 -0400
Subject: [PATCH 1/3] [CIR] Upstream AddressSpace casting support

---
 .../CIR/Dialect/Builder/CIRBaseBuilder.h  |  9 +++
 clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp   | 41 +++
 clang/lib/CIR/CodeGen/CIRGenExpr.cpp  | 19 +-
 clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp| 22 ++
 clang/lib/CIR/CodeGen/CIRGenFunction.h|  4 ++
 clang/lib/CIR/CodeGen/CIRGenModule.cpp| 17 +
 clang/lib/CIR/CodeGen/CIRGenModule.h  |  6 ++
 clang/lib/CIR/CodeGen/CIRGenTypes.cpp |  2 +-
 clang/lib/CIR/CodeGen/TargetInfo.cpp  | 13 
 clang/lib/CIR/CodeGen/TargetInfo.h| 13 
 clang/test/CIR/address-space-conversion.cpp   | 68 +++
 11 files changed, 196 insertions(+), 18 deletions(-)
 create mode 100644 clang/test/CIR/address-space-conversion.cpp

diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h 
b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
index cef8624e65d57..bf4a9b8438982 100644
--- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
+++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
@@ -424,6 +424,15 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
 return createBitcast(src, getPointerTo(newPointeeTy));
   }
 
+  mlir::Value createAddrSpaceCast(mlir::Location loc, mlir::Value src,
+  mlir::Type newTy) {
+return createCast(loc, cir::CastKind::address_space, src, newTy);
+  }
+
+  mlir::Value createAddrSpaceCast(mlir::Value src, mlir::Type newTy) {
+return createAddrSpaceCast(src.getLoc(), src, newTy);
+  }
+
   
//======//
   // Binary Operators
   
//======//
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp 
b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index cf17de144f4d9..95e392d860518 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -58,6 +58,24 @@ static RValue emitBuiltinBitOp(CIRGenFunction &cgf, const 
CallExpr *e,
   return RValue::get(result);
 }
 
+// Initialize the alloca with the given size and alignment according to the 
lang
+// opts. Supporting only the trivial non-initialization for now.
+static void initializeAlloca(CIRGenFunction &CGF,
+ [[maybe_unused]] mlir::Value AllocaAddr,
+ [[maybe_unused]] mlir::Value Size,
+ [[maybe_unused]] CharUnits AlignmentInBytes) {
+
+  switch (CGF.getLangOpts().getTrivialAutoVarInit()) {
+  case LangOptions::TrivialAutoVarInitKind::Uninitialized:
+// Nothing to initialize.
+return;
+  case LangOptions::TrivialAutoVarInitKind::Zero:
+  case LangOptions::TrivialAutoVarInitKind::Pattern:
+assert(false && "unexpected trivial auto var init kind NYI");
+return;
+  }
+}
+
 RValue CIRGenFunction::emitRotate(const CallExpr *e, bool isRotateLeft) {
   mlir::Value input = emitScalarExpr(e->getArg(0));
   mlir::Value amount = emitScalarExpr(e->getArg(1));
@@ -172,21 +190,8 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
 builder.getUInt8Ty(), "bi_alloca", suitableAlignmentInBytes, size);
 
 // Initialize the allocated buffer if required.
-if (builtinID != Builtin::BI__builtin_alloca_uninitialized) {
-  // Initialize the alloca with the given size and alignment according to
-  // the lang opts. Only the trivial non-initialization is supported for
-  // now.
-
-  switch (getLangOpts().getTrivialAutoVarInit()) {
-  case LangOptions::TrivialAutoVarInitKind::Uninitialized:
-// Nothing to initialize.
-break;
-  case LangOptions::TrivialAutoVarInitKind::Zero:
-  case LangOptions::TrivialAutoVarInitKind::Pattern:
-cgm.errorNYI("trivial auto var init");
-break;
-  }
-}
+if (builtinID != Builtin::BI__builtin_alloca_uninitialized)
+  initializeAlloca(*this, allocaAddr, size, suitableAlignmentInBytes);
 
 // An alloca will always return a pointer to the alloca (stack) address
 // space. This address space need not be the same as the AST / Language
@@ -194,6 +199,12 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
 // the AST level this is handled within CreateTempAlloca et al., but for 
the
 // builtin / dynamic alloca we have to handle it here.
 assert(!cir::MissingFeatures::addressSpace());
+cir::AddressSpace aas = getCIRAllocaAddressSpace();
+cir::AddressSpace eas = cir::toCIRAddressSpace(
+e->getType()->getPointeeType().getAddressSpace());
+if (eas != aas) {
+  assert(false && "Non-default add

[llvm-branch-commits] [clang] [CIR] Upstream AddressSpace casting support (PR #161212)

2025-10-18 Thread David Rivera via llvm-branch-commits

https://github.com/RiverDave edited 
https://github.com/llvm/llvm-project/pull/161212
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] [CIR] Upstream `AddressSpace` conversions support (PR #161212)

2025-10-18 Thread David Rivera via llvm-branch-commits

https://github.com/RiverDave updated 
https://github.com/llvm/llvm-project/pull/161212

>From e446b4723e7df94f057d20a9ad0fdc28964bb2fd Mon Sep 17 00:00:00 2001
From: David Rivera 
Date: Mon, 29 Sep 2025 11:05:44 -0400
Subject: [PATCH 1/2] [CIR] Upstream AddressSpace casting support

---
 .../CIR/Dialect/Builder/CIRBaseBuilder.h  |  9 +++
 clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp   | 41 +++
 clang/lib/CIR/CodeGen/CIRGenExpr.cpp  | 19 +-
 clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp| 22 ++
 clang/lib/CIR/CodeGen/CIRGenFunction.h|  4 ++
 clang/lib/CIR/CodeGen/CIRGenModule.cpp| 17 +
 clang/lib/CIR/CodeGen/CIRGenModule.h  |  6 ++
 clang/lib/CIR/CodeGen/CIRGenTypes.cpp |  2 +-
 clang/lib/CIR/CodeGen/TargetInfo.cpp  | 13 
 clang/lib/CIR/CodeGen/TargetInfo.h| 12 
 clang/test/CIR/address-space-conversion.cpp   | 68 +++
 11 files changed, 195 insertions(+), 18 deletions(-)
 create mode 100644 clang/test/CIR/address-space-conversion.cpp

diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h 
b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
index cef8624e65d57..bf4a9b8438982 100644
--- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
+++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
@@ -424,6 +424,15 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
 return createBitcast(src, getPointerTo(newPointeeTy));
   }
 
+  mlir::Value createAddrSpaceCast(mlir::Location loc, mlir::Value src,
+  mlir::Type newTy) {
+return createCast(loc, cir::CastKind::address_space, src, newTy);
+  }
+
+  mlir::Value createAddrSpaceCast(mlir::Value src, mlir::Type newTy) {
+return createAddrSpaceCast(src.getLoc(), src, newTy);
+  }
+
   
//======//
   // Binary Operators
   
//======//
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp 
b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index cf17de144f4d9..95e392d860518 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -58,6 +58,24 @@ static RValue emitBuiltinBitOp(CIRGenFunction &cgf, const 
CallExpr *e,
   return RValue::get(result);
 }
 
+// Initialize the alloca with the given size and alignment according to the 
lang
+// opts. Supporting only the trivial non-initialization for now.
+static void initializeAlloca(CIRGenFunction &CGF,
+ [[maybe_unused]] mlir::Value AllocaAddr,
+ [[maybe_unused]] mlir::Value Size,
+ [[maybe_unused]] CharUnits AlignmentInBytes) {
+
+  switch (CGF.getLangOpts().getTrivialAutoVarInit()) {
+  case LangOptions::TrivialAutoVarInitKind::Uninitialized:
+// Nothing to initialize.
+return;
+  case LangOptions::TrivialAutoVarInitKind::Zero:
+  case LangOptions::TrivialAutoVarInitKind::Pattern:
+assert(false && "unexpected trivial auto var init kind NYI");
+return;
+  }
+}
+
 RValue CIRGenFunction::emitRotate(const CallExpr *e, bool isRotateLeft) {
   mlir::Value input = emitScalarExpr(e->getArg(0));
   mlir::Value amount = emitScalarExpr(e->getArg(1));
@@ -172,21 +190,8 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
 builder.getUInt8Ty(), "bi_alloca", suitableAlignmentInBytes, size);
 
 // Initialize the allocated buffer if required.
-if (builtinID != Builtin::BI__builtin_alloca_uninitialized) {
-  // Initialize the alloca with the given size and alignment according to
-  // the lang opts. Only the trivial non-initialization is supported for
-  // now.
-
-  switch (getLangOpts().getTrivialAutoVarInit()) {
-  case LangOptions::TrivialAutoVarInitKind::Uninitialized:
-// Nothing to initialize.
-break;
-  case LangOptions::TrivialAutoVarInitKind::Zero:
-  case LangOptions::TrivialAutoVarInitKind::Pattern:
-cgm.errorNYI("trivial auto var init");
-break;
-  }
-}
+if (builtinID != Builtin::BI__builtin_alloca_uninitialized)
+  initializeAlloca(*this, allocaAddr, size, suitableAlignmentInBytes);
 
 // An alloca will always return a pointer to the alloca (stack) address
 // space. This address space need not be the same as the AST / Language
@@ -194,6 +199,12 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
 // the AST level this is handled within CreateTempAlloca et al., but for 
the
 // builtin / dynamic alloca we have to handle it here.
 assert(!cir::MissingFeatures::addressSpace());
+cir::AddressSpace aas = getCIRAllocaAddressSpace();
+cir::AddressSpace eas = cir::toCIRAddressSpace(
+e->getType()->getPointeeType().getAddressSpace());
+if (eas != aas) {
+  assert(false && "Non-default add

[llvm-branch-commits] [clang] [CIR] Upstream AddressSpace casting support (PR #161212)

2025-10-18 Thread David Rivera via llvm-branch-commits

https://github.com/RiverDave edited 
https://github.com/llvm/llvm-project/pull/161212
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] [CIR] Upstream `AddressSpace` conversions support (PR #161212)

2025-10-18 Thread David Rivera via llvm-branch-commits


@@ -423,7 +423,10 @@ LogicalResult cir::CastOp::verify() {
 auto resPtrTy = mlir::dyn_cast(resType);
 
 if (srcPtrTy && resPtrTy) {
-  return success();
+  if (srcPtrTy.getAddrSpace() != resPtrTy.getAddrSpace()) {

RiverDave wrote:

Good point, I've addressed this on top of the function before the cast type is 
checked. Let me now if that's sufficient.

https://github.com/llvm/llvm-project/pull/161212
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] [CIR] Upstream AddressSpace casting support (PR #161212)

2025-10-18 Thread David Rivera via llvm-branch-commits

https://github.com/RiverDave updated 
https://github.com/llvm/llvm-project/pull/161212

>From 52f8f0f33fc6507e40a50dfc15bc072a20646d41 Mon Sep 17 00:00:00 2001
From: David Rivera 
Date: Mon, 29 Sep 2025 11:05:44 -0400
Subject: [PATCH] [CIR] Upstream AddressSpace casting support

---
 .../CIR/Dialect/Builder/CIRBaseBuilder.h  |  9 +++
 clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp   | 43 +++-
 clang/lib/CIR/CodeGen/CIRGenExpr.cpp  | 19 +-
 clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp| 22 ++
 clang/lib/CIR/CodeGen/CIRGenFunction.h|  4 ++
 clang/lib/CIR/CodeGen/CIRGenModule.cpp| 17 +
 clang/lib/CIR/CodeGen/CIRGenModule.h  |  6 ++
 clang/lib/CIR/CodeGen/CIRGenTypes.cpp |  2 +-
 clang/lib/CIR/CodeGen/TargetInfo.cpp  | 12 
 clang/lib/CIR/CodeGen/TargetInfo.h| 12 
 clang/test/CIR/address-space-conversion.cpp   | 68 +++
 11 files changed, 195 insertions(+), 19 deletions(-)
 create mode 100644 clang/test/CIR/address-space-conversion.cpp

diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h 
b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
index cef8624e65d57..bf4a9b8438982 100644
--- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
+++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
@@ -424,6 +424,15 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
 return createBitcast(src, getPointerTo(newPointeeTy));
   }
 
+  mlir::Value createAddrSpaceCast(mlir::Location loc, mlir::Value src,
+  mlir::Type newTy) {
+return createCast(loc, cir::CastKind::address_space, src, newTy);
+  }
+
+  mlir::Value createAddrSpaceCast(mlir::Value src, mlir::Type newTy) {
+return createAddrSpaceCast(src.getLoc(), src, newTy);
+  }
+
   
//======//
   // Binary Operators
   
//======//
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp 
b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index cf17de144f4d9..d17a73a2b5cbe 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -58,6 +58,24 @@ static RValue emitBuiltinBitOp(CIRGenFunction &cgf, const 
CallExpr *e,
   return RValue::get(result);
 }
 
+// Initialize the alloca with the given size and alignment according to the 
lang
+// opts. Supporting only the trivial non-initialization for now.
+static void initializeAlloca(CIRGenFunction &CGF,
+ [[maybe_unused]] mlir::Value AllocaAddr,
+ [[maybe_unused]] mlir::Value Size,
+ [[maybe_unused]] CharUnits AlignmentInBytes) {
+
+  switch (CGF.getLangOpts().getTrivialAutoVarInit()) {
+  case LangOptions::TrivialAutoVarInitKind::Uninitialized:
+// Nothing to initialize.
+return;
+  case LangOptions::TrivialAutoVarInitKind::Zero:
+  case LangOptions::TrivialAutoVarInitKind::Pattern:
+assert(false && "unexpected trivial auto var init kind NYI");
+return;
+  }
+}
+
 RValue CIRGenFunction::emitRotate(const CallExpr *e, bool isRotateLeft) {
   mlir::Value input = emitScalarExpr(e->getArg(0));
   mlir::Value amount = emitScalarExpr(e->getArg(1));
@@ -167,26 +185,13 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
 // FIXME(cir): It may make sense to allow AllocaOp of type `u8` to return a
 // pointer of type `void *`. This will require a change to the allocaOp
 // verifier.
-mlir::Value allocaAddr = builder.createAlloca(
+auto allocaAddr = builder.createAlloca(
 getLoc(e->getSourceRange()), builder.getUInt8PtrTy(),
 builder.getUInt8Ty(), "bi_alloca", suitableAlignmentInBytes, size);
 
 // Initialize the allocated buffer if required.
-if (builtinID != Builtin::BI__builtin_alloca_uninitialized) {
-  // Initialize the alloca with the given size and alignment according to
-  // the lang opts. Only the trivial non-initialization is supported for
-  // now.
-
-  switch (getLangOpts().getTrivialAutoVarInit()) {
-  case LangOptions::TrivialAutoVarInitKind::Uninitialized:
-// Nothing to initialize.
-break;
-  case LangOptions::TrivialAutoVarInitKind::Zero:
-  case LangOptions::TrivialAutoVarInitKind::Pattern:
-cgm.errorNYI("trivial auto var init");
-break;
-  }
-}
+if (builtinID != Builtin::BI__builtin_alloca_uninitialized)
+  initializeAlloca(*this, allocaAddr, size, suitableAlignmentInBytes);
 
 // An alloca will always return a pointer to the alloca (stack) address
 // space. This address space need not be the same as the AST / Language
@@ -194,6 +199,12 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
 // the AST level this is handled within CreateTempAlloca et al., but for 
the
 // bu

[llvm-branch-commits] [clang] [CIR] Upstream `AddressSpace` conversions support (PR #161212)

2025-10-18 Thread David Rivera via llvm-branch-commits

https://github.com/RiverDave updated 
https://github.com/llvm/llvm-project/pull/161212

>From baaea0b9d214bd5940f4b16909d47f491918c584 Mon Sep 17 00:00:00 2001
From: David Rivera 
Date: Mon, 29 Sep 2025 11:05:44 -0400
Subject: [PATCH 1/4] [CIR] Upstream AddressSpace casting support

---
 .../CIR/Dialect/Builder/CIRBaseBuilder.h  |  9 +++
 clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp   | 41 +++
 clang/lib/CIR/CodeGen/CIRGenExpr.cpp  | 19 +-
 clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp| 22 ++
 clang/lib/CIR/CodeGen/CIRGenFunction.h|  4 ++
 clang/lib/CIR/CodeGen/CIRGenModule.cpp| 17 +
 clang/lib/CIR/CodeGen/CIRGenModule.h  |  6 ++
 clang/lib/CIR/CodeGen/CIRGenTypes.cpp |  2 +-
 clang/lib/CIR/CodeGen/TargetInfo.cpp  | 13 
 clang/lib/CIR/CodeGen/TargetInfo.h| 13 
 clang/test/CIR/address-space-conversion.cpp   | 68 +++
 11 files changed, 196 insertions(+), 18 deletions(-)
 create mode 100644 clang/test/CIR/address-space-conversion.cpp

diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h 
b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
index b875fac9b7969..4765934582bb8 100644
--- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
+++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
@@ -442,6 +442,15 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
 return createBitcast(src, getPointerTo(newPointeeTy));
   }
 
+  mlir::Value createAddrSpaceCast(mlir::Location loc, mlir::Value src,
+  mlir::Type newTy) {
+return createCast(loc, cir::CastKind::address_space, src, newTy);
+  }
+
+  mlir::Value createAddrSpaceCast(mlir::Value src, mlir::Type newTy) {
+return createAddrSpaceCast(src.getLoc(), src, newTy);
+  }
+
   
//======//
   // Binary Operators
   
//======//
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp 
b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index cf17de144f4d9..95e392d860518 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -58,6 +58,24 @@ static RValue emitBuiltinBitOp(CIRGenFunction &cgf, const 
CallExpr *e,
   return RValue::get(result);
 }
 
+// Initialize the alloca with the given size and alignment according to the 
lang
+// opts. Supporting only the trivial non-initialization for now.
+static void initializeAlloca(CIRGenFunction &CGF,
+ [[maybe_unused]] mlir::Value AllocaAddr,
+ [[maybe_unused]] mlir::Value Size,
+ [[maybe_unused]] CharUnits AlignmentInBytes) {
+
+  switch (CGF.getLangOpts().getTrivialAutoVarInit()) {
+  case LangOptions::TrivialAutoVarInitKind::Uninitialized:
+// Nothing to initialize.
+return;
+  case LangOptions::TrivialAutoVarInitKind::Zero:
+  case LangOptions::TrivialAutoVarInitKind::Pattern:
+assert(false && "unexpected trivial auto var init kind NYI");
+return;
+  }
+}
+
 RValue CIRGenFunction::emitRotate(const CallExpr *e, bool isRotateLeft) {
   mlir::Value input = emitScalarExpr(e->getArg(0));
   mlir::Value amount = emitScalarExpr(e->getArg(1));
@@ -172,21 +190,8 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
 builder.getUInt8Ty(), "bi_alloca", suitableAlignmentInBytes, size);
 
 // Initialize the allocated buffer if required.
-if (builtinID != Builtin::BI__builtin_alloca_uninitialized) {
-  // Initialize the alloca with the given size and alignment according to
-  // the lang opts. Only the trivial non-initialization is supported for
-  // now.
-
-  switch (getLangOpts().getTrivialAutoVarInit()) {
-  case LangOptions::TrivialAutoVarInitKind::Uninitialized:
-// Nothing to initialize.
-break;
-  case LangOptions::TrivialAutoVarInitKind::Zero:
-  case LangOptions::TrivialAutoVarInitKind::Pattern:
-cgm.errorNYI("trivial auto var init");
-break;
-  }
-}
+if (builtinID != Builtin::BI__builtin_alloca_uninitialized)
+  initializeAlloca(*this, allocaAddr, size, suitableAlignmentInBytes);
 
 // An alloca will always return a pointer to the alloca (stack) address
 // space. This address space need not be the same as the AST / Language
@@ -194,6 +199,12 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
 // the AST level this is handled within CreateTempAlloca et al., but for 
the
 // builtin / dynamic alloca we have to handle it here.
 assert(!cir::MissingFeatures::addressSpace());
+cir::AddressSpace aas = getCIRAllocaAddressSpace();
+cir::AddressSpace eas = cir::toCIRAddressSpace(
+e->getType()->getPointeeType().getAddressSpace());
+if (eas != aas) {
+  assert(false && "Non-default add

[llvm-branch-commits] [clang] [CIR] Upstream `AddressSpace` conversions support (PR #161212)

2025-10-17 Thread David Rivera via llvm-branch-commits

https://github.com/RiverDave updated 
https://github.com/llvm/llvm-project/pull/161212

>From e446b4723e7df94f057d20a9ad0fdc28964bb2fd Mon Sep 17 00:00:00 2001
From: David Rivera 
Date: Mon, 29 Sep 2025 11:05:44 -0400
Subject: [PATCH 1/2] [CIR] Upstream AddressSpace casting support

---
 .../CIR/Dialect/Builder/CIRBaseBuilder.h  |  9 +++
 clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp   | 41 +++
 clang/lib/CIR/CodeGen/CIRGenExpr.cpp  | 19 +-
 clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp| 22 ++
 clang/lib/CIR/CodeGen/CIRGenFunction.h|  4 ++
 clang/lib/CIR/CodeGen/CIRGenModule.cpp| 17 +
 clang/lib/CIR/CodeGen/CIRGenModule.h  |  6 ++
 clang/lib/CIR/CodeGen/CIRGenTypes.cpp |  2 +-
 clang/lib/CIR/CodeGen/TargetInfo.cpp  | 13 
 clang/lib/CIR/CodeGen/TargetInfo.h| 12 
 clang/test/CIR/address-space-conversion.cpp   | 68 +++
 11 files changed, 195 insertions(+), 18 deletions(-)
 create mode 100644 clang/test/CIR/address-space-conversion.cpp

diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h 
b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
index cef8624e65d57..bf4a9b8438982 100644
--- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
+++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
@@ -424,6 +424,15 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
 return createBitcast(src, getPointerTo(newPointeeTy));
   }
 
+  mlir::Value createAddrSpaceCast(mlir::Location loc, mlir::Value src,
+  mlir::Type newTy) {
+return createCast(loc, cir::CastKind::address_space, src, newTy);
+  }
+
+  mlir::Value createAddrSpaceCast(mlir::Value src, mlir::Type newTy) {
+return createAddrSpaceCast(src.getLoc(), src, newTy);
+  }
+
   
//======//
   // Binary Operators
   
//======//
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp 
b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index cf17de144f4d9..95e392d860518 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -58,6 +58,24 @@ static RValue emitBuiltinBitOp(CIRGenFunction &cgf, const 
CallExpr *e,
   return RValue::get(result);
 }
 
+// Initialize the alloca with the given size and alignment according to the 
lang
+// opts. Supporting only the trivial non-initialization for now.
+static void initializeAlloca(CIRGenFunction &CGF,
+ [[maybe_unused]] mlir::Value AllocaAddr,
+ [[maybe_unused]] mlir::Value Size,
+ [[maybe_unused]] CharUnits AlignmentInBytes) {
+
+  switch (CGF.getLangOpts().getTrivialAutoVarInit()) {
+  case LangOptions::TrivialAutoVarInitKind::Uninitialized:
+// Nothing to initialize.
+return;
+  case LangOptions::TrivialAutoVarInitKind::Zero:
+  case LangOptions::TrivialAutoVarInitKind::Pattern:
+assert(false && "unexpected trivial auto var init kind NYI");
+return;
+  }
+}
+
 RValue CIRGenFunction::emitRotate(const CallExpr *e, bool isRotateLeft) {
   mlir::Value input = emitScalarExpr(e->getArg(0));
   mlir::Value amount = emitScalarExpr(e->getArg(1));
@@ -172,21 +190,8 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
 builder.getUInt8Ty(), "bi_alloca", suitableAlignmentInBytes, size);
 
 // Initialize the allocated buffer if required.
-if (builtinID != Builtin::BI__builtin_alloca_uninitialized) {
-  // Initialize the alloca with the given size and alignment according to
-  // the lang opts. Only the trivial non-initialization is supported for
-  // now.
-
-  switch (getLangOpts().getTrivialAutoVarInit()) {
-  case LangOptions::TrivialAutoVarInitKind::Uninitialized:
-// Nothing to initialize.
-break;
-  case LangOptions::TrivialAutoVarInitKind::Zero:
-  case LangOptions::TrivialAutoVarInitKind::Pattern:
-cgm.errorNYI("trivial auto var init");
-break;
-  }
-}
+if (builtinID != Builtin::BI__builtin_alloca_uninitialized)
+  initializeAlloca(*this, allocaAddr, size, suitableAlignmentInBytes);
 
 // An alloca will always return a pointer to the alloca (stack) address
 // space. This address space need not be the same as the AST / Language
@@ -194,6 +199,12 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
 // the AST level this is handled within CreateTempAlloca et al., but for 
the
 // builtin / dynamic alloca we have to handle it here.
 assert(!cir::MissingFeatures::addressSpace());
+cir::AddressSpace aas = getCIRAllocaAddressSpace();
+cir::AddressSpace eas = cir::toCIRAddressSpace(
+e->getType()->getPointeeType().getAddressSpace());
+if (eas != aas) {
+  assert(false && "Non-default add

[llvm-branch-commits] [clang] [CIR] Upstream AddressSpace casting support (PR #161212)

2025-10-18 Thread David Rivera via llvm-branch-commits

https://github.com/RiverDave ready_for_review 
https://github.com/llvm/llvm-project/pull/161212
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] [CIR] Upstream AddressSpace casting support (PR #161212)

2025-10-18 Thread David Rivera via llvm-branch-commits

https://github.com/RiverDave created 
https://github.com/llvm/llvm-project/pull/161212

None

>From 22568bd8200bef8a4f6759d4bcbdecfc91a0bf3e Mon Sep 17 00:00:00 2001
From: David Rivera 
Date: Mon, 29 Sep 2025 11:05:44 -0400
Subject: [PATCH] [CIR] Upstream AddressSpace casting support

---
 .../CIR/Dialect/Builder/CIRBaseBuilder.h  |  9 +++
 clang/include/clang/CIR/Dialect/IR/CIRTypes.h |  2 +
 clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp   | 43 +++-
 clang/lib/CIR/CodeGen/CIRGenExpr.cpp  | 19 +-
 clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp| 21 ++
 clang/lib/CIR/CodeGen/CIRGenFunction.h|  4 ++
 clang/lib/CIR/CodeGen/CIRGenModule.cpp| 17 +
 clang/lib/CIR/CodeGen/CIRGenModule.h  |  6 ++
 clang/lib/CIR/CodeGen/CIRGenTypes.cpp |  2 +-
 clang/lib/CIR/CodeGen/TargetInfo.cpp  | 12 
 clang/lib/CIR/CodeGen/TargetInfo.h| 12 
 clang/test/CIR/address-space-conversion.cpp   | 68 +++
 12 files changed, 196 insertions(+), 19 deletions(-)
 create mode 100644 clang/test/CIR/address-space-conversion.cpp

diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h 
b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
index cef8624e65d57..bf4a9b8438982 100644
--- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
+++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
@@ -424,6 +424,15 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
 return createBitcast(src, getPointerTo(newPointeeTy));
   }
 
+  mlir::Value createAddrSpaceCast(mlir::Location loc, mlir::Value src,
+  mlir::Type newTy) {
+return createCast(loc, cir::CastKind::address_space, src, newTy);
+  }
+
+  mlir::Value createAddrSpaceCast(mlir::Value src, mlir::Type newTy) {
+return createAddrSpaceCast(src.getLoc(), src, newTy);
+  }
+
   
//======//
   // Binary Operators
   
//======//
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.h 
b/clang/include/clang/CIR/Dialect/IR/CIRTypes.h
index 6a2b02ce46cd6..2f4191091f97e 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.h
+++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.h
@@ -19,6 +19,8 @@
 #include "clang/Basic/AddressSpaces.h"
 #include "clang/CIR/Dialect/IR/CIROpsEnums.h"
 #include "clang/CIR/Interfaces/CIRTypeInterfaces.h"
+#include "clang/CIR/Dialect/IR/CIROpsEnums.h"
+#include "clang/Basic/AddressSpaces.h"
 
 namespace cir {
 
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp 
b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index cf17de144f4d9..d17a73a2b5cbe 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -58,6 +58,24 @@ static RValue emitBuiltinBitOp(CIRGenFunction &cgf, const 
CallExpr *e,
   return RValue::get(result);
 }
 
+// Initialize the alloca with the given size and alignment according to the 
lang
+// opts. Supporting only the trivial non-initialization for now.
+static void initializeAlloca(CIRGenFunction &CGF,
+ [[maybe_unused]] mlir::Value AllocaAddr,
+ [[maybe_unused]] mlir::Value Size,
+ [[maybe_unused]] CharUnits AlignmentInBytes) {
+
+  switch (CGF.getLangOpts().getTrivialAutoVarInit()) {
+  case LangOptions::TrivialAutoVarInitKind::Uninitialized:
+// Nothing to initialize.
+return;
+  case LangOptions::TrivialAutoVarInitKind::Zero:
+  case LangOptions::TrivialAutoVarInitKind::Pattern:
+assert(false && "unexpected trivial auto var init kind NYI");
+return;
+  }
+}
+
 RValue CIRGenFunction::emitRotate(const CallExpr *e, bool isRotateLeft) {
   mlir::Value input = emitScalarExpr(e->getArg(0));
   mlir::Value amount = emitScalarExpr(e->getArg(1));
@@ -167,26 +185,13 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
 // FIXME(cir): It may make sense to allow AllocaOp of type `u8` to return a
 // pointer of type `void *`. This will require a change to the allocaOp
 // verifier.
-mlir::Value allocaAddr = builder.createAlloca(
+auto allocaAddr = builder.createAlloca(
 getLoc(e->getSourceRange()), builder.getUInt8PtrTy(),
 builder.getUInt8Ty(), "bi_alloca", suitableAlignmentInBytes, size);
 
 // Initialize the allocated buffer if required.
-if (builtinID != Builtin::BI__builtin_alloca_uninitialized) {
-  // Initialize the alloca with the given size and alignment according to
-  // the lang opts. Only the trivial non-initialization is supported for
-  // now.
-
-  switch (getLangOpts().getTrivialAutoVarInit()) {
-  case LangOptions::TrivialAutoVarInitKind::Uninitialized:
-// Nothing to initialize.
-break;
-  case LangOptions::TrivialAutoVarInitKind::Zero:
-  case LangOptions::TrivialAutoVarInitKind::Pa

[llvm-branch-commits] [clang] [CIR] Upstream `AddressSpace` conversions support (PR #161212)

2025-10-18 Thread David Rivera via llvm-branch-commits

https://github.com/RiverDave edited 
https://github.com/llvm/llvm-project/pull/161212
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] [CIR] Upstream `AddressSpace` conversions support (PR #161212)

2025-10-02 Thread David Rivera via llvm-branch-commits

https://github.com/RiverDave edited 
https://github.com/llvm/llvm-project/pull/161212
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] [CIR] Upstream `AddressSpace` conversions support (PR #161212)

2025-10-02 Thread David Rivera via llvm-branch-commits

https://github.com/RiverDave updated 
https://github.com/llvm/llvm-project/pull/161212

>From baaea0b9d214bd5940f4b16909d47f491918c584 Mon Sep 17 00:00:00 2001
From: David Rivera 
Date: Mon, 29 Sep 2025 11:05:44 -0400
Subject: [PATCH 1/4] [CIR] Upstream AddressSpace casting support

---
 .../CIR/Dialect/Builder/CIRBaseBuilder.h  |  9 +++
 clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp   | 41 +++
 clang/lib/CIR/CodeGen/CIRGenExpr.cpp  | 19 +-
 clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp| 22 ++
 clang/lib/CIR/CodeGen/CIRGenFunction.h|  4 ++
 clang/lib/CIR/CodeGen/CIRGenModule.cpp| 17 +
 clang/lib/CIR/CodeGen/CIRGenModule.h  |  6 ++
 clang/lib/CIR/CodeGen/CIRGenTypes.cpp |  2 +-
 clang/lib/CIR/CodeGen/TargetInfo.cpp  | 13 
 clang/lib/CIR/CodeGen/TargetInfo.h| 13 
 clang/test/CIR/address-space-conversion.cpp   | 68 +++
 11 files changed, 196 insertions(+), 18 deletions(-)
 create mode 100644 clang/test/CIR/address-space-conversion.cpp

diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h 
b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
index b875fac9b7969..4765934582bb8 100644
--- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
+++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
@@ -442,6 +442,15 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
 return createBitcast(src, getPointerTo(newPointeeTy));
   }
 
+  mlir::Value createAddrSpaceCast(mlir::Location loc, mlir::Value src,
+  mlir::Type newTy) {
+return createCast(loc, cir::CastKind::address_space, src, newTy);
+  }
+
+  mlir::Value createAddrSpaceCast(mlir::Value src, mlir::Type newTy) {
+return createAddrSpaceCast(src.getLoc(), src, newTy);
+  }
+
   
//======//
   // Binary Operators
   
//======//
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp 
b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index cf17de144f4d9..95e392d860518 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -58,6 +58,24 @@ static RValue emitBuiltinBitOp(CIRGenFunction &cgf, const 
CallExpr *e,
   return RValue::get(result);
 }
 
+// Initialize the alloca with the given size and alignment according to the 
lang
+// opts. Supporting only the trivial non-initialization for now.
+static void initializeAlloca(CIRGenFunction &CGF,
+ [[maybe_unused]] mlir::Value AllocaAddr,
+ [[maybe_unused]] mlir::Value Size,
+ [[maybe_unused]] CharUnits AlignmentInBytes) {
+
+  switch (CGF.getLangOpts().getTrivialAutoVarInit()) {
+  case LangOptions::TrivialAutoVarInitKind::Uninitialized:
+// Nothing to initialize.
+return;
+  case LangOptions::TrivialAutoVarInitKind::Zero:
+  case LangOptions::TrivialAutoVarInitKind::Pattern:
+assert(false && "unexpected trivial auto var init kind NYI");
+return;
+  }
+}
+
 RValue CIRGenFunction::emitRotate(const CallExpr *e, bool isRotateLeft) {
   mlir::Value input = emitScalarExpr(e->getArg(0));
   mlir::Value amount = emitScalarExpr(e->getArg(1));
@@ -172,21 +190,8 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
 builder.getUInt8Ty(), "bi_alloca", suitableAlignmentInBytes, size);
 
 // Initialize the allocated buffer if required.
-if (builtinID != Builtin::BI__builtin_alloca_uninitialized) {
-  // Initialize the alloca with the given size and alignment according to
-  // the lang opts. Only the trivial non-initialization is supported for
-  // now.
-
-  switch (getLangOpts().getTrivialAutoVarInit()) {
-  case LangOptions::TrivialAutoVarInitKind::Uninitialized:
-// Nothing to initialize.
-break;
-  case LangOptions::TrivialAutoVarInitKind::Zero:
-  case LangOptions::TrivialAutoVarInitKind::Pattern:
-cgm.errorNYI("trivial auto var init");
-break;
-  }
-}
+if (builtinID != Builtin::BI__builtin_alloca_uninitialized)
+  initializeAlloca(*this, allocaAddr, size, suitableAlignmentInBytes);
 
 // An alloca will always return a pointer to the alloca (stack) address
 // space. This address space need not be the same as the AST / Language
@@ -194,6 +199,12 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
 // the AST level this is handled within CreateTempAlloca et al., but for 
the
 // builtin / dynamic alloca we have to handle it here.
 assert(!cir::MissingFeatures::addressSpace());
+cir::AddressSpace aas = getCIRAllocaAddressSpace();
+cir::AddressSpace eas = cir::toCIRAddressSpace(
+e->getType()->getPointeeType().getAddressSpace());
+if (eas != aas) {
+  assert(false && "Non-default add

[llvm-branch-commits] [clang] [CIR] Upstream `AddressSpace` conversions support (PR #161212)

2025-10-02 Thread David Rivera via llvm-branch-commits

RiverDave wrote:

I updated this based on the recent feedback on #161028 

I made a change tn the function: `performAddrSpaceCast` and I opted for getting 
rid of the `LangAS` parameters for both source and destination, there’s to main 
reasons:

1. They were redundant and not utilized.
2. In 
[OG](https://github.com/llvm/llvm-project/blob/487cdf14f67e95f61a42389bd168b32c00995ea4/clang/lib/CodeGen/TargetInfo.cpp#L149)
 they seem to be used to name SSA values in casts? I do not think that applies 
to CIR/MLIR in any way (correct me if I’m wrong)
3. This change made not long ago: 
https://github.com/llvm/llvm-project/pull/138866 => this applies only to dest, 
however as I previously mentioned, I don't see the point in replicating OG in 
that regard.

https://github.com/llvm/llvm-project/pull/161212
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] [CIR] Upstream `AddressSpace` conversions support (PR #161212)

2025-10-02 Thread David Rivera via llvm-branch-commits


@@ -172,28 +190,21 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
 builder.getUInt8Ty(), "bi_alloca", suitableAlignmentInBytes, size);
 
 // Initialize the allocated buffer if required.
-if (builtinID != Builtin::BI__builtin_alloca_uninitialized) {
-  // Initialize the alloca with the given size and alignment according to
-  // the lang opts. Only the trivial non-initialization is supported for
-  // now.
-
-  switch (getLangOpts().getTrivialAutoVarInit()) {
-  case LangOptions::TrivialAutoVarInitKind::Uninitialized:
-// Nothing to initialize.
-break;
-  case LangOptions::TrivialAutoVarInitKind::Zero:
-  case LangOptions::TrivialAutoVarInitKind::Pattern:
-cgm.errorNYI("trivial auto var init");
-break;
-  }
-}
+if (builtinID != Builtin::BI__builtin_alloca_uninitialized)
+  initializeAlloca(*this, allocaAddr, size, suitableAlignmentInBytes);
 
 // An alloca will always return a pointer to the alloca (stack) address
 // space. This address space need not be the same as the AST / Language
 // default (e.g. in C / C++ auto vars are in the generic address space). At
 // the AST level this is handled within CreateTempAlloca et al., but for 
the
 // builtin / dynamic alloca we have to handle it here.
 assert(!cir::MissingFeatures::addressSpace());
+cir::AddressSpace aas = getCIRAllocaAddressSpace();
+cir::AddressSpace eas = cir::toCIRAddressSpace(
+e->getType()->getPointeeType().getAddressSpace());
+if (eas != aas) {
+  assert(false && "Non-default address space for alloca NYI");

RiverDave wrote:

I prefer we defer this for a different PR. Reason is: We cannot simply perform 
an address space cast in a case like this where pointee types differ. (src 
differs from allocaDest)
see:

```cpp
//cpp
void test_builtin_alloca_addrspace() {
  // Alloca happens in default address space (0), then we cast to address space 
1
  void *raw_ptr = __builtin_alloca(sizeof(int));
  int __attribute__((address_space(1))) *as1_ptr = 
  (int __attribute__((address_space(1))) *)raw_ptr;
}

//cir
"cir.func"() <{dso_local, function_type = !cir.func<()>, global_visibility = 
#cir, linkage = 0 : i32, sym_name = 
"_Z29test_builtin_alloca_addrspacev"}> ({
%0 = "cir.alloca"() <{alignment = 8 : i64, allocaType = !cir.ptr, 
init, name = "raw_ptr"}> : () -> !cir.ptr> loc(#loc11)
%1 = "cir.const"() <{value = #cir.int<4> : !u64i}> : () -> !u64i loc(#loc12)
%2 = "cir.alloca"(%1) <{alignment = 16 : i64, allocaType = !u8i, name = 
"bi_alloca"}> : (!u64i) -> !cir.ptr loc(#loc13)
%3 = "cir.alloca"() <{alignment = 8 : i64, allocaType = !cir.ptr, init, name = "as1_ptr"}> : () -> 
!cir.ptr> loc(#loc14)
%4 = "cir.cast"(%2) <{kind = 1 : i32}> : (!cir.ptr) -> 
!cir.ptr loc(#loc13)
"cir.store"(%4, %0) <{alignment = 8 : i64}> : (!cir.ptr, 
!cir.ptr>) -> () loc(#loc11)
%5 = "cir.load"(%0) <{alignment = 8 : i64}> : (!cir.ptr>) 
-> !cir.ptr loc(#loc9)
/* CAST IS INVALID HERE =>*/%6 = "cir.cast"(%5) <{kind = 63 : i32}> : 
(!cir.ptr) -> !cir.ptr loc(#loc9)
"cir.store"(%6, %3) <{alignment = 8 : i64}> : (!cir.ptr, !cir.ptr>) 
-> () loc(#loc14)
"cir.return"() : () -> () loc(#loc2)
  }) : () -> () loc(#loc10)
  // OG:
  
  
```

As you can see, we’d hit the verifier (address casts are valid as long as both 
pointees are of the same type). I assume this is not the case in OG since they 
moved to utilize opaque/generic ptrs.

I assume the solution is to introduce an intermediate bitcast. (I experimented 
this on my own and worked fine, OG IR generated seemed to be equal). But again 
if you want we can dive more into that in the future.

https://github.com/llvm/llvm-project/pull/161212
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] [CIR] Upstream `AddressSpace` conversions support (PR #161212)

2025-10-02 Thread David Rivera via llvm-branch-commits

https://github.com/RiverDave updated 
https://github.com/llvm/llvm-project/pull/161212

>From baaea0b9d214bd5940f4b16909d47f491918c584 Mon Sep 17 00:00:00 2001
From: David Rivera 
Date: Mon, 29 Sep 2025 11:05:44 -0400
Subject: [PATCH 1/4] [CIR] Upstream AddressSpace casting support

---
 .../CIR/Dialect/Builder/CIRBaseBuilder.h  |  9 +++
 clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp   | 41 +++
 clang/lib/CIR/CodeGen/CIRGenExpr.cpp  | 19 +-
 clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp| 22 ++
 clang/lib/CIR/CodeGen/CIRGenFunction.h|  4 ++
 clang/lib/CIR/CodeGen/CIRGenModule.cpp| 17 +
 clang/lib/CIR/CodeGen/CIRGenModule.h  |  6 ++
 clang/lib/CIR/CodeGen/CIRGenTypes.cpp |  2 +-
 clang/lib/CIR/CodeGen/TargetInfo.cpp  | 13 
 clang/lib/CIR/CodeGen/TargetInfo.h| 13 
 clang/test/CIR/address-space-conversion.cpp   | 68 +++
 11 files changed, 196 insertions(+), 18 deletions(-)
 create mode 100644 clang/test/CIR/address-space-conversion.cpp

diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h 
b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
index b875fac9b7969..4765934582bb8 100644
--- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
+++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
@@ -442,6 +442,15 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
 return createBitcast(src, getPointerTo(newPointeeTy));
   }
 
+  mlir::Value createAddrSpaceCast(mlir::Location loc, mlir::Value src,
+  mlir::Type newTy) {
+return createCast(loc, cir::CastKind::address_space, src, newTy);
+  }
+
+  mlir::Value createAddrSpaceCast(mlir::Value src, mlir::Type newTy) {
+return createAddrSpaceCast(src.getLoc(), src, newTy);
+  }
+
   
//======//
   // Binary Operators
   
//======//
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp 
b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index cf17de144f4d9..95e392d860518 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -58,6 +58,24 @@ static RValue emitBuiltinBitOp(CIRGenFunction &cgf, const 
CallExpr *e,
   return RValue::get(result);
 }
 
+// Initialize the alloca with the given size and alignment according to the 
lang
+// opts. Supporting only the trivial non-initialization for now.
+static void initializeAlloca(CIRGenFunction &CGF,
+ [[maybe_unused]] mlir::Value AllocaAddr,
+ [[maybe_unused]] mlir::Value Size,
+ [[maybe_unused]] CharUnits AlignmentInBytes) {
+
+  switch (CGF.getLangOpts().getTrivialAutoVarInit()) {
+  case LangOptions::TrivialAutoVarInitKind::Uninitialized:
+// Nothing to initialize.
+return;
+  case LangOptions::TrivialAutoVarInitKind::Zero:
+  case LangOptions::TrivialAutoVarInitKind::Pattern:
+assert(false && "unexpected trivial auto var init kind NYI");
+return;
+  }
+}
+
 RValue CIRGenFunction::emitRotate(const CallExpr *e, bool isRotateLeft) {
   mlir::Value input = emitScalarExpr(e->getArg(0));
   mlir::Value amount = emitScalarExpr(e->getArg(1));
@@ -172,21 +190,8 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
 builder.getUInt8Ty(), "bi_alloca", suitableAlignmentInBytes, size);
 
 // Initialize the allocated buffer if required.
-if (builtinID != Builtin::BI__builtin_alloca_uninitialized) {
-  // Initialize the alloca with the given size and alignment according to
-  // the lang opts. Only the trivial non-initialization is supported for
-  // now.
-
-  switch (getLangOpts().getTrivialAutoVarInit()) {
-  case LangOptions::TrivialAutoVarInitKind::Uninitialized:
-// Nothing to initialize.
-break;
-  case LangOptions::TrivialAutoVarInitKind::Zero:
-  case LangOptions::TrivialAutoVarInitKind::Pattern:
-cgm.errorNYI("trivial auto var init");
-break;
-  }
-}
+if (builtinID != Builtin::BI__builtin_alloca_uninitialized)
+  initializeAlloca(*this, allocaAddr, size, suitableAlignmentInBytes);
 
 // An alloca will always return a pointer to the alloca (stack) address
 // space. This address space need not be the same as the AST / Language
@@ -194,6 +199,12 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
 // the AST level this is handled within CreateTempAlloca et al., but for 
the
 // builtin / dynamic alloca we have to handle it here.
 assert(!cir::MissingFeatures::addressSpace());
+cir::AddressSpace aas = getCIRAllocaAddressSpace();
+cir::AddressSpace eas = cir::toCIRAddressSpace(
+e->getType()->getPointeeType().getAddressSpace());
+if (eas != aas) {
+  assert(false && "Non-default add

[llvm-branch-commits] [clang] [CIR] Upstream `AddressSpace` conversions support (PR #161212)

2025-10-02 Thread David Rivera via llvm-branch-commits

https://github.com/RiverDave deleted 
https://github.com/llvm/llvm-project/pull/161212
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] [CIR] Upstream `AddressSpace` conversions support (PR #161212)

2025-09-30 Thread David Rivera via llvm-branch-commits


@@ -2283,7 +2295,10 @@ Address CIRGenFunction::createTempAlloca(mlir::Type ty, 
CharUnits align,
   // be different from the type defined by the language. For example,
   // in C++ the auto variables are in the default address space. Therefore
   // cast alloca to the default address space when necessary.
-  assert(!cir::MissingFeatures::addressSpace());
+  if (auto astAS = cir::toCIRAddressSpace(cgm.getLangTempAllocaAddressSpace());
+  getCIRAllocaAddressSpace() != astAS) {

RiverDave wrote:

I left a comment in place. as you mentioned I'll tackle this soon in a 
different PR.

https://github.com/llvm/llvm-project/pull/161212
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] [CIR] Upstream AddressSpace casting support (PR #161212)

2025-09-29 Thread David Rivera via llvm-branch-commits

RiverDave wrote:

> [!WARNING]
> This pull request is not mergeable via GitHub because a downstack PR is 
> open. Once all requirements are satisfied, merge this PR as a stack  href="https://app.graphite.dev/github/pr/llvm/llvm-project/161212?utm_source=stack-comment-downstack-mergeability-warning";
>  >on Graphite.
> https://graphite.dev/docs/merge-pull-requests";>Learn more

* **#161212** https://app.graphite.dev/github/pr/llvm/llvm-project/161212?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/> 👈 https://app.graphite.dev/github/pr/llvm/llvm-project/161212?utm_source=stack-comment-view-in-graphite";
 target="_blank">(View in Graphite)
* **#161028** https://app.graphite.dev/github/pr/llvm/llvm-project/161028?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* `main`




This stack of pull requests is managed by https://graphite.dev?utm-source=stack-comment";>Graphite. Learn 
more about https://stacking.dev/?utm_source=stack-comment";>stacking.


https://github.com/llvm/llvm-project/pull/161212
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] [CIR] Upstream AddressSpace casting support (PR #161212)

2025-10-02 Thread David Rivera via llvm-branch-commits

https://github.com/RiverDave updated 
https://github.com/llvm/llvm-project/pull/161212

>From a6313408ef8e8f9ad129e39cb0d8d0f2fb2f0ee3 Mon Sep 17 00:00:00 2001
From: David Rivera 
Date: Mon, 29 Sep 2025 11:05:44 -0400
Subject: [PATCH] [CIR] Upstream AddressSpace casting support

---
 .../CIR/Dialect/Builder/CIRBaseBuilder.h  |  9 +++
 clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp   | 41 +++
 clang/lib/CIR/CodeGen/CIRGenExpr.cpp  | 19 +-
 clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp| 22 ++
 clang/lib/CIR/CodeGen/CIRGenFunction.h|  4 ++
 clang/lib/CIR/CodeGen/CIRGenModule.cpp| 17 +
 clang/lib/CIR/CodeGen/CIRGenModule.h  |  6 ++
 clang/lib/CIR/CodeGen/CIRGenTypes.cpp |  2 +-
 clang/lib/CIR/CodeGen/TargetInfo.cpp  | 13 
 clang/lib/CIR/CodeGen/TargetInfo.h| 12 
 clang/test/CIR/address-space-conversion.cpp   | 68 +++
 11 files changed, 195 insertions(+), 18 deletions(-)
 create mode 100644 clang/test/CIR/address-space-conversion.cpp

diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h 
b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
index cef8624e65d57..bf4a9b8438982 100644
--- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
+++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
@@ -424,6 +424,15 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
 return createBitcast(src, getPointerTo(newPointeeTy));
   }
 
+  mlir::Value createAddrSpaceCast(mlir::Location loc, mlir::Value src,
+  mlir::Type newTy) {
+return createCast(loc, cir::CastKind::address_space, src, newTy);
+  }
+
+  mlir::Value createAddrSpaceCast(mlir::Value src, mlir::Type newTy) {
+return createAddrSpaceCast(src.getLoc(), src, newTy);
+  }
+
   
//======//
   // Binary Operators
   
//======//
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp 
b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index cf17de144f4d9..95e392d860518 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -58,6 +58,24 @@ static RValue emitBuiltinBitOp(CIRGenFunction &cgf, const 
CallExpr *e,
   return RValue::get(result);
 }
 
+// Initialize the alloca with the given size and alignment according to the 
lang
+// opts. Supporting only the trivial non-initialization for now.
+static void initializeAlloca(CIRGenFunction &CGF,
+ [[maybe_unused]] mlir::Value AllocaAddr,
+ [[maybe_unused]] mlir::Value Size,
+ [[maybe_unused]] CharUnits AlignmentInBytes) {
+
+  switch (CGF.getLangOpts().getTrivialAutoVarInit()) {
+  case LangOptions::TrivialAutoVarInitKind::Uninitialized:
+// Nothing to initialize.
+return;
+  case LangOptions::TrivialAutoVarInitKind::Zero:
+  case LangOptions::TrivialAutoVarInitKind::Pattern:
+assert(false && "unexpected trivial auto var init kind NYI");
+return;
+  }
+}
+
 RValue CIRGenFunction::emitRotate(const CallExpr *e, bool isRotateLeft) {
   mlir::Value input = emitScalarExpr(e->getArg(0));
   mlir::Value amount = emitScalarExpr(e->getArg(1));
@@ -172,21 +190,8 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
 builder.getUInt8Ty(), "bi_alloca", suitableAlignmentInBytes, size);
 
 // Initialize the allocated buffer if required.
-if (builtinID != Builtin::BI__builtin_alloca_uninitialized) {
-  // Initialize the alloca with the given size and alignment according to
-  // the lang opts. Only the trivial non-initialization is supported for
-  // now.
-
-  switch (getLangOpts().getTrivialAutoVarInit()) {
-  case LangOptions::TrivialAutoVarInitKind::Uninitialized:
-// Nothing to initialize.
-break;
-  case LangOptions::TrivialAutoVarInitKind::Zero:
-  case LangOptions::TrivialAutoVarInitKind::Pattern:
-cgm.errorNYI("trivial auto var init");
-break;
-  }
-}
+if (builtinID != Builtin::BI__builtin_alloca_uninitialized)
+  initializeAlloca(*this, allocaAddr, size, suitableAlignmentInBytes);
 
 // An alloca will always return a pointer to the alloca (stack) address
 // space. This address space need not be the same as the AST / Language
@@ -194,6 +199,12 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
 // the AST level this is handled within CreateTempAlloca et al., but for 
the
 // builtin / dynamic alloca we have to handle it here.
 assert(!cir::MissingFeatures::addressSpace());
+cir::AddressSpace aas = getCIRAllocaAddressSpace();
+cir::AddressSpace eas = cir::toCIRAddressSpace(
+e->getType()->getPointeeType().getAddressSpace());
+if (eas != aas) {
+  assert(false && "Non-default address