Skip to content
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

[TargetLowering] fix index OOB #67494

Merged
merged 2 commits into from
Sep 26, 2023

Conversation

nickdesaulniers
Copy link
Member

I accidentally introduced this in

commit 330fa7d ("[TargetLowering] Deduplicate choosing InlineAsm
constraint between ISels (#67057)")

Fix forward.

I accidentally introduced this in

commit 330fa7d ("[TargetLowering] Deduplicate choosing InlineAsm
constraint between ISels (llvm#67057)")

Fix forward.
@nickdesaulniers nickdesaulniers marked this pull request as draft September 26, 2023 21:51
@llvmbot llvmbot added backend:X86 llvm:SelectionDAG SelectionDAGISel as well labels Sep 26, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Sep 26, 2023

@llvm/pr-subscribers-llvm-selectiondag

@llvm/pr-subscribers-backend-x86

Changes

I accidentally introduced this in

commit 330fa7d ("[TargetLowering] Deduplicate choosing InlineAsm
constraint between ISels (#67057)")

Fix forward.


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

3 Files Affected:

  • (modified) llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp (+8-1)
  • (modified) llvm/test/CodeGen/X86/inline-asm-bad-constraint-n.ll (+6)
  • (modified) llvm/test/CodeGen/X86/inline-asm-n-constraint.ll (+4)
diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
index 3b259d99f0029ca..911d63a75458b54 100644
--- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
@@ -5889,10 +5889,17 @@ void TargetLowering::ComputeConstraintToUse(AsmOperandInfo &OpInfo,
     for (const unsigned E = G.size();
          BestIdx < E && (G[BestIdx].second == TargetLowering::C_Other ||
                          G[BestIdx].second == TargetLowering::C_Immediate);
-         ++BestIdx)
+         ++BestIdx) {
       if (lowerImmediateIfPossible(G[BestIdx], Op, DAG, *this))
         break;
+      // If we're out of constraints, just pick the first one.
+      if (BestIdx + 1 == E) {
+        BestIdx = 0;
+        break;
+      }
+    }
 
+    assert(BestIdx < G.size() && "bad index for best constraint");
     OpInfo.ConstraintCode = G[BestIdx].first;
     OpInfo.ConstraintType = G[BestIdx].second;
   }
diff --git a/llvm/test/CodeGen/X86/inline-asm-bad-constraint-n.ll b/llvm/test/CodeGen/X86/inline-asm-bad-constraint-n.ll
index 41d8a9baa8dd4e1..d27a74620013bbd 100644
--- a/llvm/test/CodeGen/X86/inline-asm-bad-constraint-n.ll
+++ b/llvm/test/CodeGen/X86/inline-asm-bad-constraint-n.ll
@@ -8,3 +8,9 @@ define void @foo() {
   call void asm sideeffect "foo $0", "n"(ptr %a) nounwind
   ret void
 }
+
+; CHECK: error: invalid operand for inline asm constraint 'i'
+define void @bar(i32 %v) {
+  call void asm "", "in"(i32 %v)
+  ret void
+}
diff --git a/llvm/test/CodeGen/X86/inline-asm-n-constraint.ll b/llvm/test/CodeGen/X86/inline-asm-n-constraint.ll
index 669e464f1f6f89f..4774d43ee333f0b 100644
--- a/llvm/test/CodeGen/X86/inline-asm-n-constraint.ll
+++ b/llvm/test/CodeGen/X86/inline-asm-n-constraint.ll
@@ -7,6 +7,10 @@ define void @foo() {
   call void asm sideeffect "foo $0", "n"(i32 42) nounwind
 ; CHECK:      #APP
 ; CHECK-NEXT: foo    $42
+; CHECK-NEXT: #NO_APP
+  call void asm "# $0", "in"(i32 1392848979)
+; CHECK-NEXT: #APP
+; CHECK-NEXT: # $1392848979
 ; CHECK-NEXT: #NO_APP
   ret void
 ; CHECK-NEXT: retq

@nickdesaulniers nickdesaulniers merged commit 35a364f into llvm:main Sep 26, 2023
2 checks passed
@nickdesaulniers nickdesaulniers deleted the ff_choose_constraint branch September 26, 2023 22:50
legrosbuffle pushed a commit to legrosbuffle/llvm-project that referenced this pull request Sep 29, 2023
I accidentally introduced this in

commit 330fa7d ("[TargetLowering] Deduplicate choosing InlineAsm
constraint between ISels (llvm#67057)")

Fix forward.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:X86 llvm:SelectionDAG SelectionDAGISel as well
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants