Skip to content

[SDAG] Remove noundef workaround for range metadata/attributes #141745

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 1 commit into from
May 30, 2025

Conversation

nikic
Copy link
Contributor

@nikic nikic commented May 28, 2025

In https://reviews.llvm.org/D157685 I changed SDAG to only transfer range metadata to SDAG if it also has !noundef. At the time, this was necessary because SDAG incorrectly propagated poison when folding logical and/or to bitwise and/or.

The root cause of that issue has since been addressed by #84924, so drop the workaround now.

@llvmbot
Copy link
Member

llvmbot commented May 28, 2025

@llvm/pr-subscribers-backend-aarch64
@llvm/pr-subscribers-backend-x86

@llvm/pr-subscribers-llvm-selectiondag

Author: Nikita Popov (nikic)

Changes

In https://reviews.llvm.org/D157685 I changed SDAG to only transfer range metadata to SDAG if it also has !noundef. At the time, this was necessary because SDAG incorrectly propagated poison when folding logical and/or to bitwise and/or.

The root cause of that issue has since been addressed by #84924, so drop the workaround now.


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

3 Files Affected:

  • (modified) llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp (+3-13)
  • (modified) llvm/test/CodeGen/AArch64/lower-range-metadata-func-call.ll (+1-1)
  • (modified) llvm/test/CodeGen/X86/pr37063.ll (+1-1)
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
index 693a7f59629cb..77771ee56e828 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
@@ -4468,23 +4468,13 @@ void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
 }
 
 static const MDNode *getRangeMetadata(const Instruction &I) {
-  // If !noundef is not present, then !range violation results in a poison
-  // value rather than immediate undefined behavior. In theory, transferring
-  // these annotations to SDAG is fine, but in practice there are key SDAG
-  // transforms that are known not to be poison-safe, such as folding logical
-  // and/or to bitwise and/or. For now, only transfer !range if !noundef is
-  // also present.
-  if (!I.hasMetadata(LLVMContext::MD_noundef))
-    return nullptr;
   return I.getMetadata(LLVMContext::MD_range);
 }
 
 static std::optional<ConstantRange> getRange(const Instruction &I) {
-  if (const auto *CB = dyn_cast<CallBase>(&I)) {
-    // see comment in getRangeMetadata about this check
-    if (CB->hasRetAttr(Attribute::NoUndef))
-      return CB->getRange();
-  }
+  if (const auto *CB = dyn_cast<CallBase>(&I))
+    if (std::optional<ConstantRange> CR = CB->getRange())
+      return CR;
   if (const MDNode *Range = getRangeMetadata(I))
     return getConstantRangeFromMetadata(*Range);
   return std::nullopt;
diff --git a/llvm/test/CodeGen/AArch64/lower-range-metadata-func-call.ll b/llvm/test/CodeGen/AArch64/lower-range-metadata-func-call.ll
index 5af8189b4e0d7..c4fe0199c51eb 100644
--- a/llvm/test/CodeGen/AArch64/lower-range-metadata-func-call.ll
+++ b/llvm/test/CodeGen/AArch64/lower-range-metadata-func-call.ll
@@ -48,7 +48,7 @@ entry:
 
 ; CHECK-LABEL: {{^}}test_call_known_max_range_attr_no_noundef:
 ; CHECK: bl foo
-; CHECK: and w{{[0-9]+}}, w0, #0x3ff
+; CHECK-NOT: and
 ; CHECK: ret
 define i32 @test_call_known_max_range_attr_no_noundef() #0 {
 entry:
diff --git a/llvm/test/CodeGen/X86/pr37063.ll b/llvm/test/CodeGen/X86/pr37063.ll
index 281da0c11f1f7..1d98ea921b748 100644
--- a/llvm/test/CodeGen/X86/pr37063.ll
+++ b/llvm/test/CodeGen/X86/pr37063.ll
@@ -7,7 +7,7 @@ define void @foo(ptr) {
 ; CHECK-LABEL: foo:
 ; CHECK:       # %bb.0: # %start
 ; CHECK-NEXT:    movl (%rdi), %eax
-; CHECK-NEXT:    andl $6, %eax
+; CHECK-NEXT:    andl $-2, %eax
 ; CHECK-NEXT:    cmpl $4, %eax
 ; CHECK-NEXT:    jne bar # TAILCALL
 ; CHECK-NEXT:  # %bb.1: # %bb1

Copy link
Collaborator

@bjope bjope left a comment

Choose a reason for hiding this comment

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

LG

@@ -48,7 +48,7 @@ entry:

; CHECK-LABEL: {{^}}test_call_known_max_range_attr_no_noundef:
; CHECK: bl foo
; CHECK: and w{{[0-9]+}}, w0, #0x3ff
; CHECK-NOT: and
Copy link
Contributor

Choose a reason for hiding this comment

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

comprehensive -NEXT checks would be better

In https://reviews.llvm.org/D157685 I changed SDAG to only transfer
range metadata to SDAG if it also has !noundef. At the time, this
was necessary because SDAG incorrectly propagated poison when
folding logical and/or to bitwise and/or.

The root cause of that issue has since been addressed by
llvm#84924, so drop the
workaround now.
@nikic nikic force-pushed the remove-range-workaround branch from db36591 to d9980ae Compare May 30, 2025 08:52
@nikic nikic merged commit ea096c9 into llvm:main May 30, 2025
6 of 11 checks passed
@nikic nikic deleted the remove-range-workaround branch May 30, 2025 08:56
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Jun 3, 2025
…141745)

In https://reviews.llvm.org/D157685 I changed SDAG to only transfer
range metadata to SDAG if it also has !noundef. At the time, this was
necessary because SDAG incorrectly propagated poison when folding
logical and/or to bitwise and/or.

The root cause of that issue has since been addressed by
llvm#84924, so drop the workaround
now.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants