[PATCH] D118823: AMDGPU: Implement isAsmClobberable

Matt Arsenault via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 2 10:22:13 PST 2022


arsenm created this revision.
arsenm added a reviewer: AMDGPU.
Herald added subscribers: foad, kerbowa, hiraditya, t-tye, tpr, dstuttard, yaxunl, nhaehnle, jvesely, kzhuravl.
arsenm requested review of this revision.
Herald added a subscriber: wdng.
Herald added a project: LLVM.

Warn on inline assembly clobbering reserved registers. It should also
warn on at least some reserved register defs, but that isn't happening
right now. If you have a def and re-use of a register we reserve, the
register coalescer will eliminate the intermediate virtual
register. When the reserved reg def is introduced later by the
backend, it will end up clobbering the value the register coalescer
assumed was live through the range.

      

There is also isInlineAsmReadOnlyReg, although I don't understand what
the distinction really is. It's called in SelectionDAGBuilder, long
before the set of reserved registers is frozen so I'm not sure how
that can possibly work reliably.

      

Unfortunately this is also using the ugly tablegenerated names for the
registers.


https://reviews.llvm.org/D118823

Files:
  llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp
  llvm/lib/Target/AMDGPU/SIRegisterInfo.h
  llvm/test/CodeGen/AMDGPU/inline-asm-reserved-regs.ll


Index: llvm/test/CodeGen/AMDGPU/inline-asm-reserved-regs.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/AMDGPU/inline-asm-reserved-regs.ll
@@ -0,0 +1,52 @@
+; RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx900 -verify-machineinstrs -o /dev/null 2>&1 %s | FileCheck -check-prefix=ERR %s
+
+; ERR: warning: inline asm clobber list contains reserved registers: VGPR42
+; ERR: note: Reserved registers on the clobber list may not be preserved across the asm statement, and clobbering them may lead to undefined behaviour.
+define amdgpu_kernel void @clobber_occupancy_limited_vgpr() #0 {
+entry:
+  call void asm sideeffect "; clobber $0", "~{v42}"()
+  ret void
+}
+
+; ERR: warning: inline asm clobber list contains reserved registers: VGPR42_VGPR43
+; ERR: note: Reserved registers on the clobber list may not be preserved across the asm statement, and clobbering them may lead to undefined behaviour.
+define amdgpu_kernel void @clobber_occupancy_limited_vgpr64() #0 {
+entry:
+  call void asm sideeffect "; clobber $0", "~{v[42:43]}"()
+  ret void
+}
+
+; ERR: warning: inline asm clobber list contains reserved registers: M0
+; ERR: note: Reserved registers on the clobber list may not be preserved across the asm statement, and clobbering them may lead to undefined behaviour.
+define amdgpu_kernel void @clobber_m0() {
+entry:
+  call void asm sideeffect "; clobber $0", "~{m0}"()
+  ret void
+}
+
+; ERR: warning: inline asm clobber list contains reserved registers: EXEC
+; ERR: note: Reserved registers on the clobber list may not be preserved across the asm statement, and clobbering them may lead to undefined behaviour.
+define amdgpu_kernel void @clobber_exec() {
+entry:
+  call void asm sideeffect "; clobber $0", "~{exec}"()
+  ret void
+}
+
+; ERR: warning: inline asm clobber list contains reserved registers: EXEC_LO
+; ERR: note: Reserved registers on the clobber list may not be preserved across the asm statement, and clobbering them may lead to undefined behaviour.
+define amdgpu_kernel void @clobber_exec_lo() {
+entry:
+  call void asm sideeffect "; clobber $0", "~{exec_lo}"()
+  ret void
+}
+
+; FIXME: This should warn too
+; ERR-NOT: warning
+define amdgpu_kernel void @def_exec(i64 addrspace(1)* %ptr) {
+entry:
+  %exec = call i64 asm sideeffect "; def $0", "={exec}"()
+  store i64 %exec, i64 addrspace(1)* %ptr
+  ret void
+}
+
+attributes #0 = { "amdgpu-waves-per-eu"="10,10" }
Index: llvm/lib/Target/AMDGPU/SIRegisterInfo.h
===================================================================
--- llvm/lib/Target/AMDGPU/SIRegisterInfo.h
+++ llvm/lib/Target/AMDGPU/SIRegisterInfo.h
@@ -64,6 +64,8 @@
   MCRegister reservedPrivateSegmentBufferReg(const MachineFunction &MF) const;
 
   BitVector getReservedRegs(const MachineFunction &MF) const override;
+  bool isAsmClobberable(const MachineFunction &MF,
+                        MCRegister PhysReg) const override;
 
   const MCPhysReg *getCalleeSavedRegs(const MachineFunction *MF) const override;
   const MCPhysReg *getCalleeSavedRegsViaCopy(const MachineFunction *MF) const;
Index: llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp
===================================================================
--- llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp
+++ llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp
@@ -690,6 +690,11 @@
   return Reserved;
 }
 
+bool SIRegisterInfo::isAsmClobberable(const MachineFunction &MF,
+                                      MCRegister PhysReg) const {
+  return !MF.getRegInfo().isReserved(PhysReg);
+}
+
 bool SIRegisterInfo::shouldRealignStack(const MachineFunction &MF) const {
   const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
   // On entry, the base address is 0, so it can't possibly need any more


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D118823.405341.patch
Type: text/x-patch
Size: 3788 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220202/a775eeb9/attachment-0001.bin>


More information about the llvm-commits mailing list