Skip to content

Conversation

mikolaj-pirog
Copy link
Member

shouldUseDebugInstrRef can return different value than useDebugInstrRef, since the first depends on opt level which can change. Inconsistent usage can lead to errors later.

I believe that using should... instead of use... here is a result of a minor error during this: https://github.com/llvm/llvm-project/pull/94149/files#diff-8ec547e1244562c5837ed180dd9bed61b3cd960ef90bb6002ea2db41a67ed693

Notice how before the change InstrRef is assigned value from should... before the opt change. Now, it's done after -- opt change happens here:

bool SelectionDAGISelLegacy::runOnMachineFunction(MachineFunction &MF) {
...
  // Decide what flavour of variable location debug-info will be used, before
  // we change the optimisation level.
  MF.setUseDebugInstrRef(MF.shouldUseDebugInstrRef());
....

  return Selector->runOnMachineFunction(MF);
}

Then runOnMachineFunction uses should..., which after opt change may return different value than it did previously.

@llvmbot llvmbot added the llvm:SelectionDAG SelectionDAGISel as well label Sep 25, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 25, 2025

@llvm/pr-subscribers-debuginfo

@llvm/pr-subscribers-llvm-selectiondag

Author: Mikołaj Piróg (mikolaj-pirog)

Changes

shouldUseDebugInstrRef can return different value than useDebugInstrRef, since the first depends on opt level which can change. Inconsistent usage can lead to errors later.

I believe that using should... instead of use... here is a result of a minor error during this: https://github.com/llvm/llvm-project/pull/94149/files#diff-8ec547e1244562c5837ed180dd9bed61b3cd960ef90bb6002ea2db41a67ed693

Notice how before the change InstrRef is assigned value from should... before the opt change. Now, it's done after -- opt change happens here:

bool SelectionDAGISelLegacy::runOnMachineFunction(MachineFunction &MF) {
...
  // Decide what flavour of variable location debug-info will be used, before
  // we change the optimisation level.
  MF.setUseDebugInstrRef(MF.shouldUseDebugInstrRef());
....

  return Selector->runOnMachineFunction(MF);
}

Then runOnMachineFunction uses should..., which after opt change may return different value than it did previously.


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

1 Files Affected:

  • (modified) llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp (+1-1)
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
index e61558c59bf0d..214edfc7c07f3 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
@@ -566,7 +566,7 @@ bool SelectionDAGISel::runOnMachineFunction(MachineFunction &mf) {
   SwiftError->setFunction(mf);
   const Function &Fn = mf.getFunction();
 
-  bool InstrRef = mf.shouldUseDebugInstrRef();
+  bool InstrRef = mf.useDebugInstrRef();
 
   FuncInfo->set(MF->getFunction(), *MF, CurDAG);
 

@e-kud e-kud requested review from RKSimon and arsenm September 25, 2025 11:45
@e-kud
Copy link
Contributor

e-kud commented Sep 25, 2025

I think it is worth mentioning that the main problem happens while using opt-bisect. When the optimization level changes.
My concern here is whether we need shouldUseDebugInstrRef at all? Or maybe we need to check whether we are bisecting in shouldUseDebugInstrRef and return a consistent result for it?

UPD: We need some testing as well...

@mikolaj-pirog
Copy link
Member Author

I think it is worth mentioning that the main problem happens while using opt-bisect. When the optimization level changes. My concern here is whether we need shouldUseDebugInstrRef at all? Or maybe we need to check whether we are bisecting in shouldUseDebugInstrRef and return a consistent result for it?

UPD: We need some testing as well...

My understanding of the shouldUseDebugInstrRef is that it's a helper that's used to set UseInstrRef; it should never be used outside of initialization (and indeed the usage removed in this PR is the only one)

@RKSimon RKSimon removed their request for review September 29, 2025 15:24
@mikolaj-pirog mikolaj-pirog force-pushed the mpirog/fix-wrong-instr-ref branch from 9203e78 to fb7dd26 Compare October 7, 2025 08:46
@e-kud
Copy link
Contributor

e-kud commented Oct 7, 2025

I think it is worth mentioning that the main problem happens while using opt-bisect. When the optimization level changes. My concern here is whether we need shouldUseDebugInstrRef at all? Or maybe we need to check whether we are bisecting in shouldUseDebugInstrRef and return a consistent result for it?
UPD: We need some testing as well...

My understanding of the shouldUseDebugInstrRef is that it's a helper that's used to set UseInstrRef; it should never be used outside of initialization (and indeed the usage removed in this PR is the only one)

Oh, I see. I just saw three uses, and they looked very similar to me. But indeed this one should use the attribute that we've just set earlier.

Copy link
Member

@jmorse jmorse left a comment

Choose a reason for hiding this comment

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

LGTM, I think this matches my expectations for those functions. The primary reason for this facility existing is that SelectionDAG changed the optimisation-level flags according to things like function attributes. This made it important to a) pick which "mode" of debugging-information was to be in at one point in time, and b) to store that independently of any other information.

(Perhaps things have changed since then; but that's the motivation behind the code).

Copy link
Contributor

@e-kud e-kud left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@mikolaj-pirog mikolaj-pirog merged commit 91f4db7 into llvm:main Oct 8, 2025
10 checks passed
svkeerthy pushed a commit that referenced this pull request Oct 9, 2025
`shouldUseDebugInstrRef` can return different value than
`useDebugInstrRef`, since the first depends on opt level which can
change. Inconsistent usage can lead to errors later.

I believe that using `should...` instead of `use...` here is a result of
a minor error during this:
https://github.com/llvm/llvm-project/pull/94149/files#diff-8ec547e1244562c5837ed180dd9bed61b3cd960ef90bb6002ea2db41a67ed693

Notice how before the change `InstrRef` is assigned value from
`should...` *before* the opt change. Now, it's done after -- opt change
happens here:
```c
bool SelectionDAGISelLegacy::runOnMachineFunction(MachineFunction &MF) {
...
  // Decide what flavour of variable location debug-info will be used, before
  // we change the optimisation level.
  MF.setUseDebugInstrRef(MF.shouldUseDebugInstrRef());
....

  return Selector->runOnMachineFunction(MF);
}
```

Then `runOnMachineFunction` uses `should...`, which after opt change may
return different value than it did previously.
clingfei pushed a commit to clingfei/llvm-project that referenced this pull request Oct 10, 2025
…60686)

`shouldUseDebugInstrRef` can return different value than
`useDebugInstrRef`, since the first depends on opt level which can
change. Inconsistent usage can lead to errors later.

I believe that using `should...` instead of `use...` here is a result of
a minor error during this:
https://github.com/llvm/llvm-project/pull/94149/files#diff-8ec547e1244562c5837ed180dd9bed61b3cd960ef90bb6002ea2db41a67ed693

Notice how before the change `InstrRef` is assigned value from
`should...` *before* the opt change. Now, it's done after -- opt change
happens here:
```c
bool SelectionDAGISelLegacy::runOnMachineFunction(MachineFunction &MF) {
...
  // Decide what flavour of variable location debug-info will be used, before
  // we change the optimisation level.
  MF.setUseDebugInstrRef(MF.shouldUseDebugInstrRef());
....

  return Selector->runOnMachineFunction(MF);
}
```

Then `runOnMachineFunction` uses `should...`, which after opt change may
return different value than it did previously.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

debuginfo llvm:SelectionDAG SelectionDAGISel as well

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants