[llvm-branch-commits] [clang] [CIR] Upstream ComplexImagPtrOp for ComplexType (PR #144236)

2025-07-11 Thread Henrich Lauko via llvm-branch-commits


@@ -1775,6 +1775,44 @@ OpFoldResult cir::ComplexCreateOp::fold(FoldAdaptor 
adaptor) {
   return cir::ConstComplexAttr::get(realAttr, imagAttr);
 }
 
+//===--===//

xlauko wrote:

These are copy/paste. Can you extract one common implementation.


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


[llvm-branch-commits] [clang] [CIR] Upstream ComplexImagPtrOp for ComplexType (PR #144236)

2025-07-11 Thread Henrich Lauko via llvm-branch-commits


@@ -2385,4 +2385,62 @@ def ComplexCreateOp : CIR_Op<"complex.create", [Pure, 
SameTypeOperands]> {
   let hasFolder = 1;
 }
 
+//===--===//
+// ComplexRealPtrOp
+//===--===//
+
+def ComplexRealPtrOp : CIR_Op<"complex.real_ptr", [Pure]> {
+  let summary = "Derive a pointer to the real part of a complex value";
+  let description = [{
+`cir.complex.real_ptr` operation takes a pointer operand that points to a
+complex value of type `!cir.complex` and yields a pointer to the real part
+of the operand.
+
+Example:
+
+```mlir
+%1 = cir.complex.real_ptr %0 : !cir.ptr> -> 
!cir.ptr
+```
+  }];
+
+  let results = (outs CIR_PtrToIntOrFloatType:$result);
+  let arguments = (ins CIR_PtrToComplexType:$operand);
+
+  let assemblyFormat = [{
+$operand `:`
+qualified(type($operand)) `->` qualified(type($result)) attr-dict
+  }];
+
+  let hasVerifier = 1;
+}
+
+//===--===//
+// ComplexImagPtrOp
+//===--===//
+
+def ComplexImagPtrOp : CIR_Op<"complex.imag_ptr", [Pure]> {
+  let summary = "Derive a pointer to the imaginary part of a complex value";
+  let description = [{
+`cir.complex.imag_ptr` operation takes a pointer operand that points to a
+complex value of type `!cir.complex` and yields a pointer to the imaginary
+part of the operand.
+
+Example:
+
+```mlir
+%1 = cir.complex.imag_ptr %0 : !cir.ptr> -> 
!cir.ptr

xlauko wrote:

fix to 80 cols

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


[llvm-branch-commits] [clang] [CIR] Upstream ComplexImagPtrOp for ComplexType (PR #144236)

2025-07-11 Thread Henrich Lauko via llvm-branch-commits


@@ -1775,6 +1775,44 @@ OpFoldResult cir::ComplexCreateOp::fold(FoldAdaptor 
adaptor) {
   return cir::ConstComplexAttr::get(realAttr, imagAttr);
 }
 
+//===--===//
+// ComplexRealPtrOp
+//===--===//
+
+LogicalResult cir::ComplexRealPtrOp::verify() {
+  mlir::Type resultPointeeTy = getType().getPointee();
+  cir::PointerType operandPtrTy = getOperand().getType();
+  auto operandPointeeTy =
+  mlir::cast(operandPtrTy.getPointee());
+
+  if (resultPointeeTy != operandPointeeTy.getElementType()) {
+emitOpError()
+<< "cir.complex.real_ptr result type does not match operand type";
+return failure();
+  }
+
+  return success();
+}
+
+//===--===//
+// ComplexImagPtrOp
+//===--===//
+
+LogicalResult cir::ComplexImagPtrOp::verify() {
+  mlir::Type resultPointeeTy = getType().getPointee();
+  cir::PointerType operandPtrTy = getOperand().getType();
+  auto operandPointeeTy =
+  mlir::cast(operandPtrTy.getPointee());
+
+  if (resultPointeeTy != operandPointeeTy.getElementType()) {
+emitOpError()
+<< "cir.complex.imag_ptr result type does not match operand type";
+return failure();

xlauko wrote:

```suggestion
return emitOpError()
<< "cir.complex.imag_ptr result type does not match operand type";
```

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


[llvm-branch-commits] [clang] [CIR] Upstream ComplexImagPtrOp for ComplexType (PR #144236)

2025-07-11 Thread Henrich Lauko via llvm-branch-commits


@@ -1775,6 +1775,44 @@ OpFoldResult cir::ComplexCreateOp::fold(FoldAdaptor 
adaptor) {
   return cir::ConstComplexAttr::get(realAttr, imagAttr);
 }
 
+//===--===//
+// ComplexRealPtrOp
+//===--===//
+
+LogicalResult cir::ComplexRealPtrOp::verify() {
+  mlir::Type resultPointeeTy = getType().getPointee();
+  cir::PointerType operandPtrTy = getOperand().getType();
+  auto operandPointeeTy =
+  mlir::cast(operandPtrTy.getPointee());
+
+  if (resultPointeeTy != operandPointeeTy.getElementType()) {
+emitOpError()
+<< "cir.complex.real_ptr result type does not match operand type";
+return failure();

xlauko wrote:

```suggestion
return emitOpError()
<< "cir.complex.real_ptr result type does not match operand type";
```

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


[llvm-branch-commits] [clang] [CIR] Upstream ComplexImagPtrOp for ComplexType (PR #144236)

2025-06-17 Thread Bruno Cardoso Lopes via llvm-branch-commits

https://github.com/bcardosolopes commented:

This might look a bit different given the residesign of the other pieces, so 
I'm gonna hold on reviewing this for now, let me know when this is ready again.

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


[llvm-branch-commits] [clang] [CIR] Upstream ComplexImagPtrOp for ComplexType (PR #144236)

2025-06-14 Thread Amr Hesham via llvm-branch-commits

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


[llvm-branch-commits] [clang] [CIR] Upstream ComplexImagPtrOp for ComplexType (PR #144236)

2025-06-14 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Amr Hesham (AmrDeveloper)


Changes



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


7 Files Affected:

- (modified) clang/include/clang/CIR/Dialect/IR/CIROps.td (+29) 
- (modified) clang/lib/CIR/CodeGen/CIRGenBuilder.h (+14) 
- (modified) clang/lib/CIR/CodeGen/CIRGenExpr.cpp (+3-6) 
- (modified) clang/lib/CIR/Dialect/IR/CIRDialect.cpp (+19) 
- (modified) clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp (+19-1) 
- (modified) clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h (+10) 
- (modified) clang/test/CIR/CodeGen/complex.cpp (+14) 


``diff
diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td 
b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index 17279f0a9985a..f90784c99602c 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -2414,4 +2414,33 @@ def ComplexRealPtrOp : CIR_Op<"complex.real_ptr", 
[Pure]> {
   let hasVerifier = 1;
 }
 
+//===--===//
+// ComplexImagPtrOp
+//===--===//
+
+def ComplexImagPtrOp : CIR_Op<"complex.imag_ptr", [Pure]> {
+  let summary = "Derive a pointer to the imaginary part of a complex value";
+  let description = [{
+`cir.complex.imag_ptr` operation takes a pointer operand that points to a
+complex value of type `!cir.complex` and yields a pointer to the imaginary
+part of the operand.
+
+Example:
+
+```mlir
+%1 = cir.complex.imag_ptr %0 : !cir.ptr> -> 
!cir.ptr
+```
+  }];
+
+  let results = (outs CIR_PtrToIntOrFloatType:$result);
+  let arguments = (ins CIR_PtrToComplexType:$operand);
+
+  let assemblyFormat = [{
+$operand `:`
+qualified(type($operand)) `->` qualified(type($result)) attr-dict
+  }];
+
+  let hasVerifier = 1;
+}
+
 #endif // CLANG_CIR_DIALECT_IR_CIROPS_TD
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h 
b/clang/lib/CIR/CodeGen/CIRGenBuilder.h
index 3f7ea5bccb6d5..df3f4d65610a6 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h
+++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h
@@ -379,6 +379,20 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
 return Address{createRealPtr(loc, addr.getPointer()), addr.getAlignment()};
   }
 
+  /// Create a cir.complex.imag_ptr operation that derives a pointer to the
+  /// imaginary part of the complex value pointed to by the specified pointer
+  /// value.
+  mlir::Value createImagPtr(mlir::Location loc, mlir::Value value) {
+auto srcPtrTy = mlir::cast(value.getType());
+auto srcComplexTy = mlir::cast(srcPtrTy.getPointee());
+return create(
+loc, getPointerTo(srcComplexTy.getElementType()), value);
+  }
+
+  Address createImagPtr(mlir::Location loc, Address addr) {
+return Address{createImagPtr(loc, addr.getPointer()), addr.getAlignment()};
+  }
+
   /// Create a cir.ptr_stride operation to get access to an array element.
   /// \p idx is the index of the element to access, \p shouldDecay is true if
   /// the result should decay to a pointer to the element type.
diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp 
b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
index a682586562e04..8e3d9ab620621 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
@@ -541,11 +541,6 @@ LValue CIRGenFunction::emitUnaryOpLValue(const 
UnaryOperator *e) {
   }
   case UO_Real:
   case UO_Imag: {
-if (op == UO_Imag) {
-  cgm.errorNYI(e->getSourceRange(), "UnaryOp real/imag");
-  return LValue();
-}
-
 LValue lv = emitLValue(e->getSubExpr());
 assert(lv.isSimple() && "real/imag on non-ordinary l-value");
 
@@ -560,7 +555,9 @@ LValue CIRGenFunction::emitUnaryOpLValue(const 
UnaryOperator *e) {
 QualType exprTy = 
getContext().getCanonicalType(e->getSubExpr()->getType());
 QualType elemTy = exprTy->castAs()->getElementType();
 mlir::Location loc = getLoc(e->getExprLoc());
-Address component = builder.createRealPtr(loc, lv.getAddress());
+Address component = op == UO_Real
+? builder.createRealPtr(loc, lv.getAddress())
+: builder.createImagPtr(loc, lv.getAddress());
 LValue elemLV = makeAddrLValue(component, elemTy);
 elemLV.getQuals().addQualifiers(lv.getQuals());
 return elemLV;
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp 
b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index 99ae4dd59120a..40488f6af5676 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -1794,6 +1794,25 @@ LogicalResult cir::ComplexRealPtrOp::verify() {
   return success();
 }
 
+//===--===//
+// ComplexImagPtrOp
+//===--===//
+
+LogicalResult cir::ComplexImagPtrOp::verify() {
+  mlir::Type resultPoi

[llvm-branch-commits] [clang] [CIR] Upstream ComplexImagPtrOp for ComplexType (PR #144236)

2025-06-14 Thread Amr Hesham via llvm-branch-commits

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


[llvm-branch-commits] [clang] [CIR] Upstream ComplexImagPtrOp for ComplexType (PR #144236)

2025-06-14 Thread Amr Hesham via llvm-branch-commits

AmrDeveloper 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/144236?utm_source=stack-comment-downstack-mergeability-warning";
>  >on Graphite.
> https://graphite.dev/docs/merge-pull-requests";>Learn more

* **#144236** https://app.graphite.dev/github/pr/llvm/llvm-project/144236?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/144236?utm_source=stack-comment-view-in-graphite";
 target="_blank">(View in Graphite)
* **#144235** https://app.graphite.dev/github/pr/llvm/llvm-project/144235?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/144236
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] [CIR] Upstream ComplexImagPtrOp for ComplexType (PR #144236)

2025-06-14 Thread Amr Hesham via llvm-branch-commits

https://github.com/AmrDeveloper created 
https://github.com/llvm/llvm-project/pull/144236

None

>From dc824e4ff23a7ff5124de5a21362e5b543c55d27 Mon Sep 17 00:00:00 2001
From: AmrDeveloper 
Date: Sat, 14 Jun 2025 22:19:08 +0200
Subject: [PATCH] [CIR] Upstream ComplexImagPtrOp for ComplexType

---
 clang/include/clang/CIR/Dialect/IR/CIROps.td  | 29 +++
 clang/lib/CIR/CodeGen/CIRGenBuilder.h | 14 +
 clang/lib/CIR/CodeGen/CIRGenExpr.cpp  |  9 ++
 clang/lib/CIR/Dialect/IR/CIRDialect.cpp   | 19 
 .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 20 -
 .../CIR/Lowering/DirectToLLVM/LowerToLLVM.h   | 10 +++
 clang/test/CIR/CodeGen/complex.cpp| 14 +
 7 files changed, 108 insertions(+), 7 deletions(-)

diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td 
b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index 17279f0a9985a..f90784c99602c 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -2414,4 +2414,33 @@ def ComplexRealPtrOp : CIR_Op<"complex.real_ptr", 
[Pure]> {
   let hasVerifier = 1;
 }
 
+//===--===//
+// ComplexImagPtrOp
+//===--===//
+
+def ComplexImagPtrOp : CIR_Op<"complex.imag_ptr", [Pure]> {
+  let summary = "Derive a pointer to the imaginary part of a complex value";
+  let description = [{
+`cir.complex.imag_ptr` operation takes a pointer operand that points to a
+complex value of type `!cir.complex` and yields a pointer to the imaginary
+part of the operand.
+
+Example:
+
+```mlir
+%1 = cir.complex.imag_ptr %0 : !cir.ptr> -> 
!cir.ptr
+```
+  }];
+
+  let results = (outs CIR_PtrToIntOrFloatType:$result);
+  let arguments = (ins CIR_PtrToComplexType:$operand);
+
+  let assemblyFormat = [{
+$operand `:`
+qualified(type($operand)) `->` qualified(type($result)) attr-dict
+  }];
+
+  let hasVerifier = 1;
+}
+
 #endif // CLANG_CIR_DIALECT_IR_CIROPS_TD
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h 
b/clang/lib/CIR/CodeGen/CIRGenBuilder.h
index 3f7ea5bccb6d5..df3f4d65610a6 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h
+++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h
@@ -379,6 +379,20 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
 return Address{createRealPtr(loc, addr.getPointer()), addr.getAlignment()};
   }
 
+  /// Create a cir.complex.imag_ptr operation that derives a pointer to the
+  /// imaginary part of the complex value pointed to by the specified pointer
+  /// value.
+  mlir::Value createImagPtr(mlir::Location loc, mlir::Value value) {
+auto srcPtrTy = mlir::cast(value.getType());
+auto srcComplexTy = mlir::cast(srcPtrTy.getPointee());
+return create(
+loc, getPointerTo(srcComplexTy.getElementType()), value);
+  }
+
+  Address createImagPtr(mlir::Location loc, Address addr) {
+return Address{createImagPtr(loc, addr.getPointer()), addr.getAlignment()};
+  }
+
   /// Create a cir.ptr_stride operation to get access to an array element.
   /// \p idx is the index of the element to access, \p shouldDecay is true if
   /// the result should decay to a pointer to the element type.
diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp 
b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
index a682586562e04..8e3d9ab620621 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
@@ -541,11 +541,6 @@ LValue CIRGenFunction::emitUnaryOpLValue(const 
UnaryOperator *e) {
   }
   case UO_Real:
   case UO_Imag: {
-if (op == UO_Imag) {
-  cgm.errorNYI(e->getSourceRange(), "UnaryOp real/imag");
-  return LValue();
-}
-
 LValue lv = emitLValue(e->getSubExpr());
 assert(lv.isSimple() && "real/imag on non-ordinary l-value");
 
@@ -560,7 +555,9 @@ LValue CIRGenFunction::emitUnaryOpLValue(const 
UnaryOperator *e) {
 QualType exprTy = 
getContext().getCanonicalType(e->getSubExpr()->getType());
 QualType elemTy = exprTy->castAs()->getElementType();
 mlir::Location loc = getLoc(e->getExprLoc());
-Address component = builder.createRealPtr(loc, lv.getAddress());
+Address component = op == UO_Real
+? builder.createRealPtr(loc, lv.getAddress())
+: builder.createImagPtr(loc, lv.getAddress());
 LValue elemLV = makeAddrLValue(component, elemTy);
 elemLV.getQuals().addQualifiers(lv.getQuals());
 return elemLV;
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp 
b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index 99ae4dd59120a..40488f6af5676 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -1794,6 +1794,25 @@ LogicalResult cir::ComplexRealPtrOp::verify() {
   return success();
 }
 
+//===--===//
+// ComplexImagPtrOp
+