Skip to content

Conversation

@mkuron
Copy link
Contributor

@mkuron mkuron commented Sep 28, 2024

Ensures that struct padding is not skipped, as it may contain actual data if the struct is really a union.

The patch originated from a discussion on #53710, and @Artem-B suggested I post it as a pull request. This is my first LLVM code contribution, so I may need a bit of help in getting this into a shape acceptable to merge.

I updated the existing test cases that were failing after my change, which lead to some changes from align 4 to align 8 on the store. The result actually looks correct to me -- the corresponding argument and the corresponding alloca both have align 8, only the store previously used a narrower alignment than it could have.

Fixes #53710

@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Sep 28, 2024

@llvm/pr-subscribers-backend-nvptx

Author: Michael Kuron (mkuron)

Changes

Ensures that struct padding is not skipped, as it may contain actual data if the struct is really a union.

The patch originated from a discussion on #53710, and @Artem-B suggested I post it as a pull request. This is my first LLVM code contribution, so I may need a bit of help creating a suitable test case.

Fixes #53710


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

1 Files Affected:

  • (modified) llvm/lib/Target/NVPTX/NVPTXLowerArgs.cpp (+4-1)
diff --git a/llvm/lib/Target/NVPTX/NVPTXLowerArgs.cpp b/llvm/lib/Target/NVPTX/NVPTXLowerArgs.cpp
index 082546c4dd72f8..bfeac304af61b9 100644
--- a/llvm/lib/Target/NVPTX/NVPTXLowerArgs.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXLowerArgs.cpp
@@ -626,8 +626,11 @@ void NVPTXLowerArgs::handleByValParam(const NVPTXTargetMachine &TM,
     // Be sure to propagate alignment to this load; LLVM doesn't know that NVPTX
     // addrspacecast preserves alignment.  Since params are constant, this load
     // is definitely not volatile.
+    const auto StructBytes = *AllocA->getAllocationSize(DL);
+    Type *ByteType = Type::getIntNTy(Func->getContext(), 8);
+    Type *OpaqueType = ArrayType::get(ByteType, StructBytes);
     LoadInst *LI =
-        new LoadInst(StructType, ArgInParam, Arg->getName(),
+        new LoadInst(OpaqueType, ArgInParam, Arg->getName(),
                      /*isVolatile=*/false, AllocA->getAlign(), FirstInst);
     new StoreInst(LI, AllocA, FirstInst);
   }

Ensures that struct padding is not skipped, as it may contain actual
data if the struct is really a union.

Fixes #53710
Copy link
Member

@Artem-B Artem-B left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm apparently bad at clicking on "Submit review" button. This comment's been sitting there unsubmitted for a while. Sorry about that.

@mkuron mkuron requested a review from Artem-B October 6, 2024 17:12
Copy link
Member

@Artem-B Artem-B left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, with a test nit.

@mkuron
Copy link
Contributor Author

mkuron commented Oct 12, 2024

@Artem-B, do we need another approval? Who can merge?

@Artem-B
Copy link
Member

Artem-B commented Oct 14, 2024

You are good to go. If you do not have commit access I can merge the patch on your behalf.

@mkuron
Copy link
Contributor Author

mkuron commented Oct 14, 2024

@Artem-B, please do merge it.

@Artem-B Artem-B merged commit 96c3207 into llvm:main Oct 14, 2024
9 checks passed
@github-actions
Copy link

@mkuron Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

DanielCChen pushed a commit to DanielCChen/llvm-project that referenced this pull request Oct 16, 2024
Ensures that struct padding is not skipped, as it may contain actual
data if the struct is really a union.

The patch originated from a discussion on llvm#53710

Fixes llvm#53710
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.

CUDA: unions in kernel arguments not copied completely if member contains padding

3 participants