[PATCH] D119247: [NVPTX] Use align attribute for kernel pointer arg alignment

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 8 07:31:20 PST 2022


nikic created this revision.
nikic added a reviewer: tra.
Herald added subscribers: asavonic, hiraditya, Anastasia, jholewinski.
nikic requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Instead of determining the alignment based on the pointer element type (which is incompatible with opaque pointers), make use of alignment annotations added by the frontend.

In particular, clang will add alignment attributes to OpenCL kernels since D118894 <https://reviews.llvm.org/D118894>.


https://reviews.llvm.org/D119247

Files:
  llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
  llvm/test/CodeGen/NVPTX/nvcl-param-align.ll


Index: llvm/test/CodeGen/NVPTX/nvcl-param-align.ll
===================================================================
--- llvm/test/CodeGen/NVPTX/nvcl-param-align.ll
+++ llvm/test/CodeGen/NVPTX/nvcl-param-align.ll
@@ -3,9 +3,8 @@
 target triple = "nvptx-unknown-nvcl"
 
 ; CHECK-LABEL: .entry foo(
-define void @foo(i64 %img, i64 %sampler, <5 x float>* %v) {
-; The parameter alignment should be the next power of 2 of 5xsizeof(float),
-; which is 32.
+define void @foo(i64 %img, i64 %sampler, <5 x float>* align 32 %v) {
+; The parameter alignment is determined by the align attribute.
 ; CHECK: .param .u32 .ptr .align 32 foo_param_2
   ret void
 }
Index: llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
===================================================================
--- llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
+++ llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
@@ -1335,34 +1335,6 @@
   }
 }
 
-static unsigned int getOpenCLAlignment(const DataLayout &DL, Type *Ty) {
-  if (Ty->isSingleValueType())
-    return DL.getPrefTypeAlignment(Ty);
-
-  auto *ATy = dyn_cast<ArrayType>(Ty);
-  if (ATy)
-    return getOpenCLAlignment(DL, ATy->getElementType());
-
-  auto *STy = dyn_cast<StructType>(Ty);
-  if (STy) {
-    unsigned int alignStruct = 1;
-    // Go through each element of the struct and find the
-    // largest alignment.
-    for (unsigned i = 0, e = STy->getNumElements(); i != e; i++) {
-      Type *ETy = STy->getElementType(i);
-      unsigned int align = getOpenCLAlignment(DL, ETy);
-      if (align > alignStruct)
-        alignStruct = align;
-    }
-    return alignStruct;
-  }
-
-  auto *FTy = dyn_cast<FunctionType>(Ty);
-  if (FTy)
-    return DL.getPointerPrefAlignment().value();
-  return DL.getPrefTypeAlignment(Ty);
-}
-
 void NVPTXAsmPrinter::printParamName(Function::const_arg_iterator I,
                                      int paramIndex, raw_ostream &O) {
   getSymbol(I->getParent())->print(O, MAI);
@@ -1454,7 +1426,6 @@
 
           if (static_cast<NVPTXTargetMachine &>(TM).getDrvInterface() !=
               NVPTX::CUDA) {
-            Type *ETy = PTy->getPointerElementType();
             int addrSpace = PTy->getAddressSpace();
             switch (addrSpace) {
             default:
@@ -1470,7 +1441,8 @@
               O << ".ptr .global ";
               break;
             }
-            O << ".align " << (int)getOpenCLAlignment(DL, ETy) << " ";
+            Align ParamAlign = I->getParamAlign().valueOrOne();
+            O << ".align " << ParamAlign.value() << " ";
           }
           printParamName(I, paramIndex, O);
           continue;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119247.406818.patch
Type: text/x-patch
Size: 2599 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220208/1998bc01/attachment.bin>


More information about the llvm-commits mailing list