-
Notifications
You must be signed in to change notification settings - Fork 10
add worklist sorting to support nested specializations #513
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
Open
jandrej
wants to merge
7
commits into
main
Choose a base branch
from
nested-jit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b750519
add worklist sorting to support nested specializations for cpu
jandrej 6b591bc
gpu pass
jandrej fcb0f34
hip
jandrej 0291f4b
linkage
jandrej 5371ab7
format
jandrej 85e7cf6
make kernels translation unit agnostic
jandrej 54e25c9
don't expose symbol to hip
jandrej 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 |
|---|---|---|
|
|
@@ -58,6 +58,7 @@ | |
| #include <llvm/IR/Function.h> | ||
| #include <llvm/IR/GlobalValue.h> | ||
| #include <llvm/IR/GlobalVariable.h> | ||
| #include <llvm/IR/InstIterator.h> | ||
| #include <llvm/IR/InstrTypes.h> | ||
| #include <llvm/IR/Instruction.h> | ||
| #include <llvm/IR/Instructions.h> | ||
|
|
@@ -205,7 +206,7 @@ class ProteusPassImpl { | |
|
|
||
| if (hasDeviceLaunchKernelCalls(M)) { | ||
| instrumentLambdaLaunchCallsites(M, StubToKernelMap); | ||
| emitJitLaunchKernelCall(M); | ||
| emitJitLaunchKernelCall(M, StubToKernelMap); | ||
| } | ||
|
|
||
| instrumentRegisterFunction(M); | ||
|
|
@@ -228,15 +229,17 @@ class ProteusPassImpl { | |
| JitWorkList.push_back(&JFI); | ||
| } | ||
|
|
||
| // IMPORTANT: Build all per-function JIT modules before rewriting any of the | ||
| // original functions into stubs. Otherwise, later JIT module extraction can | ||
| // accidentally clone the already-rewritten stub (and its mutable globals), | ||
| // producing invalid JIT IR (e.g. external globals with InternalLinkage). | ||
| // See unit test lambda_def_register_once, which tests this ordering. | ||
| for (auto *JFI : JitWorkList) | ||
| // IMPORTANT: A JIT function is rewritten into its dispatch stub before the | ||
| // module of any JIT function that contains it is extracted, so the | ||
| // enclosing module carries the nested dispatch and the inner region | ||
| // specializes on its own runtime constants. Ordering innermost-first is | ||
| // what makes that happen; the stub's mutable bookkeeping globals are | ||
| // cloned by definition (see emitJitModuleHost) so the cloned IR stays | ||
| // valid. See unit tests lambda_def_register_once and lambda_nested. | ||
| for (auto *JFI : sortJitWorkListInnermostFirst(JitWorkList)) { | ||
| emitJitModuleHost(M, *JFI); | ||
| for (auto *JFI : JitWorkList) | ||
| emitJitEntryCall(M, *JFI); | ||
| } | ||
|
|
||
| DEBUG(Logger::logs("proteus-pass") | ||
| << "=== Post Original Host Module\n" | ||
|
|
@@ -776,6 +779,81 @@ class ProteusPassImpl { | |
| } | ||
| } | ||
|
|
||
| using JitWorkListEntry = decltype(JitFunctionInfoMap)::value_type; | ||
|
|
||
| // JIT functions reachable from F's body, looking through ordinary calls but | ||
| // stopping at another JIT function -- those are the regions nested in F. | ||
| static SmallPtrSet<Function *, 8> | ||
| findNestedJitFunctions(Function &F, | ||
| const SmallPtrSetImpl<Function *> &JitFunctions) { | ||
| SmallPtrSet<Function *, 8> Nested; | ||
| SmallPtrSet<Function *, 16> Visited; | ||
| SmallVector<Function *, 16> Worklist{&F}; | ||
|
|
||
| while (!Worklist.empty()) { | ||
| Function *Current = Worklist.pop_back_val(); | ||
| if (!Visited.insert(Current).second) | ||
| continue; | ||
|
|
||
| for (Instruction &I : instructions(*Current)) { | ||
| auto *CB = dyn_cast<CallBase>(&I); | ||
| if (!CB) | ||
| continue; | ||
|
|
||
| Function *Callee = CB->getCalledFunction(); | ||
| if (!Callee || Callee->isDeclaration() || Callee == &F) | ||
| continue; | ||
|
|
||
| if (JitFunctions.contains(Callee)) { | ||
| Nested.insert(Callee); | ||
| continue; | ||
| } | ||
|
|
||
| Worklist.push_back(Callee); | ||
| } | ||
| } | ||
|
|
||
| return Nested; | ||
| } | ||
|
|
||
| // Post-order over the nesting relation, so an inner JIT function is always | ||
| // processed before the ones containing it. Recursion through JIT functions | ||
| // has no innermost region, so a cycle keeps its original relative order. | ||
| SmallVector<JitWorkListEntry *, 16> sortJitWorkListInnermostFirst( | ||
|
Collaborator
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. this topological ordering is a good change as well to make sure we have a deterministic ordering |
||
| const SmallVectorImpl<JitWorkListEntry *> &JitWorkList) { | ||
| SmallPtrSet<Function *, 16> JitFunctions; | ||
| DenseMap<Function *, JitWorkListEntry *> FnToEntry; | ||
| for (auto *JFI : JitWorkList) { | ||
| JitFunctions.insert(JFI->first); | ||
| FnToEntry[JFI->first] = JFI; | ||
| } | ||
|
|
||
| DenseMap<Function *, SmallPtrSet<Function *, 8>> Nested; | ||
| for (auto *JFI : JitWorkList) | ||
| Nested[JFI->first] = findNestedJitFunctions(*JFI->first, JitFunctions); | ||
|
|
||
| SmallVector<JitWorkListEntry *, 16> Ordered; | ||
| SmallPtrSet<Function *, 16> Done; | ||
| SmallPtrSet<Function *, 16> OnStack; | ||
|
|
||
| std::function<void(Function *)> Visit = [&](Function *F) { | ||
| if (Done.contains(F) || !OnStack.insert(F).second) | ||
| return; | ||
|
|
||
| for (Function *Inner : Nested[F]) | ||
| Visit(Inner); | ||
|
|
||
| OnStack.erase(F); | ||
| if (Done.insert(F).second) | ||
| Ordered.push_back(FnToEntry[F]); | ||
| }; | ||
|
|
||
| for (auto *JFI : JitWorkList) | ||
| Visit(JFI->first); | ||
|
|
||
| return Ordered; | ||
| } | ||
|
|
||
| void emitJitModuleHost(Module &M, | ||
| std::pair<Function *, JitFunctionInfo> &JITInfo) { | ||
| Function *JITFn = JITInfo.first; | ||
|
|
@@ -786,9 +864,17 @@ class ProteusPassImpl { | |
| if (isCoverageGlobal(*GV)) | ||
| return true; | ||
|
|
||
| if (const GlobalVariable *G = dyn_cast<GlobalVariable>(GV)) | ||
| if (const GlobalVariable *G = dyn_cast<GlobalVariable>(GV)) { | ||
| // Bookkeeping globals of a nested dispatch stub are per-callsite | ||
| // state, so the enclosing JIT module gets its own definitions. They | ||
| // are mutable and internal, so cloning them as declarations would | ||
| // produce invalid IR. | ||
| if (G->getName().starts_with(".proteus.")) | ||
| return true; | ||
|
|
||
| if (!G->isConstant()) | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| }); | ||
|
|
@@ -1499,7 +1585,7 @@ class ProteusPassImpl { | |
| return true; | ||
| } | ||
|
|
||
| FunctionCallee getJitLaunchKernelFn(Module &M) { | ||
| FunctionCallee getJitLaunchKernelFn(Module &M, bool LookupByName) { | ||
| FunctionType *JitLaunchKernelFnTy = nullptr; | ||
|
|
||
| assert(LaunchFunctionName && "Expected valid launch function name"); | ||
|
|
@@ -1517,21 +1603,32 @@ class ProteusPassImpl { | |
| "PROTEUS_ENABLE_CUDA|PROTEUS_ENABLE_HIP compilation flags " | ||
| "for ProteusPass"); | ||
|
|
||
| StringRef EntryName = LookupByName ? "__proteus_launch_kernel_by_name" | ||
| : "__proteus_launch_kernel"; | ||
| FunctionCallee JitLaunchKernelFn = | ||
| M.getOrInsertFunction("__proteus_launch_kernel", JitLaunchKernelFnTy); | ||
| M.getOrInsertFunction(EntryName, JitLaunchKernelFnTy); | ||
|
|
||
| return JitLaunchKernelFn; | ||
| } | ||
|
|
||
| void replaceWithJitLaunchKernel(Module &M, CallBase *LaunchKernelCB) { | ||
| FunctionCallee JitLaunchKernelFn = getJitLaunchKernelFn(M); | ||
| std::string getKernelLookupKey(Module &M, const Function &KernelStub) { | ||
| return getUniqueFileID(M) + ":" + KernelStub.getName().str(); | ||
| } | ||
|
|
||
| void replaceWithJitLaunchKernel(Module &M, CallBase *LaunchKernelCB, | ||
| Function *KernelStub) { | ||
| FunctionCallee JitLaunchKernelFn = | ||
| getJitLaunchKernelFn(M, KernelStub != nullptr); | ||
|
|
||
| // Insert before the launch kernel call instruction. | ||
| IRBuilder<> Builder(LaunchKernelCB); | ||
| CallBase *CallOrInvoke = nullptr; | ||
|
|
||
| SmallVector<Value *> Args = {LaunchKernelCB->arg_begin(), | ||
| LaunchKernelCB->arg_end()}; | ||
| if (KernelStub) | ||
| Args[0] = Builder.CreateGlobalString(getKernelLookupKey(M, *KernelStub), | ||
| ".proteus.kernel.lookup"); | ||
|
|
||
| if (isa<CallInst>(LaunchKernelCB)) { | ||
| CallOrInvoke = Builder.CreateCall(JitLaunchKernelFn, Args); | ||
|
|
@@ -1551,7 +1648,8 @@ class ProteusPassImpl { | |
| LaunchKernelCB->eraseFromParent(); | ||
| } | ||
|
|
||
| void emitJitLaunchKernelCall(Module &M) { | ||
| void emitJitLaunchKernelCall( | ||
| Module &M, const DenseMap<Value *, GlobalVariable *> &StubToKernelMap) { | ||
| Function *LaunchKernelFn = nullptr; | ||
| if (!LaunchFunctionName) { | ||
| reportFatalError( | ||
|
|
@@ -1580,8 +1678,17 @@ class ProteusPassImpl { | |
| ToBeReplaced.push_back(CB); | ||
| } | ||
|
|
||
| for (CallBase *CB : ToBeReplaced) | ||
| replaceWithJitLaunchKernel(M, CB); | ||
| for (CallBase *CB : ToBeReplaced) { | ||
| Function *KernelStub = nullptr; | ||
| Value *Stub = getStubGV(CB->getArgOperand(0)); | ||
| auto *StubFn = dyn_cast_or_null<Function>(Stub); | ||
| auto It = StubToKernelMap.find(Stub); | ||
| if (StubFn && It != StubToKernelMap.end() && | ||
| JitFunctionInfoMap.contains(StubFn)) | ||
| KernelStub = StubFn; | ||
|
|
||
| replaceWithJitLaunchKernel(M, CB, KernelStub); | ||
| } | ||
| } | ||
|
|
||
| FunctionCallee getJitRegisterFatBinaryFn(Module &M) { | ||
|
|
@@ -1747,12 +1854,14 @@ class ProteusPassImpl { | |
| // __proteus_register_function(void *Handle, | ||
| // void *Kernel, | ||
| // char const *KernelName, | ||
| // char const *KernelLookupKey, | ||
| // RuntimeConstantInfo **RCInfoArrayPtr, | ||
| // int32_t NumRCs) | ||
| FunctionType *JitRegisterFunctionFnTy = FunctionType::get( | ||
| Types.VoidTy, | ||
| {Types.PtrTy, Types.PtrTy, Types.PtrTy, Types.PtrTy, Types.Int32Ty}, | ||
| /* isVarArg=*/false); | ||
| FunctionType *JitRegisterFunctionFnTy = | ||
| FunctionType::get(Types.VoidTy, | ||
| {Types.PtrTy, Types.PtrTy, Types.PtrTy, Types.PtrTy, | ||
| Types.PtrTy, Types.Int32Ty}, | ||
| /* isVarArg=*/false); | ||
| FunctionCallee JitRegisterKernelFn = M.getOrInsertFunction( | ||
| "__proteus_register_function", JitRegisterFunctionFnTy); | ||
|
|
||
|
|
@@ -1823,11 +1932,13 @@ class ProteusPassImpl { | |
| ConstantInt::get(Builder.getInt32Ty(), NumRuntimeConstants); | ||
|
|
||
| FunctionCallee JitRegisterFunction = getJitRegisterFunctionFn(M); | ||
| auto *KernelLookupKey = Builder.CreateGlobalString( | ||
| getKernelLookupKey(M, *FunctionToRegister), ".proteus.kernel.lookup"); | ||
|
|
||
| Builder.CreateCall(JitRegisterFunction, | ||
| {RegisterCB->getArgOperand(0), | ||
| RegisterCB->getArgOperand(1), | ||
| RegisterCB->getArgOperand(2), | ||
| RegisterCB->getArgOperand(2), KernelLookupKey, | ||
| RuntimeConstantInfoPtrArray, NumRCsValue}); | ||
|
|
||
| auto HelperIt = | ||
|
|
||
Oops, something went wrong.
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.
Let's avoid duplicating the interface for the nested device case. I think if we pull this out, we will be ready to go.