From fd282a63fd709cdcdcdc2dacef58144dbf9b15c1 Mon Sep 17 00:00:00 2001 From: Orlando Cazalet-Hyams Date: Thu, 3 Apr 2025 13:36:59 +0100 Subject: [PATCH 1/3] [KeyInstr] Complex assignment atoms This patch is part of a stack that teaches Clang to generate Key Instructions metadata for C and C++. The Key Instructions project is introduced, including a "quick summary" section at the top which adds context for this PR, here: 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. The Clang-side work is demoed here: https://github.com/llvm/llvm-project/pull/130943 --- clang/lib/CodeGen/CGExprComplex.cpp | 10 ++++- .../test/DebugInfo/KeyInstructions/complex.c | 40 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 clang/test/DebugInfo/KeyInstructions/complex.c diff --git a/clang/lib/CodeGen/CGExprComplex.cpp b/clang/lib/CodeGen/CGExprComplex.cpp index f556594f4a9ec..600f61f1b325f 100644 --- a/clang/lib/CodeGen/CGExprComplex.cpp +++ b/clang/lib/CodeGen/CGExprComplex.cpp @@ -461,8 +461,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); } @@ -1209,6 +1213,7 @@ LValue ComplexExprEmitter:: EmitCompoundAssignLValue(const CompoundAssignOperator *E, ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&), RValue &Val) { + ApplyAtomGroup Grp(CGF.getDebugInfo()); TestAndClearIgnoreReal(); TestAndClearIgnoreImag(); QualType LHSTy = E->getLHS()->getType(); @@ -1356,6 +1361,7 @@ LValue ComplexExprEmitter::EmitBinAssignLValue(const BinaryOperator *E, } ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) { + ApplyAtomGroup Grp(CGF.getDebugInfo()); ComplexPairTy Val; LValue LV = EmitBinAssignLValue(E, Val); diff --git a/clang/test/DebugInfo/KeyInstructions/complex.c b/clang/test/DebugInfo/KeyInstructions/complex.c new file mode 100644 index 0000000000000..b97314e815bdc --- /dev/null +++ b/clang/test/DebugInfo/KeyInstructions/complex.c @@ -0,0 +1,40 @@ + +// RUN: %clang -gkey-instructions -x c++ %s -gmlt -gcolumn-info -S -emit-llvm -o - -Wno-unused-variable \ +// RUN: | FileCheck %s --implicit-check-not atomGroup --implicit-check-not atomRank + +// RUN: %clang -gkey-instructions -x c %s -gmlt -gcolumn-info -S -emit-llvm -o - -Wno-unused-variable \ +// RUN: | FileCheck %s --implicit-check-not atomGroup --implicit-check-not atomRank + +_Complex float ci; +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: %add = fadd float %0, %1, !dbg [[G4R2:!.*]] +// CHECK: store float %add, ptr getelementptr inbounds nuw ({ float, float }, ptr @ci, i32 0, i32 1){{.*}}, !dbg [[G4R1:!.*]] + __imag ci = __imag ci + __imag ci; +} + +// 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) From 03a0a1cef3049e16e6f8802275b07e4e49014ad6 Mon Sep 17 00:00:00 2001 From: Orlando Cazalet-Hyams Date: Wed, 21 May 2025 14:58:42 +0100 Subject: [PATCH 2/3] cc1 --- clang/test/DebugInfo/KeyInstructions/complex.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang/test/DebugInfo/KeyInstructions/complex.c b/clang/test/DebugInfo/KeyInstructions/complex.c index b97314e815bdc..f922e5ff71a58 100644 --- a/clang/test/DebugInfo/KeyInstructions/complex.c +++ b/clang/test/DebugInfo/KeyInstructions/complex.c @@ -1,8 +1,8 @@ -// RUN: %clang -gkey-instructions -x c++ %s -gmlt -gcolumn-info -S -emit-llvm -o - -Wno-unused-variable \ +// RUN: %clang_cc1 -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 -gkey-instructions -x c %s -gmlt -gcolumn-info -S -emit-llvm -o - -Wno-unused-variable \ +// RUN: %clang_cc1 -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 _Complex float ci; From 4ca985019d3884907185a89bda6aed2cc7050135 Mon Sep 17 00:00:00 2001 From: Orlando Cazalet-Hyams Date: Wed, 21 May 2025 15:09:04 +0100 Subject: [PATCH 3/3] whitespace --- clang/test/DebugInfo/KeyInstructions/complex.c | 1 - 1 file changed, 1 deletion(-) diff --git a/clang/test/DebugInfo/KeyInstructions/complex.c b/clang/test/DebugInfo/KeyInstructions/complex.c index f922e5ff71a58..ff8557afb91e7 100644 --- a/clang/test/DebugInfo/KeyInstructions/complex.c +++ b/clang/test/DebugInfo/KeyInstructions/complex.c @@ -1,4 +1,3 @@ - // RUN: %clang_cc1 -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