-
Notifications
You must be signed in to change notification settings - Fork 803
[SYCL] Initial printf support for non-constant AS format strings #5069
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
Merged
Changes from 15 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
7f05718
[SYCL] Initial printf support for non-constant AS format strings
0039b2c
Code style & a few conceptual upgrades
a24796d
Register the pass with the new pass manager
b5dc85a
Merge remote-tracking branch 'intel/sycl' into printf-address-space
f134ee8
Ensure correct llvm::Type* for literal & more refactoring
c16fcbb
Improve the function search
41eaf3b
Ugly implementation for the O1 case
a26f7c8
Extend value stripping to loads/stores for O0 cases
9150b8d
Replace call operands instead of re-creating calls
f467434
Huge refactoring
00066b6
More refactoring & descriptive comments added
896f296
Fix comment style
2fa7434
Refactor getCASLiteral back to being a function
ac0372b
Merge remote-tracking branch 'intel/sycl' into printf-address-space
1e83f16
Avoid "null character SetName" assertions caused by StringRef specifics
5c5b875
Improve formatting
e5ae867
Avoid using the error-prone Module::getOrInsertGlobal w/ callback
39efbb4
Wrap the newly created globals into constant pointer casts if needed
4d1c6be
Replace the wrapper calls directly with builtin calls
87d4137
Add LIT tests
6c8122b
Simplify call replacing (since we're always dealing with constants)
c1563da
Correct registration with the new PM & run LITs with that as well
f783309
[Review] Move/rename the LIT tests
52f7ca7
[Review] Address LIT-related comments
a37be74
[Review] Address code comments, pt. I
d2fb5f9
[Review][RFC] Address code comments, pt. II
6959690
[Review] Address code comments, pt. III
dd68d58
[Review] Update CODEOWNERS
d74fb42
[Review] Address code comments, pt. IV
040752c
[Review] Update the comment section in builtins.hpp
ee76f9d
Fix LIT setup for negative checks & factor them out into a separate test
1e3493e
Remove unneeded CHECK-NOT's from regular LIT
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| //===------- MutatePrintfAddrspace.h - SYCL printf AS mutation Pass -------===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // A transformation pass which detects non-constant address space | ||
| // literals usage for the first argument of SYCL experimental printf | ||
| // function, and moves the string literal to constant address | ||
| // space. This a temporary solution for printf's support of generic | ||
| // address space literals; the pass should be dropped once SYCL device | ||
| // backends learn to handle the generic address-spaced argument properly. | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "llvm/IR/Module.h" | ||
| #include "llvm/IR/PassManager.h" | ||
|
|
||
| namespace llvm { | ||
|
|
||
| class SYCLMutatePrintfAddrspacePass | ||
| : public PassInfoMixin<SYCLMutatePrintfAddrspacePass> { | ||
| public: | ||
| PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM); | ||
| }; | ||
|
|
||
| ModulePass *createSYCLMutatePrintfAddrspaceLegacyPass(); | ||
|
|
||
| } // namespace llvm |
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,225 @@ | ||
| //===------ MutatePrintfAddrspace.cpp - SYCL printf AS mutation Pass ------===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // A transformation pass which detects non-constant address space | ||
| // literals usage for the first argument of SYCL experimental printf | ||
| // function, and moves the string literal to constant address | ||
| // space. This a temporary solution for printf's support of generic | ||
| // address space literals; the pass should be dropped once SYCL device | ||
| // backends learn to handle the generic address-spaced argument properly. | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "llvm/SYCLLowerIR/MutatePrintfAddrspace.h" | ||
|
|
||
| #include "llvm/Analysis/ValueTracking.h" | ||
| #include "llvm/IR/IRBuilder.h" | ||
| #include "llvm/IR/Instructions.h" | ||
| #include "llvm/InitializePasses.h" | ||
|
|
||
| using namespace llvm; | ||
|
|
||
| namespace { | ||
| // Wrapper for the pass to make it working with the old pass manager | ||
| class SYCLMutatePrintfAddrspaceLegacyPass : public ModulePass { | ||
| public: | ||
| static char ID; | ||
| SYCLMutatePrintfAddrspaceLegacyPass() : ModulePass(ID) { | ||
| initializeSYCLMutatePrintfAddrspaceLegacyPassPass( | ||
| *PassRegistry::getPassRegistry()); | ||
| } | ||
|
|
||
| // run the SYCLMutatePrintfAddrspace pass on the specified module | ||
| bool runOnModule(Module &M) override { | ||
| ModuleAnalysisManager MAM; | ||
| auto PA = Impl.run(M, MAM); | ||
| return !PA.areAllPreserved(); | ||
| } | ||
|
|
||
| private: | ||
| SYCLMutatePrintfAddrspacePass Impl; | ||
| }; | ||
|
|
||
| struct CallReplacer; | ||
AGindinson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| class AddrspaceReplacer { | ||
| public: | ||
| static constexpr unsigned ConstantAddrspaceID = 2; | ||
|
|
||
| AddrspaceReplacer() = delete; | ||
| AddrspaceReplacer(Module *M) : M(M) { | ||
| Type *Int8Ty = Type::getInt8Ty(M->getContext()); | ||
| CASLiteralType = PointerType::get(Int8Ty, ConstantAddrspaceID); | ||
| } | ||
| ~AddrspaceReplacer() { | ||
| for (Function *F : FunctionsToDrop) | ||
| F->eraseFromParent(); | ||
| } | ||
| size_t getReplacedCallsCount() { return ReplacedCallsCount; } | ||
|
|
||
| void runOnFunctionDeclaration(Function *F); | ||
|
|
||
| private: | ||
| Module *M; | ||
| PointerType *CASLiteralType; | ||
| size_t ReplacedCallsCount = 0; | ||
| /// If the variadic version gets picked during FE compilation, we'll only have | ||
| /// 1 function to replace. However, unique declarations are emitted for each | ||
| /// of the non-variadic (variadic template) calls. | ||
| SmallVector<Function *, 8> FunctionsToDrop; | ||
|
|
||
| Function *getCASPrintfFunction(Function *GenericASPrintfFunc); | ||
| Constant *getCASLiteral(GlobalVariable *GenericASLiteral); | ||
| CallReplacer prepareCASArgForCall(CallInst *CI); | ||
| }; | ||
| } // namespace | ||
|
|
||
| char SYCLMutatePrintfAddrspaceLegacyPass::ID = 0; | ||
| INITIALIZE_PASS(SYCLMutatePrintfAddrspaceLegacyPass, | ||
| "SYCLMutatePrintfAddrspace", | ||
| "Move SYCL printf literal arguments to constant address space", | ||
| false, false) | ||
|
|
||
| // Public interface to the SYCLMutatePrintfAddrspacePass. | ||
| ModulePass *llvm::createSYCLMutatePrintfAddrspaceLegacyPass() { | ||
| return new SYCLMutatePrintfAddrspaceLegacyPass(); | ||
| } | ||
|
|
||
| PreservedAnalyses | ||
| SYCLMutatePrintfAddrspacePass::run(Module &M, ModuleAnalysisManager &MAM) { | ||
| AddrspaceReplacer ASReplacer(&M); | ||
AGindinson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for (Function &F : M) { | ||
| if (!F.isDeclaration()) | ||
| continue; | ||
AGindinson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!F.getName().startswith("_Z18__spirv_ocl_printf")) | ||
| continue; | ||
| ASReplacer.runOnFunctionDeclaration(&F); | ||
| } | ||
| return ASReplacer.getReplacedCallsCount() ? PreservedAnalyses::all() | ||
| : PreservedAnalyses::none(); | ||
| } | ||
|
|
||
| /// Helper implementations | ||
| namespace { | ||
|
|
||
| /// Encapsulates the update of CallInst's literal argument. | ||
| struct CallReplacer { | ||
AGindinson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| CallReplacer(CallInst *CI, Value *CASArg) : CI(CI), CASArg(CASArg) {} | ||
| void replaceWithFunction(Function *CASPrintf) { | ||
| CI->setArgOperand(0, CASArg); | ||
| CI->setCalledFunction(CASPrintf); | ||
| } | ||
|
|
||
| private: | ||
| CallInst *CI; | ||
| Value *CASArg; | ||
| }; | ||
|
|
||
| void AddrspaceReplacer::runOnFunctionDeclaration(Function *F) { | ||
| auto *LiteralType = F->getArg(0)->getType(); | ||
| if (LiteralType->getPointerAddressSpace() == ConstantAddrspaceID) | ||
| // No need to replace the literal type and its printf users | ||
| return; | ||
| Function *CASPrintfFunc = getCASPrintfFunction(F); | ||
AGindinson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| SmallVector<CallReplacer, 16> CallsToReplace; | ||
AGindinson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for (User *U : F->users()) { | ||
| if (!isa<CallInst>(U)) | ||
| continue; | ||
| auto *CI = cast<CallInst>(U); | ||
| CallsToReplace.emplace_back(prepareCASArgForCall(CI)); | ||
| } | ||
| for (CallReplacer &CR : CallsToReplace) { | ||
| CR.replaceWithFunction(CASPrintfFunc); | ||
| ++ReplacedCallsCount; | ||
| } | ||
| if (F->hasNUses(0)) | ||
| FunctionsToDrop.emplace_back(F); | ||
| } | ||
|
|
||
| /// Get the constant addrspace version of the __spirv_ocl_printf declaration, or | ||
| /// generate it if the IR module doesn't have it yet. Also make it variadic so | ||
| /// that it could replace all non-variadic generic AS versions. | ||
| Function * | ||
| AddrspaceReplacer::getCASPrintfFunction(Function *GenericASPrintfFunc) { | ||
| auto *CASPrintfFuncTy = | ||
| FunctionType::get(GenericASPrintfFunc->getReturnType(), CASLiteralType, | ||
| /*isVarArg=*/true); | ||
| FunctionCallee CASPrintfFunc = | ||
| M->getOrInsertFunction("_Z18__spirv_ocl_printfPU3AS2Kcz", CASPrintfFuncTy, | ||
AGindinson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| GenericASPrintfFunc->getAttributes()); | ||
| auto *Callee = cast<Function>(CASPrintfFunc.getCallee()); | ||
| Callee->setCallingConv(CallingConv::SPIR_FUNC); | ||
| Callee->setDSOLocal(true); | ||
| return Callee; | ||
| } | ||
|
|
||
| /// Generate the constant addrspace version of the generic addrspace-residing | ||
| /// global string. If one exists already, get it from the module. | ||
| Constant *AddrspaceReplacer::getCASLiteral(GlobalVariable *GenericASLiteral) { | ||
| // Appending the stable suffix ensures that only one CAS copy is made for each | ||
| // string. In case of the matching name, llvm::Module APIs will ensure that | ||
| // the existing global is returned. | ||
| std::string CASLiteralName = GenericASLiteral->getName().str() + "._AS2"; | ||
| IRBuilder<> Builder(M->getContext()); | ||
| Constant *Res = M->getOrInsertGlobal(CASLiteralName, CASLiteralType, [&] { | ||
| StringRef LiteralValue; | ||
| getConstantStringInfo(GenericASLiteral, LiteralValue); | ||
| GlobalVariable *GV = Builder.CreateGlobalString( | ||
| LiteralValue, CASLiteralName, ConstantAddrspaceID, M); | ||
| GV->setLinkage(GlobalValue::LinkageTypes::InternalLinkage); | ||
| GV->setUnnamedAddr(GlobalValue::UnnamedAddr::None); | ||
| return GV; | ||
| }); | ||
| // TODO: Create the literal in another way to ensure correct type instead | ||
| Res->mutateType(CASLiteralType); | ||
| return Res; | ||
| } | ||
|
|
||
| /// The function's effect is similar to V->stripPointerCastsAndAliases(), but | ||
| /// also strips load/store aliases. | ||
| Value *stripToMemorySource(Value *V) { | ||
| Value *MemoryAccess = V; | ||
| if (auto *LI = dyn_cast<LoadInst>(MemoryAccess)) { | ||
| Value *LoadSource = LI->getPointerOperand(); | ||
| auto *Store = cast<StoreInst>(*llvm::find_if( | ||
| LoadSource->users(), [](User *U) { return isa<StoreInst>(U); })); | ||
| MemoryAccess = Store->getValueOperand(); | ||
| } | ||
| return MemoryAccess->stripPointerCastsAndAliases(); | ||
| } | ||
|
|
||
| /// This key function reaches the global string used as an argument to a | ||
| /// __spirv_ocl_printf call. It then generates a constant AS copy of that global | ||
| /// (or gets an existing one). For the return value, the call instruction is | ||
| /// paired with its future constant addrspace string argument (or with the | ||
| /// wrapper function argument that accepts the global). | ||
| CallReplacer AddrspaceReplacer::prepareCASArgForCall(CallInst *CI) { | ||
| Value *Stripped = stripToMemorySource(CI->getArgOperand(0)); | ||
| Value *CASPrintfOperand = nullptr; | ||
| if (auto *Arg = dyn_cast<Argument>(Stripped)) { | ||
| Function *WrapperFunc = Arg->getParent(); | ||
| Arg->mutateType(CASLiteralType); | ||
| // The global literal is passed to __spirv_ocl_printf via a wrapper function | ||
| // argument. Keep the __spirv_ocl_printf call operand pointing at that | ||
| // argument. | ||
| CASPrintfOperand = Arg; | ||
| for (User *WrapperU : WrapperFunc->users()) { | ||
| auto *WrapperCI = cast<CallInst>(WrapperU); | ||
| Value *StrippedArg = stripToMemorySource(WrapperCI->getArgOperand(0)); | ||
| // We're only expecting 1 level of wrappers, so cast unconditionally | ||
| auto *Literal = cast<GlobalVariable>(StrippedArg); | ||
| Constant *CASLiteral = getCASLiteral(Literal); | ||
| // Replace the wrapper call's argument - __spirv_ocl_printf will end up | ||
| // pointing at the same constant AS string. | ||
| WrapperCI->setArgOperand(0, CASLiteral); | ||
| } | ||
| } else if (auto *Literal = dyn_cast<GlobalVariable>(Stripped)) { | ||
| CASPrintfOperand = getCASLiteral(Literal); | ||
| } else | ||
AGindinson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| llvm_unreachable("Unexpected literal operand type for device-side printf"); | ||
| return {CI, CASPrintfOperand}; | ||
| } | ||
| } // namespace | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.