[llvm] [TargetLowering] fix index OOB (PR #67494)

Nick Desaulniers via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 26 14:51:00 PDT 2023


https://github.com/nickdesaulniers created https://github.com/llvm/llvm-project/pull/67494

I accidentally introduced this in

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

Fix forward.


>From 24689dd5840c12189968883d155b79f86966408c Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Tue, 26 Sep 2023 14:49:54 -0700
Subject: [PATCH] [TargetLowering] fix index OOB

I accidentally introduced this in

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

Fix forward.
---
 llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp     | 9 ++++++++-
 llvm/test/CodeGen/X86/inline-asm-bad-constraint-n.ll | 6 ++++++
 llvm/test/CodeGen/X86/inline-asm-n-constraint.ll     | 4 ++++
 3 files changed, 18 insertions(+), 1 deletion(-)

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



More information about the llvm-commits mailing list