[PATCH] D140581: [NVPTX] Enforce minumum alignment of 4 for byval parametrs in a function prototype

Pavel Kopyl via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 22 12:39:53 PST 2022


pavelkopyl created this revision.
Herald added subscribers: mattd, gchakrabarti, asavonic, hiraditya.
Herald added a project: All.
pavelkopyl requested review of this revision.
Herald added subscribers: llvm-commits, jholewinski.
Herald added a project: LLVM.

As a result, we have identical alignment calculation of byval parameters for

- LowerCall() - getting alignment of an argument (.param)
- emitFunctionParamList() - getting alignment of a parameter (.param) in a function declaration
- getPrototype() - getting alignment of a parameter (.param) in a function prototypes that is used for indirect calls

This change is required to avoid ptxas error:
'Alignment of argument does not match formal parameter'. This
error happens even in cases where it logically shouldn't.
For instance:

.param .align 4 .b8 param0[4];
 ...
 callprototype ()_ (.param .align 2 .b8 _[4]);
 ...

Here we allocate 'param0' with alignment of 4 and it should be
fine to pass it to a function that requires minimum alignment of 2.
At least ptxas v12.0 rejects this code.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D140581

Files:
  llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
  llvm/test/CodeGen/NVPTX/call_bitcast_byval.ll


Index: llvm/test/CodeGen/NVPTX/call_bitcast_byval.ll
===================================================================
--- llvm/test/CodeGen/NVPTX/call_bitcast_byval.ll
+++ llvm/test/CodeGen/NVPTX/call_bitcast_byval.ll
@@ -37,9 +37,9 @@
   %fp = call ptr @usefp(ptr @callee)
   ; CHECK: .param .align 4 .b8 param0[4];
   ; CHECK: st.param.v2.b16 [param0+0]
-  ; CHECK: .callprototype ()_ (.param .align 2 .b8 _[4]);
+  ; CHECK: .callprototype ()_ (.param .align 4 .b8 _[4]);
   call void %fp(ptr byval(%"class.complex") null)
   ret void
 }
 
-declare %complex_half @_Z20__spirv_GroupCMulKHRjjN5__spv12complex_halfE()
+declare %complex_half @_Z20__spirv_GroupCMulKHRjjN5__spv12complex_halfE(i32, i32, ptr byval(%"class.complex"))
Index: llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
===================================================================
--- llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
+++ llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
@@ -1422,6 +1422,11 @@
     Align AlignCandidate = getFunctionParamOptimizedAlign(F, ETy, DL);
     ParamByValAlign = std::max(ParamByValAlign, AlignCandidate);
 
+    // Enforce minumum alignment of 4 to work around ptxas miscompile
+    // for sm_50+. See corresponding alignment adjustment in
+    // emitFunctionParamList() for details.
+    ParamByValAlign = std::max(ParamByValAlign, Align(4));
+
     O << ".param .align " << ParamByValAlign.value() << " .b8 ";
     O << "_";
     O << "[" << Outs[OIdx].Flags.getByValSize() << "]";


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140581.484933.patch
Type: text/x-patch
Size: 1488 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221222/1f94a7ad/attachment.bin>


More information about the llvm-commits mailing list