Skip to content

[X86] Fix 32-bit immediate assertion and convert into backend error #123872

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

wesleywiser
Copy link
Member

The assertion previously did not work correctly because the operand was being truncated to an int prior to comparison.

Change the assertion into a a reported error as suggested in #101840 (comment) by @arsenm

@llvmbot
Copy link
Member

llvmbot commented Jan 22, 2025

@llvm/pr-subscribers-backend-x86

Author: Wesley Wiser (wesleywiser)

Changes

The assertion previously did not work correctly because the operand was being truncated to an int prior to comparison.

Change the assertion into a a reported error as suggested in #101840 (comment) by @arsenm


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

1 Files Affected:

  • (modified) llvm/lib/Target/X86/X86RegisterInfo.cpp (+4-5)
diff --git a/llvm/lib/Target/X86/X86RegisterInfo.cpp b/llvm/lib/Target/X86/X86RegisterInfo.cpp
index 4faf8bca4f9e02..8a35de5ca98f11 100644
--- a/llvm/lib/Target/X86/X86RegisterInfo.cpp
+++ b/llvm/lib/Target/X86/X86RegisterInfo.cpp
@@ -965,11 +965,10 @@ X86RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
   }
 
   if (MI.getOperand(FIOperandNum+3).isImm()) {
-    // Offset is a 32-bit integer.
-    int Imm = (int)(MI.getOperand(FIOperandNum + 3).getImm());
-    int Offset = FIOffset + Imm;
-    assert((!Is64Bit || isInt<32>((long long)FIOffset + Imm)) &&
-           "Requesting 64-bit offset in 32-bit immediate!");
+    int64_t Imm = MI.getOperand(FIOperandNum + 3).getImm();
+    int Offset = FIOffset + (int)Imm;
+    if (!Is64Bit && !isInt<32>((int64_t)FIOffset + Imm))
+      MI.emitGenericError(("Requesting 64-bit offset in 32-bit immediate: " + MF.getName()).str());
     if (Offset != 0 || !tryOptimizeLEAtoMOV(II))
       MI.getOperand(FIOperandNum + 3).ChangeToImmediate(Offset);
   } else {

Copy link

github-actions bot commented Jan 22, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@wesleywiser wesleywiser force-pushed the fix_32_bit_immediate_assert branch from c82b6db to 0dd16eb Compare January 22, 2025 03:56
@abhishek-kaushik22
Copy link
Contributor

Maybe the IR from #121932 could be added as test

define dso_local i32 @main() #0 {
entry:
  %x = alloca [2147483647 x i8], align 16
  %arrayidx = getelementptr inbounds [2147483647 x i8], ptr %x, i64 0, i64 -42
  store i8 33, ptr %arrayidx, align 2
  ret i32 0
}

attributes #0 = { noinline nounwind optnone uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }

@wesleywiser wesleywiser force-pushed the fix_32_bit_immediate_assert branch from 0dd16eb to ecc78a2 Compare January 23, 2025 04:57
@RKSimon
Copy link
Collaborator

RKSimon commented Jan 31, 2025

Please can you add test coverage for #113856 as well - note its a fast-isel bug

@wesleywiser wesleywiser force-pushed the fix_32_bit_immediate_assert branch from ecc78a2 to 79d4afd Compare February 8, 2025 17:32
@wesleywiser
Copy link
Member Author

Thanks for finding those issues! I've added a test to cover #113856.

The assertion previously did not work correctly because the operand was
being truncated to an `int` prior to comparison.
@wesleywiser wesleywiser force-pushed the fix_32_bit_immediate_assert branch from 79d4afd to 58e318d Compare February 11, 2025 00:07
int Imm = (int)(MI.getOperand(FIOperandNum + 3).getImm());
int Offset = FIOffset + Imm;
assert((!Is64Bit || isInt<32>((long long)FIOffset + Imm)) &&
"Requesting 64-bit offset in 32-bit immediate!");
Copy link
Collaborator

Choose a reason for hiding this comment

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

This assert was checking to see if Offset addition overflowed on x86-64 - haven't we lost that check now?

int64_t Imm = MI.getOperand(FIOperandNum + 3).getImm();
int Offset = FIOffset + (int)Imm;
if (!Is64Bit && !isInt<32>((int64_t)FIOffset + Imm))
MI.emitGenericError("requesting 64-bit offset in 32-bit immediate");
if (Offset != 0 || !tryOptimizeLEAtoMOV(II))
Copy link
Collaborator

Choose a reason for hiding this comment

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

If Imm == -FIOffset wouldn't that mean the operand isn't correctly set to zero?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment