Author: Orlando Cazalet-Hyams
Date: 2025-05-27T11:06:46+01:00
New Revision: 059885c703e9de601d868061a0c344837a81aaf4

URL: 
https://github.com/llvm/llvm-project/commit/059885c703e9de601d868061a0c344837a81aaf4
DIFF: 
https://github.com/llvm/llvm-project/commit/059885c703e9de601d868061a0c344837a81aaf4.diff

LOG: [KeyInstr] Complex assignment atoms (#134638)

This patch is part of a stack that teaches Clang to generate Key Instructions
metadata for C and C++.

RFC:
https://discourse.llvm.org/t/rfc-improving-is-stmt-placement-for-better-interactive-debugging/82668

The feature is only functional in LLVM if LLVM is built with CMake flag
LLVM_EXPERIMENTAL_KEY_INSTRUCTIONs. Eventually that flag will be removed.

Added: 
    clang/test/DebugInfo/KeyInstructions/complex.c

Modified: 
    clang/lib/CodeGen/CGExprComplex.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGExprComplex.cpp 
b/clang/lib/CodeGen/CGExprComplex.cpp
index f556594f4a9ec..f8a946a76554a 100644
--- a/clang/lib/CodeGen/CGExprComplex.cpp
+++ b/clang/lib/CodeGen/CGExprComplex.cpp
@@ -364,15 +364,19 @@ class ComplexExprEmitter
 
   // Compound assignments.
   ComplexPairTy VisitBinAddAssign(const CompoundAssignOperator *E) {
+    ApplyAtomGroup Grp(CGF.getDebugInfo());
     return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinAdd);
   }
   ComplexPairTy VisitBinSubAssign(const CompoundAssignOperator *E) {
+    ApplyAtomGroup Grp(CGF.getDebugInfo());
     return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinSub);
   }
   ComplexPairTy VisitBinMulAssign(const CompoundAssignOperator *E) {
+    ApplyAtomGroup Grp(CGF.getDebugInfo());
     return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinMul);
   }
   ComplexPairTy VisitBinDivAssign(const CompoundAssignOperator *E) {
+    ApplyAtomGroup Grp(CGF.getDebugInfo());
     return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinDiv);
   }
 
@@ -461,8 +465,12 @@ void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy 
Val, LValue lvalue,
   Address RealPtr = CGF.emitAddrOfRealComponent(Ptr, lvalue.getType());
   Address ImagPtr = CGF.emitAddrOfImagComponent(Ptr, lvalue.getType());
 
-  Builder.CreateStore(Val.first, RealPtr, lvalue.isVolatileQualified());
-  Builder.CreateStore(Val.second, ImagPtr, lvalue.isVolatileQualified());
+  auto *R =
+      Builder.CreateStore(Val.first, RealPtr, lvalue.isVolatileQualified());
+  CGF.addInstToCurrentSourceAtom(R, Val.first);
+  auto *I =
+      Builder.CreateStore(Val.second, ImagPtr, lvalue.isVolatileQualified());
+  CGF.addInstToCurrentSourceAtom(I, Val.second);
 }
 
 
@@ -1357,6 +1365,7 @@ LValue ComplexExprEmitter::EmitBinAssignLValue(const 
BinaryOperator *E,
 
 ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) {
   ComplexPairTy Val;
+  ApplyAtomGroup Grp(CGF.getDebugInfo());
   LValue LV = EmitBinAssignLValue(E, Val);
 
   // The result of an assignment in C is the assigned r-value.
@@ -1532,6 +1541,7 @@ static CompoundFunc getComplexOp(BinaryOperatorKind Op) {
 
 LValue CodeGenFunction::
 EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E) {
+  ApplyAtomGroup Grp(getDebugInfo());
   CompoundFunc Op = getComplexOp(E->getOpcode());
   RValue Val;
   return ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val);
@@ -1540,6 +1550,8 @@ EmitComplexCompoundAssignmentLValue(const 
CompoundAssignOperator *E) {
 LValue CodeGenFunction::
 EmitScalarCompoundAssignWithComplex(const CompoundAssignOperator *E,
                                     llvm::Value *&Result) {
+  // Key Instructions: Don't need to create an atom group here; one will 
already
+  // be active through scalar handling code.
   CompoundFunc Op = getComplexOp(E->getOpcode());
   RValue Val;
   LValue Ret = ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val);

diff  --git a/clang/test/DebugInfo/KeyInstructions/complex.c 
b/clang/test/DebugInfo/KeyInstructions/complex.c
new file mode 100644
index 0000000000000..9647d0e4009ee
--- /dev/null
+++ b/clang/test/DebugInfo/KeyInstructions/complex.c
@@ -0,0 +1,97 @@
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -gkey-instructions -x c++ %s 
-debug-info-kind=line-tables-only -emit-llvm -o - \
+// RUN: | FileCheck %s --implicit-check-not atomGroup --implicit-check-not 
atomRank
+
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -gkey-instructions -x c %s 
-debug-info-kind=line-tables-only -emit-llvm -o - \
+// RUN: | FileCheck %s --implicit-check-not atomGroup --implicit-check-not 
atomRank --check-prefixes=CHECK,CHECK-C
+
+_Complex float ci;
+float f;
+void test() {
+// CHECK: %ci.real = load float, ptr @ci{{.*}}, !dbg [[G1R2:!.*]]
+// CHECK: %ci.imag = load float, ptr getelementptr inbounds nuw ({ float, 
float }, ptr @ci, i32 0, i32 1){{.*}}, !dbg [[G1R2]]
+// CHECK: store float %ci.real, ptr %lc.realp{{.*}}, !dbg [[G1R1:!.*]]
+// CHECK: store float %ci.imag, ptr %lc.imagp{{.*}}, !dbg [[G1R1]]
+  _Complex float lc = ci;
+
+// CHECK: %ci.real1 = load float, ptr @ci{{.*}}, !dbg [[G2R2:!.*]]
+// CHECK: %ci.imag2 = load float, ptr getelementptr inbounds nuw ({ float, 
float }, ptr @ci, i32 0, i32 1){{.*}}, !dbg [[G2R2]]
+// CHECK: store float %ci.real1, ptr @ci{{.*}}, !dbg [[G2R1:!.*]]
+// CHECK: store float %ci.imag2, ptr getelementptr inbounds nuw ({ float, 
float }, ptr @ci, i32 0, i32 1){{.*}}, !dbg [[G2R1]]
+  ci = ci;
+
+// CHECK: %add.r = fadd float %ci.real5, %ci.real3, !dbg [[G3R2:!.*]]
+// CHECK: %add.i = fadd float %ci.imag6, %ci.imag4, !dbg [[G3R2]]
+// CHECK: store float %add.r, ptr @ci{{.*}}, !dbg [[G3R1:!.*]]
+// CHECK: store float %add.i, ptr getelementptr inbounds nuw ({ float, float 
}, ptr @ci, i32 0, i32 1){{.*}}, !dbg [[G3R1]]
+  ci += ci;
+
+// CHECK: %sub.r = fsub float %ci.real9, %ci.real7, !dbg [[G4R2:!.*]]
+// CHECK: %sub.i = fsub float %ci.imag10, %ci.imag8, !dbg [[G4R2]]
+// CHECK: store float %sub.r, ptr @ci, align 4, !dbg [[G4R1:!.*]]
+// CHECK: store float %sub.i, ptr getelementptr inbounds nuw ({ float, float 
}, ptr @ci, i32 0, i32 1){{.*}}, !dbg [[G4R1]]
+  ci -= ci;
+
+// There's control flow introduced here to skip around nan and lib calls
+// (which is ignored in the test as none of those insts need be key). This does
+// make PHIs "backup" instructions, which is... odd. FIXME: Do we want to make
+// the instructions producing the values in the PHI backups instead/too?
+// CHECK: %real_mul_phi = phi float [ %mul_r, %entry ], [ %mul_r, 
%complex_mul_imag_nan ], [ %coerce.real, %complex_mul_libcall ], !dbg 
[[G5R2:!.*]]
+// CHECK: %imag_mul_phi = phi float [ %mul_i, %entry ], [ %mul_i, 
%complex_mul_imag_nan ], [ %coerce.imag, %complex_mul_libcall ], !dbg [[G5R2]]
+// CHECK: store float %real_mul_phi, ptr @ci, align 4, !dbg [[G5R1:!.*]]
+// CHECK: store float %imag_mul_phi, ptr getelementptr inbounds nuw ({ float, 
float }, ptr @ci, i32 0, i32 1), align 4, !dbg [[G5R1]]
+  ci *= ci;
+
+// div goes straight to lib call, which gets saved into a temp.
+// CHECK: %coerce21.real = load float, ptr %coerce21.realp, align 4, !dbg 
[[G6R2:!.*]]
+// CHECK: %coerce21.imag = load float, ptr %coerce21.imagp, align 4, !dbg 
[[G6R2]]
+// CHECK: store float %coerce21.real, ptr @ci, align 4, !dbg [[G6R1:!.*]]
+// CHECK: store float %coerce21.imag, ptr getelementptr inbounds nuw ({ float, 
float }, ptr @ci, i32 0, i32 1), align 4, !dbg [[G6R1]]
+  ci /= ci;
+
+// CHECK: %add = fadd float %0, %1, !dbg [[G7R2:!.*]]
+// CHECK: store float %add, ptr getelementptr inbounds nuw ({ float, float }, 
ptr @ci, i32 0, i32 1){{.*}}, !dbg [[G7R1:!.*]]
+  __imag ci = __imag ci + __imag ci;
+
+#ifndef __cplusplus
+// CHECK-C: %2 = load float, ptr @f, align 4
+// CHECK-C: %add.r24 = fadd float %2, %ci.real22, !dbg [[G8R2:!.*]]
+// CHECK-C: store float %add.r24, ptr @f, align 4, !dbg [[G8R1:!.*]]
+  f += ci;
+
+// CHECK-C: %3 = load float, ptr @f, align 4
+// CHECK-C: %sub.r27 = fsub float %3, %ci.real25, !dbg [[G9R2:!.*]]
+// CHECK-C: store float %sub.r27, ptr @f, align 4, !dbg [[G9R1:!.*]]
+  f -= ci;
+
+// CHECK-C: %coerce32.real = load float, ptr %coerce32.realp, align 4, !dbg 
[[G10R2:!.*]]
+// CHECK-C: store float %coerce32.real, ptr @f, align 4, !dbg [[G10R1:!.*]]
+  f /= ci;
+
+// CHECK-C: %mul.rl = fmul float %5, %ci.real33, !dbg [[G11R2:!.*]]
+// CHECK-C: store float %mul.rl, ptr @f, align 4, !dbg [[G11R1:!.*]]
+  f *= ci;
+#endif
+}
+
+// CHECK: [[G1R2]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 2)
+// CHECK: [[G1R1]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 1)
+// CHECK: [[G2R2]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 2)
+// CHECK: [[G2R1]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 1)
+// CHECK: [[G3R2]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 2)
+// CHECK: [[G3R1]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 1)
+// CHECK: [[G4R2]] = !DILocation({{.*}}, atomGroup: 4, atomRank: 2)
+// CHECK: [[G4R1]] = !DILocation({{.*}}, atomGroup: 4, atomRank: 1)
+// CHECK: [[G5R2]] = !DILocation({{.*}}, atomGroup: 5, atomRank: 2)
+// CHECK: [[G5R1]] = !DILocation({{.*}}, atomGroup: 5, atomRank: 1)
+// CHECK: [[G6R2]] = !DILocation({{.*}}, atomGroup: 6, atomRank: 2)
+// CHECK: [[G6R1]] = !DILocation({{.*}}, atomGroup: 6, atomRank: 1)
+// CHECK: [[G7R2]] = !DILocation({{.*}}, atomGroup: 7, atomRank: 2)
+// CHECK: [[G7R1]] = !DILocation({{.*}}, atomGroup: 7, atomRank: 1)
+// CHECK-C: [[G8R2]] = !DILocation({{.*}}, atomGroup: 8, atomRank: 2)
+// CHECK-C: [[G8R1]] = !DILocation({{.*}}, atomGroup: 8, atomRank: 1)
+// CHECK-C: [[G9R2]] = !DILocation({{.*}}, atomGroup: 9, atomRank: 2)
+// CHECK-C: [[G9R1]] = !DILocation({{.*}}, atomGroup: 9, atomRank: 1)
+// CHECK-C: [[G10R2]] = !DILocation({{.*}}, atomGroup: 10, atomRank: 2)
+// CHECK-C: [[G10R1]] = !DILocation({{.*}}, atomGroup: 10, atomRank: 1)
+// CHECK-C: [[G11R2]] = !DILocation({{.*}}, atomGroup: 11, atomRank: 2)
+// CHECK-C: [[G11R1]] = !DILocation({{.*}}, atomGroup: 11, atomRank: 1)


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to