Skip to content

Conversation

@AmrDeveloper
Copy link
Member

Upstream UnaryDeref support for ComplexType

#141365

@llvmbot llvmbot added clang Clang issues not falling into any other category ClangIR Anything related to the ClangIR project labels Jul 2, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 2, 2025

@llvm/pr-subscribers-clang

Author: Amr Hesham (AmrDeveloper)

Changes

Upstream UnaryDeref support for ComplexType

#141365


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

2 Files Affected:

  • (modified) clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp (+5)
  • (modified) clang/test/CIR/CodeGen/complex.cpp (+28)
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp b/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp
index 3dc47dd205244..3220d849f04c5 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp
@@ -50,6 +50,7 @@ class ComplexExprEmitter : public StmtVisitor<ComplexExprEmitter, mlir::Value> {
   mlir::Value VisitInitListExpr(const InitListExpr *e);
   mlir::Value VisitImaginaryLiteral(const ImaginaryLiteral *il);
   mlir::Value VisitParenExpr(ParenExpr *e);
+  mlir::Value VisitUnaryDeref(const Expr *e);
 };
 } // namespace
 
@@ -231,6 +232,10 @@ mlir::Value ComplexExprEmitter::VisitParenExpr(ParenExpr *e) {
   return Visit(e->getSubExpr());
 }
 
+mlir::Value ComplexExprEmitter::VisitUnaryDeref(const Expr *e) {
+  return emitLoadOfLValue(e);
+}
+
 LValue CIRGenFunction::emitComplexAssignmentLValue(const BinaryOperator *e) {
   assert(e->getOpcode() == BO_Assign && "Expected assign op");
 
diff --git a/clang/test/CIR/CodeGen/complex.cpp b/clang/test/CIR/CodeGen/complex.cpp
index 78d7a2024490b..530c6292351ce 100644
--- a/clang/test/CIR/CodeGen/complex.cpp
+++ b/clang/test/CIR/CodeGen/complex.cpp
@@ -607,3 +607,31 @@ void foo24() {
 // OGCG: %[[RESULT_IMAG_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[RESULT]], i32 0, i32 1
 // OGCG: store i32 %[[ELEM_REAL]], ptr %[[RESULT_REAL_PTR]], align 4
 // OGCG: store i32 %[[ELEM_IMAG]], ptr %[[RESULT_IMAG_PTR]], align 4
+
+void foo26(int _Complex* a) {
+  int _Complex b = *a;
+}
+
+// CIR: %[[COMPLEX_A_PTR:.*]] = cir.alloca !cir.ptr<!cir.complex<!s32i>>, !cir.ptr<!cir.ptr<!cir.complex<!s32i>>>, ["a", init]
+// CIR: %[[COMPLEX_B:.*]] = cir.alloca !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>, ["b", init]
+// CIR: %[[COMPLEX_A:.*]] = cir.load deref {{.*}} %[[COMPLEX_A_PTR]] : !cir.ptr<!cir.ptr<!cir.complex<!s32i>>>, !cir.ptr<!cir.complex<!s32i>>
+// CIR: %[[TMP:.*]] = cir.load{{.*}} %[[COMPLEX_A]] : !cir.ptr<!cir.complex<!s32i>>, !cir.complex<!s32i>
+// CIR: cir.store{{.*}} %[[TMP]], %[[COMPLEX_B]] : !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>
+
+// LLVM: %[[COMPLEX_A_PTR:.*]] = alloca ptr, i64 1, align 8
+// LLVM: %[[COMPLEX_B:.*]] = alloca { i32, i32 }, i64 1, align 4
+// LLVM: %[[COMPLEX_A:.*]] = load ptr, ptr %[[COMPLEX_A_PTR]], align 8
+// LLVM: %[[TMP:.*]] = load { i32, i32 }, ptr %[[COMPLEX_A]], align 4
+// LLVM: store { i32, i32 } %[[TMP]], ptr %[[COMPLEX_B]], align 4
+
+// OGCG: %[[COMPLEX_A_PTR:.*]] = alloca ptr, align 8
+// OGCG: %[[COMPLEX_B:.*]] = alloca { i32, i32 }, align 4
+// OGCG: %[[COMPLEX_A:.*]] = load ptr, ptr %[[COMPLEX_A_PTR]], align 8
+// OGCG: %[[A_REAL_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[COMPLEX_A]], i32 0, i32 0
+// OGCG: %[[A_REAL:.*]] = load i32, ptr %[[A_REAL_PTR]], align 4
+// OGCG: %[[A_IMAG_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[COMPLEX_A]], i32 0, i32 1
+// OGCG: %[[A_IMAG:.*]] = load i32, ptr %[[A_IMAG_PTR]], align 4
+// OGCG: %[[B_REAL_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[COMPLEX_B]], i32 0, i32 0
+// OGCG: %[[B_IMAG_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[COMPLEX_B]], i32 0, i32 1
+// OGCG: store i32 %[[A_REAL]], ptr %[[B_REAL_PTR]], align 4
+// OGCG: store i32 %[[A_IMAG]], ptr %[[B_IMAG_PTR]], align 4

@llvmbot
Copy link
Member

llvmbot commented Jul 2, 2025

@llvm/pr-subscribers-clangir

Author: Amr Hesham (AmrDeveloper)

Changes

Upstream UnaryDeref support for ComplexType

#141365


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

2 Files Affected:

  • (modified) clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp (+5)
  • (modified) clang/test/CIR/CodeGen/complex.cpp (+28)
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp b/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp
index 3dc47dd205244..3220d849f04c5 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp
@@ -50,6 +50,7 @@ class ComplexExprEmitter : public StmtVisitor<ComplexExprEmitter, mlir::Value> {
   mlir::Value VisitInitListExpr(const InitListExpr *e);
   mlir::Value VisitImaginaryLiteral(const ImaginaryLiteral *il);
   mlir::Value VisitParenExpr(ParenExpr *e);
+  mlir::Value VisitUnaryDeref(const Expr *e);
 };
 } // namespace
 
@@ -231,6 +232,10 @@ mlir::Value ComplexExprEmitter::VisitParenExpr(ParenExpr *e) {
   return Visit(e->getSubExpr());
 }
 
+mlir::Value ComplexExprEmitter::VisitUnaryDeref(const Expr *e) {
+  return emitLoadOfLValue(e);
+}
+
 LValue CIRGenFunction::emitComplexAssignmentLValue(const BinaryOperator *e) {
   assert(e->getOpcode() == BO_Assign && "Expected assign op");
 
diff --git a/clang/test/CIR/CodeGen/complex.cpp b/clang/test/CIR/CodeGen/complex.cpp
index 78d7a2024490b..530c6292351ce 100644
--- a/clang/test/CIR/CodeGen/complex.cpp
+++ b/clang/test/CIR/CodeGen/complex.cpp
@@ -607,3 +607,31 @@ void foo24() {
 // OGCG: %[[RESULT_IMAG_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[RESULT]], i32 0, i32 1
 // OGCG: store i32 %[[ELEM_REAL]], ptr %[[RESULT_REAL_PTR]], align 4
 // OGCG: store i32 %[[ELEM_IMAG]], ptr %[[RESULT_IMAG_PTR]], align 4
+
+void foo26(int _Complex* a) {
+  int _Complex b = *a;
+}
+
+// CIR: %[[COMPLEX_A_PTR:.*]] = cir.alloca !cir.ptr<!cir.complex<!s32i>>, !cir.ptr<!cir.ptr<!cir.complex<!s32i>>>, ["a", init]
+// CIR: %[[COMPLEX_B:.*]] = cir.alloca !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>, ["b", init]
+// CIR: %[[COMPLEX_A:.*]] = cir.load deref {{.*}} %[[COMPLEX_A_PTR]] : !cir.ptr<!cir.ptr<!cir.complex<!s32i>>>, !cir.ptr<!cir.complex<!s32i>>
+// CIR: %[[TMP:.*]] = cir.load{{.*}} %[[COMPLEX_A]] : !cir.ptr<!cir.complex<!s32i>>, !cir.complex<!s32i>
+// CIR: cir.store{{.*}} %[[TMP]], %[[COMPLEX_B]] : !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>
+
+// LLVM: %[[COMPLEX_A_PTR:.*]] = alloca ptr, i64 1, align 8
+// LLVM: %[[COMPLEX_B:.*]] = alloca { i32, i32 }, i64 1, align 4
+// LLVM: %[[COMPLEX_A:.*]] = load ptr, ptr %[[COMPLEX_A_PTR]], align 8
+// LLVM: %[[TMP:.*]] = load { i32, i32 }, ptr %[[COMPLEX_A]], align 4
+// LLVM: store { i32, i32 } %[[TMP]], ptr %[[COMPLEX_B]], align 4
+
+// OGCG: %[[COMPLEX_A_PTR:.*]] = alloca ptr, align 8
+// OGCG: %[[COMPLEX_B:.*]] = alloca { i32, i32 }, align 4
+// OGCG: %[[COMPLEX_A:.*]] = load ptr, ptr %[[COMPLEX_A_PTR]], align 8
+// OGCG: %[[A_REAL_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[COMPLEX_A]], i32 0, i32 0
+// OGCG: %[[A_REAL:.*]] = load i32, ptr %[[A_REAL_PTR]], align 4
+// OGCG: %[[A_IMAG_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[COMPLEX_A]], i32 0, i32 1
+// OGCG: %[[A_IMAG:.*]] = load i32, ptr %[[A_IMAG_PTR]], align 4
+// OGCG: %[[B_REAL_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[COMPLEX_B]], i32 0, i32 0
+// OGCG: %[[B_IMAG_PTR:.*]] = getelementptr inbounds nuw { i32, i32 }, ptr %[[COMPLEX_B]], i32 0, i32 1
+// OGCG: store i32 %[[A_REAL]], ptr %[[B_REAL_PTR]], align 4
+// OGCG: store i32 %[[A_IMAG]], ptr %[[B_IMAG_PTR]], align 4

Copy link
Member

@bcardosolopes bcardosolopes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@AmrDeveloper AmrDeveloper force-pushed the cir_complex_deref_expr branch from 2354885 to 255b4ee Compare July 3, 2025 16:15
@AmrDeveloper AmrDeveloper merged commit 1aa3969 into llvm:main Jul 3, 2025
9 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Jul 3, 2025

LLVM Buildbot has detected a new failure on builder flang-arm64-windows-msvc running on linaro-armv8-windows-msvc-01 while building clang at step 7 "test-build-unified-tree-check-flang".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/207/builds/3327

Here is the relevant piece of the build log for the reference
Step 7 (test-build-unified-tree-check-flang) failure: test (failure)
******************** TEST 'Flang :: Semantics/OpenMP/loop-association.f90' FAILED ********************
Exit Code: 1

Command Output (stdout):
--
# RUN: at line 1
"C:\Users\tcwg\scoop\apps\python\current\python.exe" C:\Users\tcwg\llvm-worker\flang-arm64-windows-msvc\llvm-project\flang\test\Semantics\OpenMP/../test_errors.py C:\Users\tcwg\llvm-worker\flang-arm64-windows-msvc\llvm-project\flang\test\Semantics\OpenMP\loop-association.f90 c:\users\tcwg\llvm-worker\flang-arm64-windows-msvc\build\bin\flang.exe -fopenmp
# executed command: 'C:\Users\tcwg\scoop\apps\python\current\python.exe' 'C:\Users\tcwg\llvm-worker\flang-arm64-windows-msvc\llvm-project\flang\test\Semantics\OpenMP/../test_errors.py' 'C:\Users\tcwg\llvm-worker\flang-arm64-windows-msvc\llvm-project\flang\test\Semantics\OpenMP\loop-association.f90' 'c:\users\tcwg\llvm-worker\flang-arm64-windows-msvc\build\bin\flang.exe' -fopenmp
# .---command stdout------------
# | PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
# | Stack dump:
# | 0.	Program arguments: C:\\Users\\tcwg\\llvm-worker\\flang-arm64-windows-msvc\\build\\bin\\flang -fc1 -triple aarch64-pc-windows-msvc19.39.33523 -fsyntax-only -mrelocation-model pic -pic-level 2 -target-cpu generic -target-feature +v8a -target-feature +fp-armv8 -target-feature +neon --dependent-lib=clang_rt.builtins-aarch64.lib -D_MT --dependent-lib=libcmt --dependent-lib=flang_rt.runtime.static.lib -D_MSC_VER=1939 -D_MSC_FULL_VER=193933523 -D_WIN32 -D_M_ARM64=1 -fopenmp -resource-dir C:\\Users\\tcwg\\llvm-worker\\flang-arm64-windows-msvc\\build\\lib\\clang\\21 -mframe-pointer=none -x f95 C:\\Users\\tcwg\\llvm-worker\\flang-arm64-windows-msvc\\llvm-project\\flang\\test\\Semantics\\OpenMP\\loop-association.f90
# | Exception Code: 0xC0000005
# |  #0 0x00007ff740681bc4 (C:\Users\tcwg\llvm-worker\flang-arm64-windows-msvc\build\bin\flang.exe+0x1a91bc4)
# |  #1 0x00007ff74067f8ac (C:\Users\tcwg\llvm-worker\flang-arm64-windows-msvc\build\bin\flang.exe+0x1a8f8ac)
# |  #2 0x00007ff7406810e0 (C:\Users\tcwg\llvm-worker\flang-arm64-windows-msvc\build\bin\flang.exe+0x1a910e0)
# |  #3 0x00007ff7406807e8 (C:\Users\tcwg\llvm-worker\flang-arm64-windows-msvc\build\bin\flang.exe+0x1a907e8)
# |  #4 0x00007ff74067fd48 (C:\Users\tcwg\llvm-worker\flang-arm64-windows-msvc\build\bin\flang.exe+0x1a8fd48)
# |  #5 0x00007ff7406836c4 (C:\Users\tcwg\llvm-worker\flang-arm64-windows-msvc\build\bin\flang.exe+0x1a936c4)
# |  #6 0x00007ff74067d2a8 (C:\Users\tcwg\llvm-worker\flang-arm64-windows-msvc\build\bin\flang.exe+0x1a8d2a8)
# |  #7 0x00007ff73f49c7b8 (C:\Users\tcwg\llvm-worker\flang-arm64-windows-msvc\build\bin\flang.exe+0x8ac7b8)
# |  #8 0x00007ff73f4e5c18 (C:\Users\tcwg\llvm-worker\flang-arm64-windows-msvc\build\bin\flang.exe+0x8f5c18)
# |  #9 0x00007ff73f58ead4 (C:\Users\tcwg\llvm-worker\flang-arm64-windows-msvc\build\bin\flang.exe+0x99ead4)
# | #10 0x00007ff73f4e5054 (C:\Users\tcwg\llvm-worker\flang-arm64-windows-msvc\build\bin\flang.exe+0x8f5054)
# | #11 0x00007ff73ec88ac0 (C:\Users\tcwg\llvm-worker\flang-arm64-windows-msvc\build\bin\flang.exe+0x98ac0)
# | #12 0x00007ff73ec9ea88 (C:\Users\tcwg\llvm-worker\flang-arm64-windows-msvc\build\bin\flang.exe+0xaea88)
# | #13 0x00007ff73ebf34e8 (C:\Users\tcwg\llvm-worker\flang-arm64-windows-msvc\build\bin\flang.exe+0x34e8)
# | #14 0x00007ff73ebf20a4 (C:\Users\tcwg\llvm-worker\flang-arm64-windows-msvc\build\bin\flang.exe+0x20a4)
# | #15 0x00007ff7438ae1b8 mlir::detail::FallbackTypeIDResolver::registerImplicitTypeID(class llvm::StringRef) (C:\Users\tcwg\llvm-worker\flang-arm64-windows-msvc\build\bin\flang.exe+0x4cbe1b8)
# | #16 0xcc227ff7438ae254
# | flang: error: flang frontend command failed due to signal (use -v to see invocation)
# | flang version 21.0.0git (https://github.com/llvm/llvm-project.git 1aa39690aa73ad227ef1fb94ddb340d462cfea01)
# | Target: aarch64-pc-windows-msvc
# | Thread model: posix
# | InstalledDir: C:\Users\tcwg\llvm-worker\flang-arm64-windows-msvc\build\bin
# | Build config: +assertions
# | flang: note: diagnostic msg: 
# | ********************
# | 
# | PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
# | Preprocessed source(s) and associated run script(s) are located at:
# | flang: note: diagnostic msg: C:\Users\tcwg\AppData\Local\Temp\lit-tmp-mzoydncw\loop-association-25b7de
# | flang: note: diagnostic msg: C:\Users\tcwg\AppData\Local\Temp\lit-tmp-mzoydncw\loop-association-25b7de.sh
# | flang: note: diagnostic msg: 
# | 
# | ********************
# | 
# `-----------------------------
# error: command failed with exit status: 1

...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang Clang issues not falling into any other category ClangIR Anything related to the ClangIR project

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants