Skip to content

Add option to generate additional debug info for expression dereferencing pointer to pointers. #94100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jun 3, 2024

Conversation

huangjd
Copy link
Contributor

@huangjd huangjd commented Jun 1, 2024

This is another attempt to land #81545, which was reverted.

Fixed test case so that it is consistent on all platforms.

…ter of pointers.

Such expression does correspond to a variable in the source code thus
does not have a debug location. However the user may want to collect
sampling counter for memory accesses to analyze usage frequency of class
members. By enabling -fdebug_info_for_pointer_type a psuedo variable and
its debug info is generated in place whenever there's an intermediate
expression with pointer access.
variable is generated is located at the end of BB
Copy link
Collaborator

@dwblaikie dwblaikie left a comment

Choose a reason for hiding this comment

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

Looks worth a shot. Please commit this at a time where you can respond to bot fail-mail for a while after the commit.

And ensure the commit message has most/all of the details of the original commit, plus the original commit and revert hashes, and a little detail on the updates to address the reason for the revert (adding the explicit triple to the test).

Copy link

github-actions bot commented Jun 1, 2024

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff 9261ab708e37c2d6499ac063045f816d25a5919c 4841c8752abab8c5e05a97d8826cdca376f4ab0c -- clang/test/CodeGenCXX/debug-info-ptr-to-ptr.cpp clang/lib/CodeGen/CGDebugInfo.cpp clang/lib/CodeGen/CGDebugInfo.h clang/lib/CodeGen/CGExprScalar.cpp
View the diff from clang-format here.
diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index 6c332b2ae9c..2ed7dbe2854 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -5706,9 +5706,9 @@ void CGDebugInfo::EmitPseudoVariable(CGBuilderTy &Builder,
   // Emit debug info for materialized Value.
   unsigned Line = Builder.getCurrentDebugLocation().getLine();
   unsigned Column = Builder.getCurrentDebugLocation().getCol();
-  llvm::DILocalVariable *D = DBuilder.createAutoVariable(
-      LexicalBlockStack.back(), "", nullptr, 0, Type, false,
-      llvm::DINode::FlagArtificial);
+  llvm::DILocalVariable *D =
+      DBuilder.createAutoVariable(LexicalBlockStack.back(), "", nullptr, 0,
+                                  Type, false, llvm::DINode::FlagArtificial);
   llvm::DILocation *DIL =
       llvm::DILocation::get(CGM.getLLVMContext(), Line, Column,
                             LexicalBlockStack.back(), CurInlinedAt);

@huangjd huangjd merged commit 5cb0078 into llvm:main Jun 3, 2024
6 of 7 checks passed
@huangjd huangjd deleted the debuginfoforpointertype branch June 4, 2024 01:27
@jmorse
Copy link
Member

jmorse commented Jun 10, 2024

G'day -- we've got some tests for -O0 that, with this patch applied, generate different code with-and-without the -g flag, if -fdebug-info-for-profiling is enabled. Example godbolt: https://godbolt.org/z/qooo5Eah1 .

It seems the intention of this patch is generating additional loads and stores with debug-info enabled, which is usually highly undesirable. It's guarded by -fdebug-info-for-profiling, however we (Sony) use this flag in a bunch of places, and would prefer it to continue generating stable + consistent code. A couple of possibilities:

  • Put this behaviour behind a cc1 flag, I think that was the plan in the original PR?
  • Instead of creating an alloca, store+load, and dbg.declare, connect a dbg.value to the relevant pointer Value.

The 2nd point is more work, but it means you're only adding more debug-info, and nothing else. A dbg.value connected to the pointer Value is the inevitable consequence of the alloca+store+load idiom after mem2reg/SROA anyway.

@dwblaikie
Copy link
Collaborator

G'day -- we've got some tests for -O0 that, with this patch applied, generate different code with-and-without the -g flag, if -fdebug-info-for-profiling is enabled. Example godbolt: https://godbolt.org/z/qooo5Eah1 .

It seems the intention of this patch is generating additional loads and stores with debug-info enabled, which is usually highly undesirable.

I hope that isn't the intent, I thought the intent was just to add more debug info for loads and stores to carry more intermediate type information.

Sorry I missed that this patch does change clangs generated ir. Seriously overlooked on my part.

  • Instead of creating an alloca, store+load, and dbg.declare, connect a dbg.value to the relevant pointer Value.

The 2nd point is more work, but it means you're only adding more debug-info, and nothing else. A dbg.value connected to the pointer Value is the inevitable consequence of the alloca+store+load idiom after mem2reg/SROA anyway.

Yeah, pretty sure (2) would be the preferred direction.

@huangjd
Copy link
Contributor Author

huangjd commented Jun 10, 2024

By just specifying -g Even without -fdebug-info-for-profiling it is going to introduce debug variables as a sequence of alloca-store-load too, so is it a requirement to guarantee -O0 output stays identical with or without debug info?

Emitting a dbg.value was what I originally though of, but there is a IR validation pass after frontend codegen that does not permit it, not sure if there's other design reason for this check

@jmorse
Copy link
Member

jmorse commented Jun 10, 2024

dblaikie wrote:

I hope that isn't the intent, I thought the intent was just [...]

Ah, by which I mean "it's not an accident",

By just specifying -g Even without -fdebug-info-for-profiling it is going to introduce debug variables as a sequence of alloca-store-load too, so is it a requirement to guarantee -O0 output stays identical with or without debug info?

It's a strong objective of LLVM in general to ensure debug-info doesn't have an effect on codegen decisions -- it just makes everything unreliable and hard to predict. It can cause situations where an end user finds their programs behave differently with -g. Having a cc1 option to enable this kind of behaviour is fine as they're relatively experimental, for us the issue is that it's affecting an existing driver option.

Emitting a dbg.value was what I originally though of, but there is a IR validation pass after frontend codegen that does not permit it, not sure if there's other design reason for this check

Interesting -- I'll admit I'm not especially familiar with stuff right at the front end, but I'm sure I've seen some emails go past about handling dbg.values earlier than normal. @SLTozer @OCHyams were there some Swift related patches that got SROA handling dbg.values correctly, and if so, maybe it's alright for clang to do it too? (@jmorse is still catching up with email).

@SLTozer
Copy link
Contributor

SLTozer commented Jun 10, 2024

Yes, to my knowledge Swift emits dbg.values out of its front end, so there's probably no reason (on the LLVM side) that clang couldn't do so as well. There might be good reasons internal to clang that it doesn't use debug values atm, and it's not inconceivable that we'd hit some cases that LLVM handles poorly at the moment (right now assignment tracking depends on reading dbg.declares, for example), but that's something that could be tackled in a separate review.

In any case, as has been said this is definitely an error and should be reverted - the best solution is probably to rework this to use dbg.values instead.

@huangjd
Copy link
Contributor Author

huangjd commented Jun 11, 2024

Ok lets revert this first, and I will work on the dbg.value implementation

huangjd added a commit to huangjd/llvm-project that referenced this pull request Jun 11, 2024
…ereferencing pointer to pointers. (llvm#94100)"

This reverts commit 5cb0078.
huangjd added a commit that referenced this pull request Jun 11, 2024
…ereferencing pointer to pointers. #94100" (#95174)

The option is causing the binary output to be different when compiled
under `-O0`, because it introduce dbg.declare on pseudovariables. Going
to change this implementation to use dbg.value instead.
huangjd added a commit to huangjd/llvm-project that referenced this pull request Jun 12, 2024
…eferencing pointer to pointers

This is a different implementation to llvm#94100, which has been reverted.

When -fdebug-info-for-profiling is specified, for any Load expression if
the pointer operand is not a declared variable, clang will emit debug
info describing the type of the pointer operand (which can be an
intermediate expr)
huangjd added a commit that referenced this pull request Jun 15, 2024
…eferencing pointer to pointers (#95298)

This is a different implementation to #94100, which has been reverted.

When -fdebug-info-for-profiling is specified, for any Load expression if
the pointer operand is not a declared variable, clang will emit debug
info describing the type of the pointer operand (which can be an
intermediate expr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants