-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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 all commits
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. | ||
// | ||
// 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 bool isInvariantLoad(const LoadInst *LI, const bool IsKernelFn) { | ||
// Don't bother with non-global loads | ||
if (LI->getPointerAddressSpace() != NVPTXAS::ADDRESS_SPACE_GLOBAL) | ||
return false; | ||
|
||
// If the load is already marked as invariant, we don't need to do anything | ||
if (LI->getMetadata(LLVMContext::MD_invariant_load)) | ||
return false; | ||
|
||
// 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. | ||
SmallVector<const Value *, 8> Objs; | ||
getUnderlyingObjects(LI->getPointerOperand(), Objs); | ||
|
||
return all_of(Objs, [&](const Value *V) { | ||
if (const auto *A = dyn_cast<const Argument>(V)) | ||
return IsKernelFn && ((A->onlyReadsMemory() && A->hasNoAliasAttr()) || | ||
isParamGridConstant(*A)); | ||
if (const auto *GV = dyn_cast<const GlobalVariable>(V)) | ||
return GV->isConstant(); | ||
return false; | ||
}); | ||
} | ||
|
||
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)) { | ||
if (isInvariantLoad(LI, IsKernelFn)) { | ||
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
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,33 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5 | ||
; RUN: llc < %s -mcpu=sm_70 | FileCheck %s | ||
; RUN: %if ptxas %{ llc < %s -mcpu=sm_70 | %ptxas-verify %} | ||
|
||
target triple = "nvptx64-nvidia-cuda" | ||
|
||
%struct = type { [2 x i64] } | ||
@G = external constant %struct | ||
|
||
define void @foo() { | ||
; CHECK-LABEL: foo( | ||
; CHECK: { | ||
; CHECK-NEXT: .reg .b64 %rd<3>; | ||
; CHECK-EMPTY: | ||
; CHECK-NEXT: // %bb.0: | ||
; CHECK-NEXT: ld.global.u64 %rd1, [G]; | ||
; CHECK-NEXT: ld.global.u64 %rd2, [G+8]; | ||
; CHECK-NEXT: { // callseq 0, 0 | ||
; CHECK-NEXT: .param .align 8 .b8 param0[16]; | ||
; CHECK-NEXT: st.param.b64 [param0], %rd1; | ||
; CHECK-NEXT: st.param.b64 [param0+8], %rd2; | ||
; CHECK-NEXT: call.uni | ||
; CHECK-NEXT: bar, | ||
; CHECK-NEXT: ( | ||
; CHECK-NEXT: param0 | ||
; CHECK-NEXT: ); | ||
; CHECK-NEXT: } // callseq 0 | ||
; CHECK-NEXT: ret; | ||
call void @bar(ptr byval(%struct) @G) | ||
ret void | ||
} | ||
|
||
declare void @bar(ptr) |
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.
We can also consider grid constant arguments.