-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[NVPTX] Pull invariant load identification into IR pass #138015
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
AlexMaclean
merged 4 commits into
llvm:main
from
AlexMaclean:dev/amaclean/upstream/ldg-xform
May 1, 2025
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| //===------ NVPTXTagInvariantLoads.cpp - Tag invariant loads --------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This file implements invaraint load tagging. It traverses load instructions | ||
| // in a function, and determines if each load can be tagged as invariant. | ||
| // | ||
| // We currently infer invariance for loads from | ||
| // - constant global variables, and | ||
| // - kernel function pointer params that are noalias (i.e. __restrict) and | ||
| // never written to. | ||
|
Comment on lines
+14
to
+15
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can also consider grid constant arguments. |
||
| // | ||
| // TODO: Perform a more powerful invariance analysis (ideally IPO). | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "NVPTXUtilities.h" | ||
| #include "llvm/Analysis/ValueTracking.h" | ||
| #include "llvm/IR/InstIterator.h" | ||
| #include "llvm/IR/Instructions.h" | ||
| #include "llvm/IR/Metadata.h" | ||
| #include "llvm/Support/NVPTXAddrSpace.h" | ||
|
|
||
| using namespace llvm; | ||
|
|
||
| static void markLoadsAsInvariant(LoadInst *LI) { | ||
| LI->setMetadata(LLVMContext::MD_invariant_load, | ||
| MDNode::get(LI->getContext(), {})); | ||
| } | ||
|
|
||
| static bool tagInvariantLoads(Function &F) { | ||
| const bool IsKernelFn = isKernelFunction(F); | ||
|
|
||
| bool Changed = false; | ||
| for (auto &I : instructions(F)) { | ||
| if (auto *LI = dyn_cast<LoadInst>(&I)) { | ||
|
|
||
| // Don't bother with non-global loads | ||
| if (LI->getPointerAddressSpace() != NVPTXAS::ADDRESS_SPACE_GLOBAL) | ||
| continue; | ||
|
|
||
| if (LI->getMetadata(LLVMContext::MD_invariant_load)) | ||
| continue; | ||
|
|
||
| SmallVector<const Value *, 8> Objs; | ||
|
|
||
| // We use getUnderlyingObjects() here instead of getUnderlyingObject() | ||
| // mainly because the former looks through phi nodes while the latter does | ||
| // not. We need to look through phi nodes to handle pointer induction | ||
| // variables. | ||
|
|
||
| getUnderlyingObjects(LI->getPointerOperand(), Objs); | ||
|
|
||
| const bool IsInvariant = all_of(Objs, [&](const Value *V) { | ||
| if (const auto *A = dyn_cast<const Argument>(V)) | ||
| return IsKernelFn && A->onlyReadsMemory() && A->hasNoAliasAttr(); | ||
| if (const auto *GV = dyn_cast<const GlobalVariable>(V)) | ||
| return GV->isConstant(); | ||
| return false; | ||
| }); | ||
|
|
||
| if (IsInvariant) { | ||
| markLoadsAsInvariant(LI); | ||
| Changed = true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return Changed; | ||
| } | ||
|
|
||
| namespace { | ||
|
|
||
| struct NVPTXTagInvariantLoadLegacyPass : public FunctionPass { | ||
| static char ID; | ||
|
|
||
| NVPTXTagInvariantLoadLegacyPass() : FunctionPass(ID) {} | ||
| bool runOnFunction(Function &F) override; | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| INITIALIZE_PASS(NVPTXTagInvariantLoadLegacyPass, "nvptx-tag-invariant-loads", | ||
| "NVPTX Tag Invariant Loads", false, false) | ||
|
|
||
| bool NVPTXTagInvariantLoadLegacyPass::runOnFunction(Function &F) { | ||
| return tagInvariantLoads(F); | ||
| } | ||
|
|
||
| char NVPTXTagInvariantLoadLegacyPass::ID = 0; | ||
|
|
||
| FunctionPass *llvm::createNVPTXTagInvariantLoadsPass() { | ||
| return new NVPTXTagInvariantLoadLegacyPass(); | ||
| } | ||
|
|
||
| PreservedAnalyses NVPTXTagInvariantLoadsPass::run(Function &F, | ||
| FunctionAnalysisManager &) { | ||
| return tagInvariantLoads(F) ? PreservedAnalyses::none() | ||
| : PreservedAnalyses::all(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May as well make N a constref.