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

[AMDGPU] Reject misaligned SGPR constraints for inline asm #123590

Merged
merged 3 commits into from
Jan 20, 2025

Conversation

ritter-x2a
Copy link
Member

The indices of SGPR register pairs need to be 2-aligned and SGPR quadruplets need to be 4-aligned. With this patch, we report an error when inline asm register constraints specify a misaligned register index, instead of silently dropping the specified index.

Fixes #123208

The indices of SGPR register pairs need to be 2-aligned and SGPR
quadruplets need to be 4-aligned. With this patch, we report an error
when inline asm register constraints specify a misaligned register
index, instead of silently dropping the specified index.

Fixes llvm#123208
@ritter-x2a ritter-x2a requested a review from arsenm January 20, 2025 11:21
@ritter-x2a ritter-x2a self-assigned this Jan 20, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 20, 2025

@llvm/pr-subscribers-backend-amdgpu

Author: Fabian Ritter (ritter-x2a)

Changes

The indices of SGPR register pairs need to be 2-aligned and SGPR quadruplets need to be 4-aligned. With this patch, we report an error when inline asm register constraints specify a misaligned register index, instead of silently dropping the specified index.

Fixes #123208


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

2 Files Affected:

  • (modified) llvm/lib/Target/AMDGPU/SIISelLowering.cpp (+6)
  • (modified) llvm/test/CodeGen/AMDGPU/inlineasm-mismatched-size-error.ll (+51)
diff --git a/llvm/lib/Target/AMDGPU/SIISelLowering.cpp b/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
index c4b1038b12d042..592a72e109543e 100644
--- a/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
+++ b/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
@@ -15877,6 +15877,12 @@ SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI_,
             RC = TRI->getAGPRClassForBitWidth(Width);
           if (RC) {
             Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0, RC);
+            if (Reg == 0U) {
+              // The register class does not contain the requested register,
+              // e.g., because it is an SGPR pair that would violate alignment
+              // requirements.
+              return std::pair(0U, nullptr);
+            }
             return std::pair(Reg, RC);
           }
         }
diff --git a/llvm/test/CodeGen/AMDGPU/inlineasm-mismatched-size-error.ll b/llvm/test/CodeGen/AMDGPU/inlineasm-mismatched-size-error.ll
index 723e0f2e7152d0..e8accc1c8a0f31 100644
--- a/llvm/test/CodeGen/AMDGPU/inlineasm-mismatched-size-error.ll
+++ b/llvm/test/CodeGen/AMDGPU/inlineasm-mismatched-size-error.ll
@@ -102,3 +102,54 @@ define <2 x i8> @inline_asm_2xi8_in_s_def() {
   %r = and <2 x i8> %phys, %virt
   ret <2 x i8> %r
 }
+
+
+; The register is wide enough, but it does not satisfy alignment constraints:
+
+; ERR: error: couldn't allocate input reg for constraint '{s[1:2]}'
+define void @misaligned_sgpr_2xi32_in(<2 x i32> inreg %arg0) {
+  call void asm sideeffect "; use $0", "{s[1:2]}"(<2 x i32> %arg0)
+  ret void
+}
+
+; ERR: error: couldn't allocate input reg for constraint '{s[23:24]}'
+define void @misaligned_sgpr_2xi32_in_23(<2 x i32> inreg %arg0) {
+  call void asm sideeffect "; use $0", "{s[23:24]}"(<2 x i32> %arg0)
+  ret void
+}
+
+; ERR: error: couldn't allocate input reg for constraint '{s[1:4]}'
+define void @misaligned_sgpr_4xi32_in(<4 x i32> inreg %arg0) {
+  call void asm sideeffect "; use $0", "{s[1:4]}"(<4 x i32> %arg0)
+  ret void
+}
+
+; ERR: error: couldn't allocate input reg for constraint '{s[2:5]}'
+define void @misaligned_sgpr_4xi32_in_2(<4 x i32> inreg %arg0) {
+  call void asm sideeffect "; use $0", "{s[2:5]}"(<4 x i32> %arg0)
+  ret void
+}
+
+; ERR: error: couldn't allocate output register for constraint '{s[1:2]}'
+define <2 x i32> @misaligned_sgpr_2xi32_out() {
+  %asm = call <2 x i32> asm sideeffect "; def $0", "={s[1:2]}"()
+  ret <2 x i32> %asm
+}
+
+; ERR: error: couldn't allocate output register for constraint '{s[23:24]}'
+define <2 x i32> @misaligned_sgpr_2xi32_out_23() {
+  %asm = call <2 x i32> asm sideeffect "; def $0", "={s[23:24]}"()
+  ret <2 x i32> %asm
+}
+
+; ERR: error: couldn't allocate output register for constraint '{s[1:4]}'
+define <4 x i32> @misaligned_sgpr_4xi32_out() {
+  %asm = call <4 x i32> asm sideeffect "; def $0", "={s[1:4]}"()
+  ret <4 x i32> %asm
+}
+
+; ERR: error: couldn't allocate output register for constraint '{s[2:5]}'
+define <4 x i32> @misaligned_sgpr_4xi32_out_2() {
+  %asm = call <4 x i32> asm sideeffect "; def $0", "={s[2:5]}"()
+  ret <4 x i32> %asm
+}

llvm/lib/Target/AMDGPU/SIISelLowering.cpp Outdated Show resolved Hide resolved
Copy link

github-actions bot commented Jan 20, 2025

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

@ritter-x2a ritter-x2a merged commit cc5eba1 into llvm:main Jan 20, 2025
8 checks passed
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.

AMDGPU silently converts incorrect physical register asm constraint to virtual register
3 participants