-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[speculative-execution] Hoists debug values unnecessarily. #85782
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
[speculative-execution] Hoists debug values unnecessarily. #85782
Conversation
After https://reviews.llvm.org/D81730: `SpeculativeExecutionPass::considerHoistingFromTo` hoists instructions, including debug intrinsics, as long as none of their used values are instructions that appear prior in the block that are not being hoisted. This behaviour has been duplicated for DPValues to get rid of a binary difference. The correct solution is not hoist these debug values at all, whichever format they're in.
@llvm/pr-subscribers-debuginfo @llvm/pr-subscribers-llvm-transforms Author: Carlos Alberto Enciso (CarlosAlbertoEnciso) ChangesAfter https://reviews.llvm.org/D81730: This behaviour has been duplicated for DPValues to get rid of a binary difference. The correct solution is not hoist these debug values at all, whichever format they're in. Full diff: https://github.com/llvm/llvm-project/pull/85782.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/Scalar/SpeculativeExecution.cpp b/llvm/lib/Transforms/Scalar/SpeculativeExecution.cpp
index 44ea6e9ffefa2b..7ac3cf6357e7c4 100644
--- a/llvm/lib/Transforms/Scalar/SpeculativeExecution.cpp
+++ b/llvm/lib/Transforms/Scalar/SpeculativeExecution.cpp
@@ -260,10 +260,31 @@ static InstructionCost ComputeSpeculationCost(const Instruction *I,
}
}
+// Do not hoist any debug info intrinsics.
+// ...
+// if (cond) {
+// x = y * z;
+// foo();
+// }
+// ...
+// -------- Which then becomes:
+// ...
+// if.then:
+// %x = mul i32 %y, %z
+// call void @llvm.dbg.value(%x, !"x", !DIExpression())
+// call void foo()
+//
+// If SpeculativeExecution might decide to hoist the 'y * z' calculation
+// out of the 'if' block, because it is more efficient that way, so the
+// '%x = mul i32 %y, %z' moves to the block above. But it might also
+// decide to hoist the 'llvm.dbg.value' call.
+// This is incorrect, because even if we've moved the calculation of
+// 'y * z', we should not see the value of 'x' change unless we
+// actually go inside the 'if' block.
+
bool SpeculativeExecutionPass::considerHoistingFromTo(
BasicBlock &FromBlock, BasicBlock &ToBlock) {
SmallPtrSet<const Instruction *, 8> NotHoisted;
- SmallDenseMap<const Instruction *, SmallVector<DPValue *>> DPValuesToHoist;
auto HasNoUnhoistedInstr = [&NotHoisted](auto Values) {
for (const Value *V : Values) {
if (const auto *I = dyn_cast_or_null<Instruction>(V))
@@ -274,15 +295,8 @@ bool SpeculativeExecutionPass::considerHoistingFromTo(
};
auto AllPrecedingUsesFromBlockHoisted =
[&HasNoUnhoistedInstr](const User *U) {
- // Debug variable has special operand to check it's not hoisted.
- if (const auto *DVI = dyn_cast<DbgVariableIntrinsic>(U))
- return HasNoUnhoistedInstr(DVI->location_ops());
-
- // Usially debug label intrinsic corresponds to label in LLVM IR. In
- // these cases we should not move it here.
- // TODO: Possible special processing needed to detect it is related to a
- // hoisted instruction.
- if (isa<DbgLabelInst>(U))
+ // Do not hoist any debug info intrinsics.
+ if (isa<DbgInfoIntrinsic>(U))
return false;
return HasNoUnhoistedInstr(U->operand_values());
@@ -291,12 +305,6 @@ bool SpeculativeExecutionPass::considerHoistingFromTo(
InstructionCost TotalSpeculationCost = 0;
unsigned NotHoistedInstCount = 0;
for (const auto &I : FromBlock) {
- // Make note of any DPValues that need hoisting. DPLabels
- // get left behind just like llvm.dbg.labels.
- for (DPValue &DPV : filterDbgVars(I.getDbgRecordRange())) {
- if (HasNoUnhoistedInstr(DPV.location_ops()))
- DPValuesToHoist[DPV.getInstruction()].push_back(&DPV);
- }
const InstructionCost Cost = ComputeSpeculationCost(&I, *TTI);
if (Cost.isValid() && isSafeToSpeculativelyExecute(&I) &&
AllPrecedingUsesFromBlockHoisted(&I)) {
@@ -314,16 +322,6 @@ bool SpeculativeExecutionPass::considerHoistingFromTo(
}
for (auto I = FromBlock.begin(); I != FromBlock.end();) {
- // If any DPValues attached to this instruction should be hoisted, hoist
- // them now - they will end up attached to either the next hoisted
- // instruction or the ToBlock terminator.
- if (DPValuesToHoist.contains(&*I)) {
- for (auto *DPV : DPValuesToHoist[&*I]) {
- DPV->removeFromParent();
- ToBlock.insertDbgRecordBefore(DPV,
- ToBlock.getTerminator()->getIterator());
- }
- }
// We have to increment I before moving Current as moving Current
// changes the list that I is iterating through.
auto Current = I;
diff --git a/llvm/test/Transforms/SpeculativeExecution/PR46267.ll b/llvm/test/Transforms/SpeculativeExecution/PR46267.ll
index d940ee6a7863d7..69dac2220d9a64 100644
--- a/llvm/test/Transforms/SpeculativeExecution/PR46267.ll
+++ b/llvm/test/Transforms/SpeculativeExecution/PR46267.ll
@@ -31,7 +31,6 @@ define void @f(i32 %i) {
entry:
; CHECK-LABEL: @f(
; CHECK: %a2 = add i32 %i, 0
-; CHECK-NEXT: call void @llvm.dbg.value(metadata i32 %a2
br i1 undef, label %land.rhs, label %land.end
land.rhs: ; preds = %entry
@@ -42,6 +41,7 @@ land.rhs: ; preds = %entry
; CHECK-NEXT: %a0 = load i32, ptr undef, align 1
; CHECK-NEXT: call void @llvm.dbg.value(metadata i32 %a0
; CHECK-NEXT: call void @llvm.dbg.label
+; CHECK-NEXT: call void @llvm.dbg.value(metadata i32 %a2
call void @llvm.dbg.label(metadata !11), !dbg !10
%y = alloca i32, align 4
call void @llvm.dbg.declare(metadata ptr %y, metadata !14, metadata !DIExpression()), !dbg !10
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, this change sounds right in isolation. I have a slight worry that this could lead to missing debug records if everything else in the block is hoisted, and then that block (containing only debug records) is deleted later. But ultimately that probably shouldn't be a concern for this pass; passes that delete blocks are responsible for making sure the right thing happens in those cases.
// call void @llvm.dbg.value(%x, !"x", !DIExpression()) | ||
// call void foo() | ||
// | ||
// If SpeculativeExecution might decide to hoist the 'y * z' calculation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: remove If
?
After https://reviews.llvm.org/D81730: `SpeculativeExecutionPass::considerHoistingFromTo` hoists instructions, including debug intrinsics, as long as none of their used values are instructions that appear prior in the block that are not being hoisted. This behaviour has been duplicated for DPValues to get rid of a binary difference. The correct solution is not hoist these debug values at all, whichever format they're in. nit: Remove 'If' from comment.
1f3a036
to
f2c2cdf
Compare
After https://reviews.llvm.org/D81730:
SpeculativeExecutionPass::considerHoistingFromTo
hoists instructions, including debug intrinsics, as long as none of their used values are instructions that appear prior in the block that are not being hoisted.This behaviour has been duplicated for DPValues to get rid of a binary difference.
The correct solution is not hoist these debug values at all, whichever format they're in.