This is an archive of the discontinued LLVM Phabricator instance.

[NVPTX] Enforce minumum alignment of 4 for byval parametrs in a function prototype
ClosedPublic

Authored by pavelkopyl on Dec 22 2022, 12:39 PM.

Details

Summary

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.

Diff Detail

Event Timeline

pavelkopyl created this revision.Dec 22 2022, 12:39 PM
pavelkopyl requested review of this revision.Dec 22 2022, 12:39 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 22 2022, 12:39 PM

In your commit message:

  • s/parametrs/parameters

I'd prefer to split off a separate test that only checks the alignment of various calls, rather than reuse the byval bitcast test. Otherwise LGTM, but I'd wait on someone more familiar with ptx, e.g. ~tra

  • Rebased
  • Added fixes according to review notes

In your commit message:

  • s/parametrs/parameters

I'd prefer to split off a separate test that only checks the alignment of various calls, rather than reuse the byval bitcast test. Otherwise LGTM, but I'd wait on someone more familiar with ptx, e.g. ~tra

Thank you for review. I added new case for checking .param alignment in function prototype to llvm/test/CodeGen/NVPTX/param-align.ll

tra accepted this revision.Jan 3 2023, 3:13 PM

Looks good overall.

llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
1425

This seems to be the same adjustment we do in LowerCall below. Perhaps we can consolidate alignment calculation into a single helper function.

1560–1562

Here...

This revision is now accepted and ready to land.Jan 3 2023, 3:13 PM
pavelkopyl updated this revision to Diff 487011.Jan 6 2023, 4:24 PM
  • Rebased
  • Added hepler function for computing byval param alignments of device functions
pavelkopyl added inline comments.Jan 6 2023, 4:31 PM
llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
1425

Thank you for suggestion. Actually I introduced a helper to use in all three places: emitFunctionParamList (in AsmPrinter), getPrototype and LowerCall. Is that OK?

tra accepted this revision.Jan 6 2023, 4:33 PM

LGTM. Thank you!

llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
1615–1621

Nit. This all could be folded into

Align OptimalAlign = isKernelFunc 
        ? getOptimalAlignForParam(ETy);
        : TLI->getFunctionByValParamAlign(F, ETy, PAL.getParamAlignment(paramIndex).valueOrOne(), DL);
pavelkopyl updated this revision to Diff 487129.Jan 7 2023, 4:41 PM
  • Fixes according to review notes
llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
1615–1621

Thank you. Done.